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.
 
 

168 lines
6.2 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() { ActionOperate.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. public void MqttMessage(IMessage message)
  22. {
  23. GetAbsMessageServer.AddOrder(message);
  24. GetAbsMessageServer.GetBatchingInfo(message);
  25. GetAbsMessageServer.GetRecipeBom(message);
  26. }
  27. /// <summary>
  28. /// 获取物料信息
  29. /// </summary>
  30. public void GetBatchingInfo()
  31. {
  32. GetAbsMessageServer.GetBatchingInfo(InternetInfo.ClientId);
  33. }
  34. /// <summary>
  35. /// 订单状态更改
  36. /// </summary>
  37. /// <param name="subid"></param>
  38. /// <param name="status"></param>
  39. /// <returns></returns>
  40. public bool OrderChanged(string subid, ORDER_STATUS status)
  41. {
  42. bool res = GetAbsMessageServer.OrderStatusChange(subid, status);
  43. if (res)
  44. {
  45. ActionOperate.GetInstance.Send("OrderStatusChange", new OrderStatusChange()
  46. {
  47. CookingStatus = status,
  48. SuborderId = subid
  49. });
  50. }
  51. return res;
  52. }
  53. private AbstractMessageServer GetAbstractMessageServer()
  54. {
  55. string NameSpace = "HBLConsole.Business";
  56. Type type = Assembly.Load(NameSpace).GetType($"{NameSpace}.MessageServer.{DeviceType}");
  57. if (type == null)
  58. type = Assembly.Load(NameSpace).GetType($"{NameSpace}.MessageServer.Base");
  59. return Activator.CreateInstance(type) as AbstractMessageServer;
  60. }
  61. public IControl control { get; set; }
  62. public IGvl GVL { get; set; }
  63. public IAlarm Alarm { get; set; }
  64. /// <summary>
  65. /// 设备初始化
  66. /// </summary>
  67. public void DeviceInit()
  68. {
  69. string NameSpace = $"HBLConsole.{DeviceType}";//Load 加载的是dll的名称,GetType获取的是全命名空间下的类
  70. Type type = Assembly.Load(NameSpace).GetType($"{NameSpace}.Control_{DeviceType}");
  71. control = Activator.CreateInstance(type) as IControl;
  72. //control = (IControl)type?.GetProperty("Instance").GetValue(null);
  73. //IControl business = (IControl)type?.GetProperty("GetInstance").GetValue(null);
  74. GetBatchingInfo();
  75. GetInterfaceData();
  76. control?.Init();
  77. ActionOperate.GetInstance.Register(new Action<object>((o) => { control?.DataParse(o); }), "DataParse");
  78. ActionOperate.GetInstance.Register(new Action(() => { control?.ConnectOk(); }), $"{GeneralConfig.DeviceType.ToString()}/ConnectOk");
  79. ActionOperate.GetInstance.Register(new Action<object>((o) => { control?.SimOrder(o); }), "SimOrder");
  80. ActionOperate.GetInstance.Register(new Action<object>((o) => { control?.IotBroadcast(o); }), "IotBroadcast");
  81. ConnectHelper.GetInstance.Init();
  82. }
  83. /// <summary>
  84. /// 获取接口数据
  85. /// </summary>
  86. private void GetInterfaceData()
  87. {
  88. Type type = control?.GetType();
  89. //通过字段查找
  90. var Fields = type.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
  91. if (Fields != null)
  92. {
  93. foreach (var item in Fields)
  94. {
  95. var interfaces = item.FieldType.GetInterfaces();
  96. if (interfaces != null)
  97. {
  98. foreach (var inters in interfaces)
  99. {
  100. if (inters.Name.Equals("IGvl"))
  101. {
  102. GVL = (item.GetValue(control)) as IGvl;
  103. }
  104. else if (inters.Name.Equals("IAlarm"))
  105. {
  106. Alarm = (item.GetValue(control)) as IAlarm;
  107. }
  108. }
  109. }
  110. }
  111. }
  112. //if (GVL != null && Alarm != null) return;
  113. ////通过属性查找
  114. //var prop = type.GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
  115. //if (prop != null)
  116. //{
  117. // foreach (var item in prop)
  118. // {
  119. // var interfaces = item.PropertyType.GetInterfaces();
  120. // if (interfaces != null)
  121. // {
  122. // foreach (var inters in interfaces)
  123. // {
  124. // if (inters.Name.Equals("IGvl"))
  125. // {
  126. // GVL = (type?.GetProperty(item.Name).GetValue(control, null)) as IGvl;
  127. // }
  128. // else if (inters.Name.Equals("IAlarm"))
  129. // {
  130. // Alarm = (type?.GetProperty(item.Name).GetValue(control, null)) as IAlarm;
  131. // }
  132. // }
  133. // }
  134. // }
  135. //}
  136. //return default;
  137. }
  138. public object GetDebugControl()
  139. {
  140. if (null == debugControl)
  141. {
  142. debugControl = Assembly.Load("HBLConsole.Debug").CreateInstance($"HBLConsole.Debug.Debug_{DeviceType}");
  143. }
  144. return debugControl;
  145. }
  146. }
  147. }