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 变量 /// /// 查询出当前设备信息 /// public DeviceTable device; /// /// 大屏上报Model /// public IOTDevSXModel iOTDevSXModel = new IOTDevSXModel() { }; /// /// 属性状态 /// public string NodeStatus = "{\"data\":[{\"Id\":0,\"VarName\":\"InitComplete\",\"PLCAddress\":\"M100.0\",\"Notes\":\"初始化完成\",\"ModbusTcpAddress\":\"1120\",\"CurrentValue\":\"False\"},{\"Id\":1,\"VarName\":\"TurntableInPlace\",\"PLCAddress\":\"M100.1\",\"Notes\":\"转台到位\",\"ModbusTcpAddress\":\"1121\",\"CurrentValue\":\"False\"},{\"Id\":2,\"VarName\":\"RBTakeNoodleComplete\",\"PLCAddress\":\"M100.2\",\"Notes\":\"机器人取面完成\",\"ModbusTcpAddress\":\"1122\",\"CurrentValue\":\"False\"},{\"Id\":3,\"VarName\":\"AxisIdle_1\",\"PLCAddress\":\"M100.3\",\"Notes\":\"轴空闲_1\",\"ModbusTcpAddress\":\"1123\",\"CurrentValue\":\"False\"},{\"Id\":4,\"VarName\":\"AxisIdle_2\",\"PLCAddress\":\"M100.4\",\"Notes\":\"轴空闲_2\",\"ModbusTcpAddress\":\"1124\",\"CurrentValue\":\"False\"},{\"Id\":5,\"VarName\":\"AxisAllowInvertedNoodle_1\",\"PLCAddress\":\"M100.5\",\"Notes\":\"轴允许倒面_1\",\"ModbusTcpAddress\":\"1125\",\"CurrentValue\":\"False\"},{\"Id\":6,\"VarName\":\"AxisAllowInvertedNoodle_2\",\"PLCAddress\":\"M100.6\",\"Notes\":\"轴允许倒面_2\",\"ModbusTcpAddress\":\"1126\",\"CurrentValue\":\"False\"},{\"Id\":7,\"VarName\":\"AxisAllowInvertedSoup_1\",\"PLCAddress\":\"M101.0\",\"Notes\":\"轴允许倒浇头_1\",\"ModbusTcpAddress\":\"1127\",\"CurrentValue\":\"False\"},{\"Id\":8,\"VarName\":\"AxisAllowInvertedSoup_2\",\"PLCAddress\":\"M101.0\",\"Notes\":\"轴允许倒浇头_2\",\"ModbusTcpAddress\":\"1128\",\"CurrentValue\":\"False\"},{\"Id\":9,\"VarName\":\"SoupHeatComplete_1\",\"PLCAddress\":\"M101.1\",\"Notes\":\"浇头加热完成_1\",\"ModbusTcpAddress\":\"1129\",\"CurrentValue\":\"False\"},{\"Id\":10,\"VarName\":\"SoupHeatComplete_2\",\"PLCAddress\":\"M101.2\",\"Notes\":\"浇头加热完成_2\",\"ModbusTcpAddress\":\"1130\",\"CurrentValue\":\"False\"},{\"Id\":11,\"VarName\":\"CookNoodleBasketIdle_1\",\"PLCAddress\":\"M101.3\",\"Notes\":\"煮面篮空闲_1\",\"ModbusTcpAddress\":\"1131\",\"CurrentValue\":\"False\"},{\"Id\":12,\"VarName\":\"CookNoodleBasketIdle_2\",\"PLCAddress\":\"M101.4\",\"Notes\":\"煮面篮空闲_2\",\"ModbusTcpAddress\":\"1132\",\"CurrentValue\":\"False\"},{\"Id\":13,\"VarName\":\"CookNoodleBasketIdle_3\",\"PLCAddress\":\"M101.5\",\"Notes\":\"煮面篮空闲_3\",\"ModbusTcpAddress\":\"1133\",\"CurrentValue\":\"False\"},{\"Id\":14,\"VarName\":\"CookNoodleBasketIdle_4\",\"PLCAddress\":\"M101.6\",\"Notes\":\"煮面篮空闲_4\",\"ModbusTcpAddress\":\"1134\",\"CurrentValue\":\"False\"},{\"Id\":15,\"VarName\":\"CookNoodleBasketIdle_5\",\"PLCAddress\":\"M101.7\",\"Notes\":\"煮面篮空闲_5\",\"ModbusTcpAddress\":\"1135\",\"CurrentValue\":\"False\"},{\"Id\":16,\"VarName\":\"CookNoodleBasketIdle_6\",\"PLCAddress\":\"M101.8\",\"Notes\":\"煮面篮空闲_6\",\"ModbusTcpAddress\":\"1136\",\"CurrentValue\":\"False\"},{\"Id\":17,\"VarName\":\"CookNoodleComplete_1\",\"PLCAddress\":\"M102.1\",\"Notes\":\"煮面完成_1\",\"ModbusTcpAddress\":\"1137\",\"CurrentValue\":\"False\"},{\"Id\":18,\"VarName\":\"CookNoodleComplete_2\",\"PLCAddress\":\"M102.2\",\"Notes\":\"煮面完成_2\",\"ModbusTcpAddress\":\"1138\",\"CurrentValue\":\"False\"},{\"Id\":19,\"VarName\":\"CookNoodleComplete_3\",\"PLCAddress\":\"M102.3\",\"Notes\":\"煮面完成_3\",\"ModbusTcpAddress\":\"1139\",\"CurrentValue\":\"False\"},{\"Id\":20,\"VarName\":\"CookNoodleComplete_4\",\"PLCAddress\":\"M102.4\",\"Notes\":\"煮面完成_4\",\"ModbusTcpAddress\":\"1140\",\"CurrentValue\":\"False\"},{\"Id\":21,\"VarName\":\"CookNoodleComplete_5\",\"PLCAddress\":\"M102.5\",\"Notes\":\"煮面完成_5\",\"ModbusTcpAddress\":\"1141\",\"CurrentValue\":\"False\"},{\"Id\":22,\"VarName\":\"CookNoodleComplete_6\",\"PLCAddress\":\"M102.6\",\"Notes\":\"煮面完成_6\",\"ModbusTcpAddress\":\"1142\",\"CurrentValue\":\"False\"},{\"Id\":23,\"VarName\":\"TakeNoodleRobotIdle\",\"PLCAddress\":\"M102.7\",\"Notes\":\"取面机器人空闲状态\",\"ModbusTcpAddress\":\"1143\",\"CurrentValue\":\"False\"},{\"Id\":24,\"VarName\":\"TakeSoupRobotIdle\",\"PLCAddress\":\"M103.0\",\"Notes\":\"取浇头机器人空闲状态\",\"ModbusTcpAddress\":\"1144\",\"CurrentValue\":\"False\"},{\"Id\":25,\"VarName\":\"BreakMechanismIdle_1\",\"PLCAddress\":\"M103.1\",\"Notes\":\"破口机构空闲_1\",\"ModbusTcpAddress\":\"1145\",\"CurrentValue\":\"False\"},{\"Id\":26,\"VarName\":\"BreakMechanismIdle_2\",\"PLCAddress\":\"M103.2\",\"Notes\":\"破口机构空闲_2\",\"ModbusTcpAddress\":\"1146\",\"CurrentValue\":\"False\"},{\"Id\":27,\"VarName\":\"TakeSoupComplete\",\"PLCAddress\":\"M103.3\",\"Notes\":\"取浇头完成\",\"ModbusTcpAddress\":\"1147\",\"CurrentValue\":\"False\"},{\"Id\":28,\"VarName\":\"PutNoodleTakeMealComplete\",\"PLCAddress\":\"M103.4\",\"Notes\":\"放面至取餐口完成\",\"ModbusTcpAddress\":\"1148\",\"CurrentValue\":\"False\"},{\"Id\":29,\"VarName\":\"Initing\",\"PLCAddress\":\"M103.5\",\"Notes\":\"设备初始化中\",\"ModbusTcpAddress\":\"1149\",\"CurrentValue\":\"False\"},{\"Id\":30,\"VarName\":\"TurntableFeedbackloc\",\"PLCAddress\":\"VW1240\",\"Notes\":\"转台反馈位置\",\"ModbusTcpAddress\":\"720\",\"CurrentValue\":\"0\"},{\"Id\":31,\"VarName\":\"TurntableLowerLimit\",\"PLCAddress\":\"M120.0\",\"Notes\":\"转台下限位\",\"ModbusTcpAddress\":\"1280\",\"CurrentValue\":\"False\"},{\"Id\":32,\"VarName\":\"TurntableUpLimit\",\"PLCAddress\":\"M120.1\",\"Notes\":\"转台上限位\",\"ModbusTcpAddress\":\"1281\",\"CurrentValue\":\"False\"},{\"Id\":33,\"VarName\":\"SoupMaterialShortage_1\",\"PLCAddress\":\"M120.2\",\"Notes\":\"浇头缺料_1\",\"ModbusTcpAddress\":\"1282\",\"CurrentValue\":\"False\"},{\"Id\":34,\"VarName\":\"SoupMaterialShortage_2\",\"PLCAddress\":\"M120.3\",\"Notes\":\"浇头缺料_2\",\"ModbusTcpAddress\":\"1283\",\"CurrentValue\":\"False\"},{\"Id\":35,\"VarName\":\"SoupMaterialShortage_3\",\"PLCAddress\":\"M120.4\",\"Notes\":\"浇头缺料_3\",\"ModbusTcpAddress\":\"1284\",\"CurrentValue\":\"False\"},{\"Id\":36,\"VarName\":\"SoupMaterialShortage_4\",\"PLCAddress\":\"M120.5\",\"Notes\":\"浇头缺料_4\",\"ModbusTcpAddress\":\"1285\",\"CurrentValue\":\"False\"},{\"Id\":37,\"VarName\":\"SoupMaterialShortage_5\",\"PLCAddress\":\"M120.6\",\"Notes\":\"浇头缺料_5\",\"ModbusTcpAddress\":\"1286\",\"CurrentValue\":\"False\"},{\"Id\":38,\"VarName\":\"OutMealDetect_1\",\"PLCAddress\":\"M121.0\",\"Notes\":\"出餐口检测_1\",\"ModbusTcpAddress\":\"1287\",\"CurrentValue\":\"False\"},{\"Id\":39,\"VarName\":\"OutMealDetect_2\",\"PLCAddress\":\"M121.0\",\"Notes\":\"出餐口检测_2\",\"ModbusTcpAddress\":\"1288\",\"CurrentValue\":\"False\"},{\"Id\":40,\"VarName\":\"OutMealDetect_3\",\"PLCAddress\":\"M121.0\",\"Notes\":\"出餐口检测_3\",\"ModbusTcpAddress\":\"1289\",\"CurrentValue\":\"False\"},{\"Id\":41,\"VarName\":\"TemperatureReached\",\"PLCAddress\":\"M121.2\",\"Notes\":\"温度到达\",\"ModbusTcpAddress\":\"1290\",\"CurrentValue\":\"False\"},{\"Id\":42,\"VarName\":\"AllowPutSoup\",\"PLCAddress\":\"\",\"Notes\":\"允许倒浇头\",\"ModbusTcpAddress\":\"\",\"CurrentValue\":\"False\"},{\"Id\":43,\"VarName\":\"AllowTakeSoup\",\"PLCAddress\":\"\",\"Notes\":\"允许取浇头\",\"ModbusTcpAddress\":\"\",\"CurrentValue\":\"False\"},{\"Id\":44,\"VarName\":\"PutNoodleLoc\",\"PLCAddress\":\"\",\"Notes\":\"放面位置\",\"ModbusTcpAddress\":\"\",\"CurrentValue\":\"0\"},{\"Id\":45,\"VarName\":\"AxisIdleIndex\",\"PLCAddress\":\"\",\"Notes\":\"轴空闲位置\",\"ModbusTcpAddress\":\"\",\"CurrentValue\":\"-1\"},{\"Id\":46,\"VarName\":\"AxisIdleLockIndex\",\"PLCAddress\":\"\",\"Notes\":\"轴空闲互锁位置\",\"ModbusTcpAddress\":\"\",\"CurrentValue\":\"0\"},{\"Id\":47,\"VarName\":\"TurntableLoc\",\"PLCAddress\":\"\",\"Notes\":\"转台位置\",\"ModbusTcpAddress\":\"\",\"CurrentValue\":\"0\"},{\"Id\":48,\"VarName\":\"RobotTaskInterlock\",\"PLCAddress\":\"\",\"Notes\":\"机器人任务互锁信号\",\"ModbusTcpAddress\":\"\",\"CurrentValue\":\"False\"},{\"Id\":49,\"VarName\":\"TakeBowlInterlock\",\"PLCAddress\":\"\",\"Notes\":\"取碗互锁信号\",\"ModbusTcpAddress\":\"\",\"CurrentValue\":\"False\"},{\"Id\":50,\"VarName\":\"TakeNoodleInterlock\",\"PLCAddress\":\"\",\"Notes\":\"取面互锁信号\",\"ModbusTcpAddress\":\"\",\"CurrentValue\":\"False\"},{\"Id\":51,\"VarName\":\"OutNoodleing\",\"PLCAddress\":\"\",\"Notes\":\"出面中\",\"ModbusTcpAddress\":\"\",\"CurrentValue\":\"False\"},{\"Id\":52,\"VarName\":\"AllowTakeNoodle\",\"PLCAddress\":\"\",\"Notes\":\"允许取面\",\"ModbusTcpAddress\":\"\",\"CurrentValue\":\"False\"},{\"Id\":53,\"VarName\":\"TurntableInterlock\",\"PLCAddress\":\"\",\"Notes\":\"转台互锁信号\",\"ModbusTcpAddress\":\"\",\"CurrentValue\":\"False\"},{\"Id\":54,\"VarName\":\"CookNodeState_1\",\"PLCAddress\":null,\"Notes\":\"煮面炉互锁_1\",\"ModbusTcpAddress\":null,\"CurrentValue\":\"False\"},{\"Id\":55,\"VarName\":\"CookNodeState_2\",\"PLCAddress\":null,\"Notes\":\"煮面炉互锁_2\",\"ModbusTcpAddress\":null,\"CurrentValue\":\"False\"},{\"Id\":56,\"VarName\":\"CookNodeState_3\",\"PLCAddress\":null,\"Notes\":\"煮面炉互锁_3\",\"ModbusTcpAddress\":null,\"CurrentValue\":\"False\"},{\"Id\":57,\"VarName\":\"CookNodeState_4\",\"PLCAddress\":null,\"Notes\":\"煮面炉互锁_4\",\"ModbusTcpAddress\":null,\"CurrentValue\":\"False\"},{\"Id\":58,\"VarName\":\"CookNodeState_5\",\"PLCAddress\":null,\"Notes\":\"煮面炉互锁_5\",\"ModbusTcpAddress\":null,\"CurrentValue\":\"False\"},{\"Id\":59,\"VarName\":\"CookNodeState_6\",\"PLCAddress\":null,\"Notes\":\"煮面炉互锁_6\",\"ModbusTcpAddress\":null,\"CurrentValue\":\"False\"},{\"Id\":60,\"VarName\":\"AxisIdleLock_1\",\"PLCAddress\":null,\"Notes\":\"轴空闲互锁_1\",\"ModbusTcpAddress\":null,\"CurrentValue\":\"False\"},{\"Id\":61,\"VarName\":\"AxisIdleLock_2\",\"PLCAddress\":null,\"Notes\":\"轴空闲互锁_2\",\"ModbusTcpAddress\":null,\"CurrentValue\":\"False\"},{\"Id\":62,\"VarName\":\"AllowRun\",\"PLCAddress\":\"\",\"Notes\":\"允许运行\",\"ModbusTcpAddress\":\"\",\"CurrentValue\":\"False\"}]}"; #endregion #region IOT上报公共调用 /// /// 上报告警消息 /// 调用示例: SendAlarmMessage(new AlarmTable { AlarmTime = DateTime.Now,AlarmType = "1",AlarmMessage = "煮面机异常",AlarmVla = "煮面机" }); /// /// 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>(new IotModel { @params = new AlarmIOT { GJXX = Tools.JsonConvertTools(alarmTable) } }); IOTDevServer.GetInstance().IOT_Publish(IOTDevServer.PubTopic,json); } } /// /// 上报日志消息 /// 调用示例:SendLogMessage(new LogTable { LogTime = DateTime.Now,LogType = "1",LogMessage = "程序异常",LogVla = "程序" }); /// /// 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>(new IotModel { @params = new LogIOT { SZXX = Tools.JsonConvertTools(logTable) } }); IOTDevServer.GetInstance().IOT_Publish(IOTDevServer.PubTopic,json); } } /// /// 上报节点状态消息 /// 调用示例:SendNodeStatusMessage("json字符串"); /// /// public void SendNodeStatusMessage(string json) { //if (IOTDevServer.client!=null && IOTDevServer.client.IsConnected && device != null) //{ // string jsonstr = Tools.JsonConvertTools>(new IotModel { @params = new NodeStatusIOT { NodeStatus = Tools.JsonConvertTools(json) } }); // IOTDevServer.GetInstance().IOT_Publish(IOTDevServer.PubTopic,jsonstr); //} NodeStatus = json; } /// /// 上报属性状态 /// 调用示例:SendTargetMessage(); /// /// public void SendTargetMessage() { if (IOTDevServer.client != null && IOTDevServer.client.IsConnected && device != null) { #region 私有变量 bool IsAllowRun = false; bool TemperatureReached = false; #endregion string kzsx = Tools.JsonConvertTools(new DevSX{ data = new List { new DevSXBase { SXMC="", SXLX="" } }}); string jbsx = Tools.JsonConvertTools(new DevSX { data = new List { new DevSXBase { SXMC="设备硬件",SXLX="机器人",SXStatus=ModbusTcpHelper.GetInstance.Connected ,SXYCMS=ModbusTcpHelper.GetInstance.Connected?"":DateTime.Now+" 异常,机器人未连接,疑似未连接网络...."}, new DevSXBase { SXMC="设备软件",SXLX="上位机",SXStatus=true } } }); switch (GeneralConfig.DeviceType) { case BPA.Message.Enum.DeviceClientType.MORKD: break; case BPA.Message.Enum.DeviceClientType.MORKS: IsAllowRun = GetTypeValue("AllowRun"); TemperatureReached = GetTypeValue("TemperatureReached"); bool MissingBowl = GetTypeValue("MissingBowl"); bool MissingBowlSignal2 = GetTypeValue("MissingBowlSignal2"); bool IsNoodles = GetTypeValue("IsNoodles");//转台 bool AllowFallNoodle = GetTypeValue("AllowFallNoodle");//是否允许到面 bool[] CookNoodlesComplete = GetTypeValue("CookNoodlesComplete");//煮面完成上升信号 bool isCookNoodles = CookNoodlesComplete.ToList().Find(o => o); //设置扩展属性与状态,连接上机器人后才会处罚 if (IsAllowRun) { kzsx = Tools.JsonConvertTools(new DevSX { data = new List { 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+" 异常,配料机未配完料,疑似碗未到配料机下方或者配料机未工作...."} } }); } break; case BPA.Message.Enum.DeviceClientType.MORKI: break; case BPA.Message.Enum.DeviceClientType.MORKC: break; case BPA.Message.Enum.DeviceClientType.MORKIC: break; default: break; } string jsonstr = Tools.JsonConvertTools>(new IotModel { @params = new TargetIOT { JBSX = jbsx,KZSX = kzsx,NodeStatus= NodeStatus } }); IOTDevServer.GetInstance().IOT_Publish(IOTDevServer.PubTopic,jsonstr); } } #endregion #region 外部关闭或初始化IOT调用 /// /// 初始化IOT连接 /// 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((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; } /// /// 关闭IOT连接 /// public static void Close() { if (IOTDevServer.client != null) IOTDevServer.GetInstance().Disconnect(); } #endregion #region 云端订阅主题消息接收 /// /// 接收云端消息 /// /// /// private void DevIOTActionHandler(string topic, string message) { if (IOTDevServer.BroadcastTopic == topic && !string.IsNullOrEmpty(message))//广播主题消息,将广播消息发送到相应客户端 { IOTCommandModel iOTCommand = Tools.JsonToObjectTools(message); if (iOTCommand.deviceName == device.devicename) ActionManage.GetInstance.Send("IotBroadcast", iOTCommand); } } #endregion #region 私有函数 /// /// 获取属性状态 /// private T GetTypeValue(string str) { return (T)SimpleFactory.GetInstance.GVL?.GetType().GetProperty(str)?.GetValue(SimpleFactory.GetInstance.GVL,null); } #endregion } /// /// 日志上报 /// public class LogIOT { public string SZXX { get; set; } } /// /// 告警上报 /// public class AlarmIOT { public string GJXX { get; set; } } /// /// 节点状态上报 /// public class NodeStatusIOT { public string NodeStatus { get; set; } } /// /// 基本属性与扩展属性 /// public class TargetIOT { /// /// 基本属性 /// public string JBSX { get; set; } /// /// 扩展属性 /// public string KZSX { get; set; } /// /// 属性状态 /// public string NodeStatus { get; set; } } }