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.
 
 

201 lines
7.8 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. namespace HBLConsole.Factory
  11. {
  12. public class SimpleFactory
  13. {
  14. private volatile static SimpleFactory _Instance;
  15. public static SimpleFactory GetInstance => _Instance ?? (_Instance = new SimpleFactory());
  16. private SimpleFactory() { ActionManage.GetInstance.Register(new Action(() => { GetInterfaceData(); }), "ResetProgram"); }
  17. public AbstractMessageServer GetAbsMessageServer => _GetAbsMessageServer ?? (_GetAbsMessageServer = GetAbstractMessageServer());
  18. private AbstractMessageServer _GetAbsMessageServer;
  19. private string DeviceType => GeneralConfig.DeviceType.ToString();
  20. private object debugControl;
  21. private string GvlName = string.Empty;
  22. public void MqttMessage(IMessage message)
  23. {
  24. GetAbsMessageServer.AddOrder(message);
  25. GetAbsMessageServer.GetBatchingInfo(message);
  26. GetAbsMessageServer.GetRecipeBom(message);
  27. }
  28. /// <summary>
  29. /// 获取物料信息
  30. /// </summary>
  31. public void GetBatchingInfo()
  32. {
  33. GetAbsMessageServer.GetBatchingInfo(InternetInfo.ClientId);
  34. }
  35. /// <summary>
  36. /// 订单状态更改
  37. /// </summary>
  38. /// <param name="subid"></param>
  39. /// <param name="status"></param>
  40. /// <returns></returns>
  41. public bool OrderChanged(string subid, ORDER_STATUS status)
  42. {
  43. bool res = GetAbsMessageServer.OrderStatusChange(subid, status);
  44. if (res)
  45. {
  46. ActionManage.GetInstance.Send("OrderStatusChange", new OrderStatusChange()
  47. {
  48. CookingStatus = status,
  49. SuborderId = subid
  50. });
  51. }
  52. return res;
  53. }
  54. public void CreateGvl()
  55. {
  56. string NameSpace = $"HBLConsole.{DeviceType}";
  57. Type type = Assembly.Load(NameSpace).GetType($"{NameSpace}.GVL_{DeviceType}");
  58. var tempGvl = Activator.CreateInstance(type);
  59. var FieldValue = control.GetType().GetField(GvlName, BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public);
  60. FieldValue?.SetValue(control, null);
  61. FieldValue?.SetValue(control, tempGvl);
  62. GetInterfaceData();
  63. }
  64. private AbstractMessageServer GetAbstractMessageServer()
  65. {
  66. string NameSpace = "HBLConsole.Business";
  67. Type type = Assembly.Load(NameSpace).GetType($"{NameSpace}.AbstractServer.{DeviceType}");
  68. if (type == null)
  69. type = Assembly.Load(NameSpace).GetType($"{NameSpace}.AbstractServer.Base");
  70. return Activator.CreateInstance(type) as AbstractMessageServer;
  71. }
  72. private void GetControlBase()
  73. {
  74. string NameSpace = "HBLConsole.Business";
  75. Type type = Assembly.Load(NameSpace).GetType($"{NameSpace}.AbstractServer.ControlBase");
  76. controlAbstract = Activator.CreateInstance(type) as ControlAbstract;
  77. if (controlAbstract != null)
  78. {
  79. controlAbstract.MainAction = new Action(() => { control?.Main(); });
  80. controlAbstract.ReadDataAction = new Action(() => { control?.ReadData(); });
  81. controlAbstract.Init();
  82. }
  83. }
  84. public IControl control { get; set; }
  85. public IGvl GVL { get; set; }
  86. public IAlarm Alarm { get; set; }
  87. public IHardwareStatus HardwareStatus { get; set; }
  88. public ControlAbstract controlAbstract { get; set; }
  89. /// <summary>
  90. /// 设备初始化
  91. /// </summary>
  92. public void DeviceInit()
  93. {
  94. string NameSpace = $"HBLConsole.{DeviceType}";//Load 加载的是dll的名称,GetType获取的是全命名空间下的类
  95. Type type = Assembly.Load(NameSpace).GetType($"{NameSpace}.Control_{DeviceType}");
  96. control = Activator.CreateInstance(type) as IControl;
  97. //control = (IControl)type?.GetProperty("Instance").GetValue(null);
  98. //IControl business = (IControl)type?.GetProperty("GetInstance").GetValue(null);
  99. GetBatchingInfo();
  100. GetInterfaceData();
  101. control?.Init();
  102. ActionManage.GetInstance.Register(new Action<object>((o) => { control?.DataParse(o); }), "DataParse");
  103. ActionManage.GetInstance.Register(new Action(() => { control?.ConnectOk(); }), $"{GeneralConfig.DeviceType.ToString()}/ConnectOk");
  104. ActionManage.GetInstance.Register(new Action<object>((o) => { control?.SimOrder(o); }), "SimOrder");
  105. ActionManage.GetInstance.Register(new Action<object>((o) => { control?.IotBroadcast(o); }), "IotBroadcast");
  106. ConnectHelper.GetInstance.Init();
  107. ActionManage.GetInstance.Send("监控数据初始化");
  108. //GetControlBase();
  109. }
  110. /// <summary>
  111. /// 获取接口数据
  112. /// </summary>
  113. private void GetInterfaceData()
  114. {
  115. Type type = control?.GetType();
  116. //通过字段查找
  117. var Fields = type.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
  118. if (Fields != null)
  119. {
  120. foreach (var item in Fields)
  121. {
  122. var interfaces = item.FieldType.GetInterfaces();
  123. if (interfaces != null)
  124. {
  125. foreach (var inters in interfaces)
  126. {
  127. if (inters.Name.Equals("IGvl"))
  128. {
  129. GVL = (item.GetValue(control)) as IGvl;
  130. GvlName = item.Name;
  131. }
  132. else if (inters.Name.Equals("IAlarm"))
  133. {
  134. Alarm = (item.GetValue(control)) as IAlarm;
  135. }
  136. else if (inters.Name.Equals("IHardwareStatus"))
  137. {
  138. HardwareStatus = (item.GetValue(control)) as IHardwareStatus;
  139. }
  140. }
  141. }
  142. }
  143. }
  144. //if (GVL != null && Alarm != null) return;
  145. ////通过属性查找
  146. //var prop = type.GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
  147. //if (prop != null)
  148. //{
  149. // foreach (var item in prop)
  150. // {
  151. // var interfaces = item.PropertyType.GetInterfaces();
  152. // if (interfaces != null)
  153. // {
  154. // foreach (var inters in interfaces)
  155. // {
  156. // if (inters.Name.Equals("IGvl"))
  157. // {
  158. // GVL = (type?.GetProperty(item.Name).GetValue(control, null)) as IGvl;
  159. // }
  160. // else if (inters.Name.Equals("IAlarm"))
  161. // {
  162. // Alarm = (type?.GetProperty(item.Name).GetValue(control, null)) as IAlarm;
  163. // }
  164. // }
  165. // }
  166. // }
  167. //}
  168. //return default;
  169. }
  170. public object GetDebugControl()
  171. {
  172. if (null == debugControl)
  173. {
  174. debugControl = Assembly.Load("HBLConsole.Debug").CreateInstance($"HBLConsole.Debug.Debug_{DeviceType}");
  175. }
  176. return debugControl;
  177. }
  178. }
  179. }