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.
 
 

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