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

138 lines
4.9 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.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 devices = Plugin.GetInstance().GetPlugin<ConfigMgr>().GetDeviceConfigs();
  45. foreach (var device in devices)
  46. {
  47. var deviceTemp = Assembly.Load(device.Module.Substring(0, device.Module.LastIndexOf('.'))).CreateInstance(device.Module) as IDevice;
  48. deviceTemp.Name = device.Name;
  49. deviceTemp.DeviceId = device.DeviceId;
  50. foreach (var pars in device.Parameters)
  51. {
  52. deviceTemp.GetType().GetProperty(pars.Key).SetValue(deviceTemp, Convert.ChangeType(pars.Value, deviceTemp.GetType().GetProperty(pars.Key).PropertyType));
  53. }
  54. List<IPeripheral> peripherals = new List<IPeripheral>();
  55. foreach (var peripheral in device.Peripherals)
  56. {
  57. var peripheralTemp = Assembly.Load(peripheral.Module.Substring(0, peripheral.Module.LastIndexOf('.'))).CreateInstance(peripheral.Module) as IPeripheral;
  58. foreach (var pars in peripheral.Parameters)
  59. {
  60. peripheralTemp.GetType().GetProperty(pars.Key).SetValue(peripheralTemp, Convert.ChangeType(pars.Value, peripheralTemp.GetType().GetProperty(pars.Key).PropertyType));
  61. }
  62. peripherals.Add(peripheralTemp);
  63. }
  64. deviceTemp.Initliaze(peripherals);
  65. this.devices.Add(deviceTemp);
  66. }
  67. }
  68. public void StartService()
  69. {
  70. this.devices.ForEach(device =>
  71. {
  72. device.StartMain();
  73. #region 获取物料数据
  74. string result = string.Empty;
  75. for (int i = 0; i < 2; i++)
  76. {
  77. try
  78. {
  79. int PushType = i;//0:主料 1:辅料
  80. int clientId = device.DeviceId;
  81. var jsondata = new { clientId, PushType };
  82. string header = $"[/stock/GetItemInfo]_[{DateTime.Now.Ticks}]".AESEncrypt();
  83. string url = $"{InternetInfo.StockServer}GetItemInfo";
  84. result = APIHelper.GetInstance.HttpRequest(url, header, jsondata, RequestType.POST);
  85. if (PushType == 1)
  86. {
  87. EventBus.EventBus.GetInstance().Publish(new RecipeBomEvent()
  88. {
  89. DeviceId = device.DeviceId,
  90. recipeBoms = JsonConvert.DeserializeObject<RecipeBoms>(result)
  91. });
  92. MessageLog.GetInstance.Show("接收到辅料信息");
  93. }
  94. else if (PushType == 0)
  95. {
  96. var apiData = JsonConvert.DeserializeObject<OrderMaterialDelivery>(result);
  97. EventBus.EventBus.GetInstance().Publish(new MaterialDeliveryEvent()
  98. {
  99. DeviceId = device.DeviceId,
  100. orderMaterialDelivery = apiData
  101. });
  102. MessageLog.GetInstance.Show("接收到物料信息");
  103. apiData?.BatchingInfo?.ForEach(x =>
  104. {
  105. MessageLog.GetInstance.Show($"物料ID:=[{x.BatchingId}],{x.BatchingLoc}号位置:{x.BatchingCount}");
  106. });
  107. }
  108. }
  109. catch (Exception ex)
  110. {
  111. MessageLog.GetInstance.ShowEx(ex.ToString());
  112. }
  113. }
  114. #endregion
  115. });
  116. }
  117. public void StopService()
  118. {
  119. this.devices.ForEach(device => device.Stop());
  120. }
  121. }
  122. }