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

123 lines
4.3 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. LoadDevice();
  31. }
  32. public List<IDevice> GetDevices()
  33. {
  34. return devices;
  35. }
  36. /// <summary>
  37. /// 设备加载
  38. /// </summary>
  39. private void LoadDevice()
  40. {
  41. var devices = Plugin.GetInstance().GetPlugin<ConfigMgr>().GetDeviceConfigs();
  42. foreach (var device in devices)
  43. {
  44. var deviceTemp = Assembly.Load(device.Module.Substring(0, device.Module.LastIndexOf('.'))).CreateInstance(device.Module) as IDevice;
  45. deviceTemp.Name = device.Name;
  46. deviceTemp.DeviceId = device.DeviceId;
  47. List<IPeripheral> peripherals = new List<IPeripheral>();
  48. foreach (var peripheral in device.Peripherals)
  49. {
  50. var peripheralTemp = Assembly.Load(peripheral.Module.Substring(0, peripheral.Module.LastIndexOf('.'))).CreateInstance(peripheral.Module) as IPeripheral;
  51. foreach (var pars in peripheral.Parameters)
  52. {
  53. peripheralTemp.GetType().GetProperty(pars.Key).SetValue(peripheralTemp, Convert.ChangeType(pars.Value, peripheralTemp.GetType().GetProperty(pars.Key).PropertyType));
  54. }
  55. peripherals.Add(peripheralTemp);
  56. }
  57. deviceTemp.Initliaze(peripherals);
  58. this.devices.Add(deviceTemp);
  59. }
  60. }
  61. public void StartService()
  62. {
  63. this.devices.ForEach(device =>
  64. {
  65. device.StartMain();
  66. #region 获取物料数据
  67. string result = string.Empty;
  68. for (int i = 0; i < 2; i++)
  69. {
  70. try
  71. {
  72. int PushType = i;//0:主料 1:辅料
  73. var jsondata = new { device.DeviceId, PushType };
  74. string header = $"[{InternetInfo.StockServer}/stock/GetItemInfo]_[{DateTime.Now.Ticks}]".AESEncrypt();
  75. string url = $"{InternetInfo.StockServer}/stock/GetItemInfo";
  76. result = APIHelper.GetInstance.HttpRequest(url, header, jsondata, RequestType.POST);
  77. if (PushType == 1)
  78. {
  79. EventBus.EventBus.GetInstance().Publish(new RecipeBomEvent()
  80. {
  81. Id = device.DeviceId,
  82. recipeBoms = JsonConvert.DeserializeObject<RecipeBoms>(result)
  83. });
  84. MessageLog.GetInstance.Show("接收到辅料信息");
  85. }
  86. else if (PushType == 0)
  87. {
  88. EventBus.EventBus.GetInstance().Publish(new MaterialDeliveryEvent()
  89. {
  90. Id = device.DeviceId,
  91. orderMaterialDelivery = JsonConvert.DeserializeObject<OrderMaterialDelivery>(result)
  92. });
  93. MessageLog.GetInstance.Show("接收到物料信息");
  94. }
  95. }
  96. catch (Exception ex)
  97. {
  98. MessageLog.GetInstance.ShowEx(ex.ToString());
  99. }
  100. }
  101. #endregion
  102. });
  103. }
  104. public void StopService()
  105. {
  106. this.devices.ForEach(device => device.Stop());
  107. }
  108. }
  109. }