终端一体化运控平台
Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
 
 
 

170 Zeilen
6.6 KiB

  1. using BPA.Message;
  2. using BPASmartClient.Device;
  3. using BPASmartClient.EventBus;
  4. using BPASmartClient.Helper;
  5. using BPASmartClient.Http;
  6. using BPASmartClient.Message;
  7. using BPASmartClient.Model;
  8. using BPASmartClient.Model.小炒机;
  9. using BPASmartClient.Model.调酒机;
  10. using BPASmartClient.Peripheral;
  11. using Newtonsoft.Json;
  12. using System;
  13. using System.Collections.Generic;
  14. using System.Collections.ObjectModel;
  15. using System.Linq;
  16. using System.Reflection;
  17. using System.Text;
  18. using System.Threading.Tasks;
  19. namespace BPASmartClient.Business
  20. {
  21. /// <summary>
  22. /// 设备管理器,统一管理所有设备资源
  23. /// </summary>
  24. public class DeviceMgr : IPlugin
  25. {
  26. //设备集合
  27. private List<IDevice> devices = new List<IDevice>();
  28. public void Dispose()
  29. {
  30. }
  31. public void Initialize()
  32. {
  33. }
  34. public void Start()
  35. {
  36. LoadDevice();
  37. }
  38. public List<IDevice> GetDevices()
  39. {
  40. return devices;
  41. }
  42. /// <summary>
  43. /// 设备加载
  44. /// </summary>
  45. private void LoadDevice()
  46. {
  47. var result = Plugin.GetInstance().GetPlugin<ConfigMgr>()?.deviceConfigModelJsons;
  48. //var text = TextHelper.GetInstance.ReadTextInfo("StartShop", "DeviceConfig");
  49. //string path = $"{LocaPath.GetInstance().GetDeviceConfigPath}{text}.json";
  50. //if (File.Exists(path))
  51. //{
  52. //string JsonString = File.ReadAllText(path);
  53. //var result = JsonConvert.DeserializeObject<ObservableCollection<DeviceConfigModelJson>>(JsonString);
  54. if (result != null)
  55. {
  56. foreach (var shop in result)//店铺集合
  57. {
  58. foreach (var device in shop.deviceModels)//设备集合
  59. {
  60. string Namespace = device.DeviceNamespace.Substring(0, device.DeviceNamespace.LastIndexOf('.'));
  61. var deviceTemp = Assembly.Load(Namespace).CreateInstance(device.DeviceNamespace) as IDevice;
  62. deviceTemp.Name = device?.DeviceName;
  63. deviceTemp.DeviceId = int.Parse(device.DeviceId);
  64. //通讯模块
  65. List<IPeripheral> peripherals = new List<IPeripheral>();
  66. foreach (var comms in device.communicationDevcies)//通讯集合
  67. {
  68. string IPeripheralNamespace = comms.CommunicationNamespace.Substring(0, comms.CommunicationNamespace.LastIndexOf('.'));
  69. var peripheralTemp = Assembly.Load(IPeripheralNamespace).CreateInstance(comms.CommunicationNamespace) as IPeripheral;
  70. peripheralTemp.variables = comms.variables;
  71. peripheralTemp.communicationPar = comms.communicationPar;
  72. peripherals.Add(peripheralTemp);
  73. }
  74. deviceTemp.Initliaze(peripherals);
  75. this.devices.Add(deviceTemp);
  76. }
  77. }
  78. }
  79. //}
  80. }
  81. public void StartService()
  82. {
  83. this.devices.ForEach(device =>
  84. {
  85. device.StartMain();
  86. #region 获取物料数据
  87. string result = string.Empty;
  88. for (int i = 0; i < 4; i++)
  89. {
  90. try
  91. {
  92. int PushType = i;//0:主料 1:辅料
  93. int clientId = device.DeviceId;
  94. var jsondata = new { clientId, PushType };
  95. string header = $"[/stock/GetItemInfo]_[{DateTime.Now.Ticks}]".AESEncrypt();
  96. string url = $"{InternetInfo.StockServer}GetItemInfo";
  97. result = APIHelper.GetInstance.HttpRequest(url, header, jsondata, RequestType.POST);
  98. if (PushType == 1)
  99. {
  100. new RecipeBomEvent()
  101. {
  102. DeviceId = device.DeviceId,
  103. recipeBoms = JsonConvert.DeserializeObject<RecipeBoms>(result)
  104. }.Publish();
  105. MessageLog.GetInstance.Show("接收到【 API 】获取的辅料信息");
  106. }
  107. else if (PushType == 0)
  108. {
  109. var apiData = JsonConvert.DeserializeObject<OrderMaterialDelivery>(result);
  110. new MaterialDeliveryEvent()
  111. {
  112. DeviceId = device.DeviceId,
  113. orderMaterialDelivery = apiData
  114. }.Publish();
  115. MessageLog.GetInstance.Show("接收到【 API 】获取的物料信息");
  116. apiData?.BatchingInfo?.ForEach(x =>
  117. {
  118. MessageLog.GetInstance.Show($"物料ID:=[{x.BatchingId}],{x.BatchingLoc}号位置:{x.BatchingCount}");
  119. });
  120. }
  121. else if (PushType == 2)//小炒API流程获取,待定
  122. {
  123. new StirFryGoodsEvent()
  124. {
  125. DeviceId = device.DeviceId,
  126. stirFrymessage = JsonConvert.DeserializeObject<StirFryPushMessage>(result)
  127. }.Publish();
  128. MessageLog.GetInstance.Show("接收到【 API 】获取的小炒流程信息");
  129. }
  130. else if (PushType == 3)
  131. {
  132. new MorkMWGoodsEvent()
  133. {
  134. DeviceId = device.DeviceId,
  135. morkMWPushMessage = JsonConvert.DeserializeObject<MORKMWPushMessage>(result)
  136. }.Publish();
  137. MessageLog.GetInstance.Show("接收到【API】获取的调酒机配方信息");
  138. }
  139. }
  140. catch (Exception ex)
  141. {
  142. MessageLog.GetInstance.ShowEx(ex.ToString());
  143. }
  144. }
  145. #endregion
  146. });
  147. }
  148. public void StopService()
  149. {
  150. this.devices.ForEach(device => device.Stop());
  151. }
  152. }
  153. }