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

317 lines
12 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. new KLMCoffee_MakeCoffeeEvent() { DeviceId=DeviceId, KLMDrinkFaultCode = (KLMDrinkFaultType)(morkT.morkOrderPushesCoffee.ElementAt(0).Loc) }.Publish(); //接咖啡控制 //DrCoffeeDrinksCode.热水
  86. morkT.IsCoffeeMake = true;
  87. }
  88. else if (morkT.MakeCoffeeFinish && IsHealth && morkT.morkOrderPushesCoffee.Count > 0 && morkT.IsCoffeeMake)
  89. {
  90. OrderChange(morkT.morkOrderPushesCoffee.ElementAt(0).SuborderId, BPA.Message.Enum.ORDER_STATUS.COMPLETED_TAKE);
  91. DeviceProcessLogShow($"{morkT.morkOrderPushesCoffee.ElementAt(0).GoodName}制作完成");
  92. if (morkT.morkOrderPushesCoffee.TryDequeue(out OrderLocInfo orderloc))
  93. {
  94. morkT.IsCoffeeMake = false;
  95. morkT.MakeCoffeeFinish = false;
  96. Thread.Sleep(1000);
  97. };
  98. }
  99. }
  100. private T McuRead<T>(string tagName, object par)
  101. {
  102. new ReadMcu() { DeviceId = DeviceId, TagName = tagName, ReadPar = par };
  103. if (peripheralStatus.ContainsKey(tagName))
  104. {
  105. if (peripheralStatus[tagName] != null)
  106. {
  107. return (T)peripheralStatus[tagName];
  108. }
  109. }
  110. return default;
  111. }
  112. public void SimOrder<T>(T simOrder)
  113. {
  114. }
  115. public override void DoMain()
  116. {
  117. if (Json<KeepDataBase>.Data.IsVerify)
  118. {
  119. IsHealth = true;
  120. }
  121. IsHealth = true;
  122. serverInit();
  123. DataParse();
  124. }
  125. private void serverInit()
  126. {
  127. EventBus.EventBus.GetInstance().Subscribe<MaterialDeliveryEvent>(DeviceId, delegate (IEvent @event, EventCallBackHandle callBack)
  128. {
  129. if (@event == null) return;
  130. if (@event is MaterialDeliveryEvent material)
  131. {
  132. orderMaterialDelivery = material.orderMaterialDelivery;
  133. }
  134. });
  135. }
  136. private void DataParse()
  137. {
  138. EventBus.EventBus.GetInstance().Subscribe<DoOrderEvent>(DeviceId, delegate (IEvent @event, EventCallBackHandle callBackHandle)
  139. {
  140. if (@event == null) return;
  141. if (@event is DoOrderEvent order)
  142. {
  143. if (order.MorkOrder.GoodBatchings == null) return;
  144. OrderCount++;
  145. DeviceProcessLogShow($"接收到{OrderCount}次订单");
  146. batchings = PolymerBatching.BuildAll();
  147. //商品类型
  148. GOODS_TYPE currentGoodsType = GOODS_TYPE.NEITHER;
  149. foreach (var item in order.MorkOrder.GoodBatchings)
  150. {
  151. var res = orderMaterialDelivery?.BatchingInfo?.FirstOrDefault(p => p.BatchingId == item.BatchingId);
  152. if (res != null)
  153. {
  154. //验证商品是做的某种饮料
  155. if (ValidateGoodsByBatching(res.BatchingLoc) != GOODS_TYPE.NEITHER)
  156. {
  157. //获取当前物料所属商品类型
  158. currentGoodsType = ValidateGoodsByBatching(res.BatchingLoc);
  159. }
  160. switch (batchings[res.BatchingLoc].BatchingClass)
  161. {
  162. case BATCHING_CLASS.HOLDER:
  163. holderLoc = res.BatchingLoc;
  164. break;
  165. case BATCHING_CLASS.MAIN_MATERIAL:
  166. mainMaterialLoc = res.BatchingLoc;
  167. break;
  168. }
  169. //根据商品类型执行具体制作流程
  170. switch (currentGoodsType)
  171. {
  172. case GOODS_TYPE.COFFEE:
  173. if (morkT.morkOrderPushesCoffee.FirstOrDefault(p => p.SuborderId == order.MorkOrder.SuborderId) == null)
  174. {
  175. morkT.morkOrderPushesCoffee.Enqueue(new OrderLocInfo()
  176. {
  177. SuborderId = order.MorkOrder.SuborderId,
  178. BatchingId = res.BatchingId,
  179. Loc = ushort.Parse(mainMaterialLoc),
  180. GoodName = order.MorkOrder.GoodsName,
  181. });
  182. }
  183. break;
  184. case GOODS_TYPE.NEITHER:
  185. DeviceProcessLogShow("未知的商品类型");
  186. break;
  187. }
  188. }
  189. }
  190. }
  191. });
  192. }
  193. public override void Stop()
  194. {
  195. }
  196. public override void ReadData()
  197. {
  198. }
  199. private bool bFirstTrig_Coffee = false;
  200. public override void MainTask()
  201. {
  202. GetStatus("CoffeeStatus", new Action<object>((o) =>
  203. {
  204. if (o is int b)
  205. {
  206. morkT.coffeeState = (K95SysTemStatus)b;
  207. }
  208. }));
  209. GetStatus("CoffeeIsConnected", new Action<object>((o) =>
  210. {
  211. if (o is bool b)
  212. {
  213. morkT.KLMCoffeeIsConnected = b;
  214. }
  215. }));
  216. if ( morkT.morkOrderPushesCoffee.Count > 0) {
  217. if (morkT.KLMCoffeeIsConnected)
  218. {
  219. if (morkT.IsCoffeeMake)
  220. {
  221. EventBus.EventBus.GetInstance().Subscribe<KLMCoffee_CoffeEndCookEvent>(DeviceId, delegate (IEvent @event, EventCallBackHandle callBack)
  222. {
  223. bFirstTrig_Coffee = false;
  224. morkT.MakeCoffeeFinish = true;
  225. });
  226. if (morkT.MakeCoffeeFinish != true)
  227. {
  228. if (!bFirstTrig_Coffee)
  229. {
  230. bFirstTrig_Coffee = true;
  231. delayTimeOut_Coffee = DateTime.Now;
  232. }
  233. else if (DateTime.Now.Subtract(delayTimeOut_Coffee).TotalSeconds > 60 && bFirstTrig_Coffee == true)
  234. {
  235. DeviceProcessLogShow("接咖啡超时,接咖啡结束,等待取咖啡");
  236. bFirstTrig_Coffee = false;
  237. morkT.MakeCoffeeFinish = true;
  238. }
  239. }
  240. }
  241. DoCoffee();
  242. }
  243. else
  244. {
  245. MessageLog.GetInstance.ShowEx("未读取到咖啡机设备心跳");
  246. }
  247. }
  248. }
  249. public override void ResetProgram()
  250. {
  251. }
  252. public override void SimOrder()
  253. {
  254. EventBus.EventBus.GetInstance().Subscribe<MorkTSimOrder>(0, delegate (IEvent @event, EventCallBackHandle callBackHandle)
  255. {
  256. string guid = Guid.NewGuid().ToString();
  257. if (@event != null && @event is MorkTSimOrder msm)
  258. {
  259. DeviceProcessLogShow("----开始模拟订单----");
  260. morkT.morkOrderPushesCoffee.Enqueue(new OrderLocInfo() { Loc = (ushort)msm.KLMDrinkFaultCode, SuborderId = guid, GoodName = "模拟咖啡订单" });
  261. }
  262. });
  263. /*
  264. string aa = calLrcCommon("01" + "05" + "0000000D");
  265. string bb = ":01010001" + aa + "\r\n";
  266. */
  267. }
  268. public static string calLrcCommon(string data)
  269. {
  270. try
  271. {
  272. if (data.Length % 2 != 0)
  273. {
  274. data = data + "0";
  275. }
  276. int total = 0;
  277. int len = data.Length;
  278. int num = 0;
  279. while (num < len)
  280. {
  281. string s = data.Substring(num, 2);
  282. total += Convert.ToInt32(s, 16);
  283. num += 2;
  284. }
  285. total = ~total + 1;
  286. string checkSum = (total & 255).ToString("x").ToUpper();
  287. while (checkSum.Length < 2)
  288. {
  289. checkSum = "0" + checkSum;
  290. }
  291. return checkSum;
  292. }
  293. catch (Exception)
  294. {
  295. }
  296. return "";
  297. }
  298. }
  299. }