|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122 |
- using BPA.Message;
- using BPASmartClient.Device;
- using BPASmartClient.EventBus;
- using BPASmartClient.Helper;
- using BPASmartClient.Http;
- using BPASmartClient.Message;
- using BPASmartClient.Model;
- using BPASmartClient.Peripheral;
- using Newtonsoft.Json;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Reflection;
- using System.Text;
- using System.Threading.Tasks;
-
- namespace BPASmartClient.Business
- {
- /// <summary>
- /// 设备管理器,统一管理所有设备资源
- /// </summary>
- public class DeviceMgr : IPlugin
- {
- //设备集合
- private List<IDevice> devices = new List<IDevice>();
-
- public void Dispose()
- {
- }
-
- public void Initialize()
- {
- LoadDevice();
- }
-
- public List<IDevice> GetDevices()
- {
- return devices;
- }
-
- /// <summary>
- /// 设备加载
- /// </summary>
- private void LoadDevice()
- {
- var devices = Plugin.GetInstance().GetPlugin<ConfigMgr>().GetDeviceConfigs();
- foreach (var device in devices)
- {
- var deviceTemp = Assembly.Load(device.Module.Substring(0, device.Module.LastIndexOf('.'))).CreateInstance(device.Module) as IDevice;
- deviceTemp.Name = device.Name;
- deviceTemp.DeviceId = device.DeviceId;
-
- List<IPeripheral> peripherals = new List<IPeripheral>();
- foreach (var peripheral in device.Peripherals)
- {
- var peripheralTemp = Assembly.Load(peripheral.Module.Substring(0, peripheral.Module.LastIndexOf('.'))).CreateInstance(peripheral.Module) as IPeripheral;
- foreach (var pars in peripheral.Parameters)
- {
- peripheralTemp.GetType().GetProperty(pars.Key).SetValue(peripheralTemp, Convert.ChangeType(pars.Value, peripheralTemp.GetType().GetProperty(pars.Key).PropertyType));
- }
- peripherals.Add(peripheralTemp);
- }
- deviceTemp.Initliaze(peripherals);
- this.devices.Add(deviceTemp);
- }
- }
-
-
-
- public void StartService()
- {
- this.devices.ForEach(device =>
- {
- device.StartMain();
-
- #region 获取物料数据
- string result = string.Empty;
- for (int i = 0; i < 2; i++)
- {
- try
- {
- int PushType = i;//0:主料 1:辅料
- var jsondata = new { device.DeviceId, PushType };
- string header = $"[{InternetInfo.StockServer}/stock/GetItemInfo]_[{DateTime.Now.Ticks}]".AESEncrypt();
- string url = $"{InternetInfo.StockServer}/stock/GetItemInfo";
- result = APIHelper.GetInstance.HttpRequest(url, header, jsondata, RequestType.POST);
-
- if (PushType == 1)
- {
- EventBus.EventBus.GetInstance().Publish(new RecipeBomEvent()
- {
- Id = device.DeviceId,
- recipeBoms = JsonConvert.DeserializeObject<RecipeBoms>(result)
- });
- MessageLog.GetInstance.Show("接收到辅料信息");
- }
- else if (PushType == 0)
- {
- EventBus.EventBus.GetInstance().Publish(new MaterialDeliveryEvent()
- {
- Id = device.DeviceId,
- orderMaterialDelivery = JsonConvert.DeserializeObject<OrderMaterialDelivery>(result)
- });
- MessageLog.GetInstance.Show("接收到物料信息");
- }
- }
- catch (Exception ex)
- {
- MessageLog.GetInstance.ShowEx(ex.ToString());
- }
- }
- #endregion
-
- });
- }
-
- public void StopService()
- {
- this.devices.ForEach(device => device.Stop());
- }
- }
- }
|