终端一体化运控平台
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

340 lines
13 KiB

  1. using BPASmartClient.EventBus;
  2. using BPASmartClient.Helper;
  3. using BPASmartClient.Message;
  4. using BPASmartClient.Model;
  5. using BPASmartClient.Model.冰淇淋.Enum;
  6. using BPASmartClient.Peripheral;
  7. using BPASmartClient.SerialPort;
  8. using System;
  9. using System.Collections.Generic;
  10. using System.Linq;
  11. using System.Threading;
  12. using static BPASmartClient.EventBus.EventBus;
  13. using static BPASmartClient.GSIceCream.MessageDefine;
  14. namespace BPASmartClient.GSIceCream
  15. {
  16. public class IceCreamMachine :BasePeripheral
  17. {
  18. //通讯代理
  19. SerialPortClient commProxy = null;
  20. //是否下发指令,主线程等待
  21. private bool free = false;
  22. //心跳指令
  23. private byte[] cmdHeartDW;
  24. //数据仓库
  25. private DataStorage<byte> dataStorage = new DataStorage<byte>();
  26. //串口COM口
  27. public string PortName { get; set; }
  28. //串口波特率
  29. public string BaudRate { get; set; }
  30. //心跳时间
  31. private DateTime lastRefreshTime = DateTime.MinValue;
  32. //是否在线
  33. public bool OnLine { get { return DateTime.Now.Subtract(lastRefreshTime).TotalSeconds <= 3; } }
  34. public IceCreamMachine()
  35. {
  36. ICMSG_Heart_DW heartDW = new ICMSG_Heart_DW();
  37. cmdHeartDW = IcPack.StructureToByte(heartDW);
  38. }
  39. /// <summary>
  40. /// 主线程开始运行
  41. /// </summary>
  42. public override void Start()
  43. {
  44. try
  45. {
  46. commProxy.Start();
  47. IsConnected = true;
  48. free = false;
  49. MainLoop();
  50. }
  51. catch (Exception ex)
  52. {
  53. MessageLog.GetInstance.Show($"BPASmartClient.GSIceCream 中引发错误,IceCreamMachine 类,描述:[{ex.Message}]");
  54. MessageLog.GetInstance.ShowEx($"BPASmartClient.GSIceCream 中引发错误,IceCreamMachine 类,描述:[{ex.Message}]");
  55. }
  56. }
  57. /// <summary>
  58. /// 停止运行
  59. /// </summary>
  60. public override void Stop()
  61. {
  62. try
  63. {
  64. commProxy.Stop();
  65. IsConnected = false;
  66. free = true;
  67. }
  68. catch (Exception ex)
  69. {
  70. MessageLog.GetInstance.Show($"BPASmartClient.GSIceCream 中引发错误,IceCreamMachine 类,描述:[{ex.Message}]");
  71. MessageLog.GetInstance.ShowEx($"BPASmartClient.GSIceCream 中引发错误,IceCreamMachine 类,描述:[{ex.Message}]");
  72. }
  73. }
  74. private MSG_RESOLVE_STEP currentStep;
  75. private void MainLoop()
  76. {
  77. ThreadManage.GetInstance().StartLong(new Action(() =>
  78. {
  79. if (!free)
  80. {
  81. commProxy.SendData(cmdHeartDW);
  82. }
  83. Thread.Sleep(500);
  84. }), "冰淇淋询问线程");
  85. ThreadManage.GetInstance().StartLong(new Action(() =>
  86. {
  87. ResolveMsg();
  88. }), "冰淇淋解析线程");
  89. }
  90. int contentLength = 0;
  91. int currentContentOffset = 0;
  92. private void ResolveMsg()
  93. {
  94. List<byte> temp = new List<byte>();
  95. //一系列解包
  96. while (dataStorage.GetSize() > 0)
  97. {
  98. byte item = dataStorage.GetData();
  99. switch (currentStep)
  100. {
  101. case MSG_RESOLVE_STEP.NONE:
  102. if (item == MessageDefine.HEADER1)
  103. {
  104. temp.Add(item);
  105. currentStep = MSG_RESOLVE_STEP.HEADER1;
  106. continue;
  107. }
  108. break;
  109. case MSG_RESOLVE_STEP.HEADER1:
  110. if (item == MessageDefine.HEADER2_UP)
  111. {
  112. temp.Add(item);
  113. currentStep = MSG_RESOLVE_STEP.HEADER2;
  114. continue;
  115. }
  116. else
  117. {
  118. temp.Clear();
  119. currentStep = MSG_RESOLVE_STEP.NONE;
  120. continue;
  121. }
  122. case MSG_RESOLVE_STEP.HEADER2:
  123. switch ((IC_CMD)item)
  124. {
  125. case IC_CMD.HEART:
  126. temp.Add(item);
  127. contentLength = MessageDefine.MSG_LENGTH[(IC_CMD)item];
  128. currentContentOffset = 0;
  129. currentStep = MSG_RESOLVE_STEP.CMD;
  130. break;
  131. default:
  132. temp.Clear();
  133. currentStep = MSG_RESOLVE_STEP.NONE;
  134. break;
  135. }
  136. break;
  137. }
  138. int retry = 3;
  139. while (dataStorage.GetSize() < contentLength + 2 && retry >= 0)
  140. {
  141. retry--;
  142. Thread.Sleep(100);
  143. }
  144. if (retry < 0)
  145. {
  146. currentStep = MSG_RESOLVE_STEP.NONE;
  147. currentContentOffset = 0;
  148. contentLength = 0;
  149. continue;
  150. }
  151. while (currentContentOffset < contentLength)
  152. {
  153. item = dataStorage.GetData();
  154. temp.Add(item);
  155. currentContentOffset++;
  156. }
  157. retry = 3;
  158. while (dataStorage.GetSize() < 2 && retry >= 0)
  159. {
  160. retry--;
  161. Thread.Sleep(100);
  162. }
  163. temp.Add(dataStorage.GetData());
  164. temp.Add(dataStorage.GetData());
  165. ProcessMsg(temp.ToArray());
  166. currentStep = MSG_RESOLVE_STEP.NONE;
  167. continue;
  168. }
  169. Thread.Sleep(5);
  170. }
  171. private void ProcessHeart(ICMSG_Heart_UP heartUpMsg)
  172. {
  173. status["CurrentMode"] = heartUpMsg.MS;
  174. status["YLWD"] = BitConverter.ToInt16(new byte[] { heartUpMsg.YLWD_L,heartUpMsg.YLWD_H },0);
  175. status["HQWD"] = BitConverter.ToInt16(new byte[] { heartUpMsg.HQWD_L,heartUpMsg.HQWD_H },0);
  176. status["HJWD"] = BitConverter.ToInt16(new byte[] { heartUpMsg.HJWD_L,heartUpMsg.HJWD_H },0);
  177. status["DL"] = BitConverter.ToInt16(new byte[] { heartUpMsg.DL_L,heartUpMsg.DL_H },0);
  178. status["Fault"] = (MORKI_FAULT)BitConverter.ToInt16(new byte[] { heartUpMsg.GZ_L,heartUpMsg.GZ_H },0);
  179. status["CXB"] = heartUpMsg.CXB;
  180. status["DLCompleted"] = (heartUpMsg.DLTJ >> 4 & 1) == 1;
  181. if (RTrig.GetInstance("打料完成检测").Start((bool)status["DLCompleted"]))
  182. {
  183. MessageLog.GetInstance.Show("打料完成");
  184. }
  185. if (RTrig.GetInstance("打料中检测").Start(!(bool)status["DLCompleted"]))
  186. {
  187. MessageLog.GetInstance.Show("打料中");
  188. }
  189. }
  190. private void ProcessModeUp(ICMSG_MODE_UP modeUpMsg)
  191. {
  192. MessageLog.GetInstance.Show(string.Format("模式返回为:{0}",modeUpMsg.Mode));
  193. }
  194. public void ProcessMsg(byte[] data)
  195. {
  196. lastRefreshTime = DateTime.Now;
  197. try
  198. {
  199. if (data.Length < 5)
  200. return;
  201. switch (data[2])
  202. {
  203. case (byte)IC_CMD.HEART:
  204. var msg = IcPack.ByteToStructure<ICMSG_Heart_UP>(data.ToArray());
  205. ProcessHeart(msg);
  206. break;
  207. case (byte)IC_CMD.MODE:
  208. var modeUp = IcPack.ByteToStructure<ICMSG_MODE_UP>(data.ToArray());
  209. ProcessModeUp(modeUp);
  210. break;
  211. }
  212. }
  213. catch (Exception ex)
  214. {
  215. }
  216. }
  217. protected override void InitStatus()
  218. {
  219. status["YLWD"] = (short)0;
  220. status["HQWD"] = (short)0;
  221. status["HJWD"] = (short)0;
  222. status["DL"] = (short)0;
  223. status["DY"] = (short)0;
  224. status["CurrentMode"] = MORKI_MODE.待机模式;
  225. status["Fault"] = MORKI_FAULT.未发生故障;
  226. status["CXB"] = (byte)0;
  227. status["CXB_Threshold"] = (byte)0;
  228. status["DLCompleted"] = true;
  229. }
  230. public override void Init()
  231. {
  232. commProxy = new SerialPortClient(PortName,(BaudRates)Enum.Parse(typeof(BaudRates),BaudRate));
  233. commProxy.SetDataStorage(dataStorage);
  234. //广深冰淇淋机模式设置
  235. EventBus.EventBus.GetInstance().Subscribe<GSIceCream_ModeSetEvent>(DeviceId,delegate (IEvent @event,EventCallBackHandle callBack)
  236. {
  237. try
  238. {
  239. free = true;
  240. Thread.Sleep(200);
  241. var data = IcPack.StructureToByte(ICMSG_MODE_DW.Build(((GSIceCream_ModeSetEvent)@event).Mode));
  242. commProxy.SendData(data);
  243. Thread.Sleep(200);
  244. free = false;
  245. MessageLog.GetInstance.Show(string.Format("设置模式[{0}]",Enum.GetName(typeof(MORKI_MODE),((GSIceCream_ModeSetEvent)@event).Mode)));
  246. }
  247. catch (Exception ex)
  248. {
  249. MessageLog.GetInstance.Show($"BPASmartClient.GSIceCream 中引发错误,IceCreamMachine 类,描述:[{ex.Message}]");
  250. MessageLog.GetInstance.ShowEx($"BPASmartClient.GSIceCream 中引发错误,IceCreamMachine 类,描述:[{ex.Message}]");
  251. }
  252. });
  253. //广深冰淇淋机打料
  254. EventBus.EventBus.GetInstance().Subscribe<GSIceCream_DischargeEvent>(DeviceId,delegate (IEvent @event,EventCallBackHandle callBack)
  255. {
  256. try
  257. {
  258. if ((MORKI_FAULT)status["Fault"] != MORKI_FAULT.未发生故障)
  259. {
  260. MessageLog.GetInstance.Show(string.Format("当前存在故障[{0}%],不允许制作",(MORKI_FAULT)status["Fault"]));
  261. new GSIceCream_EndCookEvent() { Id = DeviceId,Status = false }.Publish();
  262. return;
  263. }
  264. if ((byte)status["CXB"] <= 86)
  265. {
  266. MessageLog.GetInstance.Show(string.Format("当前成型比[{0}%],低于86%,不允许制作",(byte)status["CXB"]));
  267. new GSIceCream_EndCookEvent() { Id = DeviceId,Status = false }.Publish();
  268. return;
  269. }
  270. bool modeRight = (MORKI_MODE)status["CurrentMode"] == MORKI_MODE.制冷模式;
  271. if (!modeRight)
  272. {
  273. free = true;
  274. Thread.Sleep(200);
  275. var temp = IcPack.StructureToByte(ICMSG_MODE_DW.Build(MORKI_MODE.制冷模式));
  276. commProxy.SendData(temp);
  277. Thread.Sleep(200);
  278. free = false;
  279. MessageLog.GetInstance.Show(string.Format("出料操作->设置模式[{0}]",MORKI_MODE.制冷模式));
  280. DateTime freeTime = DateTime.Now.AddSeconds(5);
  281. while (DateTime.Now < freeTime)
  282. {
  283. Thread.Sleep(10);
  284. modeRight = (MORKI_MODE)status["CurrentMode"] == MORKI_MODE.制冷模式;
  285. if (modeRight)
  286. break;
  287. }
  288. }
  289. if (modeRight)
  290. {
  291. free = true;
  292. Thread.Sleep(200);
  293. var data = IcPack.StructureToByte(ICMSG_MODE_DW.Build(MORKI_MODE.打料));
  294. commProxy.SendData(data);
  295. Thread.Sleep(200);
  296. free = false;
  297. new GSIceCream_EndCookEvent() { Id = DeviceId,Status =true}.Publish();
  298. MessageLog.GetInstance.Show(string.Format("出料操作->设置模式[{0}]",MORKI_MODE.打料));
  299. }
  300. else
  301. {
  302. MessageLog.GetInstance.Show(string.Format("出料操作->模式切换失败,当前模式[{0}],不允许出料",(MORKI_MODE)status["CurrentMode"]));
  303. new GSIceCream_EndCookEvent() { Id = DeviceId,Status = false }.Publish();
  304. }
  305. }
  306. catch (Exception ex)
  307. {
  308. MessageLog.GetInstance.Show($"BPASmartClient.GSIceCream 中引发错误,IceCreamMachine 类,描述:[{ex.Message}]");
  309. MessageLog.GetInstance.ShowEx($"BPASmartClient.GSIceCream 中引发错误,IceCreamMachine 类,描述:[{ex.Message}]");
  310. }
  311. });
  312. }
  313. }
  314. }