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 { /// /// 设备管理器,统一管理所有设备资源 /// public class DeviceMgr : IPlugin { //设备集合 private List devices = new List(); public void Dispose() { } public void Initialize() { } public void Start() { LoadDevice(); } public List GetDevices() { return devices; } /// /// 设备加载 /// private void LoadDevice() { var devices = Plugin.GetInstance().GetPlugin().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 peripherals = new List(); 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:辅料 int clientId = device.DeviceId; var jsondata = new { clientId, PushType }; string header = $"[/stock/GetItemInfo]_[{DateTime.Now.Ticks}]".AESEncrypt(); string url = $"{InternetInfo.StockServer}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(result) }); MessageLog.GetInstance.Show("接收到辅料信息"); } else if (PushType == 0) { var apiData = JsonConvert.DeserializeObject(result); EventBus.EventBus.GetInstance().Publish(new MaterialDeliveryEvent() { Id = device.DeviceId, orderMaterialDelivery = apiData }); MessageLog.GetInstance.Show("接收到物料信息"); apiData?.BatchingInfo?.ForEach(x => { MessageLog.GetInstance.Show($"物料ID:=[{x.BatchingId}],{x.BatchingLoc}号位置:{x.BatchingCount}"); }); } } catch (Exception ex) { MessageLog.GetInstance.ShowEx(ex.ToString()); } } #endregion }); } public void StopService() { this.devices.ForEach(device => device.Stop()); } } }