RisshiChillerHandler.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. using MECF.Framework.Common.Communications;
  2. using Newtonsoft.Json.Linq;
  3. using System.Linq;
  4. using System.Text;
  5. namespace MECF.Framework.RT.EquipmentLibrary.HardwareUnits.Chillers.RisshiChiller
  6. {
  7. public abstract class RisshiChillerHandler : HandlerBase
  8. {
  9. public RisshiChiller Device { get; }
  10. public string _command;
  11. protected string _parameter;
  12. protected RisshiChillerHandler(RisshiChiller device, string command, string parameter = null)
  13. : base(BuildMessage(command, parameter))
  14. {
  15. Device = device;
  16. _command = command;
  17. _parameter = parameter;
  18. Name = command;
  19. }
  20. private static char _startLine = (char)1;
  21. private static char _endLine = (char)3;
  22. private static string BuildMessage( string command, string parameter)
  23. {
  24. command = "ALIG " + command;
  25. string commandContent = parameter == null ? $"{command}" : $"{command} {parameter}";
  26. string sLength = commandContent.Length.ToString("D2");
  27. var charArray = (sLength + commandContent).ToArray();
  28. int sum = charArray.Sum(chr => (int)chr);
  29. string sSum = sum.ToString("X");
  30. if (sSum.Length < 2)
  31. return _startLine + sLength + commandContent + "0" + sSum + _endLine.ToString();
  32. else
  33. return _startLine + sLength + commandContent + sSum.Substring(sSum.Length - 2) + _endLine.ToString();
  34. }
  35. public override bool HandleMessage(MessageBase msg, out bool transactionComplete)
  36. {
  37. RisshiChillerMessage response = msg as RisshiChillerMessage;
  38. ResponseMessage = msg;
  39. if (response.IsAck)
  40. {
  41. SetState(EnumHandlerState.Acked);
  42. }
  43. if (response.IsResponse)
  44. {
  45. SetState(EnumHandlerState.Completed);
  46. transactionComplete = true;
  47. return true;
  48. }
  49. transactionComplete = false;
  50. return false;
  51. }
  52. }
  53. public class RisshiChillerRawCommandHandler : RisshiChillerHandler
  54. {
  55. public RisshiChillerRawCommandHandler(RisshiChiller device, string command, string parameter = null)
  56. : base(device, command, parameter)
  57. {
  58. }
  59. public override bool HandleMessage(MessageBase msg, out bool handled)
  60. {
  61. var result = msg as RisshiChillerMessage;
  62. Device.NoteRawCommandInfo(_command, result.RawMessage);
  63. ResponseMessage = msg;
  64. handled = true;
  65. return true;
  66. }
  67. }
  68. public class RisshiChillerXferHandler : RisshiChillerHandler
  69. {
  70. public RisshiChillerXferHandler(RisshiChiller device, string jsonParameter)
  71. : base(device, "XFER", BuildCommandParameter(jsonParameter))
  72. {
  73. }
  74. static string BuildCommandParameter(string jsonParameter)
  75. {
  76. JObject jsonObject = JObject.Parse(jsonParameter);
  77. StringBuilder paraBuilder = new StringBuilder();
  78. foreach (var item in jsonObject)
  79. {
  80. if (item.Key.Contains("Slot") || item.Key == "Arm")
  81. paraBuilder.Append(" " + item.Key.ToUpper() + " " + item.Value);
  82. else if(item.Value!=null && item.Value.ToString().Length > 0)
  83. paraBuilder.Append(" " + item.Value);
  84. }
  85. return paraBuilder.Length > 0 ? paraBuilder.ToString() : null;
  86. }
  87. public override bool HandleMessage(MessageBase msg, out bool handled)
  88. {
  89. var result = msg as RisshiChillerMessage;
  90. if (result.IsError)
  91. {
  92. Device.NoteError(result.Data);
  93. }
  94. else
  95. {
  96. Device.NoteError(null);
  97. Device.NoteActionCompleted(_command);
  98. }
  99. ResponseMessage = msg;
  100. handled = true;
  101. return true;
  102. }
  103. }
  104. public class RisshiChillerPickHandler : RisshiChillerHandler
  105. {
  106. public RisshiChillerPickHandler(RisshiChiller device, string jsonParameter)
  107. : base(device, "PICK", BuildCommandParameter(jsonParameter))
  108. {
  109. }
  110. //N 2 R EX Z DOWN SLOT 1 ARM A
  111. static string BuildCommandParameter(string jsonParameter)
  112. {
  113. JObject jsonObject = JObject.Parse(jsonParameter);
  114. StringBuilder paraBuilder = new StringBuilder();
  115. foreach (var item in jsonObject)
  116. {
  117. if (paraBuilder.Length == 0)
  118. {
  119. paraBuilder.Append(item.Value);
  120. }
  121. else
  122. {
  123. if(item.Key == "Slot" || item.Key == "Arm")
  124. paraBuilder.Append(" " + item.Key.ToUpper() + " " + item.Value);
  125. else
  126. paraBuilder.Append(" " + item.Value);
  127. }
  128. }
  129. return paraBuilder.Length > 0 ? paraBuilder.ToString() : null;
  130. }
  131. public override bool HandleMessage(MessageBase msg, out bool handled)
  132. {
  133. var result = msg as RisshiChillerMessage;
  134. if (result.IsError)
  135. {
  136. Device.NoteError(result.Data);
  137. }
  138. else
  139. {
  140. Device.NoteError(null);
  141. Device.NoteLastPickInfo(SendText);
  142. }
  143. ResponseMessage = msg;
  144. handled = true;
  145. return true;
  146. }
  147. }
  148. public class RisshiChillerPlaceHandler : RisshiChillerHandler
  149. {
  150. public RisshiChillerPlaceHandler(RisshiChiller device, string jsonParameter)
  151. : base(device, "PLACE", BuildCommandParameter(jsonParameter))
  152. {
  153. }
  154. static string BuildCommandParameter(string jsonParameter)
  155. {
  156. JObject jsonObject = JObject.Parse(jsonParameter);
  157. StringBuilder paraBuilder = new StringBuilder();
  158. foreach (var item in jsonObject)
  159. {
  160. if (paraBuilder.Length == 0)
  161. {
  162. paraBuilder.Append(item.Value);
  163. }
  164. else
  165. {
  166. if (item.Key == "Slot" || item.Key == "Arm")
  167. paraBuilder.Append(" " + item.Key.ToUpper() + " " + item.Value);
  168. else
  169. paraBuilder.Append(" " + item.Value);
  170. }
  171. }
  172. return paraBuilder.Length > 0 ? paraBuilder.ToString() : null;
  173. }
  174. public override bool HandleMessage(MessageBase msg, out bool handled)
  175. {
  176. var result = msg as RisshiChillerMessage;
  177. if (result.IsError)
  178. {
  179. Device.NoteError(result.Data);
  180. }
  181. else
  182. {
  183. Device.NoteError(null);
  184. Device.NoteLastPlaceInfo(SendText);
  185. }
  186. ResponseMessage = msg;
  187. handled = true;
  188. return true;
  189. }
  190. }
  191. public class RisshiChillerSimpleActionHandler : RisshiChillerHandler
  192. {
  193. public RisshiChillerSimpleActionHandler(RisshiChiller device, string command, string parameter = null)
  194. : base(device, command, parameter)
  195. {
  196. }
  197. public override bool HandleMessage(MessageBase msg, out bool handled)
  198. {
  199. var result = msg as RisshiChillerMessage;
  200. if (result.IsError)
  201. {
  202. Device.NoteError(result.Data);
  203. }
  204. else
  205. {
  206. Device.NoteError(null);
  207. Device.NoteActionCompleted(_command);
  208. }
  209. ResponseMessage = msg;
  210. handled = true;
  211. return true;
  212. }
  213. }
  214. public class RisshiChillerSimpleSetHandler : RisshiChillerHandler
  215. {
  216. public RisshiChillerSimpleSetHandler(RisshiChiller device, string command, string parameter = null)
  217. : base(device, command, parameter)
  218. {
  219. }
  220. public override bool HandleMessage(MessageBase msg, out bool handled)
  221. {
  222. var result = msg as RisshiChillerMessage;
  223. if (result.IsError)
  224. {
  225. Device.NoteError(result.Data);
  226. }
  227. else
  228. {
  229. Device.NoteError(null);
  230. Device.NoteSetCompleted(_command,_parameter);
  231. }
  232. ResponseMessage = msg;
  233. handled = true;
  234. return true;
  235. }
  236. }
  237. public class RisshiChillerSevoOnOffHandler : RisshiChillerHandler
  238. {
  239. public RisshiChillerSevoOnOffHandler(RisshiChiller device, bool isOn)
  240. : base(device, isOn ? "SVON" : "SVOFF")
  241. {
  242. }
  243. public override bool HandleMessage(MessageBase msg, out bool handled)
  244. {
  245. var result = msg as RisshiChillerMessage;
  246. if (result.IsError)
  247. {
  248. Device.NoteError(result.Data);
  249. }
  250. else
  251. {
  252. Device.NoteError(null);
  253. Device.NoteSevoOnOff(_command == "SVON");
  254. }
  255. ResponseMessage = msg;
  256. handled = true;
  257. return true;
  258. }
  259. }
  260. public class RisshiChillerSimpleQueryHandler : RisshiChillerHandler
  261. {
  262. public RisshiChillerSimpleQueryHandler(RisshiChiller device, string command, string parameter = null)
  263. : base(device, command, parameter)
  264. {
  265. }
  266. public override bool HandleMessage(MessageBase msg, out bool handled)
  267. {
  268. var result = msg as RisshiChillerMessage;
  269. if (result.IsError)
  270. {
  271. Device.NoteError(result.Data);
  272. }
  273. else
  274. {
  275. var resultArray = result.Data.Split(' ');
  276. if(resultArray.Count() >= 2 && resultArray[0] == _command)
  277. {
  278. Device.NoteError(null);
  279. Device.NoteQueryResult(_command, _parameter, string.Join(",", resultArray, 1, resultArray.Count()-1));
  280. }
  281. else
  282. {
  283. Device.NoteError($"Invalid Response '{result.Data}' for command: {_command}");
  284. }
  285. }
  286. ResponseMessage = msg;
  287. handled = true;
  288. return true;
  289. }
  290. }
  291. }