终端一体化运控平台
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.
 
 
 

186 lines
7.5 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.Peripheral;
  9. using Newtonsoft.Json;
  10. using System;
  11. using System.Collections.Generic;
  12. using System.Collections.ObjectModel;
  13. using System.Linq;
  14. using System.Reflection;
  15. using System.Text;
  16. using System.Threading.Tasks;
  17. namespace BPASmartClient.Business
  18. {
  19. /// <summary>
  20. /// 设备管理器,统一管理所有设备资源
  21. /// </summary>
  22. public class DeviceMgr : IPlugin
  23. {
  24. //设备集合
  25. private List<IDevice> devices = new List<IDevice>();
  26. public void Dispose()
  27. {
  28. }
  29. public void Initialize()
  30. {
  31. }
  32. public void Start()
  33. {
  34. LoadDevice();
  35. }
  36. public List<IDevice> GetDevices()
  37. {
  38. return devices;
  39. }
  40. /// <summary>
  41. /// 设备加载
  42. /// </summary>
  43. private void LoadDevice()
  44. {
  45. #region 启动设备对象
  46. var text = TextHelper.GetInstance.ReadTextInfo("StartShop", "DeviceConfig");
  47. string path = $"{LocaPath.GetInstance().GetDeviceConfigPath}{text}.json";
  48. if (File.Exists(path))
  49. {
  50. string JsonString = File.ReadAllText(path);
  51. var result = JsonConvert.DeserializeObject<ObservableCollection<DeviceConfigModelJson>>(JsonString);
  52. if (result != null)
  53. {
  54. foreach (var shop in result)//店铺集合
  55. {
  56. foreach (var device in shop.deviceModels)//设备集合
  57. {
  58. string Namespace = device.DeviceNamespace.Substring(0, device.DeviceNamespace.LastIndexOf('.'));
  59. //string startModel = device.DeviceNamespace.Substring(device.DeviceNamespace.LastIndexOf('.') + 1);
  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. //string IPeripheralStartModel = comms.CommunicationNamespace.Substring(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. #endregion
  81. //var devices = Plugin.GetInstance().GetPlugin<ConfigMgr>().GetDeviceConfigs();
  82. //foreach (var device in devices)
  83. //{
  84. // var deviceTemp = Assembly.Load(device.Module.Substring(0, device.Module.LastIndexOf('.'))).CreateInstance(device.Module) as IDevice;
  85. // deviceTemp.Name = device.Name;
  86. // deviceTemp.DeviceId = device.DeviceId;
  87. // foreach (var pars in device.Parameters)
  88. // {
  89. // deviceTemp.GetType().GetProperty(pars.Key).SetValue(deviceTemp, Convert.ChangeType(pars.Value, deviceTemp.GetType().GetProperty(pars.Key).PropertyType));
  90. // }
  91. // List<IPeripheral> peripherals = new List<IPeripheral>();
  92. // foreach (var peripheral in device.Peripherals)
  93. // {
  94. // var peripheralTemp = Assembly.Load(peripheral.Module.Substring(0, peripheral.Module.LastIndexOf('.'))).CreateInstance(peripheral.Module) as IPeripheral;
  95. // foreach (var pars in peripheral.Parameters)
  96. // {
  97. // peripheralTemp.GetType().GetProperty(pars.Key).SetValue(peripheralTemp, Convert.ChangeType(pars.Value, peripheralTemp.GetType().GetProperty(pars.Key).PropertyType));
  98. // }
  99. // peripherals.Add(peripheralTemp);
  100. // }
  101. // deviceTemp.Initliaze(peripherals);
  102. // this.devices.Add(deviceTemp);
  103. //}
  104. }
  105. public void StartService()
  106. {
  107. this.devices.ForEach(device =>
  108. {
  109. device.StartMain();
  110. #region 获取物料数据
  111. string result = string.Empty;
  112. for (int i = 0; i < 2; i++)
  113. {
  114. try
  115. {
  116. int PushType = i;//0:主料 1:辅料
  117. int clientId = device.DeviceId;
  118. var jsondata = new { clientId, PushType };
  119. string header = $"[/stock/GetItemInfo]_[{DateTime.Now.Ticks}]".AESEncrypt();
  120. string url = $"{InternetInfo.StockServer}GetItemInfo";
  121. result = APIHelper.GetInstance.HttpRequest(url, header, jsondata, RequestType.POST);
  122. if (PushType == 1)
  123. {
  124. device.recipeBoms = JsonConvert.DeserializeObject<RecipeBoms>(result);
  125. //EventBus.EventBus.GetInstance().Publish(new RecipeBomEvent()
  126. //{
  127. // DeviceId = device.DeviceId,
  128. // recipeBoms = JsonConvert.DeserializeObject<RecipeBoms>(result)
  129. //});
  130. MessageLog.GetInstance.Show("接收到辅料信息");
  131. }
  132. else if (PushType == 0)
  133. {
  134. var apiData = JsonConvert.DeserializeObject<OrderMaterialDelivery>(result);
  135. device.orderMaterialDelivery = apiData;
  136. //EventBus.EventBus.GetInstance().Publish(new MaterialDeliveryEvent()
  137. //{
  138. // DeviceId = device.DeviceId,
  139. // orderMaterialDelivery = apiData
  140. //});
  141. MessageLog.GetInstance.Show("接收到物料信息");
  142. apiData?.BatchingInfo?.ForEach(x =>
  143. {
  144. MessageLog.GetInstance.Show($"物料ID:=[{x.BatchingId}],{x.BatchingLoc}号位置:{x.BatchingCount}");
  145. });
  146. }
  147. }
  148. catch (Exception ex)
  149. {
  150. MessageLog.GetInstance.ShowEx(ex.ToString());
  151. }
  152. }
  153. #endregion
  154. });
  155. }
  156. public void StopService()
  157. {
  158. this.devices.ForEach(device => device.Stop());
  159. }
  160. }
  161. }