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.
 
 

182 lines
8.4 KiB

  1. using BPA.Message.IOT;
  2. using HBLConsole.Communication;
  3. using HBLConsole.GVL;
  4. using HBLConsole.Interface;
  5. using HBLConsole.Service;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Linq;
  9. using System.Reflection;
  10. using System.Text;
  11. using System.Threading;
  12. using System.Threading.Tasks;
  13. using HBLConsole.Factory;
  14. namespace HBLConsole.Business
  15. {
  16. public class IotReport
  17. {
  18. #region 单例模式
  19. private volatile static IotReport _Instance;
  20. public static IotReport GetInstance => _Instance ?? (_Instance = new IotReport());
  21. private IotReport()
  22. {
  23. IOTDev.GetInstance().Set(InternetInfo.ProductKey, InternetInfo.DeviceName, InternetInfo.DeviceSecret);
  24. IOTDev.GetInstance().CreateLinks();
  25. IOTDev.GetInstance().IOT_Subscribe(IOTDev.BroadcastTopic);//订阅广播主题
  26. IOTDev.actionIOT += actionIOTHandler;//消息数据回调
  27. IOTDev.UNConnectMqtt += new Action<string>((o) => { MessageLog.GetInstance.Show(o); });
  28. if (IOTDev.client.IsConnected) MessageLog.GetInstance.Show($"设备{InternetInfo.DeviceName}阿里云连接成功.");
  29. else MessageLog.GetInstance.Show($"设备{InternetInfo.DeviceName}阿里云连接失败.不能上报业务信息");
  30. //string NameSpace = $"HBLConsole.{GeneralConfig.DeviceType}";
  31. //control = (IControl)(Assembly.Load(NameSpace).GetType($"{NameSpace}.Control_{GeneralConfig.DeviceType}"))?.GetProperty("Instance").GetValue(null);
  32. }
  33. #endregion
  34. #region 变量
  35. /// <summary>
  36. /// 大屏上报Model
  37. /// </summary>
  38. public IOTDevSXModel iOTDevSXModel = new IOTDevSXModel() { SBMC = InternetInfo.DeviceName, SBMS = InternetInfo.DeviceMS };
  39. /// <summary>
  40. /// 当前控制的设备控件
  41. /// </summary>
  42. //public IControl control;
  43. #endregion
  44. #region 事件
  45. /// <summary>
  46. /// 关闭IOT连接
  47. /// </summary>
  48. public static void Close()
  49. {
  50. if (IOTDev.client != null)
  51. IOTDev.GetInstance().Disconnect();
  52. }
  53. /// <summary>
  54. /// 获取属性状态
  55. /// </summary>
  56. public T GetTypeValue<T>(string str)
  57. {
  58. return (T)SimpleFactory.GetInstance.GVL.GetType().GetProperty(str).GetValue(SimpleFactory.GetInstance.GVL, null);
  59. //return (T)control.GetT().GetType().GetProperty(str).GetValue(control.GetT(),null);
  60. }
  61. /// <summary>
  62. /// 设置上报数据
  63. /// </summary>
  64. public void SetData()
  65. {
  66. #region 上报数据整理
  67. #region 基本数据
  68. //设置基本属性与状态
  69. iOTDevSXModel.SetJBSX(new DevSX
  70. {
  71. data = new List<DevSXBase>
  72. {
  73. new DevSXBase { SXMC="设备硬件",SXLX="机器人",SXStatus=ModbusTcpHelper.GetInstance.Connected
  74. ,SXYCMS=ModbusTcpHelper.GetInstance.Connected?"":DateTime.Now+" 异常,机器人未连接,疑似未连接网络...."},
  75. new DevSXBase { SXMC="设备软件",SXLX="上位机",SXStatus=true }
  76. }
  77. });
  78. #endregion
  79. #region 扩展数据
  80. bool IsAllowRun = GetTypeValue<bool>("AllowRun");
  81. bool TemperatureReached = GetTypeValue<bool>("TemperatureReached");
  82. bool MissingBowl = GetTypeValue<bool>("MissingBowl");
  83. bool MissingBowlSignal2 = GetTypeValue<bool>("MissingBowlSignal2");
  84. bool IsNoodles = GetTypeValue<bool>("IsNoodles");//转台
  85. bool AllowFallNoodle = GetTypeValue<bool>("AllowFallNoodle");//是否允许到面
  86. bool[] CookNoodlesComplete = GetTypeValue<bool[]>("CookNoodlesComplete");//煮面完成上升信号
  87. bool isCookNoodles = CookNoodlesComplete.ToList().Find(o => o);
  88. //设置扩展属性与状态,连接上机器人后才会处罚
  89. if (IsAllowRun)
  90. {
  91. iOTDevSXModel.SetKZSX(new DevSX
  92. {
  93. data = new List<DevSXBase>
  94. {
  95. new DevSXBase { SXMC="设备硬件",SXLX="煮面炉",SXStatus= !TemperatureReached ,
  96. SXYCMS=!TemperatureReached?"":DateTime.Now+" 异常,煮面炉温度不够,疑似正在加热或者未工作...."},
  97. new DevSXBase { SXMC="设备硬件",SXLX="装小碗结构",SXStatus=MissingBowl ,
  98. SXYCMS=MissingBowl?"":DateTime.Now+" 异常,缺小碗...."},
  99. new DevSXBase { SXMC="设备硬件",SXLX="装大碗结构",SXStatus=MissingBowlSignal2 ,
  100. SXYCMS=MissingBowlSignal2?"":DateTime.Now+" 异常,缺大碗...."},
  101. new DevSXBase { SXMC="设备硬件",SXLX="取面转台",SXStatus=IsNoodles ,
  102. SXYCMS=IsNoodles?"":DateTime.Now+" 异常,转台位置缺少物料...."},
  103. new DevSXBase { SXMC="设备硬件",SXLX="配料机",SXStatus=!(!AllowFallNoodle && isCookNoodles) ,
  104. SXYCMS=!((!AllowFallNoodle && isCookNoodles))?"":DateTime.Now+" 异常,配料机未配完料,疑似碗未到配料机下方或者配料机未工作...."}
  105. }
  106. });
  107. }
  108. else
  109. {
  110. iOTDevSXModel.SetKZSX(new DevSX { data = new List<DevSXBase> { new DevSXBase { SXMC = "", SXLX = "" } } });
  111. }
  112. #endregion
  113. //设置告警消息
  114. #region 告警消息
  115. List<DevSXBase> bases = Tools.JsonToObjectTools<DevSX>(iOTDevSXModel.JBSX)?.data?.ToList().FindAll(par => par.SXStatus == false);
  116. if (bases != null) bases.AddRange(Tools.JsonToObjectTools<DevSX>(iOTDevSXModel.KZSX)?.data?.ToList().FindAll(par => par.SXStatus == false));
  117. List<AlarmModel> alarms = new List<AlarmModel>();
  118. bases?.ForEach(par => alarms.Add(new AlarmModel { DeviceMC = iOTDevSXModel.SBMC, DeviceSJ = DateTime.Now.ToString(), AlarmCD = "一般", DeviceZT = "未处理", DeviceMS = par.SXYCMS }));
  119. iOTDevSXModel.SetGJXX(new AlarmMessage { data = alarms });
  120. #endregion
  121. bool InitComplete = GetTypeValue<bool>("InitComplete");
  122. bool RobotTakeNoodle = GetTypeValue<bool>("RobotTakeNoodle");
  123. bool RobotOutMeal = GetTypeValue<bool>("RobotOutMeal");
  124. iOTDevSXModel.SetLCSB(new ProcessMessage
  125. {
  126. data = new List<ProcessModel>
  127. {
  128. new ProcessModel { ProcessName="开机",IsMark=IsAllowRun,ProcessMS=IsAllowRun?"机器人开机成功":"" },
  129. new ProcessModel { ProcessName="初始化",IsMark=InitComplete,ProcessMS=InitComplete?"机器人正在初始化成功":"" },
  130. new ProcessModel { ProcessName="取面",IsMark=RobotTakeNoodle,ProcessMS=RobotTakeNoodle?"机器人正在取面过程中....":""},
  131. new ProcessModel { ProcessName="出餐",IsMark=AllowFallNoodle,ProcessMS=AllowFallNoodle?"机器人正在出餐过程中....":"" }
  132. }
  133. });
  134. #endregion
  135. }
  136. /// <summary>
  137. /// 初始化上报流程
  138. /// </summary>
  139. public void Init()
  140. {
  141. ThreadManage.GetInstance.StartLong(new Action(() =>
  142. {
  143. if (IOTDev.client.IsConnected && GeneralConfig.DeviceType.ToString() == "MORKS")
  144. {
  145. //SetData();
  146. IOTDev.GetInstance().IOT_Publish(IOTDev.PubTopic, iOTDevSXModel.Tojson());
  147. }
  148. Thread.Sleep(5000);
  149. }), "设备IOT上报云端");
  150. }
  151. /// <summary>
  152. /// 接收云端消息
  153. /// </summary>
  154. /// <param name="topic"></param>
  155. /// <param name="message"></param>
  156. private void actionIOTHandler(string topic, string message)
  157. {
  158. if (IOTDev.BroadcastTopic == topic && !string.IsNullOrEmpty(message))//广播主题消息,将广播消息发送到相应客户端
  159. {
  160. IOTCommandModel iOTCommand = Tools.JsonToObjectTools<IOTCommandModel>(message);
  161. if (iOTCommand.deviceName == InternetInfo.DeviceName)
  162. ActionManage.GetInstance.Send("IotBroadcast", iOTCommand);
  163. }
  164. }
  165. #endregion
  166. }
  167. }