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.
 
 

309 lines
13 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. /// 调用示例:SendTargetMessage();
  67. /// </summary>
  68. /// <param name="json"></param>
  69. public void SendTargetMessage()
  70. {
  71. if (IOTDevServer.client != null && IOTDevServer.client.IsConnected && device != null)
  72. {
  73. #region 赋值基本属性与状态
  74. bool IsAllowRun = false;
  75. bool TemperatureReached = false;
  76. iOTDevSXModel.KZSX = Tools.JsonConvertTools<DevSX>(new DevSX { data = new List<DevSXBase> { new DevSXBase { SXMC = "", SXLX = "" } } });
  77. iOTDevSXModel.JBSX = Tools.JsonConvertTools<DevSX>(new DevSX
  78. {
  79. data = new List<DevSXBase>
  80. {
  81. new DevSXBase { SXMC="设备硬件",SXLX="机器人",SXStatus=ModbusTcpHelper.GetInstance.Connected
  82. ,SXYCMS=ModbusTcpHelper.GetInstance.Connected?"":DateTime.Now+" 异常,机器人未连接,疑似未连接网络...."},
  83. new DevSXBase { SXMC="设备软件",SXLX="上位机",SXStatus=true }
  84. }
  85. });
  86. #endregion
  87. switch (GeneralConfig.DeviceType)
  88. {
  89. case BPA.Message.Enum.DeviceClientType.MORKD:
  90. break;
  91. case BPA.Message.Enum.DeviceClientType.MORKS:
  92. #region 赋值扩展属性与状态
  93. IsAllowRun = GetTypeValue<bool>("AllowRun");
  94. TemperatureReached = GetTypeValue<bool>("TemperatureReached");
  95. bool MissingBowl = GetTypeValue<bool>("MissingBowl");
  96. bool MissingBowlSignal2 = GetTypeValue<bool>("MissingBowlSignal2");
  97. bool IsNoodles = GetTypeValue<bool>("IsNoodles");//转台
  98. bool AllowFallNoodle = GetTypeValue<bool>("AllowFallNoodle");//是否允许到面
  99. bool[] CookNoodlesComplete = GetTypeValue<bool[]>("CookNoodlesComplete");//煮面完成上升信号
  100. bool isCookNoodles = CookNoodlesComplete.ToList().Find(o => o);
  101. //设置扩展属性与状态,连接上机器人后才会处罚
  102. if (IsAllowRun)
  103. {
  104. iOTDevSXModel.KZSX = Tools.JsonConvertTools<DevSX>(new DevSX
  105. {
  106. data = new List<DevSXBase>
  107. {
  108. new DevSXBase { SXMC="设备硬件",SXLX="煮面炉",SXStatus= !TemperatureReached ,
  109. SXYCMS=!TemperatureReached?"":DateTime.Now+" 异常,煮面炉温度不够,疑似正在加热或者未工作...."},
  110. new DevSXBase { SXMC="设备硬件",SXLX="装小碗结构",SXStatus=MissingBowl ,
  111. SXYCMS=MissingBowl?"":DateTime.Now+" 异常,缺小碗...."},
  112. new DevSXBase { SXMC="设备硬件",SXLX="装大碗结构",SXStatus=MissingBowlSignal2 ,
  113. SXYCMS=MissingBowlSignal2?"":DateTime.Now+" 异常,缺大碗...."},
  114. new DevSXBase { SXMC="设备硬件",SXLX="取面转台",SXStatus=IsNoodles ,
  115. SXYCMS=IsNoodles?"":DateTime.Now+" 异常,转台位置缺少物料...."},
  116. new DevSXBase { SXMC="设备硬件",SXLX="配料机",SXStatus=!(!AllowFallNoodle && isCookNoodles) ,
  117. SXYCMS=!((!AllowFallNoodle && isCookNoodles))?"":DateTime.Now+" 异常,配料机未配完料,疑似碗未到配料机下方或者配料机未工作...."}
  118. }
  119. });
  120. }
  121. #endregion
  122. break;
  123. case BPA.Message.Enum.DeviceClientType.MORKI:
  124. break;
  125. case BPA.Message.Enum.DeviceClientType.MORKC:
  126. break;
  127. case BPA.Message.Enum.DeviceClientType.MORKIC:
  128. break;
  129. default:
  130. break;
  131. }
  132. IOTDevServer.GetInstance().IOT_Publish(IOTDevServer.PubTopic, iOTDevSXModel.Tojson());
  133. }
  134. }
  135. #endregion
  136. #region API调用
  137. /// <summary>
  138. /// 增加告警信息
  139. /// </summary>
  140. /// <param name="alarmTable"></param>
  141. /// <returns>返回ID</returns>
  142. public string HttpAddAlarm(AlarmTable alarmTable)
  143. {
  144. string id=string.Empty;
  145. try
  146. {
  147. if (IOTDevServer.client != null && IOTDevServer.client.IsConnected && device != null)
  148. {
  149. string url = InternetInfo.IotApiAddress + "/api/Alarm/Create";
  150. alarmTable.ClientId = InternetInfo.ClientId.ToString();
  151. alarmTable.devicename = device.devicename;
  152. string redata= HttpRequestHelper.HttpPostRequest(url,Tools.JsonConvertTools(alarmTable));
  153. if (!string.IsNullOrEmpty(redata))
  154. {
  155. JsonMsg<AlarmTable> msg = Tools.JsonToObjectTools<JsonMsg<AlarmTable>>(redata);
  156. id = msg?.obj.IdStr;
  157. }
  158. }
  159. }
  160. catch (Exception ex)
  161. {
  162. MessageLog.GetInstance.Show(ex.Message);
  163. }
  164. return id;
  165. }
  166. /// <summary>
  167. /// 根据ID删除告警信息
  168. /// </summary>
  169. /// <param name="alarm"></param>
  170. public void HttpDeleteAlarm(string id)
  171. {
  172. try
  173. {
  174. if (string.IsNullOrEmpty(id)) { MessageLog.GetInstance.Show("API调用删除告警信息,ID不能为空!"); return; }
  175. if (IOTDevServer.client != null && IOTDevServer.client.IsConnected && device != null)
  176. {
  177. string url = InternetInfo.IotApiAddress + "/api/Alarm/Delete?id="+ id;
  178. HttpRequestHelper.HttpGetRequest(url);
  179. }
  180. }
  181. catch (Exception ex)
  182. {
  183. MessageLog.GetInstance.Show(ex.Message);
  184. }
  185. }
  186. /// <summary>
  187. /// 增加日志信息
  188. /// </summary>
  189. /// <param name="alarmTable"></param>
  190. /// <returns>返回ID</returns>
  191. public string HttpAddLog(LogTable logTable)
  192. {
  193. string id = string.Empty;
  194. try
  195. {
  196. if (IOTDevServer.client != null && IOTDevServer.client.IsConnected && device != null)
  197. {
  198. string url = InternetInfo.IotApiAddress + "/api/Log/Create";
  199. logTable.ClientId = InternetInfo.ClientId.ToString();
  200. logTable.devicename = device.devicename;
  201. string redata = HttpRequestHelper.HttpPostRequest(url,Tools.JsonConvertTools(logTable));
  202. if (!string.IsNullOrEmpty(redata))
  203. {
  204. JsonMsg<LogTable> msg = Tools.JsonToObjectTools<JsonMsg<LogTable>>(redata);
  205. id = msg?.obj.IdStr;
  206. }
  207. }
  208. }
  209. catch (Exception ex)
  210. {
  211. MessageLog.GetInstance.Show(ex.Message);
  212. }
  213. return id;
  214. }
  215. #endregion
  216. #region 外部关闭或初始化IOT调用
  217. /// <summary>
  218. /// 初始化IOT连接
  219. /// </summary>
  220. public bool Initialize()
  221. {
  222. if (!string.IsNullOrEmpty(InternetInfo.IotApiAddress)) IOTDevServer.GetInstance().SetUrl(InternetInfo.IotApiAddress);
  223. if (!IOTDevServer.GetInstance().CreateLinks(InternetInfo.ClientId, out device))
  224. {
  225. MessageLog.GetInstance.Show($"设备{InternetInfo.ClientId}阿里云上没有该设备。");
  226. return false;
  227. }
  228. IOTDevServer.GetInstance().IOT_Subscribe(IOTDevServer.BroadcastTopic);//订阅广播主题
  229. IOTDevServer.DevIOTAction += DevIOTActionHandler;
  230. IOTDevServer.UNConnectMqtt += new Action<string>((o) => { MessageLog.GetInstance.Show(o); });//断网自动重连接与打印
  231. if (IOTDevServer.client.IsConnected) MessageLog.GetInstance.Show($"设备{device.devicename} {device.remark}阿里云连接成功.");
  232. else MessageLog.GetInstance.Show($"设备{device.devicename} {device.remark}阿里云连接失败.不能上报业务信息");
  233. return IOTDevServer.client.IsConnected;
  234. }
  235. /// <summary>
  236. /// 关闭IOT连接
  237. /// </summary>
  238. public static void Close()
  239. {
  240. if (IOTDevServer.client != null)
  241. IOTDevServer.GetInstance().Disconnect();
  242. }
  243. #endregion
  244. #region 云端订阅主题消息接收
  245. /// <summary>
  246. /// 接收云端消息
  247. /// </summary>
  248. /// <param name="topic"></param>
  249. /// <param name="message"></param>
  250. private void DevIOTActionHandler(string topic, string message)
  251. {
  252. if (IOTDevServer.BroadcastTopic == topic && !string.IsNullOrEmpty(message))//广播主题消息,将广播消息发送到相应客户端
  253. {
  254. IOTCommandModel iOTCommand = Tools.JsonToObjectTools<IOTCommandModel>(message);
  255. if (iOTCommand.deviceName == device.devicename)
  256. ActionManage.GetInstance.Send("IotBroadcast", iOTCommand);
  257. }
  258. }
  259. #endregion
  260. #region 私有函数
  261. /// <summary>
  262. /// 获取属性状态
  263. /// </summary>
  264. private T GetTypeValue<T>(string str)
  265. {
  266. return (T)SimpleFactory.GetInstance.GVL?.GetType().GetProperty(str)?.GetValue(SimpleFactory.GetInstance.GVL, null);
  267. }
  268. #endregion
  269. }
  270. public class IOTNode<T>
  271. {
  272. public T data { get; set; }
  273. }
  274. /// <summary>
  275. /// 日志上报
  276. /// </summary>
  277. public class LogIOT
  278. {
  279. public string SZXX { get; set; }
  280. }
  281. /// <summary>
  282. /// 告警上报
  283. /// </summary>
  284. public class AlarmIOT
  285. {
  286. public string GJXX { get; set; }
  287. }
  288. }