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

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