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