终端一体化运控平台
Não pode escolher mais do que 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.

DeviceMgr.cs 6.0 KiB

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