|
- using BPA.Message.IOT;
- using HBLConsole.Communication;
- using HBLConsole.GVL;
- using HBLConsole.Interface;
- using HBLConsole.Service;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Reflection;
- using System.Text;
- using System.Threading;
- using System.Threading.Tasks;
- using HBLConsole.Factory;
- using BPA.Message.API请求;
-
- namespace HBLConsole.Business
- {
- public class IotReport
- {
- #region 单例模式
- private volatile static IotReport _Instance;
- public static IotReport GetInstance => _Instance ?? (_Instance = new IotReport());
- #endregion
-
- #region 变量
- /// <summary>
- /// 查询出当前设备信息
- /// </summary>
- public DeviceTable device;
- /// <summary>
- /// 大屏上报Model
- /// </summary>
- public IOTDevSXModel iOTDevSXModel = new IOTDevSXModel() { };
- #endregion
-
- #region IOT上报公共调用
- /// <summary>
- /// 上报告警消息
- /// 调用示例: SendAlarmMessage(new AlarmTable { AlarmTime = DateTime.Now,AlarmType = "1",AlarmMessage = "煮面机异常",AlarmVla = "煮面机" });
- /// </summary>
- /// <param name="alarmTable"></param>
- public void SendAlarmMessage(AlarmTable alarmTable)
- {
- if (IOTDevServer.client != null && IOTDevServer.client.IsConnected && device!=null)
- {
- alarmTable.ClientId = InternetInfo.ClientId.ToString();
- alarmTable.devicename = device.devicename;
- string json = Tools.JsonConvertTools<IotModel<AlarmIOT>>(new IotModel<AlarmIOT> { @params = new AlarmIOT { GJXX = Tools.JsonConvertTools(alarmTable) } });
- IOTDevServer.GetInstance().IOT_Publish(IOTDevServer.PubTopic,json);
- }
- }
-
- /// <summary>
- /// 上报日志消息
- /// 调用示例:SendLogMessage(new LogTable { LogTime = DateTime.Now,LogType = "1",LogMessage = "程序异常",LogVla = "程序" });
- /// </summary>
- /// <param name="logTable"></param>
- public void SendLogMessage(LogTable logTable)
- {
- if (IOTDevServer.client != null && IOTDevServer.client.IsConnected && device != null)
- {
- logTable.devicename = device.devicename;
- logTable.ClientId = InternetInfo.ClientId.ToString();
- string json = Tools.JsonConvertTools<IotModel<LogIOT>>(new IotModel<LogIOT> { @params = new LogIOT { SZXX = Tools.JsonConvertTools(logTable) } });
- IOTDevServer.GetInstance().IOT_Publish(IOTDevServer.PubTopic,json);
- }
- }
-
- /// <summary>
- /// 上报节点状态消息
- /// 调用示例:SendNodeStatusMessage("json字符串");
- /// </summary>
- /// <param name="json"></param>
- public void SendNodeStatusMessage(string json)
- {
- if (IOTDevServer.client!=null && IOTDevServer.client.IsConnected && device != null)
- {
- string jsonstr = Tools.JsonConvertTools<IotModel<NodeStatusIOT>>(new IotModel<NodeStatusIOT> { @params = new NodeStatusIOT { NodeStatus = Tools.JsonConvertTools(json) } });
- IOTDevServer.GetInstance().IOT_Publish(IOTDevServer.PubTopic,jsonstr);
- }
- }
-
- /// <summary>
- /// 上报属性状态
- /// 调用示例:SendTargetMessage();
- /// </summary>
- /// <param name="json"></param>
- public void SendTargetMessage()
- {
- if (IOTDevServer.client != null && IOTDevServer.client.IsConnected && device != null)
- {
- string kzsx = string.Empty;
- string jbsx = Tools.JsonConvertTools<DevSX>(new DevSX
- {
- data = new List<DevSXBase>
- {
- new DevSXBase { SXMC="设备硬件",SXLX="机器人",SXStatus=ModbusTcpHelper.GetInstance.Connected
- ,SXYCMS=ModbusTcpHelper.GetInstance.Connected?"":DateTime.Now+" 异常,机器人未连接,疑似未连接网络...."},
- new DevSXBase { SXMC="设备软件",SXLX="上位机",SXStatus=true }
- }
- });
- bool IsAllowRun = GetTypeValue<bool>("AllowRun");
- bool TemperatureReached = GetTypeValue<bool>("TemperatureReached");
- bool MissingBowl = GetTypeValue<bool>("MissingBowl");
- bool MissingBowlSignal2 = GetTypeValue<bool>("MissingBowlSignal2");
- bool IsNoodles = GetTypeValue<bool>("IsNoodles");//转台
- bool AllowFallNoodle = GetTypeValue<bool>("AllowFallNoodle");//是否允许到面
- bool[] CookNoodlesComplete = GetTypeValue<bool[]>("CookNoodlesComplete");//煮面完成上升信号
- bool isCookNoodles = CookNoodlesComplete.ToList().Find(o => o);
- //设置扩展属性与状态,连接上机器人后才会处罚
- if (IsAllowRun)
- {
- kzsx = Tools.JsonConvertTools<DevSX>(new DevSX
- {
- data = new List<DevSXBase>
- {
- new DevSXBase { SXMC="设备硬件",SXLX="煮面炉",SXStatus= !TemperatureReached ,
- SXYCMS=!TemperatureReached?"":DateTime.Now+" 异常,煮面炉温度不够,疑似正在加热或者未工作...."},
- new DevSXBase { SXMC="设备硬件",SXLX="装小碗结构",SXStatus=MissingBowl ,
- SXYCMS=MissingBowl?"":DateTime.Now+" 异常,缺小碗...."},
- new DevSXBase { SXMC="设备硬件",SXLX="装大碗结构",SXStatus=MissingBowlSignal2 ,
- SXYCMS=MissingBowlSignal2?"":DateTime.Now+" 异常,缺大碗...."},
- new DevSXBase { SXMC="设备硬件",SXLX="取面转台",SXStatus=IsNoodles ,
- SXYCMS=IsNoodles?"":DateTime.Now+" 异常,转台位置缺少物料...."},
- new DevSXBase { SXMC="设备硬件",SXLX="配料机",SXStatus=!(!AllowFallNoodle && isCookNoodles) ,
- SXYCMS=!((!AllowFallNoodle && isCookNoodles))?"":DateTime.Now+" 异常,配料机未配完料,疑似碗未到配料机下方或者配料机未工作...."}
- }
- });
- }
- string jsonstr = Tools.JsonConvertTools<IotModel<TargetIOT>>(new IotModel<TargetIOT> { @params = new TargetIOT { JBSX = jbsx,KZSX = kzsx } });
- IOTDevServer.GetInstance().IOT_Publish(IOTDevServer.PubTopic,jsonstr);
- }
- }
- #endregion
-
- #region 外部关闭或初始化IOT调用
- /// <summary>
- /// 初始化IOT连接
- /// </summary>
- public bool Initialize()
- {
- if (!IOTDevServer.GetInstance().CreateLinks(InternetInfo.ClientId,out device))
- {
- MessageLog.GetInstance.Show($"设备{InternetInfo.ClientId}阿里云上没有该设备。");
- return false;
- }
- IOTDevServer.GetInstance().IOT_Subscribe(IOTDevServer.BroadcastTopic);//订阅广播主题
- IOTDevServer.DevIOTAction += DevIOTActionHandler;
- IOTDevServer.UNConnectMqtt += new Action<string>((o) => { MessageLog.GetInstance.Show(o); });//断网自动重连接与打印
- if (IOTDevServer.client.IsConnected) MessageLog.GetInstance.Show($"设备{device.devicename} {device.remark}阿里云连接成功.");
- else MessageLog.GetInstance.Show($"设备{device.devicename} {device.remark}阿里云连接失败.不能上报业务信息");
- return IOTDevServer.client.IsConnected;
- }
-
- /// <summary>
- /// 关闭IOT连接
- /// </summary>
- public static void Close()
- {
- if (IOTDevServer.client != null)
- IOTDevServer.GetInstance().Disconnect();
- }
- #endregion
-
- #region 云端订阅主题消息接收
- /// <summary>
- /// 接收云端消息
- /// </summary>
- /// <param name="topic"></param>
- /// <param name="message"></param>
- private void DevIOTActionHandler(string topic, string message)
- {
- if (IOTDevServer.BroadcastTopic == topic && !string.IsNullOrEmpty(message))//广播主题消息,将广播消息发送到相应客户端
- {
- IOTCommandModel iOTCommand = Tools.JsonToObjectTools<IOTCommandModel>(message);
- if (iOTCommand.deviceName == device.devicename)
- ActionManage.GetInstance.Send("IotBroadcast", iOTCommand);
- }
- }
- #endregion
-
- #region 私有函数
- /// <summary>
- /// 获取属性状态
- /// </summary>
- private T GetTypeValue<T>(string str)
- {
- return (T)SimpleFactory.GetInstance.GVL?.GetType().GetProperty(str).GetValue(SimpleFactory.GetInstance.GVL,null);
- }
- #endregion
- }
-
- /// <summary>
- /// 日志上报
- /// </summary>
- public class LogIOT
- {
- public string SZXX { get; set; }
- }
- /// <summary>
- /// 告警上报
- /// </summary>
- public class AlarmIOT
- {
- public string GJXX { get; set; }
- }
- /// <summary>
- /// 节点状态上报
- /// </summary>
- public class NodeStatusIOT
- {
- public string NodeStatus { get; set; }
- }
- /// <summary>
- /// 基本属性与扩展属性
- /// </summary>
- public class TargetIOT
- {
- /// <summary>
- /// 基本属性
- /// </summary>
- public string JBSX { get; set; }
- /// <summary>
- /// 扩展属性
- /// </summary>
- public string KZSX { get; set; }
- }
- }
|