CytEndPointHandler.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. using MECF.Framework.Common.Communications;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using Aitex.Core.RT.Event;
  5. namespace MECF.Framework.RT.EquipmentLibrary.HardwareUnits.EndPoints.CytEndPoints
  6. {
  7. public abstract class CytEndPointHandler : HandlerBase
  8. {
  9. public CytEndPoint Device { get; }
  10. public byte _command;
  11. protected CytEndPointHandler(CytEndPoint device, byte command)
  12. : base(BuildMessage(command))
  13. {
  14. Device = device;
  15. _command = command;
  16. }
  17. protected CytEndPointHandler(CytEndPoint device, byte command, byte[] buffer)
  18. : base(buffer)
  19. {
  20. Device = device;
  21. _command = command;
  22. }
  23. private static byte[] BuildMessage(byte cmd)
  24. {
  25. var header = new PacketHeader() { token = 0x0101, channel = 0, command = cmd, length = 0 };
  26. var data = new byte[PacketHeader.Size];
  27. System.Buffer.BlockCopy(ByteStructConverter.Struct2Bytes(header), 0, data, 0, PacketHeader.Size);
  28. return data;
  29. }
  30. public override bool HandleMessage(MessageBase msg, out bool transactionComplete)
  31. {
  32. transactionComplete = true;
  33. CytEndPointMessage response = msg as CytEndPointMessage;
  34. if (!response.IsComplete)
  35. return false;
  36. if (response.IsEvent)
  37. return false;
  38. if (response.Header.command != _command)
  39. return false;
  40. if (response.IsError)
  41. {
  42. if (EPDDefine.ErrorMap.ContainsKey(response.Header.errorcode))
  43. {
  44. Device.NoteError(EPDDefine.ErrorMap[response.Header.errorcode]);
  45. }
  46. else
  47. {
  48. Device.NoteError($"EPD Error {response.Header.errorcode:X}");
  49. }
  50. }
  51. return true;
  52. }
  53. }
  54. public class CytEventHandler : CytEndPointHandler
  55. {
  56. public CytEventHandler(CytEndPoint device)
  57. : base(device, (byte)EPDCommand.Event)
  58. {
  59. }
  60. public override bool HandleMessage(MessageBase msg, out bool handled)
  61. {
  62. handled = true;
  63. if (msg is CytEndPointMessage cytMsg && msg.IsEvent && cytMsg.DataReader.ReadInt(out int type) && cytMsg.DataReader.ReadInt64(out long ticket))
  64. {
  65. switch (type)
  66. {
  67. case 0x03:
  68. Device.NoteStart(ticket);
  69. break;
  70. case 0x04:
  71. Device.NoteDelayEnd(ticket);
  72. break;
  73. case 0x05:
  74. Device.NoteNormalizeStart(ticket);
  75. break;
  76. case 0x06:
  77. Device.NoteSatisfied(ticket);
  78. break;
  79. case 0x07:
  80. Device.NoteTrigger(ticket);
  81. break;
  82. case 0x08:
  83. Device.NoteStop(ticket);
  84. break;
  85. case 0x10:
  86. var dummy = new byte[16];
  87. cytMsg.DataReader.ReadBytes(dummy, 16);
  88. var tmp = new byte[256];
  89. if (cytMsg.DataReader.ReadBytes(tmp, 256))
  90. {
  91. var data = Encoding.UTF8.GetString(tmp);
  92. data = data.Substring(0, data.IndexOf('\0'));
  93. Device.NoteTrendValue(data);
  94. }
  95. break;
  96. case 0x11:
  97. var dummy11 = new byte[16];
  98. cytMsg.DataReader.ReadBytes(dummy11, 16);
  99. var tmp11 = new byte[256];
  100. if (cytMsg.DataReader.ReadBytes(tmp11, 256))
  101. {
  102. var data = Encoding.UTF8.GetString(tmp11);
  103. data = data.Substring(0, data.IndexOf('\0'));
  104. Device.NoteTrendValue(data);
  105. }
  106. break;
  107. }
  108. }
  109. return true;
  110. }
  111. }
  112. public class CytConnectHandler : CytEndPointHandler
  113. {
  114. public CytConnectHandler(CytEndPoint device)
  115. : base(device, (byte)EPDCommand.Connect)
  116. {
  117. }
  118. public override bool HandleMessage(MessageBase msg, out bool handled)
  119. {
  120. if (base.HandleMessage(msg, out handled))
  121. {
  122. Device.NoteIsConnected(true);
  123. return true;
  124. }
  125. return false;
  126. }
  127. }
  128. public class CytDisconnectHandler : CytEndPointHandler
  129. {
  130. public CytDisconnectHandler(CytEndPoint device)
  131. : base(device, (byte)EPDCommand.Disconnect)
  132. {
  133. }
  134. public override bool HandleMessage(MessageBase msg, out bool handled)
  135. {
  136. if (base.HandleMessage(msg, out handled))
  137. {
  138. Device.NoteIsConnected(false);
  139. return true;
  140. }
  141. return false;
  142. }
  143. }
  144. public class CytQueryStateHandler : CytEndPointHandler
  145. {
  146. public CytQueryStateHandler(CytEndPoint device)
  147. : base(device, (byte)EPDCommand.QueryState)
  148. {
  149. }
  150. public override bool HandleMessage(MessageBase msg, out bool handled)
  151. {
  152. if (base.HandleMessage(msg, out handled))
  153. {
  154. if (msg is CytEndPointMessage cytMsg && cytMsg.DataReader.ReadUInt16(out ushort state))
  155. {
  156. var result = state.ToString();
  157. if (EPDDefine.StateMap.ContainsKey(state))
  158. result = EPDDefine.StateMap[state];
  159. Device.NoteState(result);
  160. }
  161. return true;
  162. }
  163. return false;
  164. }
  165. }
  166. public class CytQueryModeHandler : CytEndPointHandler
  167. {
  168. public CytQueryModeHandler(CytEndPoint device)
  169. : base(device, (byte)EPDCommand.QueryOperateMode)
  170. {
  171. }
  172. public override bool HandleMessage(MessageBase msg, out bool handled)
  173. {
  174. if (base.HandleMessage(msg, out handled))
  175. {
  176. if (msg is CytEndPointMessage cytMsg && cytMsg.DataReader.ReadUInt16(out ushort mode))
  177. {
  178. var result = mode.ToString();
  179. if (EPDDefine.ModeMap.ContainsKey(mode))
  180. result = EPDDefine.ModeMap[mode];
  181. Device.NoteMode(result);
  182. }
  183. return true;
  184. }
  185. return false;
  186. }
  187. }
  188. public class CytSetModeHandler : CytEndPointHandler
  189. {
  190. public CytSetModeHandler(CytEndPoint device, bool isRemote)
  191. : base(device, (byte)EPDCommand.SetOperateMode, BuildMessage(isRemote))
  192. {
  193. }
  194. private static byte[] BuildMessage(bool isRemote)
  195. {
  196. byte mode = isRemote ? (byte)2 : (byte)1;
  197. var header = new PacketHeader() { token = 0x0101, channel = 0, command = (byte)EPDCommand.SetOperateMode, length = 4 };
  198. var data = new byte[PacketHeader.Size + 4];
  199. System.Buffer.BlockCopy(ByteStructConverter.Struct2Bytes(header), 0, data, 0, PacketHeader.Size);
  200. System.Buffer.BlockCopy(new byte[] { mode, 0, 0, 0 }, 0, data, PacketHeader.Size, 4);
  201. return data;
  202. }
  203. }
  204. public class CytQueryConfigListHandler : CytEndPointHandler
  205. {
  206. public CytQueryConfigListHandler(CytEndPoint device)
  207. : base(device, (byte)EPDCommand.QueryCfgList)
  208. {
  209. }
  210. public override bool HandleMessage(MessageBase msg, out bool handled)
  211. {
  212. if (base.HandleMessage(msg, out handled))
  213. {
  214. if (msg is CytEndPointMessage cytMsg && cytMsg.DataReader.ReadInt(out int count))
  215. {
  216. var cfgs = new List<string>();
  217. var tmp = new byte[256];
  218. for (int i = 0; i < count; i++)
  219. {
  220. if (cytMsg.DataReader.ReadBytes(tmp, 256))
  221. {
  222. var name = Encoding.UTF8.GetString(tmp);
  223. name = name.Substring(0, name.IndexOf('\0'));
  224. cfgs.Add(name);
  225. }
  226. }
  227. Device.NoteConfigList(cfgs);
  228. }
  229. return true;
  230. }
  231. return false;
  232. }
  233. }
  234. public class CytRecipeStartHandler : CytEndPointHandler
  235. {
  236. public CytRecipeStartHandler(CytEndPoint device)
  237. : base(device, (byte)EPDCommand.RecipeStart)
  238. {
  239. }
  240. }
  241. public class CytRecipeStopHandler : CytEndPointHandler
  242. {
  243. public CytRecipeStopHandler(CytEndPoint device)
  244. : base(device, (byte)EPDCommand.RecipeStop)
  245. {
  246. }
  247. }
  248. public class CytStartHandler : CytEndPointHandler
  249. {
  250. public CytStartHandler(CytEndPoint device, string configName)
  251. : base(device, (byte)EPDCommand.Start, BuildMessage(configName))
  252. {
  253. }
  254. private static byte[] BuildMessage(string configName)
  255. {
  256. var header = new PacketHeader() { token = 0x0101, channel = 0, command = (byte)EPDCommand.Start, length = 256 };
  257. var data = new byte[PacketHeader.Size + 256];
  258. System.Buffer.BlockCopy(ByteStructConverter.Struct2Bytes(header), 0, data, 0, PacketHeader.Size);
  259. char[] chars = configName.ToCharArray();
  260. Encoding.UTF8.GetBytes(chars, 0, chars.Length, data, PacketHeader.Size);
  261. return data;
  262. }
  263. }
  264. public class CytStopHandler : CytEndPointHandler
  265. {
  266. public CytStopHandler(CytEndPoint device)
  267. : base(device, (byte)EPDCommand.Stop)
  268. {
  269. }
  270. }
  271. public class CytQueryCurrentConfigHandler : CytEndPointHandler
  272. {
  273. public CytQueryCurrentConfigHandler(CytEndPoint device)
  274. : base(device, (byte)EPDCommand.QueryCurrentConfig)
  275. {
  276. }
  277. //“Name=FallValue;IntegrationTime=100,DelayTime=100;Regions:R357=156.2,R516=260.8;Trends:EP1=3586.9,EP2=1645.2”
  278. public override bool HandleMessage(MessageBase msg, out bool handled)
  279. {
  280. if (base.HandleMessage(msg, out handled))
  281. {
  282. if (msg is CytEndPointMessage cytMsg)
  283. {
  284. var tmp = new byte[256];
  285. if (cytMsg.DataReader.ReadBytes(tmp, 256))
  286. {
  287. var cfg = Encoding.UTF8.GetString(tmp);
  288. cfg = cfg.Substring(0, cfg.IndexOf('\0'));
  289. string[] items = cfg.Split(';');
  290. List<string> names = new List<string>();
  291. foreach (var item in items)
  292. {
  293. if (item.StartsWith("Regions:"))
  294. {
  295. string[] data = item.Substring("Regions:".Length).Split(',');
  296. names.AddRange(data);
  297. }
  298. else if (item.StartsWith("Trends:"))
  299. {
  300. string[] data = item.Substring("Trends:".Length).Split(',');
  301. names.AddRange(data);
  302. }
  303. else
  304. {
  305. string[] data = item.Split('=');
  306. if (data[0] == "Name")
  307. {
  308. Device.NoteCurrentConfigName(data[1]);
  309. }
  310. if (data[0] == "IntegrationTime")
  311. {
  312. //Device.NoteCurrentConfigIntegrationTime(int.Parse(data[1]));
  313. }
  314. }
  315. }
  316. Device.NoteTrendName(names);
  317. }
  318. }
  319. return true;
  320. }
  321. return false;
  322. }
  323. }
  324. }