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.
 
 

239 lines
9.3 KiB

  1. using HBLConsole.Abstract;
  2. using HBLConsole.Interface;
  3. using System;
  4. using System.Reflection;
  5. using BPA.Message.Enum;
  6. using HBLConsole.GVL;
  7. using BPA.Message;
  8. using HBLConsole.Service;
  9. using HBLConsole.Communication;
  10. using System.Threading;
  11. using System.Collections.Generic;
  12. using HBLConsole.Model;
  13. using System.Linq;
  14. //using BPA.Message;
  15. namespace HBLConsole.Factory
  16. {
  17. public class SimpleFactory
  18. {
  19. private volatile static SimpleFactory _Instance;
  20. public static SimpleFactory GetInstance => _Instance ?? (_Instance = new SimpleFactory());
  21. private SimpleFactory() { ActionManage.GetInstance.Register(new Action(() => { GetInterfaceData(); }), "ResetProgram"); }
  22. public AbstractMessageServer GetAbsMessageServer => _GetAbsMessageServer ?? (_GetAbsMessageServer = GetAbstractMessageServer());
  23. private AbstractMessageServer _GetAbsMessageServer;
  24. private string DeviceType => GeneralConfig.StartDevice;
  25. private object debugControl;
  26. private string GvlName = string.Empty;
  27. public void MqttMessage(IMessage message)
  28. {
  29. GetAbsMessageServer.AddOrder(message);
  30. GetAbsMessageServer.GetBatchingInfo(message);
  31. GetAbsMessageServer.GetRecipeBom(message);
  32. GetAbsMessageServer.Chargeback(message);
  33. }
  34. /// <summary>
  35. /// 获取物料信息
  36. /// </summary>
  37. public void GetBatchingInfo()
  38. {
  39. //GetAbsMessageServer.GetBatchingInfo(InternetInfo.ClientId);
  40. GetAbsMessageServer.GetBatchingInfo(InternetInfo.deviceId);
  41. }
  42. /// <summary>
  43. /// 订单状态更改
  44. /// </summary>
  45. /// <param name="subid"></param>
  46. /// <param name="status"></param>
  47. /// <returns></returns>
  48. public bool OrderChanged(string subid, ORDER_STATUS status)
  49. {
  50. bool res = GetAbsMessageServer.OrderStatusChange(subid, status);
  51. if (res)
  52. {
  53. ActionManage.GetInstance.Send("OrderStatusChange", new OrderStatusChange()
  54. {
  55. CookingStatus = status,
  56. SuborderId = subid
  57. });
  58. List<SubOrderStatus> subOrderStatus = new List<SubOrderStatus>();
  59. var result = Json<KeepDataBase>.Data.orderLists.FirstOrDefault(p => p.OrderPush.SuborderId == subid);
  60. if (result != null)
  61. {
  62. subOrderStatus.Add(new SubOrderStatus()
  63. {
  64. Status = (int)status,
  65. SubOrderId = subid,
  66. GoodsName = result.OrderPush.GoodsName,
  67. SortNo = result.OrderPush.SortNum.ToString(),
  68. });
  69. }
  70. //OrderStatusChangeNotify orderStatusChangeNotify = new OrderStatusChangeNotify();
  71. //orderStatusChangeNotify.SubOrderStatuses = subOrderStatus;
  72. //var temp = BPAPackage.Make(orderStatusChangeNotify);
  73. //MqttHelper.GetInstance.MqttPublishAsync(TOPIC.GetInstance.GetAppTopic(GeneralConfig.DeviceType, InternetInfo.ClientId), temp.Serialize(false));
  74. }
  75. return res;
  76. }
  77. public void CreateGvl()
  78. {
  79. string NameSpace = $"HBLConsole.{DeviceType}";
  80. Type type = Assembly.Load(NameSpace).GetType($"{NameSpace}.GVL_{DeviceType}");
  81. var tempGvl = Activator.CreateInstance(type);
  82. var FieldValue = control.GetType().GetField(GvlName, BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public);
  83. FieldValue?.SetValue(control, null);
  84. FieldValue?.SetValue(control, tempGvl);
  85. GetInterfaceData();
  86. }
  87. private AbstractMessageServer GetAbstractMessageServer()
  88. {
  89. string NameSpace = "HBLConsole.Business";
  90. Type type = Assembly.Load(NameSpace).GetType($"{NameSpace}.AbstractServer.{DeviceType}");
  91. if (type == null)
  92. type = Assembly.Load(NameSpace).GetType($"{NameSpace}.AbstractServer.Base");
  93. return Activator.CreateInstance(type) as AbstractMessageServer;
  94. }
  95. private void GetControlBase()
  96. {
  97. string NameSpace = "HBLConsole.Business";
  98. Type type = Assembly.Load(NameSpace).GetType($"{NameSpace}.AbstractServer.ControlBase");
  99. controlAbstract = Activator.CreateInstance(type) as ControlAbstract;
  100. if (controlAbstract != null)
  101. {
  102. controlAbstract.MainAction = new Action(() => { control?.Main(); });
  103. controlAbstract.ReadDataAction = new Action(() => { control?.ReadData(); });
  104. controlAbstract.Init();
  105. }
  106. }
  107. public IControl control { get; set; }
  108. public IGvl GVL { get; set; }
  109. public IAlarm Alarm { get; set; }
  110. public IHardwareStatus HardwareStatus { get; set; }
  111. public ControlAbstract controlAbstract { get; set; }
  112. /// <summary>
  113. /// 设备初始化
  114. /// </summary>
  115. public void DeviceInit()
  116. {
  117. string NameSpace = $"HBLConsole.{DeviceType}";//Load 加载的是dll的名称,GetType获取的是全命名空间下的类
  118. Type type = Assembly.Load(NameSpace).GetType($"{NameSpace}.Control_{DeviceType}");
  119. control = Activator.CreateInstance(type) as IControl;
  120. GetBatchingInfo();
  121. GetInterfaceData();
  122. control?.Init();
  123. //ActionManage.GetInstance.Register(new Action<object>((o) => { control?.DataParse(o); }), "DataParse");
  124. ActionManage.GetInstance.Register(new Action(() => { control?.ConnectOk(); }), $"{GeneralConfig.DeviceType.ToString()}/ConnectOk");
  125. ActionManage.GetInstance.Register(new Action<object>((o) => { control?.SimOrder(o); }), "SimOrder");//模拟订单委托注册
  126. ActionManage.GetInstance.Register(new Action<object>((o) => { control?.IotBroadcast(o); }), "IotBroadcast");
  127. ConnectHelper.GetInstance.Init();
  128. ActionManage.GetInstance.Send("监控数据初始化");
  129. ThreadManage.GetInstance.StartLong(new Action(() =>
  130. {
  131. while (GeneralConfig.morkOrderPushes.Count > 0)
  132. {
  133. if (GeneralConfig.morkOrderPushes.TryDequeue(out MorkOrderPush s))
  134. {
  135. control?.DataParse(s);
  136. }
  137. }
  138. Thread.Sleep(1000);
  139. }), "订单解析线程");
  140. }
  141. /// <summary>
  142. /// 获取接口数据
  143. /// </summary>
  144. private void GetInterfaceData()
  145. {
  146. Type type = control?.GetType();
  147. //通过字段查找
  148. var Fields = type.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
  149. if (Fields != null)
  150. {
  151. foreach (var item in Fields)
  152. {
  153. var interfaces = item.FieldType.GetInterfaces();
  154. if (interfaces != null)
  155. {
  156. foreach (var inters in interfaces)
  157. {
  158. if (inters.Name.Equals("IGvl"))
  159. {
  160. GVL = (item.GetValue(control)) as IGvl;
  161. GvlName = item.Name;
  162. }
  163. else if (inters.Name.Equals("IAlarm"))
  164. {
  165. Alarm = (item.GetValue(control)) as IAlarm;
  166. }
  167. else if (inters.Name.Equals("IHardwareStatus"))
  168. {
  169. HardwareStatus = (item.GetValue(control)) as IHardwareStatus;
  170. }
  171. }
  172. }
  173. }
  174. }
  175. //if (GVL != null && Alarm != null) return;
  176. ////通过属性查找
  177. //var prop = type.GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
  178. //if (prop != null)
  179. //{
  180. // foreach (var item in prop)
  181. // {
  182. // var interfaces = item.PropertyType.GetInterfaces();
  183. // if (interfaces != null)
  184. // {
  185. // foreach (var inters in interfaces)
  186. // {
  187. // if (inters.Name.Equals("IGvl"))
  188. // {
  189. // GVL = (type?.GetProperty(item.Name).GetValue(control, null)) as IGvl;
  190. // }
  191. // else if (inters.Name.Equals("IAlarm"))
  192. // {
  193. // Alarm = (type?.GetProperty(item.Name).GetValue(control, null)) as IAlarm;
  194. // }
  195. // }
  196. // }
  197. // }
  198. //}
  199. //return default;
  200. }
  201. public object GetDebugControl()
  202. {
  203. if (null == debugControl)
  204. {
  205. debugControl = Assembly.Load("HBLConsole.Debug").CreateInstance($"HBLConsole.Debug.Debug_{DeviceType}");
  206. }
  207. return debugControl;
  208. }
  209. }
  210. }