终端一体化运控平台
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 

169 linhas
6.5 KiB

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