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

182 lines
7.0 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. var deviceTemp = Assembly.Load(Namespace).CreateInstance(device.DeviceNamespace) as IDevice;
  60. deviceTemp.Name = device?.DeviceName;
  61. deviceTemp.DeviceId = int.Parse(device.DeviceId);
  62. //通讯模块
  63. List<IPeripheral> peripherals = new List<IPeripheral>();
  64. foreach (var comms in device.communicationDevcies)//通讯集合
  65. {
  66. string IPeripheralNamespace = comms.CommunicationNamespace.Substring(0, comms.CommunicationNamespace.LastIndexOf('.'));
  67. var peripheralTemp = Assembly.Load(IPeripheralNamespace).CreateInstance(comms.CommunicationNamespace) as IPeripheral;
  68. peripheralTemp.variables = comms.variables;
  69. peripheralTemp.communicationPar = comms.communicationPar;
  70. peripherals.Add(peripheralTemp);
  71. }
  72. deviceTemp.Initliaze(peripherals);
  73. this.devices.Add(deviceTemp);
  74. }
  75. }
  76. }
  77. }
  78. #endregion
  79. //var devices = Plugin.GetInstance().GetPlugin<ConfigMgr>().GetDeviceConfigs();
  80. //foreach (var device in devices)
  81. //{
  82. // var deviceTemp = Assembly.Load(device.Module.Substring(0, device.Module.LastIndexOf('.'))).CreateInstance(device.Module) as IDevice;
  83. // deviceTemp.Name = device.Name;
  84. // deviceTemp.DeviceId = device.DeviceId;
  85. // foreach (var pars in device.Parameters)
  86. // {
  87. // deviceTemp.GetType().GetProperty(pars.Key).SetValue(deviceTemp, Convert.ChangeType(pars.Value, deviceTemp.GetType().GetProperty(pars.Key).PropertyType));
  88. // }
  89. // List<IPeripheral> peripherals = new List<IPeripheral>();
  90. // foreach (var peripheral in device.Peripherals)
  91. // {
  92. // var peripheralTemp = Assembly.Load(peripheral.Module.Substring(0, peripheral.Module.LastIndexOf('.'))).CreateInstance(peripheral.Module) as IPeripheral;
  93. // foreach (var pars in peripheral.Parameters)
  94. // {
  95. // peripheralTemp.GetType().GetProperty(pars.Key).SetValue(peripheralTemp, Convert.ChangeType(pars.Value, peripheralTemp.GetType().GetProperty(pars.Key).PropertyType));
  96. // }
  97. // peripherals.Add(peripheralTemp);
  98. // }
  99. // deviceTemp.Initliaze(peripherals);
  100. // this.devices.Add(deviceTemp);
  101. //}
  102. }
  103. public void StartService()
  104. {
  105. this.devices.ForEach(device =>
  106. {
  107. device.StartMain();
  108. #region 获取物料数据
  109. string result = string.Empty;
  110. for (int i = 0; i < 2; i++)
  111. {
  112. try
  113. {
  114. int PushType = i;//0:主料 1:辅料
  115. int clientId = device.DeviceId;
  116. var jsondata = new { clientId, PushType };
  117. string header = $"[/stock/GetItemInfo]_[{DateTime.Now.Ticks}]".AESEncrypt();
  118. string url = $"{InternetInfo.StockServer}GetItemInfo";
  119. result = APIHelper.GetInstance.HttpRequest(url, header, jsondata, RequestType.POST);
  120. if (PushType == 1)
  121. {
  122. new RecipeBomEvent()
  123. {
  124. DeviceId = device.DeviceId,
  125. recipeBoms = JsonConvert.DeserializeObject<RecipeBoms>(result)
  126. }.Publish();
  127. MessageLog.GetInstance.Show("接收到【 API 】获取的辅料信息");
  128. }
  129. else if (PushType == 0)
  130. {
  131. var apiData = JsonConvert.DeserializeObject<OrderMaterialDelivery>(result);
  132. new MaterialDeliveryEvent()
  133. {
  134. DeviceId = device.DeviceId,
  135. orderMaterialDelivery = apiData
  136. }.Publish();
  137. MessageLog.GetInstance.Show("接收到【 API 】获取的物料信息");
  138. apiData?.BatchingInfo?.ForEach(x =>
  139. {
  140. MessageLog.GetInstance.Show($"物料ID:=[{x.BatchingId}],{x.BatchingLoc}号位置:{x.BatchingCount}");
  141. });
  142. }
  143. }
  144. catch (Exception ex)
  145. {
  146. MessageLog.GetInstance.ShowEx(ex.ToString());
  147. }
  148. }
  149. #endregion
  150. });
  151. }
  152. public void StopService()
  153. {
  154. this.devices.ForEach(device => device.Stop());
  155. }
  156. }
  157. }