终端一体化运控平台
25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

150 lines
5.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. var result = Plugin.GetInstance().GetPlugin<ConfigMgr>()?.deviceConfigModelJsons;
  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. }
  79. public void StartService()
  80. {
  81. this.devices.ForEach(device =>
  82. {
  83. device.StartMain();
  84. #region 获取物料数据
  85. string result = string.Empty;
  86. for (int i = 0; i < 2; i++)
  87. {
  88. try
  89. {
  90. int PushType = i;//0:主料 1:辅料
  91. int clientId = device.DeviceId;
  92. var jsondata = new { clientId, PushType };
  93. string header = $"[/stock/GetItemInfo]_[{DateTime.Now.Ticks}]".AESEncrypt();
  94. string url = $"{InternetInfo.StockServer}GetItemInfo";
  95. result = APIHelper.GetInstance.HttpRequest(url, header, jsondata, RequestType.POST);
  96. if (PushType == 1)
  97. {
  98. new RecipeBomEvent()
  99. {
  100. DeviceId = device.DeviceId,
  101. recipeBoms = JsonConvert.DeserializeObject<RecipeBoms>(result)
  102. }.Publish();
  103. MessageLog.GetInstance.Show("接收到【 API 】获取的辅料信息");
  104. }
  105. else if (PushType == 0)
  106. {
  107. var apiData = JsonConvert.DeserializeObject<OrderMaterialDelivery>(result);
  108. new MaterialDeliveryEvent()
  109. {
  110. DeviceId = device.DeviceId,
  111. orderMaterialDelivery = apiData
  112. }.Publish();
  113. MessageLog.GetInstance.Show("接收到【 API 】获取的物料信息");
  114. apiData?.BatchingInfo?.ForEach(x =>
  115. {
  116. MessageLog.GetInstance.Show($"物料ID:=[{x.BatchingId}],{x.BatchingLoc}号位置:{x.BatchingCount}");
  117. });
  118. }
  119. }
  120. catch (Exception ex)
  121. {
  122. MessageLog.GetInstance.ShowEx(ex.ToString());
  123. }
  124. }
  125. #endregion
  126. });
  127. }
  128. public void StopService()
  129. {
  130. this.devices.ForEach(device => device.Stop());
  131. }
  132. }
  133. }