终端一体化运控平台
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 

334 行
13 KiB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Configuration;
  4. using System.Linq;
  5. using System.Threading;
  6. using System.Collections.Concurrent;
  7. using System.Diagnostics;
  8. using System.Threading.Tasks;
  9. using BPASmartClient.Device;
  10. using BPA.Message.Enum;
  11. using BPA.Message;
  12. using BPASmartClient.Helper;
  13. using BPASmartClient.Model.咖啡机.Enum;
  14. using BPASmartClient.Model;
  15. using BPASmartClient.EventBus;
  16. using static BPASmartClient.EventBus.EventBus;
  17. using BPASmartClient.Model.PLC;
  18. using BPASmartClient.Model.单片机;
  19. using BPASmartClient.Business;
  20. using BPASmartClient.KLMCoffee.Protocal;
  21. using BPASmartClient.Message;
  22. namespace BPASmartClient.MorkTSingle
  23. {
  24. /*
  25. * 冰淇淋咖啡机组合套装
  26. * 物料位置:
  27. * 1:冰淇料
  28. * 2:冰淇淋杯
  29. * 5:咖啡
  30. * 6:咖啡杯
  31. * 9: 茶
  32. * 10: 茶杯
  33. */
  34. public class Control_MORKJC : BaseDevice
  35. {
  36. public override global::BPA.Message.Enum.DeviceClientType DeviceType { get { return BPA.Message.Enum.DeviceClientType.MORKT; } }
  37. GVL_MORKJC morkT = new GVL_MORKJC();
  38. //物料存放位置
  39. private Dictionary<string, PolymerBatching> batchings = new Dictionary<string, PolymerBatching>();
  40. //容器位置
  41. private string holderLoc;
  42. //主料位置
  43. private string mainMaterialLoc;
  44. /// <summary>
  45. /// 果汁机做法,true:热饮,false:冷饮
  46. /// </summary>
  47. private bool GuMake = false;
  48. private void OrderChange(string subid, ORDER_STATUS oRDER_STATUS)
  49. {
  50. EventBus.EventBus.GetInstance().Publish(new OrderStatusChangedEvent() { Status = oRDER_STATUS, SubOrderId = subid, deviceClientType = DeviceType });
  51. }
  52. //private SerialPortClient commProxy;
  53. public void ConnectOk()
  54. {
  55. }
  56. ConcurrentQueue<MorkOrderPush> morkOrderPushes = new ConcurrentQueue<MorkOrderPush>();
  57. /// <summary>
  58. ///
  59. /// </summary>
  60. /// <param name="batchingLoc">物料位置</param>
  61. private GOODS_TYPE ValidateGoodsByBatching(string batchingLoc)
  62. {
  63. if (batchings.ContainsKey(batchingLoc))
  64. return batchings[batchingLoc].GoodsType;
  65. return GOODS_TYPE.NEITHER;
  66. }
  67. private AutoResetEvent are = new AutoResetEvent(false);
  68. private void GetStatus(string key, Action<object> action)
  69. {
  70. if (peripheralStatus.ContainsKey(key))
  71. {
  72. if (peripheralStatus[key] != null)
  73. {
  74. action?.Invoke(peripheralStatus[key]);
  75. }
  76. }
  77. }
  78. DateTime delayTimeOut_Coffee;
  79. private void DoCoffee()
  80. {
  81. if (IsHealth && morkT.morkOrderPushesCoffee.Count > 0 && !morkT.IsCoffeeMake /*&& morkT.coffeeState== K95SysTemStatus.空闲状态*/)
  82. {
  83. Thread.Sleep(1000);
  84. OrderChange(morkT.morkOrderPushesCoffee.ElementAt(0).SuborderId, BPA.Message.Enum.ORDER_STATUS.COOKING);
  85. if (!GVL_MORKJC.CoffeeKind)
  86. {
  87. new KLMCoffee_MakeCoffeeEvent() { DeviceId = DeviceId, KLMDrinkFaultCode = (KLMDrinkFaultType)(morkT.morkOrderPushesCoffee.ElementAt(0).Loc) }.Publish(); //接咖啡控制 //DrCoffeeDrinksCode.热水
  88. }
  89. else
  90. {
  91. new DRCoffee_MakeCoffeeEvent() { DeviceId = DeviceId, DrinkCode = (DrCoffeeDrinksCode)int.Parse(mainMaterialLoc) }.Publish();
  92. }
  93. morkT.IsCoffeeMake = true;
  94. }
  95. else if (morkT.MakeCoffeeFinish && IsHealth && morkT.morkOrderPushesCoffee.Count > 0 && morkT.IsCoffeeMake)
  96. {
  97. OrderChange(morkT.morkOrderPushesCoffee.ElementAt(0).SuborderId, BPA.Message.Enum.ORDER_STATUS.COMPLETED_TAKE);
  98. DeviceProcessLogShow($"{morkT.morkOrderPushesCoffee.ElementAt(0).GoodName}制作完成");
  99. if (morkT.morkOrderPushesCoffee.TryDequeue(out OrderLocInfo orderloc))
  100. {
  101. morkT.IsCoffeeMake = false;
  102. morkT.MakeCoffeeFinish = false;
  103. Thread.Sleep(1000);
  104. };
  105. }
  106. }
  107. private T McuRead<T>(string tagName, object par)
  108. {
  109. new ReadMcu() { DeviceId = DeviceId, TagName = tagName, ReadPar = par };
  110. if (peripheralStatus.ContainsKey(tagName))
  111. {
  112. if (peripheralStatus[tagName] != null)
  113. {
  114. return (T)peripheralStatus[tagName];
  115. }
  116. }
  117. return default;
  118. }
  119. public void SimOrder<T>(T simOrder)
  120. {
  121. }
  122. public override void DoMain()
  123. {
  124. if (Json<KeepDataBase>.Data.IsVerify)
  125. {
  126. IsHealth = true;
  127. }
  128. IsHealth = true;
  129. serverInit();
  130. DataParse();
  131. }
  132. private void serverInit()
  133. {
  134. EventBus.EventBus.GetInstance().Subscribe<MaterialDeliveryEvent>(DeviceId, delegate (IEvent @event, EventCallBackHandle callBack)
  135. {
  136. if (@event == null) return;
  137. if (@event is MaterialDeliveryEvent material)
  138. {
  139. orderMaterialDelivery = material.orderMaterialDelivery;
  140. }
  141. });
  142. }
  143. private void DataParse()
  144. {
  145. EventBus.EventBus.GetInstance().Subscribe<DoOrderEvent>(DeviceId, delegate (IEvent @event, EventCallBackHandle callBackHandle)
  146. {
  147. if (@event == null) return;
  148. if (@event is DoOrderEvent order)
  149. {
  150. if (order.MorkOrder.GoodBatchings == null) return;
  151. OrderCount++;
  152. DeviceProcessLogShow($"接收到{OrderCount}次订单");
  153. batchings = PolymerBatching.BuildAll();
  154. //商品类型
  155. GOODS_TYPE currentGoodsType = GOODS_TYPE.NEITHER;
  156. foreach (var item in order.MorkOrder.GoodBatchings)
  157. {
  158. var res = orderMaterialDelivery?.BatchingInfo?.FirstOrDefault(p => p.BatchingId == item.BatchingId);
  159. if (res != null)
  160. {
  161. //验证商品是做的某种饮料
  162. if (ValidateGoodsByBatching(res.BatchingLoc) != GOODS_TYPE.NEITHER)
  163. {
  164. //获取当前物料所属商品类型
  165. currentGoodsType = ValidateGoodsByBatching(res.BatchingLoc);
  166. }
  167. switch (batchings[res.BatchingLoc].BatchingClass)
  168. {
  169. case BATCHING_CLASS.HOLDER:
  170. holderLoc = res.BatchingLoc;
  171. break;
  172. case BATCHING_CLASS.MAIN_MATERIAL:
  173. mainMaterialLoc = res.BatchingLoc;
  174. break;
  175. }
  176. //根据商品类型执行具体制作流程
  177. switch (currentGoodsType)
  178. {
  179. case GOODS_TYPE.COFFEE:
  180. if (morkT.morkOrderPushesCoffee.FirstOrDefault(p => p.SuborderId == order.MorkOrder.SuborderId) == null)
  181. {
  182. morkT.morkOrderPushesCoffee.Enqueue(new OrderLocInfo()
  183. {
  184. SuborderId = order.MorkOrder.SuborderId,
  185. BatchingId = res.BatchingId,
  186. Loc = ushort.Parse(mainMaterialLoc),
  187. GoodName = order.MorkOrder.GoodsName,
  188. });
  189. }
  190. break;
  191. case GOODS_TYPE.NEITHER:
  192. DeviceProcessLogShow("未知的商品类型");
  193. break;
  194. }
  195. }
  196. }
  197. }
  198. });
  199. }
  200. public override void Stop()
  201. {
  202. }
  203. public override void ReadData()
  204. {
  205. }
  206. private bool bFirstTrig_Coffee = false;
  207. public override void MainTask()
  208. {
  209. GetStatus("CoffeeStatus", new Action<object>((o) =>
  210. {
  211. if (o is int b)
  212. {
  213. morkT.coffeeState = (K95SysTemStatus)b;
  214. }
  215. }));
  216. GetStatus("CoffeeIsConnected", new Action<object>((o) =>
  217. {
  218. if (o is bool b)
  219. {
  220. morkT.KLMCoffeeIsConnected = b;
  221. }
  222. }));
  223. if ( morkT.morkOrderPushesCoffee.Count > 0) {
  224. if (morkT.KLMCoffeeIsConnected)
  225. {
  226. if (morkT.IsCoffeeMake)
  227. {
  228. if (!GVL_MORKJC.CoffeeKind)
  229. {
  230. EventBus.EventBus.GetInstance().Subscribe<KLMCoffee_CoffeEndCookEvent>(DeviceId, delegate (IEvent @event, EventCallBackHandle callBack)
  231. {
  232. bFirstTrig_Coffee = false;
  233. morkT.MakeCoffeeFinish = true;
  234. });
  235. }
  236. else
  237. {
  238. EventBus.EventBus.GetInstance().Subscribe<DRCoffee_CoffeEndCookEvent>(DeviceId, delegate (IEvent @event, EventCallBackHandle callBack)
  239. {
  240. bFirstTrig_Coffee = false;
  241. morkT.MakeCoffeeFinish = true;
  242. });
  243. }
  244. if (morkT.MakeCoffeeFinish != true)
  245. {
  246. if (!bFirstTrig_Coffee)
  247. {
  248. bFirstTrig_Coffee = true;
  249. delayTimeOut_Coffee = DateTime.Now;
  250. }
  251. else if (DateTime.Now.Subtract(delayTimeOut_Coffee).TotalSeconds > 180 && bFirstTrig_Coffee == true)
  252. {
  253. DeviceProcessLogShow("接咖啡超时,接咖啡结束,等待取咖啡");
  254. bFirstTrig_Coffee = false;
  255. morkT.MakeCoffeeFinish = true;
  256. }
  257. }
  258. }
  259. DoCoffee();
  260. }
  261. else
  262. {
  263. MessageLog.GetInstance.ShowEx("未读取到咖啡机设备心跳");
  264. }
  265. }
  266. }
  267. public override void ResetProgram()
  268. {
  269. }
  270. public override void SimOrder()
  271. {
  272. EventBus.EventBus.GetInstance().Subscribe<MorkTSimOrder>(0, delegate (IEvent @event, EventCallBackHandle callBackHandle)
  273. {
  274. string guid = Guid.NewGuid().ToString();
  275. if (@event != null && @event is MorkTSimOrder msm)
  276. {
  277. DeviceProcessLogShow("----开始模拟订单----");
  278. morkT.morkOrderPushesCoffee.Enqueue(new OrderLocInfo() { Loc = (ushort)msm.KLMDrinkFaultCode, SuborderId = guid, GoodName = "模拟咖啡订单" });
  279. }
  280. });
  281. /*string aa = calLrcCommon("01" + "01" + "0001");
  282. string bb = ":01010001" + aa + "\r\n";*/
  283. }
  284. public static string calLrcCommon(string data)
  285. {
  286. try
  287. {
  288. if (data.Length % 2 != 0)
  289. {
  290. data = data + "0";
  291. }
  292. int total = 0;
  293. int len = data.Length;
  294. int num = 0;
  295. while (num < len)
  296. {
  297. string s = data.Substring(num, 2);
  298. total += Convert.ToInt32(s, 16);
  299. num += 2;
  300. }
  301. total = ~total + 1;
  302. string checkSum = (total & 255).ToString("x").ToUpper();
  303. while (checkSum.Length < 2)
  304. {
  305. checkSum = "0" + checkSum;
  306. }
  307. return checkSum;
  308. }
  309. catch (Exception)
  310. {
  311. }
  312. return "";
  313. }
  314. }
  315. }