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.
 
 

196 lines
7.5 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 ControlAbstract controlAbstract { get; set; }
  88. /// <summary>
  89. /// 设备初始化
  90. /// </summary>
  91. public void DeviceInit()
  92. {
  93. string NameSpace = $"HBLConsole.{DeviceType}";//Load 加载的是dll的名称,GetType获取的是全命名空间下的类
  94. Type type = Assembly.Load(NameSpace).GetType($"{NameSpace}.Control_{DeviceType}");
  95. control = Activator.CreateInstance(type) as IControl;
  96. //control = (IControl)type?.GetProperty("Instance").GetValue(null);
  97. //IControl business = (IControl)type?.GetProperty("GetInstance").GetValue(null);
  98. GetBatchingInfo();
  99. GetInterfaceData();
  100. control?.Init();
  101. ActionManage.GetInstance.Register(new Action<object>((o) => { control?.DataParse(o); }), "DataParse");
  102. ActionManage.GetInstance.Register(new Action(() => { control?.ConnectOk(); }), $"{GeneralConfig.DeviceType.ToString()}/ConnectOk");
  103. ActionManage.GetInstance.Register(new Action<object>((o) => { control?.SimOrder(o); }), "SimOrder");
  104. ActionManage.GetInstance.Register(new Action<object>((o) => { control?.IotBroadcast(o); }), "IotBroadcast");
  105. ConnectHelper.GetInstance.Init();
  106. //GetControlBase();
  107. }
  108. /// <summary>
  109. /// 获取接口数据
  110. /// </summary>
  111. private void GetInterfaceData()
  112. {
  113. Type type = control?.GetType();
  114. //通过字段查找
  115. var Fields = type.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
  116. if (Fields != null)
  117. {
  118. foreach (var item in Fields)
  119. {
  120. var interfaces = item.FieldType.GetInterfaces();
  121. if (interfaces != null)
  122. {
  123. foreach (var inters in interfaces)
  124. {
  125. if (inters.Name.Equals("IGvl"))
  126. {
  127. GVL = (item.GetValue(control)) as IGvl;
  128. GvlName = item.Name;
  129. }
  130. else if (inters.Name.Equals("IAlarm"))
  131. {
  132. Alarm = (item.GetValue(control)) as IAlarm;
  133. }
  134. }
  135. }
  136. }
  137. }
  138. //if (GVL != null && Alarm != null) return;
  139. ////通过属性查找
  140. //var prop = type.GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
  141. //if (prop != null)
  142. //{
  143. // foreach (var item in prop)
  144. // {
  145. // var interfaces = item.PropertyType.GetInterfaces();
  146. // if (interfaces != null)
  147. // {
  148. // foreach (var inters in interfaces)
  149. // {
  150. // if (inters.Name.Equals("IGvl"))
  151. // {
  152. // GVL = (type?.GetProperty(item.Name).GetValue(control, null)) as IGvl;
  153. // }
  154. // else if (inters.Name.Equals("IAlarm"))
  155. // {
  156. // Alarm = (type?.GetProperty(item.Name).GetValue(control, null)) as IAlarm;
  157. // }
  158. // }
  159. // }
  160. // }
  161. //}
  162. //return default;
  163. }
  164. public object GetDebugControl()
  165. {
  166. if (null == debugControl)
  167. {
  168. debugControl = Assembly.Load("HBLConsole.Debug").CreateInstance($"HBLConsole.Debug.Debug_{DeviceType}");
  169. }
  170. return debugControl;
  171. }
  172. }
  173. }