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.
 
 

229 lines
9.7 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. using BPA.Message.API请求;
  15. namespace HBLConsole.Business
  16. {
  17. public class IotReport
  18. {
  19. #region 单例模式
  20. private volatile static IotReport _Instance;
  21. public static IotReport GetInstance => _Instance ?? (_Instance = new IotReport());
  22. #endregion
  23. #region 变量
  24. /// <summary>
  25. /// 查询出当前设备信息
  26. /// </summary>
  27. public DeviceTable device;
  28. /// <summary>
  29. /// 大屏上报Model
  30. /// </summary>
  31. public IOTDevSXModel iOTDevSXModel = new IOTDevSXModel() { };
  32. #endregion
  33. #region IOT上报公共调用
  34. /// <summary>
  35. /// 上报告警消息
  36. /// 调用示例: SendAlarmMessage(new AlarmTable { AlarmTime = DateTime.Now,AlarmType = "1",AlarmMessage = "煮面机异常",AlarmVla = "煮面机" });
  37. /// </summary>
  38. /// <param name="alarmTable"></param>
  39. public void SendAlarmMessage(AlarmTable alarmTable)
  40. {
  41. if (IOTDevServer.client != null && IOTDevServer.client.IsConnected && device!=null)
  42. {
  43. alarmTable.ClientId = InternetInfo.ClientId.ToString();
  44. alarmTable.devicename = device.devicename;
  45. string json = Tools.JsonConvertTools<IotModel<AlarmIOT>>(new IotModel<AlarmIOT> { @params = new AlarmIOT { GJXX = Tools.JsonConvertTools(alarmTable) } });
  46. IOTDevServer.GetInstance().IOT_Publish(IOTDevServer.PubTopic,json);
  47. }
  48. }
  49. /// <summary>
  50. /// 上报日志消息
  51. /// 调用示例:SendLogMessage(new LogTable { LogTime = DateTime.Now,LogType = "1",LogMessage = "程序异常",LogVla = "程序" });
  52. /// </summary>
  53. /// <param name="logTable"></param>
  54. public void SendLogMessage(LogTable logTable)
  55. {
  56. if (IOTDevServer.client != null && IOTDevServer.client.IsConnected && device != null)
  57. {
  58. logTable.devicename = device.devicename;
  59. logTable.ClientId = InternetInfo.ClientId.ToString();
  60. string json = Tools.JsonConvertTools<IotModel<LogIOT>>(new IotModel<LogIOT> { @params = new LogIOT { SZXX = Tools.JsonConvertTools(logTable) } });
  61. IOTDevServer.GetInstance().IOT_Publish(IOTDevServer.PubTopic,json);
  62. }
  63. }
  64. /// <summary>
  65. /// 上报节点状态消息
  66. /// 调用示例:SendNodeStatusMessage("json字符串");
  67. /// </summary>
  68. /// <param name="json"></param>
  69. public void SendNodeStatusMessage(string json)
  70. {
  71. if (IOTDevServer.client!=null && IOTDevServer.client.IsConnected && device != null)
  72. {
  73. string jsonstr = Tools.JsonConvertTools<IotModel<NodeStatusIOT>>(new IotModel<NodeStatusIOT> { @params = new NodeStatusIOT { NodeStatus = Tools.JsonConvertTools(json) } });
  74. IOTDevServer.GetInstance().IOT_Publish(IOTDevServer.PubTopic,jsonstr);
  75. }
  76. }
  77. /// <summary>
  78. /// 上报属性状态
  79. /// 调用示例:SendTargetMessage();
  80. /// </summary>
  81. /// <param name="json"></param>
  82. public void SendTargetMessage()
  83. {
  84. if (IOTDevServer.client != null && IOTDevServer.client.IsConnected && device != null)
  85. {
  86. string kzsx = string.Empty;
  87. string jbsx = Tools.JsonConvertTools<DevSX>(new DevSX
  88. {
  89. data = new List<DevSXBase>
  90. {
  91. new DevSXBase { SXMC="设备硬件",SXLX="机器人",SXStatus=ModbusTcpHelper.GetInstance.Connected
  92. ,SXYCMS=ModbusTcpHelper.GetInstance.Connected?"":DateTime.Now+" 异常,机器人未连接,疑似未连接网络...."},
  93. new DevSXBase { SXMC="设备软件",SXLX="上位机",SXStatus=true }
  94. }
  95. });
  96. bool IsAllowRun = GetTypeValue<bool>("AllowRun");
  97. bool TemperatureReached = GetTypeValue<bool>("TemperatureReached");
  98. bool MissingBowl = GetTypeValue<bool>("MissingBowl");
  99. bool MissingBowlSignal2 = GetTypeValue<bool>("MissingBowlSignal2");
  100. bool IsNoodles = GetTypeValue<bool>("IsNoodles");//转台
  101. bool AllowFallNoodle = GetTypeValue<bool>("AllowFallNoodle");//是否允许到面
  102. bool[] CookNoodlesComplete = GetTypeValue<bool[]>("CookNoodlesComplete");//煮面完成上升信号
  103. bool isCookNoodles = CookNoodlesComplete.ToList().Find(o => o);
  104. //设置扩展属性与状态,连接上机器人后才会处罚
  105. if (IsAllowRun)
  106. {
  107. kzsx = Tools.JsonConvertTools<DevSX>(new DevSX
  108. {
  109. data = new List<DevSXBase>
  110. {
  111. new DevSXBase { SXMC="设备硬件",SXLX="煮面炉",SXStatus= !TemperatureReached ,
  112. SXYCMS=!TemperatureReached?"":DateTime.Now+" 异常,煮面炉温度不够,疑似正在加热或者未工作...."},
  113. new DevSXBase { SXMC="设备硬件",SXLX="装小碗结构",SXStatus=MissingBowl ,
  114. SXYCMS=MissingBowl?"":DateTime.Now+" 异常,缺小碗...."},
  115. new DevSXBase { SXMC="设备硬件",SXLX="装大碗结构",SXStatus=MissingBowlSignal2 ,
  116. SXYCMS=MissingBowlSignal2?"":DateTime.Now+" 异常,缺大碗...."},
  117. new DevSXBase { SXMC="设备硬件",SXLX="取面转台",SXStatus=IsNoodles ,
  118. SXYCMS=IsNoodles?"":DateTime.Now+" 异常,转台位置缺少物料...."},
  119. new DevSXBase { SXMC="设备硬件",SXLX="配料机",SXStatus=!(!AllowFallNoodle && isCookNoodles) ,
  120. SXYCMS=!((!AllowFallNoodle && isCookNoodles))?"":DateTime.Now+" 异常,配料机未配完料,疑似碗未到配料机下方或者配料机未工作...."}
  121. }
  122. });
  123. }
  124. string jsonstr = Tools.JsonConvertTools<IotModel<TargetIOT>>(new IotModel<TargetIOT> { @params = new TargetIOT { JBSX = jbsx,KZSX = kzsx } });
  125. IOTDevServer.GetInstance().IOT_Publish(IOTDevServer.PubTopic,jsonstr);
  126. }
  127. }
  128. #endregion
  129. #region 外部关闭或初始化IOT调用
  130. /// <summary>
  131. /// 初始化IOT连接
  132. /// </summary>
  133. public bool Initialize()
  134. {
  135. if (!IOTDevServer.GetInstance().CreateLinks(InternetInfo.ClientId,out device))
  136. {
  137. MessageLog.GetInstance.Show($"设备{InternetInfo.ClientId}阿里云上没有该设备。");
  138. return false;
  139. }
  140. IOTDevServer.GetInstance().IOT_Subscribe(IOTDevServer.BroadcastTopic);//订阅广播主题
  141. IOTDevServer.DevIOTAction += DevIOTActionHandler;
  142. IOTDevServer.UNConnectMqtt += new Action<string>((o) => { MessageLog.GetInstance.Show(o); });//断网自动重连接与打印
  143. if (IOTDevServer.client.IsConnected) MessageLog.GetInstance.Show($"设备{device.devicename} {device.remark}阿里云连接成功.");
  144. else MessageLog.GetInstance.Show($"设备{device.devicename} {device.remark}阿里云连接失败.不能上报业务信息");
  145. return IOTDevServer.client.IsConnected;
  146. }
  147. /// <summary>
  148. /// 关闭IOT连接
  149. /// </summary>
  150. public static void Close()
  151. {
  152. if (IOTDevServer.client != null)
  153. IOTDevServer.GetInstance().Disconnect();
  154. }
  155. #endregion
  156. #region 云端订阅主题消息接收
  157. /// <summary>
  158. /// 接收云端消息
  159. /// </summary>
  160. /// <param name="topic"></param>
  161. /// <param name="message"></param>
  162. private void DevIOTActionHandler(string topic, string message)
  163. {
  164. if (IOTDevServer.BroadcastTopic == topic && !string.IsNullOrEmpty(message))//广播主题消息,将广播消息发送到相应客户端
  165. {
  166. IOTCommandModel iOTCommand = Tools.JsonToObjectTools<IOTCommandModel>(message);
  167. if (iOTCommand.deviceName == device.devicename)
  168. ActionManage.GetInstance.Send("IotBroadcast", iOTCommand);
  169. }
  170. }
  171. #endregion
  172. #region 私有函数
  173. /// <summary>
  174. /// 获取属性状态
  175. /// </summary>
  176. private T GetTypeValue<T>(string str)
  177. {
  178. return (T)SimpleFactory.GetInstance.GVL?.GetType().GetProperty(str).GetValue(SimpleFactory.GetInstance.GVL,null);
  179. }
  180. #endregion
  181. }
  182. /// <summary>
  183. /// 日志上报
  184. /// </summary>
  185. public class LogIOT
  186. {
  187. public string SZXX { get; set; }
  188. }
  189. /// <summary>
  190. /// 告警上报
  191. /// </summary>
  192. public class AlarmIOT
  193. {
  194. public string GJXX { get; set; }
  195. }
  196. /// <summary>
  197. /// 节点状态上报
  198. /// </summary>
  199. public class NodeStatusIOT
  200. {
  201. public string NodeStatus { get; set; }
  202. }
  203. /// <summary>
  204. /// 基本属性与扩展属性
  205. /// </summary>
  206. public class TargetIOT
  207. {
  208. /// <summary>
  209. /// 基本属性
  210. /// </summary>
  211. public string JBSX { get; set; }
  212. /// <summary>
  213. /// 扩展属性
  214. /// </summary>
  215. public string KZSX { get; set; }
  216. }
  217. }