OptClient.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. using System;
  2. using System.Collections.Generic;
  3. using SHJX.Service.Model.Control;
  4. using SHJX.Service.Common.ReadXML;
  5. using System.Collections.Concurrent;
  6. using SHJX.Service.ServerClient.Interface;
  7. using SHJX.Service.ServerClient.LiquidPump;
  8. using SHJX.Service.ServerClient.RS485Control;
  9. using SHJX.Service.ServerClient.PortControl;
  10. using SHJX.Service.Common.Logging;
  11. using Microsoft.Extensions.Logging;
  12. namespace SHJX.Service.ServerClient
  13. {
  14. public class OptClient
  15. {
  16. private ClientPort _clientPort;
  17. private PortControlImp _client;
  18. private readonly ReadConfigUtil _config;
  19. private readonly Dictionary<string, byte> _nodeIDs;
  20. private readonly ConcurrentDictionary<string, PortControlImp> _clients;
  21. private static readonly ILogger logger = LogFactory.BuildLogger(typeof(OptClient));
  22. public OptClient( ReadConfigUtil config)
  23. {
  24. _config = config;
  25. _nodeIDs = _config.ModBusNodeIDs;
  26. _clients = new ConcurrentDictionary<string, PortControlImp>();
  27. }
  28. public (string, bool) Open()
  29. {
  30. _clientPort ??= new ClientPort( _config);
  31. return _clientPort.OpenClient();
  32. }
  33. /// <summary>
  34. /// 工厂
  35. /// </summary>
  36. /// <param name="typeName"></param>
  37. /// <returns></returns>
  38. public OptClient Factory(string typeName)
  39. {
  40. try
  41. {
  42. if (_clients.TryGetValue(typeName, out var currentclient))
  43. {
  44. _client = currentclient;
  45. return this;
  46. }
  47. PortControlImp port = typeName switch
  48. {
  49. "MotorX" => new MotorXPort( _config),
  50. "MotorY" => new MotorYPort( _config),
  51. "MotorZ" => new MotorZPort( _config),
  52. "MotorT" => new MotorTPort( _config),
  53. "Dissolve" => new DissolvePort( _config),
  54. "Mani" => new ManiPort( _config),
  55. "Heat" => new HeatingControl( _config),
  56. "Cage" => new CagePort( _config),
  57. "DripNozzle" => new DripNozzlePort( _config),
  58. //"DripNozzle2" => new DripNozzlePort( _config),
  59. "TitrationStir" => new TitrationStirPort( _config),
  60. //"TitrationStir2" => new TitrationStirPort( _config),
  61. "FAS" => new FASPort( _config),
  62. "Water" => new WaterPort( _config),
  63. "Indicator" => new IndicatorPort( _config),
  64. //"Indicator2" => new IndicatorPort( _config),
  65. "Mercury" => new MercuryPort( _config),
  66. "PotassiumDichromate_Low" or "PotassiumDichromate_High" => new PotassiumDichromatePort( _config),
  67. "SilverSulfate" => new SilverSulfatePort( _config),
  68. "SilverSulfatePipe" => new SilverSulfatePipePort( _config),
  69. "Timer" => new TimerPort( _config),
  70. "DissolveFan" => new DissolveFanPort( _config),
  71. "SampleFan" => new SampleFanPort( _config),
  72. "LiquidStir" => new LiquidStirPort( _config),
  73. _ => throw new ArgumentNullException(typeName)
  74. };
  75. _clients.TryAdd(typeName, port);
  76. _client = _clients[typeName];
  77. return this;
  78. }
  79. catch (Exception ex)
  80. {
  81. logger.LogError(ex.ToString());
  82. return this;
  83. }
  84. }
  85. public bool Write(PortArgs args)
  86. {
  87. if (_nodeIDs.TryGetValue(args.TypeName, out var nodeId))
  88. args.NodeId = nodeId;
  89. var ret= _client.Write(args);
  90. return ret;
  91. }
  92. public object Read(PortArgs args)
  93. {
  94. if (_nodeIDs.TryGetValue(args.TypeName, out var nodeId))
  95. args.NodeId = nodeId;
  96. return _client.Read(args);
  97. }
  98. }
  99. }