终端一体化运控平台
25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Control_MORKJC.cs 13 KiB

1 년 전
1 년 전
1 년 전
1 년 전
1 년 전
2 년 전
1 년 전
2 년 전
1 년 전
1 년 전
1 년 전
2 년 전
2 년 전
1 년 전
2 년 전
2 년 전
1 년 전
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  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. }