|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238 |
- using HBLConsole.Abstract;
- using HBLConsole.Interface;
- using System;
- using System.Reflection;
- using BPA.Message.Enum;
- using HBLConsole.GVL;
- using BPA.Message;
- using HBLConsole.Service;
- using HBLConsole.Communication;
- using System.Threading;
- using System.Collections.Generic;
- using HBLConsole.Model;
- using System.Linq;
- //using BPA.Message;
-
-
- namespace HBLConsole.Factory
- {
- public class SimpleFactory
- {
-
- private volatile static SimpleFactory _Instance;
- public static SimpleFactory GetInstance => _Instance ?? (_Instance = new SimpleFactory());
- private SimpleFactory() { ActionManage.GetInstance.Register(new Action(() => { GetInterfaceData(); }), "ResetProgram"); }
-
- public AbstractMessageServer GetAbsMessageServer => _GetAbsMessageServer ?? (_GetAbsMessageServer = GetAbstractMessageServer());
- private AbstractMessageServer _GetAbsMessageServer;
-
- private string DeviceType => GeneralConfig.StartDevice;
-
- private object debugControl;
- private string GvlName = string.Empty;
-
- public void MqttMessage(IMessage message)
- {
- GetAbsMessageServer.AddOrder(message);
- GetAbsMessageServer.GetBatchingInfo(message);
- GetAbsMessageServer.GetRecipeBom(message);
- GetAbsMessageServer.Chargeback(message);
- }
-
- /// <summary>
- /// 获取物料信息
- /// </summary>
- public void GetBatchingInfo()
- {
- //GetAbsMessageServer.GetBatchingInfo(InternetInfo.ClientId);
- GetAbsMessageServer.GetBatchingInfo(InternetInfo.deviceId);
- }
-
- /// <summary>
- /// 订单状态更改
- /// </summary>
- /// <param name="subid"></param>
- /// <param name="status"></param>
- /// <returns></returns>
- public bool OrderChanged(string subid, ORDER_STATUS status)
- {
- bool res = GetAbsMessageServer.OrderStatusChange(subid, status);
- if (res)
- {
- ActionManage.GetInstance.Send("OrderStatusChange", new OrderStatusChange()
- {
- CookingStatus = status,
- SuborderId = subid
- });
-
-
- List<SubOrderStatus> subOrderStatus = new List<SubOrderStatus>();
- var result = Json<KeepDataBase>.Data.orderLists.FirstOrDefault(p => p.OrderPush.SuborderId == subid);
- if (result != null)
- {
- subOrderStatus.Add(new SubOrderStatus()
- {
- Status = (int)status,
- SubOrderId = subid,
- GoodsName = result.OrderPush.GoodsName,
- SortNo = result.OrderPush.SortNum.ToString(),
- });
- }
-
- //OrderStatusChangeNotify orderStatusChangeNotify = new OrderStatusChangeNotify();
- //orderStatusChangeNotify.SubOrderStatuses = subOrderStatus;
- //var temp = BPAPackage.Make(orderStatusChangeNotify);
-
- //MqttHelper.GetInstance.MqttPublishAsync(TOPIC.GetInstance.GetAppTopic(GeneralConfig.DeviceType, InternetInfo.ClientId), temp.Serialize(false));
- }
- return res;
- }
-
- public void CreateGvl()
- {
- string NameSpace = $"HBLConsole.{DeviceType}";
- Type type = Assembly.Load(NameSpace).GetType($"{NameSpace}.GVL_{DeviceType}");
- var tempGvl = Activator.CreateInstance(type);
- var FieldValue = control.GetType().GetField(GvlName, BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public);
- FieldValue?.SetValue(control, null);
- FieldValue?.SetValue(control, tempGvl);
- GetInterfaceData();
- }
-
- private AbstractMessageServer GetAbstractMessageServer()
- {
- string NameSpace = "HBLConsole.Business";
- Type type = Assembly.Load(NameSpace).GetType($"{NameSpace}.AbstractServer.{DeviceType}");
- if (type == null)
- type = Assembly.Load(NameSpace).GetType($"{NameSpace}.AbstractServer.Base");
- return Activator.CreateInstance(type) as AbstractMessageServer;
- }
-
- private void GetControlBase()
- {
- string NameSpace = "HBLConsole.Business";
- Type type = Assembly.Load(NameSpace).GetType($"{NameSpace}.AbstractServer.ControlBase");
- controlAbstract = Activator.CreateInstance(type) as ControlAbstract;
- if (controlAbstract != null)
- {
- controlAbstract.MainAction = new Action(() => { control?.Main(); });
- controlAbstract.ReadDataAction = new Action(() => { control?.ReadData(); });
- controlAbstract.Init();
- }
- }
-
- public IControl control { get; set; }
- public IGvl GVL { get; set; }
- public IAlarm Alarm { get; set; }
- public IHardwareStatus HardwareStatus { get; set; }
- public ControlAbstract controlAbstract { get; set; }
-
- /// <summary>
- /// 设备初始化
- /// </summary>
- public void DeviceInit()
- {
- string NameSpace = $"HBLConsole.{DeviceType}";//Load 加载的是dll的名称,GetType获取的是全命名空间下的类
- Type type = Assembly.Load(NameSpace).GetType($"{NameSpace}.Control_{DeviceType}");
- control = Activator.CreateInstance(type) as IControl;
- GetBatchingInfo();
- GetInterfaceData();
- control?.Init();
- //ActionManage.GetInstance.Register(new Action<object>((o) => { control?.DataParse(o); }), "DataParse");
- ActionManage.GetInstance.Register(new Action(() => { control?.ConnectOk(); }), $"{GeneralConfig.DeviceType.ToString()}/ConnectOk");
- ActionManage.GetInstance.Register(new Action<object>((o) => { control?.SimOrder(o); }), "SimOrder");//模拟订单委托注册
- ActionManage.GetInstance.Register(new Action<object>((o) => { control?.IotBroadcast(o); }), "IotBroadcast");
- ConnectHelper.GetInstance.Init();
- ActionManage.GetInstance.Send("监控数据初始化");
-
- ThreadManage.GetInstance.StartLong(new Action(() =>
- {
- while (GeneralConfig.morkOrderPushes.Count > 0)
- {
- if (GeneralConfig.morkOrderPushes.TryDequeue(out MorkOrderPush s))
- {
- control?.DataParse(s);
- }
- }
- Thread.Sleep(1000);
- }), "订单解析线程");
-
-
- }
-
- /// <summary>
- /// 获取接口数据
- /// </summary>
- private void GetInterfaceData()
- {
- Type type = control?.GetType();
-
- //通过字段查找
- var Fields = type.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
- if (Fields != null)
- {
- foreach (var item in Fields)
- {
- var interfaces = item.FieldType.GetInterfaces();
- if (interfaces != null)
- {
- foreach (var inters in interfaces)
- {
- if (inters.Name.Equals("IGvl"))
- {
- GVL = (item.GetValue(control)) as IGvl;
- GvlName = item.Name;
- }
- else if (inters.Name.Equals("IAlarm"))
- {
- Alarm = (item.GetValue(control)) as IAlarm;
- }
- else if (inters.Name.Equals("IHardwareStatus"))
- {
- HardwareStatus = (item.GetValue(control)) as IHardwareStatus;
- }
- }
- }
- }
- }
-
- //if (GVL != null && Alarm != null) return;
-
- ////通过属性查找
- //var prop = type.GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
- //if (prop != null)
- //{
- // foreach (var item in prop)
- // {
- // var interfaces = item.PropertyType.GetInterfaces();
- // if (interfaces != null)
- // {
- // foreach (var inters in interfaces)
- // {
- // if (inters.Name.Equals("IGvl"))
- // {
- // GVL = (type?.GetProperty(item.Name).GetValue(control, null)) as IGvl;
- // }
- // else if (inters.Name.Equals("IAlarm"))
- // {
- // Alarm = (type?.GetProperty(item.Name).GetValue(control, null)) as IAlarm;
- // }
- // }
- // }
- // }
- //}
-
- //return default;
- }
-
-
- public object GetDebugControl()
- {
- if (null == debugControl)
- {
- debugControl = Assembly.Load("HBLConsole.Debug").CreateInstance($"HBLConsole.Debug.Debug_{DeviceType}");
- }
- return debugControl;
- }
- }
- }
|