| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Net;
- using System.Net.Sockets;
- using System.Text;
- using System.Threading.Tasks;
- using Team.FFFeederService.Interfaces;
- using TouchSocket.Core;
- using TouchSocket.Sockets;
- namespace Team.FFFeederService
- {
- public class FeederService : IFeederService
- {
- private readonly Dictionary<Guid, IVoiceCoilMotorFeeder> _feederCollection;
- private readonly object _sync = new object();
- public FeederService()
- {
- _feederCollection = new Dictionary<Guid, IVoiceCoilMotorFeeder>();
- }
- public bool CreateFeeder(Guid id, int feederNo, string name, string ip, int port, FeederBrand feederBrand)
- {
- IVoiceCoilMotorFeeder feeder;
- if (feederBrand == FeederBrand.TEAM)
- {
- var tcpClient = new TouchSocket.Sockets.TcpClient();
- var config = new TouchSocketConfig();
- config.SetRemoteIPHost(new IPHost(IPAddress.Parse(ip), port));
- config.SetTcpDataHandlingAdapter(() => { return new TerminatorPackageAdapter("\r\n"); }); //设置结束符
- config.ConfigurePlugins(a => { a.UseTcpReconnection(); }); ////如需永远尝试连接,tryCount设置为-1即可。
- //载入配置
- tcpClient.Setup(config);
- feeder = new TeamFeeder(id, feederNo, name, tcpClient);
- }
- else
- {
- feeder = new AfagFeeder(id, feederNo, name, ip, port);
- }
- feeder.Id = id;
- lock (_sync)
- {
- if (_feederCollection.ContainsKey(id))
- {
- try
- {
- // 移除旧事件并释放
- var old = _feederCollection[id];
- old.Dispose();
- }
- catch
- {
- // 忽略个别释放异常
- }
- _feederCollection[id] = feeder;
- }
- else
- {
- _feederCollection.Add(id, feeder);
- }
- }
- return true;
- }
- public Task<bool> CreateFeederAsync(Guid id, int feederNo, string name, string ip, int port, FeederBrand feederBrand)
- {
- return Task.Run(() => CreateFeeder(id, feederNo, name, ip, port, feederBrand));
- }
- /// <summary>
- /// Feeder断开连接时
- /// </summary>
- private void FeederService_FeederDisconnectedEvent(Guid feederId, ITcpClient client, ClosedEventArgs e)
- {
- // 可在这里转发或记录断开事件
- }
- /// <summary>
- /// Feeder连接上时
- /// </summary>
- private void FeederService_FeederConnectedEvent(Guid feederId, ITcpClient client, ConnectedEventArgs e)
- {
- // 可在这里转发或记录连接事件
- }
- public IVoiceCoilMotorFeeder GetFeeder(Guid id)
- {
- lock (_sync)
- {
- if (_feederCollection.TryGetValue(id, out var feeder)) return feeder;
- }
- return null;
- }
- public Task<IVoiceCoilMotorFeeder> GetFeederAsync(Guid id)
- {
- return Task.Run(() => { return GetFeeder(id); });
- }
- public IVoiceCoilMotorFeeder GetFeederByIndex(int feederNo)
- {
- lock (_sync)
- {
- foreach (var kvp in _feederCollection)
- {
- var f = kvp.Value;
- if (f == null) continue;
- try
- {
- if (f.FeederNo == feederNo) return f;
- }
- catch
- {
- // 忽略访问异常
- }
- }
- }
- return null;
- }
- public Task<IVoiceCoilMotorFeeder> GetFeederByIndexAsync(int feederNo)
- {
- return Task.Run(() => { return GetFeederByIndex(feederNo); });
- }
- public Task UnRegisterFeederAsync(Guid id)
- {
- return Task.Run(() => UnRegisterFeeder(id));
- }
- public void UnRegisterFeeder(Guid id)
- {
- IVoiceCoilMotorFeeder feeder = null;
- lock (_sync)
- {
- if (_feederCollection.TryGetValue(id, out feeder))
- {
- _feederCollection.Remove(id);
- }
- }
- if (feeder != null)
- {
- try
- {
- feeder.Dispose();
- }
- catch
- {
- // 忽略释放异常
- }
- }
- }
- public Task UnRegisterFeederByIndexAsync(int feederNo)
- {
- return Task.Run(() => UnRegisterFeederByIndex(feederNo));
- }
- public void UnRegisterFeederByIndex(int feederNo)
- {
- Guid foundId = Guid.Empty;
- lock (_sync)
- {
- foreach (var kvp in _feederCollection)
- {
- try
- {
- if (kvp.Value != null && kvp.Value.FeederNo == feederNo)
- {
- foundId = kvp.Key;
- break;
- }
- }
- catch { }
- }
- }
- if (foundId != Guid.Empty)
- {
- UnRegisterFeeder(foundId);
- }
- }
- public IReadOnlyCollection<IVoiceCoilMotorFeeder> GetAllFeeders()
- {
- lock (_sync)
- {
- return _feederCollection.Values.ToList().AsReadOnly();
- }
- }
- public Task<IReadOnlyCollection<IVoiceCoilMotorFeeder>> GetAllFeedersAsync()
- {
- return Task.Run(() => { return GetAllFeeders(); });
- }
- public bool TryGetFeeder(Guid id, out IVoiceCoilMotorFeeder feeder)
- {
- lock (_sync)
- {
- return _feederCollection.TryGetValue(id, out feeder);
- }
- }
- public Task<(bool found, IVoiceCoilMotorFeeder feeder)> TryGetFeederAsync(Guid id)
- {
- return Task.Run(() =>
- {
- IVoiceCoilMotorFeeder feeder;
- bool found;
- lock (_sync)
- {
- found = _feederCollection.TryGetValue(id, out feeder);
- }
- return (found, feeder);
- });
- }
- public bool ContainsFeeder(Guid id)
- {
- lock (_sync)
- {
- return _feederCollection.ContainsKey(id);
- }
- }
- public Task<bool> ContainsFeederAsync(Guid id)
- {
- return Task.Run(() => { return ContainsFeeder(id); });
- }
- public bool RemoveFeeder(Guid id)
- {
- IVoiceCoilMotorFeeder feeder = null;
- lock (_sync)
- {
- if (_feederCollection.TryGetValue(id, out feeder))
- {
- _feederCollection.Remove(id);
- }
- }
- if (feeder != null)
- {
- try
- {
- feeder.Dispose();
- }
- catch
- {
- // 忽略释放异常
- }
- return true;
- }
- return false;
- }
- public Task<bool> RemoveFeederAsync(Guid id)
- {
- return Task.Run(() => { return RemoveFeeder(id); });
- }
- public void RemoveAllFeeders()
- {
- List<IVoiceCoilMotorFeeder> feeders;
- lock (_sync)
- {
- feeders = _feederCollection.Values.ToList();
- _feederCollection.Clear();
- }
- foreach (var f in feeders)
- {
- try
- {
- f.Dispose();
- }
- catch { }
- }
- }
- /// <summary>
- /// 更新Feeder编号
- /// </summary>
- /// <param name="id"></param>
- /// <param name="newFeederNo"></param>
- public void UpdateFeederNo(Guid id, int newFeederNo)
- {
- lock (_sync)
- {
- if (_feederCollection.TryGetValue(id, out var feeder))
- {
- feeder.FeederNo = newFeederNo;
- }
- }
- }
- }
- }
|