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.
 
 

131 lines
5.9 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. private volatile static IotReport _Instance;
  18. public static IotReport GetInstance => _Instance ?? (_Instance = new IotReport());
  19. private IotReport()
  20. {
  21. iOTDevSXModel = new IOTDevSXModel();
  22. iOTDevSXModel.SBMC = InternetInfo.DeviceName;
  23. iOTDevSXModel.SBMS = InternetInfo.DeviceMS;
  24. iOTDevSXModel.SetKZSX(new DevSX { data = new List<DevSXBase> { new DevSXBase { SXMC = "", SXLX = "" } } });
  25. IOTDev.GetInstance().Set(InternetInfo.ProductKey, InternetInfo.DeviceName, InternetInfo.DeviceSecret);
  26. iSLinks = IOTDev.GetInstance().CreateLinks();
  27. if (iSLinks) MessageLog.GetInstance.Show($"设备{InternetInfo.DeviceName}阿里云连接成功.");
  28. else MessageLog.GetInstance.Show($"设备{InternetInfo.DeviceName}阿里云连接失败.不能上报业务信息");
  29. string NameSpace = $"HBLConsole.{GeneralConfig.DeviceType}";//Load 加载的是dll的名称,GetType获取的是全命名空间下的类
  30. Type type = Assembly.Load(NameSpace).GetType($"{NameSpace}.Control_{GeneralConfig.DeviceType}");
  31. control = Activator.CreateInstance(type) as IControl;
  32. }
  33. /// <summary>
  34. /// 是否连接成功
  35. /// </summary>
  36. public bool iSLinks = false;
  37. /// <summary>
  38. /// 大屏上报Model
  39. /// </summary>
  40. public IOTDevSXModel iOTDevSXModel = new IOTDevSXModel();
  41. /// <summary>
  42. ///
  43. /// </summary>
  44. IControl control;
  45. public void Close()
  46. {
  47. IOTDev.GetInstance().Disconnect();
  48. }
  49. public T GetTypeValue<T>(string str)
  50. {
  51. return (T)control.GetT().GetType().GetProperty(str).GetValue(control.GetT(), null);
  52. }
  53. public void SetData()
  54. {
  55. #region 上报
  56. //设置基本属性与状态
  57. iOTDevSXModel.SetJBSX(new DevSX
  58. {
  59. data = new List<DevSXBase>
  60. {
  61. new DevSXBase { SXMC="设备硬件",SXLX="机器人",SXStatus=ModbusTcpHelper.GetInstance.Connected
  62. ,SXYCMS=ModbusTcpHelper.GetInstance.Connected?"":DateTime.Now+" 异常,机器人未连接,疑似未连接网络...."},
  63. new DevSXBase { SXMC="设备软件",SXLX="上位机",SXStatus=true }
  64. }
  65. });
  66. bool IsAllowRun = GetTypeValue<bool>("AllowRun");
  67. bool TemperatureReached = GetTypeValue<bool>("TemperatureReached");
  68. bool MissingBowl = GetTypeValue<bool>("MissingBowl");
  69. bool TurntableLowerLimit = GetTypeValue<bool>("TurntableLowerLimit");
  70. //设置扩展属性与状态,连接上机器人后才会处罚
  71. if (IsAllowRun)
  72. {
  73. iOTDevSXModel.SetKZSX(new DevSX
  74. {
  75. data = new List<DevSXBase>
  76. {
  77. new DevSXBase { SXMC="设备硬件",SXLX="煮面炉",SXStatus= TemperatureReached ,
  78. SXYCMS=TemperatureReached?"":DateTime.Now+" 异常,煮面炉温度不够,疑似正在加热或者未工作...."},
  79. new DevSXBase { SXMC="设备硬件",SXLX="取碗结构",SXStatus=MissingBowl ,
  80. SXYCMS=MissingBowl?"":DateTime.Now+" 异常,缺碗...."},
  81. new DevSXBase { SXMC="设备硬件",SXLX="取面转台",SXStatus=TurntableLowerLimit ,
  82. SXYCMS=TurntableLowerLimit?"":DateTime.Now+" 异常,转台缺面...."}
  83. }
  84. });
  85. }
  86. //设置告警消息
  87. List<DevSXBase> bases = Tools.JsonToObjectTools<DevSX>(iOTDevSXModel.JBSX)?.data?.ToList().FindAll(par => par.SXStatus == false);
  88. if (bases != null) bases.AddRange(Tools.JsonToObjectTools<DevSX>(iOTDevSXModel.KZSX)?.data?.ToList().FindAll(par => par.SXStatus == false));
  89. List<AlarmModel> alarms = new List<AlarmModel>();
  90. bases?.ForEach(par => alarms.Add(new AlarmModel { DeviceMC = iOTDevSXModel.SBMC, DeviceSJ = DateTime.Now.ToString(), AlarmCD = "一般", DeviceZT = "未处理", DeviceMS = par.SXYCMS }));
  91. iOTDevSXModel.SetGJXX(new AlarmMessage { data = alarms });
  92. bool InitComplete = GetTypeValue<bool>("InitComplete");
  93. bool RobotTakeNoodle = GetTypeValue<bool>("RobotTakeNoodle");
  94. bool RobotOutMeal = GetTypeValue<bool>("RobotOutMeal");
  95. iOTDevSXModel.SetLCSB(new ProcessMessage
  96. {
  97. data = new List<ProcessModel>
  98. {
  99. new ProcessModel { ProcessName="开机",IsMark=!IsAllowRun },
  100. new ProcessModel { ProcessName="初始化",IsMark=!InitComplete },
  101. new ProcessModel { ProcessName="取面",IsMark=RobotTakeNoodle,ProcessMS=RobotTakeNoodle?"机器人正在取面过程中....":""},
  102. new ProcessModel { ProcessName="出餐",IsMark=RobotOutMeal,ProcessMS=RobotOutMeal?"机器人正在出餐过程中....":"" }
  103. }
  104. });
  105. #endregion
  106. }
  107. public void Init()
  108. {
  109. ThreadOperate.GetInstance.StartLong(new Action(() =>
  110. {
  111. if (iSLinks && GeneralConfig.DeviceType.ToString() == "MO7RKS")
  112. {
  113. SetData();
  114. IOTDev.GetInstance().IOT_Publish(IOTDev.PubTopic, iOTDevSXModel.Tojson());
  115. }
  116. Thread.Sleep(5000);
  117. }), "设备IOT上报云端");
  118. }
  119. }
  120. }