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.
 
 

177 lines
6.5 KiB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using HBLConsole.Abstract;
  7. using BPA.Message;
  8. using HBLConsole.Model;
  9. using HBLConsole.Service;
  10. using BPA.Message.Enum;
  11. using HBLConsole.GVL;
  12. using BPA.Utility;
  13. using Newtonsoft.Json;
  14. using HBLConsole.Communication;
  15. namespace HBLConsole.Business.MessageServer
  16. {
  17. public class Base : AbstractMessageServer
  18. {
  19. public override void AddOrder<T>(T orderInfo)
  20. {
  21. if (orderInfo == null) return;
  22. if (orderInfo is MorkOrderPush morkOrderpush)
  23. {
  24. OrderData order = Json<MorkOrderPushPar>.Data.morkOrderPushes.Find(par => par.OrderPush?.SuborderId == morkOrderpush.SuborderId);
  25. if (order == null)//防止重复订单
  26. {
  27. Json<MorkOrderPushPar>.Data.morkOrderPushes.Add(new OrderData()
  28. {
  29. OrderStatus = ORDER_STATUS.WAIT,
  30. IsSelected = true,
  31. OrderPush = morkOrderpush
  32. });
  33. ActionOperate.GetInstance.Send("AddOrder",morkOrderpush);
  34. ActionOperate.GetInstance.Send("DataParse",morkOrderpush);
  35. }
  36. }
  37. }
  38. /// <summary>
  39. /// 接收来自MQTT 推送的物料信息
  40. /// </summary>
  41. /// <typeparam name="T"></typeparam>
  42. /// <param name="batchingInfo"></param>
  43. public override void GetBatchingInfo<T>(T batchingInfo)
  44. {
  45. if (batchingInfo == null) return;
  46. if (batchingInfo is OrderMaterialDelivery BatchingInfos)
  47. {
  48. Json<BatchingInfoPar>.Data.orderMaterialDelivery = BatchingInfos;
  49. MessageLog.GetInstance.Show("收到推送的物料信息");
  50. }
  51. }
  52. /// <summary>
  53. /// 通过接口获取物料信息
  54. /// </summary>
  55. /// <param name="ClientId"></param>
  56. public override void GetBatchingInfo(int ClientId)
  57. {
  58. string result = string.Empty;
  59. for (int i = 0; i < 2; i++)
  60. {
  61. try
  62. {
  63. int PushType = i;//0:主料 1:辅料
  64. var jsondata = new { ClientId, PushType };
  65. string header = $"[{InternetInfo.StockServer}/GetItemInfo]_[{DateTime.Now.Ticks}]".AESEncrypt();
  66. string url = $"{InternetInfo.ApiAddress}{InternetInfo.StockServer}/GetItemInfo";
  67. //string url = $"http://192.168.0.62:7000{InternetInfo.StockServer}/GetItemInfo";
  68. result = APIHelper.GetInstance.HttpRequest(url, header, jsondata, RequestType.POST);
  69. if (PushType == 1)
  70. {
  71. Json<BatchingInfoPar>.Data.recipeBoms = JsonConvert.DeserializeObject<RecipeBoms>(result);
  72. WritePlcData();
  73. }
  74. else if (PushType == 0)
  75. {
  76. Json<BatchingInfoPar>.Data.orderMaterialDelivery = JsonConvert.DeserializeObject<OrderMaterialDelivery>(result);
  77. }
  78. }
  79. catch (Exception ex)
  80. {
  81. MessageLog.GetInstance.Show(ex.ToString());
  82. }
  83. }
  84. MessageLog.GetInstance.Show("【物料信息】");
  85. Json<BatchingInfoPar>.Data.orderMaterialDelivery?.BatchingInfo?.ForEach(x =>
  86. {
  87. MessageLog.GetInstance.Show($"{x.BatchingLoc}号位置:{x.BatchingCount}");
  88. });
  89. }
  90. /// <summary>
  91. /// 接收MQTT 推送过来的辅料信息
  92. /// </summary>
  93. /// <typeparam name="T"></typeparam>
  94. /// <param name="recipeBomInfo"></param>
  95. public override void GetRecipeBom<T>(T recipeBomInfo)
  96. {
  97. if (recipeBomInfo == null) return;
  98. if (recipeBomInfo is RecipeBoms recipeBom)
  99. {
  100. Json<BatchingInfoPar>.Data.recipeBoms = recipeBom;
  101. MessageLog.GetInstance.Show("接收到辅料信息");
  102. }
  103. WritePlcData();
  104. }
  105. /// <summary>
  106. /// 写配方数据到PLC
  107. /// </summary>
  108. private void WritePlcData()
  109. {
  110. return;
  111. //写配方数据到PLC
  112. List<ushort> recipeBoms = new List<ushort>();
  113. foreach (var item in Json<BatchingInfoPar>.Data.recipeBoms.RecipeIds)
  114. {
  115. foreach (var rec in item.Recipes)
  116. {
  117. recipeBoms.Add((ushort)rec);
  118. }
  119. }
  120. if (recipeBoms.Count > 0)
  121. {
  122. if (ModbusTcpHelper.GetInstance.Write(1100, WriteType.HoldingRegisters, recipeBoms.ToArray()))
  123. {
  124. MessageLog.GetInstance.Show("成功写入配方数据");
  125. }
  126. }
  127. }
  128. /// <summary>
  129. /// 订单状态改变
  130. /// </summary>
  131. /// <param name="subOrderId"></param>
  132. /// <param name="status"></param>
  133. /// <returns></returns>
  134. public override bool OrderStatusChange(string subOrderId, ORDER_STATUS status)
  135. {
  136. string result = string.Empty;
  137. OrderStatusChange orderStatusChange = new OrderStatusChange() { CookingStatus = status, SuborderId = subOrderId };
  138. try
  139. {
  140. string header = $"[{InternetInfo.OrderServer}/order/robotstatuschange]_[{DateTime.Now.Ticks}]".AESEncrypt();
  141. string url = $"{InternetInfo.ApiAddress}{InternetInfo.OrderServer}/order/robotstatuschange";
  142. result = APIHelper.GetInstance.HttpRequest(url, header, orderStatusChange, RequestType.POST);
  143. }
  144. catch (Exception ex)
  145. {
  146. MessageLog.GetInstance.Show(ex.ToString());
  147. }
  148. var res = JsonConvert.DeserializeObject<OrderStatusRsp>(result);
  149. return res == null ? false: res.isSuccess;
  150. }
  151. public class OrderStatusRsp
  152. {
  153. /// <summary>
  154. /// 子订单ID
  155. /// </summary>
  156. public string SuborderId { get; set; }
  157. /// <summary>
  158. /// 订单状态
  159. /// </summary>
  160. public int Result { get; set; } = 0;
  161. public bool isSuccess { get; set; }
  162. public bool data { get; set; }
  163. public string msg { get; set; }
  164. }
  165. }
  166. }