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
7.6 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. iSLinks = IOTDev.GetInstance().CreateLinks();
  24. //订阅设备控制主题
  25. IOTDev.GetInstance().IOT_Subscribe("/" + InternetInfo.ProductKey + "/" + InternetInfo.DeviceName + "/user/SetDevice");
  26. IOTDev.actionIOT += actionIOTHandler;//消息数据回调
  27. if (iSLinks) 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. /// 是否连接成功
  36. /// </summary>
  37. public bool iSLinks = false;
  38. /// <summary>
  39. /// 大屏上报Model
  40. /// </summary>
  41. public IOTDevSXModel iOTDevSXModel = new IOTDevSXModel() { SBMC = InternetInfo.DeviceName, SBMS = InternetInfo.DeviceMS };
  42. /// <summary>
  43. /// 当前控制的设备控件
  44. /// </summary>
  45. public IControl control;
  46. #endregion
  47. /// <summary>
  48. /// 关闭IOT连接
  49. /// </summary>
  50. public void Close()
  51. {
  52. IOTDev.GetInstance().Disconnect();
  53. }
  54. /// <summary>
  55. /// 获取属性状态
  56. /// </summary>
  57. public T GetTypeValue<T>(string str)
  58. {
  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. iOTDevSXModel.SetKZSX(new DevSX { data = new List<DevSXBase> { new DevSXBase { SXMC = "", SXLX = "" } } });
  110. }
  111. #endregion
  112. //设置告警消息
  113. #region 告警消息
  114. List<DevSXBase> bases = Tools.JsonToObjectTools<DevSX>(iOTDevSXModel.JBSX)?.data?.ToList().FindAll(par => par.SXStatus == false);
  115. if (bases != null) bases.AddRange(Tools.JsonToObjectTools<DevSX>(iOTDevSXModel.KZSX)?.data?.ToList().FindAll(par => par.SXStatus == false));
  116. List<AlarmModel> alarms = new List<AlarmModel>();
  117. bases?.ForEach(par => alarms.Add(new AlarmModel { DeviceMC = iOTDevSXModel.SBMC, DeviceSJ = DateTime.Now.ToString(), AlarmCD = "一般", DeviceZT = "未处理", DeviceMS = par.SXYCMS }));
  118. iOTDevSXModel.SetGJXX(new AlarmMessage { data = alarms });
  119. #endregion
  120. bool InitComplete = GetTypeValue<bool>("InitComplete");
  121. bool RobotTakeNoodle = GetTypeValue<bool>("RobotTakeNoodle");
  122. bool RobotOutMeal = GetTypeValue<bool>("RobotOutMeal");
  123. iOTDevSXModel.SetLCSB(new ProcessMessage
  124. {
  125. data = new List<ProcessModel>
  126. {
  127. new ProcessModel { ProcessName="开机",IsMark=IsAllowRun,ProcessMS=IsAllowRun?"机器人开机成功":"" },
  128. new ProcessModel { ProcessName="初始化",IsMark=InitComplete,ProcessMS=InitComplete?"机器人正在初始化成功":"" },
  129. new ProcessModel { ProcessName="取面",IsMark=RobotTakeNoodle,ProcessMS=RobotTakeNoodle?"机器人正在取面过程中....":""},
  130. new ProcessModel { ProcessName="出餐",IsMark=AllowFallNoodle,ProcessMS=AllowFallNoodle?"机器人正在出餐过程中....":"" }
  131. }
  132. });
  133. #endregion
  134. }
  135. /// <summary>
  136. /// 初始化上报流程
  137. /// </summary>
  138. public void Init()
  139. {
  140. ThreadOperate.GetInstance.StartLong(new Action(() =>
  141. {
  142. if (iSLinks && GeneralConfig.DeviceType.ToString() == "MORKS")
  143. {
  144. SetData();
  145. IOTDev.GetInstance().IOT_Publish(IOTDev.PubTopic, iOTDevSXModel.Tojson());
  146. }
  147. Thread.Sleep(5000);
  148. }), "设备IOT上报云端");
  149. }
  150. private void actionIOTHandler(string topic, string message)
  151. {
  152. }
  153. }
  154. }