| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105 |
- using System;
- using System.Collections.Generic;
- using SHJX.Service.Model.Control;
- using SHJX.Service.Common.ReadXML;
- using System.Collections.Concurrent;
- using SHJX.Service.ServerClient.Interface;
- using SHJX.Service.ServerClient.LiquidPump;
- using SHJX.Service.ServerClient.RS485Control;
- using SHJX.Service.ServerClient.PortControl;
- using SHJX.Service.Common.Logging;
- using Microsoft.Extensions.Logging;
- namespace SHJX.Service.ServerClient
- {
- public class OptClient
- {
- private ClientPort _clientPort;
- private PortControlImp _client;
- private readonly ReadConfigUtil _config;
- private readonly Dictionary<string, byte> _nodeIDs;
- private readonly ConcurrentDictionary<string, PortControlImp> _clients;
- private static readonly ILogger logger = LogFactory.BuildLogger(typeof(OptClient));
- public OptClient( ReadConfigUtil config)
- {
- _config = config;
- _nodeIDs = _config.ModBusNodeIDs;
- _clients = new ConcurrentDictionary<string, PortControlImp>();
- }
- public (string, bool) Open()
- {
- _clientPort ??= new ClientPort( _config);
- return _clientPort.OpenClient();
- }
- /// <summary>
- /// 工厂
- /// </summary>
- /// <param name="typeName"></param>
- /// <returns></returns>
- public OptClient Factory(string typeName)
- {
- try
- {
- if (_clients.TryGetValue(typeName, out var currentclient))
- {
- _client = currentclient;
- return this;
- }
- PortControlImp port = typeName switch
- {
- "MotorX" => new MotorXPort( _config),
- "MotorY" => new MotorYPort( _config),
- "MotorZ" => new MotorZPort( _config),
- "MotorT" => new MotorTPort( _config),
- "Dissolve" => new DissolvePort( _config),
- "Mani" => new ManiPort( _config),
- "Heat" => new HeatingControl( _config),
- "Cage" => new CagePort( _config),
- "DripNozzle" => new DripNozzlePort( _config),
- //"DripNozzle2" => new DripNozzlePort( _config),
- "TitrationStir" => new TitrationStirPort( _config),
- //"TitrationStir2" => new TitrationStirPort( _config),
- "FAS" => new FASPort( _config),
- "Water" => new WaterPort( _config),
- "Indicator" => new IndicatorPort( _config),
- //"Indicator2" => new IndicatorPort( _config),
- "Mercury" => new MercuryPort( _config),
- "PotassiumDichromate_Low" or "PotassiumDichromate_High" => new PotassiumDichromatePort( _config),
- "SilverSulfate" => new SilverSulfatePort( _config),
- "SilverSulfatePipe" => new SilverSulfatePipePort( _config),
- "Timer" => new TimerPort( _config),
- "DissolveFan" => new DissolveFanPort( _config),
- "SampleFan" => new SampleFanPort( _config),
- "LiquidStir" => new LiquidStirPort( _config),
- _ => throw new ArgumentNullException(typeName)
- };
- _clients.TryAdd(typeName, port);
- _client = _clients[typeName];
- return this;
- }
- catch (Exception ex)
- {
- logger.LogError(ex.ToString());
- return this;
- }
- }
- public bool Write(PortArgs args)
- {
- if (_nodeIDs.TryGetValue(args.TypeName, out var nodeId))
- args.NodeId = nodeId;
- var ret= _client.Write(args);
-
- return ret;
- }
- public object Read(PortArgs args)
- {
- if (_nodeIDs.TryGetValue(args.TypeName, out var nodeId))
- args.NodeId = nodeId;
- return _client.Read(args);
- }
- }
- }
|