|
- 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>
- /// 上报属性状态
- /// 调用示例:SendTargetMessage();
- /// </summary>
- /// <param name="json"></param>
- public void SendTargetMessage()
- {
- if (IOTDevServer.client != null && IOTDevServer.client.IsConnected && device != null)
- {
- #region 赋值基本属性与状态
- bool IsAllowRun = false;
- bool TemperatureReached = false;
- iOTDevSXModel.KZSX = Tools.JsonConvertTools<DevSX>(new DevSX { data = new List<DevSXBase> { new DevSXBase { SXMC = "", SXLX = "" } } });
- iOTDevSXModel.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 }
- }
- });
- #endregion
-
- switch (GeneralConfig.DeviceType)
- {
- case BPA.Message.Enum.DeviceClientType.MORKD:
- break;
- case BPA.Message.Enum.DeviceClientType.MORKS:
- #region 赋值扩展属性与状态
- IsAllowRun = GetTypeValue<bool>("AllowRun");
- 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)
- {
- iOTDevSXModel.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+" 异常,配料机未配完料,疑似碗未到配料机下方或者配料机未工作...."}
- }
- });
- }
- #endregion
- break;
- case BPA.Message.Enum.DeviceClientType.MORKI:
-
-
- break;
- case BPA.Message.Enum.DeviceClientType.MORKC:
-
-
- break;
- case BPA.Message.Enum.DeviceClientType.MORKIC:
-
-
- break;
- default:
- break;
- }
- IOTDevServer.GetInstance().IOT_Publish(IOTDevServer.PubTopic, iOTDevSXModel.Tojson());
- }
- }
- #endregion
-
- #region API调用
- /// <summary>
- /// 增加告警信息
- /// </summary>
- /// <param name="alarmTable"></param>
- /// <returns>返回ID</returns>
- public string HttpAddAlarm(AlarmTable alarmTable)
- {
- string id=string.Empty;
- try
- {
- if (IOTDevServer.client != null && IOTDevServer.client.IsConnected && device != null)
- {
- string url = InternetInfo.IotApiAddress + "/api/Alarm/Create";
- alarmTable.ClientId = InternetInfo.ClientId.ToString();
- alarmTable.devicename = device.devicename;
- string redata= HttpRequestHelper.HttpPostRequest(url,Tools.JsonConvertTools(alarmTable));
- if (!string.IsNullOrEmpty(redata))
- {
- JsonMsg<AlarmTable> msg = Tools.JsonToObjectTools<JsonMsg<AlarmTable>>(redata);
- id = msg?.obj.IdStr;
- }
- }
- }
- catch (Exception ex)
- {
- MessageLog.GetInstance.Show(ex.Message);
- }
- return id;
- }
- /// <summary>
- /// 根据ID删除告警信息
- /// </summary>
- /// <param name="alarm"></param>
- public void HttpDeleteAlarm(string id)
- {
- try
- {
- if (string.IsNullOrEmpty(id)) { MessageLog.GetInstance.Show("API调用删除告警信息,ID不能为空!"); return; }
- if (IOTDevServer.client != null && IOTDevServer.client.IsConnected && device != null)
- {
- string url = InternetInfo.IotApiAddress + "/api/Alarm/Delete?id="+ id;
- HttpRequestHelper.HttpGetRequest(url);
- }
- }
- catch (Exception ex)
- {
- MessageLog.GetInstance.Show(ex.Message);
- }
- }
-
- /// <summary>
- /// 增加日志信息
- /// </summary>
- /// <param name="alarmTable"></param>
- /// <returns>返回ID</returns>
- public string HttpAddLog(LogTable logTable)
- {
- string id = string.Empty;
- try
- {
- if (IOTDevServer.client != null && IOTDevServer.client.IsConnected && device != null)
- {
- string url = InternetInfo.IotApiAddress + "/api/Log/Create";
- logTable.ClientId = InternetInfo.ClientId.ToString();
- logTable.devicename = device.devicename;
- string redata = HttpRequestHelper.HttpPostRequest(url,Tools.JsonConvertTools(logTable));
- if (!string.IsNullOrEmpty(redata))
- {
- JsonMsg<LogTable> msg = Tools.JsonToObjectTools<JsonMsg<LogTable>>(redata);
- id = msg?.obj.IdStr;
- }
- }
- }
- catch (Exception ex)
- {
- MessageLog.GetInstance.Show(ex.Message);
- }
- return id;
- }
- #endregion
-
- #region 外部关闭或初始化IOT调用
- /// <summary>
- /// 初始化IOT连接
- /// </summary>
- public bool Initialize()
- {
- if (!string.IsNullOrEmpty(InternetInfo.IotApiAddress)) IOTDevServer.GetInstance().SetUrl(InternetInfo.IotApiAddress);
- 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
- }
-
- public class IOTNode<T>
- {
- public T data { get; set; }
- }
-
- /// <summary>
- /// 日志上报
- /// </summary>
- public class LogIOT
- {
- public string SZXX { get; set; }
- }
- /// <summary>
- /// 告警上报
- /// </summary>
- public class AlarmIOT
- {
- public string GJXX { get; set; }
- }
- }
|