终端一体化运控平台
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.
 
 
 

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