using System.Text; using System.Threading.Tasks; using System; using MQTTnet.Client; using Microsoft.Extensions.DependencyInjection; using MQTTnet; using MQTTnet.Protocol; using Microsoft.Extensions.Caching.Memory; using Newtonsoft.Json; using Newtonsoft.Json.Linq; using System.Collections.Generic; using System.Drawing; using Microsoft.AspNetCore.Mvc; using System.Linq; using Microsoft.Extensions.Logging; using System.Collections; using Swashbuckle.AspNetCore.SwaggerGen; //using static Ropin.IOT.MqttService.Controllers.ValueController; using Microsoft.AspNetCore.DataProtection.KeyManagement; using static Ropin.IOT.MqttService.MqttClientService; using System.Collections.Concurrent; using log4net; //using MQTTnet.Server; namespace Ropin.IOT.MqttService { public static class MqttClientService { public static IMqttClient _mqttClient; public static IMemoryCache _memoryCache; public static MqttOptions _mqttOptions; public static List _mqttDevicePointList; public static ConcurrentDictionary _mqttDevicePointDic; public static ConcurrentDictionary _mqttDevicePointNameMappingDic; private static readonly ILog log = LogManager.GetLogger(typeof(MqttClientService)); public static void AddMqttClient(this IServiceCollection services, MqttOptions mqttOptions) { _mqttDevicePointList = new List(); _mqttDevicePointList.Add(new MqttDevicePoint { Id = "08d9ce00-8c61-4abb-8a8b-26e2fbf62e76", ChineseName = "模式", BoxId = "02700123060200004510", EnglishName = "MV", GroupName = "分组1" }); _mqttDevicePointDic = new ConcurrentDictionary(); _mqttDevicePointNameMappingDic = new ConcurrentDictionary(); _mqttOptions = mqttOptions ?? new MqttOptions(); var optionsBuilder = new MqttClientOptionsBuilder() .WithTcpServer(mqttOptions.Ip, mqttOptions.Port) // 要访问的mqtt服务端的 ip 和 端口号 .WithCredentials(mqttOptions.UserName, mqttOptions.Password) // 要访问的mqtt服务端的用户名和密码 .WithClientId(Guid.NewGuid().ToString()) // 设置客户端id .WithCleanSession() .WithTls(new MqttClientOptionsBuilderTlsParameters { UseTls = false // 是否使用 tls加密 }); var clientOptions = optionsBuilder.Build(); _mqttClient = new MqttFactory().CreateMqttClient(); _mqttClient.ConnectedAsync += _mqttClient_ConnectedAsync; // 客户端连接成功事件 _mqttClient.DisconnectedAsync += _mqttClient_DisconnectedAsync; // 客户端连接关闭事件 _mqttClient.ApplicationMessageReceivedAsync += MqttClient_ApplicationMessageReceivedAsync; // 收到消息事件 _mqttClient.ConnectAsync(clientOptions); } public static void AddMqttClient1(MqttOptions mqttOptions,List DevicePointList) { log.Info(" AddMqttClient1 "); //_mqttDevicePointList = new List(); _mqttDevicePointList = DevicePointList; _mqttDevicePointDic = new ConcurrentDictionary(); _mqttDevicePointNameMappingDic = new ConcurrentDictionary(); _mqttOptions = mqttOptions ?? new MqttOptions(); var optionsBuilder = new MqttClientOptionsBuilder() .WithTcpServer(mqttOptions.Ip, mqttOptions.Port) // 要访问的mqtt服务端的 ip 和 端口号 .WithCredentials(mqttOptions.UserName, mqttOptions.Password) // 要访问的mqtt服务端的用户名和密码 .WithClientId(Guid.NewGuid().ToString()) // 设置客户端id .WithCleanSession() .WithTls(new MqttClientOptionsBuilderTlsParameters { UseTls = false // 是否使用 tls加密 }); var clientOptions = optionsBuilder.Build(); _mqttClient = new MqttFactory().CreateMqttClient(); _mqttClient.ConnectedAsync += _mqttClient_ConnectedAsync; // 客户端连接成功事件 _mqttClient.DisconnectedAsync += _mqttClient_DisconnectedAsync; // 客户端连接关闭事件 _mqttClient.ApplicationMessageReceivedAsync += MqttClient_ApplicationMessageReceivedAsync; // 收到消息事件 _mqttClient.ConnectAsync(clientOptions); } /// /// 客户端连接关闭事件 /// /// /// private static Task _mqttClient_DisconnectedAsync(MqttClientDisconnectedEventArgs arg) { Console.WriteLine($"客户端已断开与服务端的连接……"); _mqttClient.ReconnectAsync(); return Task.CompletedTask; } /// /// 客户端连接成功事件 /// /// /// private static Task _mqttClient_ConnectedAsync(MqttClientConnectedEventArgs arg) { Console.WriteLine($"客户端已连接服务端……"); #region 注释数据 ////{"data":{"wendu":"wendu","shidu":"shidu","saosheng":"saosheng","PM2.5":"PM2.5","PM10":"PM10"},"boxid":"02700123060200004509"} ////从表读取 ////{"data":{"worknormal":"node0101","workalarm":"1","powernormal":"0","handalarm":"0"},"boxid":"02700122091600005142"} //_mqttDevicePointList.Add(new MqttDevicePoint { Id = "08d9ce00-8c61-4abb-8a8b-26e2fbf62e66", ChineseName = "工作正常", BoxId = "02700122091600005142", EnglishName = "worknormal", GroupName = "分组1" }); //_mqttDevicePointList.Add(new MqttDevicePoint { Id = "08d9ce00-8c61-4abb-8a8b-26e2fbf62e67", ChineseName = "报警", BoxId = "02700122091600005142", EnglishName = "workalarm", GroupName = "分组1" }); //_mqttDevicePointList.Add(new MqttDevicePoint { Id = "08d9ce00-8c61-4abb-8a8b-26e2fbf62e68", ChineseName = "电源正常", BoxId = "02700122091600005142", EnglishName = "powernormal", GroupName = "分组1" }); //_mqttDevicePointList.Add(new MqttDevicePoint { Id = "08d9ce00-8c61-4abb-8a8b-26e2fbf62e69", ChineseName = "手工报警", BoxId = "02700122091600005142", EnglishName = "handalarm", GroupName = "分组1" }); //_mqttDevicePointList.Add(new MqttDevicePoint { Id = "08d9ce00-8c61-4abb-8a8b-26e2fbf62e70", ChineseName = "温度", BoxId = "02700123060200004509", EnglishName = "wendu", GroupName = "分组1" }); //_mqttDevicePointList.Add(new MqttDevicePoint { Id = "08d9ce00-8c61-4abb-8a8b-26e2fbf62e71", ChineseName = "湿度", BoxId = "02700123060200004509", EnglishName = "shidu", GroupName = "分组1" }); //_mqttDevicePointList.Add(new MqttDevicePoint { Id = "08d9ce00-8c61-4abb-8a8b-26e2fbf62e72", ChineseName = "噪声", BoxId = "02700123060200004509", EnglishName = "zaosheng", GroupName = "分组1" }); //_mqttDevicePointList.Add(new MqttDevicePoint { Id = "08d9ce00-8c61-4abb-8a8b-26e2fbf62e73", ChineseName = "PM2.5", BoxId = "02700123060200004509", EnglishName = "PM2.5", GroupName = "分组1" }); //_mqttDevicePointList.Add(new MqttDevicePoint { Id = "08d9ce00-8c61-4abb-8a8b-26e2fbf62e74", ChineseName = "PM10", BoxId = "02700123060200004509", EnglishName = "PM10", GroupName = "分组1" }); //_mqttDevicePointList.Add(new MqttDevicePoint { Id = "08d9ce00-8c61-4abb-8a8b-26e2fbf62e75", ChineseName = "TVOC", BoxId = "02700123060200004509", EnglishName = "TVoC", GroupName = "分组1" }); //_mqttDevicePointList.Add(new MqttDevicePoint { Id = "08d9ce00-8c61-4abb-8a8b-26e2fbf62e76", ChineseName = "模式", BoxId = "02700123060200004510", EnglishName = "MV", GroupName = "分组1" }); //_mqttDevicePointList.Add(new MqttDevicePoint { Id = "08d9ce00-8c61-4abb-8a8b-26e2fbf62e77", ChineseName = "干燥值", BoxId = "02700123060200004510", EnglishName = "DV", GroupName = "分组1" }); //_mqttDevicePointList.Add(new MqttDevicePoint { Id = "08d9ce00-8c61-4abb-8a8b-26e2fbf62e78", ChineseName = "开泵时间", BoxId = "02700123060200004510", EnglishName = "PTV", GroupName = "分组1" }); //_mqttDevicePointList.Add(new MqttDevicePoint { Id = "08d9ce00-8c61-4abb-8a8b-26e2fbf62e79", ChineseName = "等待时间", BoxId = "02700123060200004510", EnglishName = "WTV", GroupName = "分组1" }); //_mqttDevicePointList.Add(new MqttDevicePoint { Id = "08d9ce00-8c61-4abb-8a8b-26e2fbf62e80", ChineseName = "等待时间倍数", BoxId = "02700123060200004510", EnglishName = "WTM", GroupName = "分组1" }); //_mqttDevicePointList.Add(new MqttDevicePoint { Id = "08d9ce00-8c61-4abb-8a8b-26e2fbf62e81", ChineseName = "模拟量", BoxId = "02700123060200004510", EnglishName = "AO", GroupName = "分组1" }); //_mqttDevicePointList.Add(new MqttDevicePoint { Id = "08d9ce00-8c61-4abb-8a8b-26e2fbf62e82", ChineseName = "数据量", BoxId = "02700123060200004510", EnglishName = "DO", GroupName = "分组1" }); ////{"data":{"wendu":"wendu","yacha":"yahca","DI1":"DI1","DI2":"DI2","DO1":"DO1","DO2":"DO2"},"boxid":"02700123110100000884"} //_mqttDevicePointList.Add(new MqttDevicePoint { Id = "0884ce00-8c61-4abb-8a8b-26e2fbf62e83", ChineseName = "炭箱吸附运行", BoxId = "02700123110100000884", EnglishName = "xifurun", GroupName = "分组1" }); //_mqttDevicePointList.Add(new MqttDevicePoint { Id = "08d9ce00-8c61-4abb-8a8b-26e2fbf62e83", ChineseName = "温度", BoxId = "02700123110100000884", EnglishName = "wendu", GroupName = "分组1" }); //_mqttDevicePointList.Add(new MqttDevicePoint { Id = "08d9ce00-8c61-4abb-8a8b-26e2fbf62e84", ChineseName = "压差", BoxId = "02700123110100000884", EnglishName = "yacha", GroupName = "分组1" }); //_mqttDevicePointList.Add(new MqttDevicePoint { Id = "08d9ce00-8c61-4abb-8a8b-26e2fbf62e85", ChineseName = "启动", BoxId = "02700123110100000884", EnglishName = "DI1", GroupName = "分组1" }); //_mqttDevicePointList.Add(new MqttDevicePoint { Id = "08d9ce00-8c61-4abb-8a8b-26e2fbf62e86", ChineseName = "报警", BoxId = "02700123110100000884", EnglishName = "DI2", GroupName = "分组1" }); //_mqttDevicePointList.Add(new MqttDevicePoint { Id = "08d9ce00-8c61-4abb-8a8b-26e2fbf62e87", ChineseName = "电源正常", BoxId = "02700123110100000884", EnglishName = "DO1", GroupName = "分组1" }); //_mqttDevicePointList.Add(new MqttDevicePoint { Id = "08d9ce00-8c61-4abb-8a8b-26e2fbf62e88", ChineseName = "温压监测报警", BoxId = "02700123110100000884", EnglishName = "DO2", GroupName = "分组1" }); //_mqttDevicePointList.Add(new MqttDevicePoint { Id = "08d9ce00-8c61-4abb-8a8b-26e2fbf62e89", ChineseName = "废气进口浓度", BoxId = "02700123110100000884", EnglishName = "AI1", GroupName = "分组1" }); //_mqttDevicePointList.Add(new MqttDevicePoint { Id = "08d9ce00-8c61-4abb-8a8b-26e2fbf62e90", ChineseName = "废气出口浓度", BoxId = "02700123110100000884", EnglishName = "AI2", GroupName = "分组1" }); //_mqttDevicePointList.Add(new MqttDevicePoint { Id = "08d9ce00-8c61-4abb-8a8b-26e2fbf62e91", ChineseName = "一号温度", BoxId = "02700124041100008079", EnglishName = "wendu1", GroupName = "分组1" }); //_mqttDevicePointList.Add(new MqttDevicePoint { Id = "08d9ce00-8c61-4abb-8a8b-26e2fbf62e92", ChineseName = "一号压差", BoxId = "02700124041100008079", EnglishName = "yacha1", GroupName = "分组1" }); //_mqttDevicePointList.Add(new MqttDevicePoint { Id = "08d9ce00-8c61-4abb-8a8b-26e2fbf62e93", ChineseName = "设备运行", BoxId = "02700124041100008079", EnglishName = "run", GroupName = "分组1" }); //_mqttDevicePointList.Add(new MqttDevicePoint { Id = "08d9ce00-8c61-4abb-8a8b-26e2fbf62e94", ChineseName = "设备报警", BoxId = "02700124041100008079", EnglishName = "alarm", GroupName = "分组1" }); //_mqttDevicePointList.Add(new MqttDevicePoint { Id = "8079ce00-8c61-4abb-8a8b-26e2fbf62e94", ChineseName = "炭箱吸附运行", BoxId = "02700124041100008079", EnglishName = "htrun", GroupName = "分组1" }); //_mqttDevicePointList.Add(new MqttDevicePoint { Id = "08d9ce00-8c61-4abb-8a8b-26e2fbf62e95", ChineseName = "一号温度", BoxId = "02700124041100007950", EnglishName = "wendu1", GroupName = "分组1" }); //_mqttDevicePointList.Add(new MqttDevicePoint { Id = "08d9ce00-8c61-4abb-8a8b-26e2fbf62e96", ChineseName = "一号压差", BoxId = "02700124041100007950", EnglishName = "yacha1", GroupName = "分组1" }); //_mqttDevicePointList.Add(new MqttDevicePoint { Id = "08d9ce00-8c61-4abb-8a8b-26e2fbf62e97", ChineseName = "设备运行", BoxId = "02700124041100007950", EnglishName = "run", GroupName = "分组1" }); //_mqttDevicePointList.Add(new MqttDevicePoint { Id = "08d9ce00-8c61-4abb-8a8b-26e2fbf62e98", ChineseName = "设备报警", BoxId = "02700124041100007950", EnglishName = "alarm", GroupName = "分组1" }); //_mqttDevicePointList.Add(new MqttDevicePoint { Id = "7950ce00-8c61-4abb-8a8b-26e2fbf62e98", ChineseName = "除尘器运行", BoxId = "02700124041100007950", EnglishName = "qchrun", GroupName = "分组1" }); //_mqttDevicePointList.Add(new MqttDevicePoint { Id = "7764ce00-8c61-4abb-8a8b-26e2fbf62e99", ChineseName = "废气设备运行", BoxId = "02700124041100007764", EnglishName = "feiqirun", GroupName = "分组1" }); //_mqttDevicePointList.Add(new MqttDevicePoint { Id = "08d9ce00-8c61-4abb-8a8b-26e2fbf62e99", ChineseName = "一号温度", BoxId = "02700124041100007764", EnglishName = "wendu1", GroupName = "分组1" }); //_mqttDevicePointList.Add(new MqttDevicePoint { Id = "08d9ce00-8c61-4abb-8a8b-26e2fbf62100", ChineseName = "一号压差", BoxId = "02700124041100007764", EnglishName = "yacha1", GroupName = "分组1" }); //_mqttDevicePointList.Add(new MqttDevicePoint { Id = "08d9ce00-8c61-4abb-8a8b-26e2fbf62101", ChineseName = "设备运行", BoxId = "02700124041100007764", EnglishName = "run", GroupName = "分组1" }); //_mqttDevicePointList.Add(new MqttDevicePoint { Id = "08d9ce00-8c61-4abb-8a8b-26e2fbf62102", ChineseName = "设备报警", BoxId = "02700124041100007764", EnglishName = "alarm", GroupName = "分组1" }); //_mqttDevicePointList.Add(new MqttDevicePoint { Id = "08d9ce00-8c61-4abb-8a8b-26e2fbf62103", ChineseName = "一号温度", BoxId = "02700124041100006737", EnglishName = "wendu1", GroupName = "分组1" }); //_mqttDevicePointList.Add(new MqttDevicePoint { Id = "08d9ce00-8c61-4abb-8a8b-26e2fbf62104", ChineseName = "一号压差", BoxId = "02700124041100006737", EnglishName = "yacha1", GroupName = "分组1" }); //_mqttDevicePointList.Add(new MqttDevicePoint { Id = "08d9ce00-8c61-4abb-8a8b-26e2fbf62105", ChineseName = "设备运行", BoxId = "02700124041100006737", EnglishName = "run", GroupName = "分组1" }); //_mqttDevicePointList.Add(new MqttDevicePoint { Id = "08d9ce00-8c61-4abb-8a8b-26e2fbf62106", ChineseName = "设备报警", BoxId = "02700124041100006737", EnglishName = "alarm", GroupName = "分组1" }); //_mqttDevicePointList.Add(new MqttDevicePoint { Id = "08d9ce00-8c61-4abb-8a8b-26e2fbf62107", ChineseName = "一号温度", BoxId = "02700124041100008529", EnglishName = "wendu1", GroupName = "分组1" }); //_mqttDevicePointList.Add(new MqttDevicePoint { Id = "08d9ce00-8c61-4abb-8a8b-26e2fbf62108", ChineseName = "一号压差", BoxId = "02700124041100008529", EnglishName = "yacha1", GroupName = "分组1" }); //_mqttDevicePointList.Add(new MqttDevicePoint { Id = "08d9ce00-8c61-4abb-8a8b-26e2fbf62109", ChineseName = "设备运行", BoxId = "02700124041100008529", EnglishName = "run", GroupName = "分组1" }); //_mqttDevicePointList.Add(new MqttDevicePoint { Id = "08d9ce00-8c61-4abb-8a8b-26e2fbf62110", ChineseName = "设备报警", BoxId = "02700124041100008529", EnglishName = "alarm", GroupName = "分组1" }); //_mqttDevicePointList.Add(new MqttDevicePoint { Id = "08d9ce00-8c61-4abb-8a8b-26e2fbf62111", ChineseName = "一号压差", BoxId = "02700124041100007872", EnglishName = "yacha1", GroupName = "分组1" }); //_mqttDevicePointList.Add(new MqttDevicePoint { Id = "08d9ce00-8c61-4abb-8a8b-26e2fbf62112", ChineseName = "二号压差", BoxId = "02700124041100007872", EnglishName = "yacha2", GroupName = "分组1" }); //_mqttDevicePointList.Add(new MqttDevicePoint { Id = "08d9ce00-8c61-4abb-8a8b-26e2fbf62113", ChineseName = "设备运行", BoxId = "02700124041100007872", EnglishName = "run", GroupName = "分组1" }); //_mqttDevicePointList.Add(new MqttDevicePoint { Id = "08d9ce00-8c61-4abb-8a8b-26e2fbf62114", ChineseName = "设备报警", BoxId = "02700124041100007872", EnglishName = "alarm", GroupName = "分组1" }); //_mqttDevicePointList.Add(new MqttDevicePoint { Id = "08d9ce00-8c61-4abb-8a8b-26e2fbf62115", ChineseName = "一号压差", BoxId = "02700124041100007544", EnglishName = "yacha1", GroupName = "分组1" }); ////_mqttDevicePointList.Add(new MqttDevicePoint { Id = "08d9ce00-8c61-4abb-8a8b-26e2fbf62116", ChineseName = "二号压差", BoxId = "02700124041100007544", EnglishName = "yacha2", GroupName = "分组1" }); //_mqttDevicePointList.Add(new MqttDevicePoint { Id = "08d9ce00-8c61-4abb-8a8b-26e2fbf62117", ChineseName = "设备运行", BoxId = "02700124041100007544", EnglishName = "run", GroupName = "分组1" }); //_mqttDevicePointList.Add(new MqttDevicePoint { Id = "08d9ce00-8c61-4abb-8a8b-26e2fbf62118", ChineseName = "设备报警", BoxId = "02700124041100007544", EnglishName = "alarm", GroupName = "分组1" }); //_mqttDevicePointList.Add(new MqttDevicePoint { Id = "08d9ce00-8c61-4abb-8a8b-26e2fbf62119", ChineseName = "一号压差", BoxId = "02700124041100007378", EnglishName = "yacha1", GroupName = "分组1" }); ////_mqttDevicePointList.Add(new MqttDevicePoint { Id = "08d9ce00-8c61-4abb-8a8b-26e2fbf62120", ChineseName = "二号压差", BoxId = "02700124041100007378", EnglishName = "yacha2", GroupName = "分组1" }); //_mqttDevicePointList.Add(new MqttDevicePoint { Id = "08d9ce00-8c61-4abb-8a8b-26e2fbf62121", ChineseName = "设备运行", BoxId = "02700124041100007378", EnglishName = "run", GroupName = "分组1" }); //_mqttDevicePointList.Add(new MqttDevicePoint { Id = "08d9ce00-8c61-4abb-8a8b-26e2fbf62122", ChineseName = "设备报警", BoxId = "02700124041100007378", EnglishName = "alarm", GroupName = "分组1" }); //_mqttDevicePointList.Add(new MqttDevicePoint { Id = "08d9ce00-8c61-4abb-8a8c-26e2fbf62119", ChineseName = "一号压差", BoxId = "02700124041100007099", EnglishName = "yacha1", GroupName = "分组1" }); //_mqttDevicePointList.Add(new MqttDevicePoint { Id = "08d9ce00-8c61-4abb-8a8c-26e2fbf62120", ChineseName = "二号压差", BoxId = "02700124041100007099", EnglishName = "yacha2", GroupName = "分组1" }); //_mqttDevicePointList.Add(new MqttDevicePoint { Id = "08d9ce00-8c61-4abb-8a8c-26e2fbf62121", ChineseName = "三号压差", BoxId = "02700124041100007099", EnglishName = "yacha3", GroupName = "分组1" }); //_mqttDevicePointList.Add(new MqttDevicePoint { Id = "08d9ce00-8c61-4abb-8a8c-26e2fbf62122", ChineseName = "设备运行", BoxId = "02700124041100007099", EnglishName = "run", GroupName = "分组1" }); //_mqttDevicePointList.Add(new MqttDevicePoint { Id = "08d9ce00-8c61-4abb-8a8c-26e2fbf62123", ChineseName = "设备报警", BoxId = "02700124041100007099", EnglishName = "alarm", GroupName = "分组1" }); ////_mqttDevicePointList.Add(new MqttDevicePoint { Id = "08d9ce00-8c61-4abb-8a8b-26e2fbf62119", ChineseName = "一号压差", BoxId = "02700124041100007099", EnglishName = "yacha1", GroupName = "分组1" }); ////_mqttDevicePointList.Add(new MqttDevicePoint { Id = "08d9ce00-8c61-4abb-8a8b-26e2fbf62120", ChineseName = "二号压差", BoxId = "02700124041100007099", EnglishName = "yacha2", GroupName = "分组1" }); ////_mqttDevicePointList.Add(new MqttDevicePoint { Id = "08d9ce00-8c61-4abb-8a8b-26e2fbf62121", ChineseName = "三号压差", BoxId = "02700124041100007099", EnglishName = "yacha3", GroupName = "分组1" }); ////_mqttDevicePointList.Add(new MqttDevicePoint { Id = "08d9ce00-8c61-4abb-8a8b-26e2fbf62122", ChineseName = "设备运行", BoxId = "02700124041100007099", EnglishName = "run", GroupName = "分组1" }); ////_mqttDevicePointList.Add(new MqttDevicePoint { Id = "08d9ce00-8c61-4abb-8a8b-26e2fbf62123", ChineseName = "设备报警", BoxId = "02700124041100007099", EnglishName = "alarm", GroupName = "分组1" }); //_mqttDevicePointList.Add(new MqttDevicePoint { Id = "08d9ce00-8c61-4abb-8a8b-26e2fbf62124", ChineseName = "一号压差", BoxId = "02700124041100007266", EnglishName = "yacha1", GroupName = "分组1" }); //_mqttDevicePointList.Add(new MqttDevicePoint { Id = "08d9ce00-8c61-4abb-8a8b-26e2fbf62125", ChineseName = "二号压差", BoxId = "02700124041100007266", EnglishName = "yacha2", GroupName = "分组1" }); //_mqttDevicePointList.Add(new MqttDevicePoint { Id = "08d9ce00-8c61-4abb-8a8b-26e2fbf62126", ChineseName = "三号压差", BoxId = "02700124041100007266", EnglishName = "yacha3", GroupName = "分组1" }); //_mqttDevicePointList.Add(new MqttDevicePoint { Id = "08d9ce00-8c61-4abb-8a8b-26e2fbf62127", ChineseName = "设备运行", BoxId = "02700124041100007266", EnglishName = "run", GroupName = "分组1" }); //_mqttDevicePointList.Add(new MqttDevicePoint { Id = "08d9ce00-8c61-4abb-8a8b-26e2fbf62128", ChineseName = "设备报警", BoxId = "02700124041100007266", EnglishName = "alarm", GroupName = "分组1" }); //_mqttDevicePointList.Add(new MqttDevicePoint { Id = "08d9ce00-8c61-4abb-8a8b-26e2fbf62129", ChineseName = "一号压差", BoxId = "02700124041100007764", EnglishName = "yacha1", GroupName = "分组1" }); //_mqttDevicePointList.Add(new MqttDevicePoint { Id = "08d9ce00-8c61-4abb-8a8b-26e2fbf62130", ChineseName = "二号压差", BoxId = "02700124041100007764", EnglishName = "yacha2", GroupName = "分组1" }); //_mqttDevicePointList.Add(new MqttDevicePoint { Id = "08d9ce00-8c61-4abb-8a8b-26e2fbf62131", ChineseName = "三号压差", BoxId = "02700124041100007764", EnglishName = "yacha3", GroupName = "分组1" }); //_mqttDevicePointList.Add(new MqttDevicePoint { Id = "08d9ce00-8c61-4abb-8a8b-26e2fbf62132", ChineseName = "四号压差", BoxId = "02700124041100007764", EnglishName = "yacha4", GroupName = "分组1" }); //_mqttDevicePointList.Add(new MqttDevicePoint { Id = "08d9ce00-8c61-4abb-8a8b-26e2fbf62133", ChineseName = "五号压差", BoxId = "02700124041100007764", EnglishName = "yacha5", GroupName = "分组1" }); //_mqttDevicePointList.Add(new MqttDevicePoint { Id = "08d9ce00-8c61-4abb-8a8b-26e2fbf62134", ChineseName = "设备运行", BoxId = "02700124041100007764", EnglishName = "run", GroupName = "分组1" }); //_mqttDevicePointList.Add(new MqttDevicePoint { Id = "08d9ce00-8c61-4abb-8a8b-26e2fbf62135", ChineseName = "设备报警", BoxId = "02700124041100007764", EnglishName = "alarm", GroupName = "分组1" }); //_mqttDevicePointList.Add(new MqttDevicePoint { Id = "08d9ce00-8c61-4abb-8a8b-26e2fbf62136", ChineseName = "一号压差", BoxId = "02700124041100008780", EnglishName = "yacha1", GroupName = "分组1" }); //_mqttDevicePointList.Add(new MqttDevicePoint { Id = "08d9ce00-8c61-4abb-8a8b-26e2fbf62137", ChineseName = "二号压差", BoxId = "02700124041100008780", EnglishName = "yacha2", GroupName = "分组1" }); //_mqttDevicePointList.Add(new MqttDevicePoint { Id = "08d9ce00-8c61-4abb-8a8b-26e2fbf62138", ChineseName = "三号压差", BoxId = "02700124041100008780", EnglishName = "yacha3", GroupName = "分组1" }); //_mqttDevicePointList.Add(new MqttDevicePoint { Id = "08d9ce00-8c61-4abb-8a8b-26e2fbf62139", ChineseName = "四号压差", BoxId = "02700124041100008780", EnglishName = "yacha4", GroupName = "分组1" }); //_mqttDevicePointList.Add(new MqttDevicePoint { Id = "7617ce00-8c61-4abb-8a8b-26e2fbf62140", ChineseName = "废气设备运行", BoxId = "02700124041100007617", EnglishName = "feiqirun", GroupName = "分组1" }); //_mqttDevicePointList.Add(new MqttDevicePoint { Id = "08d9ce00-8c61-4abb-8a8b-26e2fbf62140", ChineseName = "一号压差", BoxId = "02700124041100007617", EnglishName = "yacha1", GroupName = "分组1" }); //_mqttDevicePointList.Add(new MqttDevicePoint { Id = "08d9ce00-8c61-4abb-8a8b-26e2fbf62141", ChineseName = "二号压差", BoxId = "02700124041100007617", EnglishName = "yacha2", GroupName = "分组1" }); //_mqttDevicePointList.Add(new MqttDevicePoint { Id = "08d9ce00-8c61-4abb-8a8b-26e2fbf62142", ChineseName = "一号温度", BoxId = "02700124041100007617", EnglishName = "wendu1", GroupName = "分组1" }); //_mqttDevicePointList.Add(new MqttDevicePoint { Id = "08d9ce00-8c61-4abb-8a8b-26e2fbf62143", ChineseName = "二号温度", BoxId = "02700124041100007617", EnglishName = "wendu2", GroupName = "分组1" }); //_mqttDevicePointList.Add(new MqttDevicePoint { Id = "08d9ce00-8c61-4abb-8a8b-26e2fbf62144", ChineseName = "设备运行", BoxId = "02700124041100007617", EnglishName = "run", GroupName = "分组1" }); //_mqttDevicePointList.Add(new MqttDevicePoint { Id = "08d9ce00-8c61-4abb-8a8b-26e2fbf62145", ChineseName = "设备报警", BoxId = "02700124041100007617", EnglishName = "alarm", GroupName = "分组1" }); //_mqttDevicePointList.Add(new MqttDevicePoint { Id = "08d9ce00-8c61-4abb-8a8b-26e2fbf62146", ChineseName = "一号压差", BoxId = "02700124051000014941", EnglishName = "yacha1", GroupName = "分组1" }); //_mqttDevicePointList.Add(new MqttDevicePoint { Id = "08d9ce00-8c61-4abb-8a8b-26e2fbf62147", ChineseName = "一号温度", BoxId = "02700124051000014941", EnglishName = "wendu1", GroupName = "分组1" }); //_mqttDevicePointList.Add(new MqttDevicePoint { Id = "08d9ce00-8c61-4abb-8a8b-26e2fbf62148", ChineseName = "废气设备运行", BoxId = "02700124051000014941", EnglishName = "feiqirun", GroupName = "分组1" }); #endregion foreach (var dp in _mqttDevicePointList) { _mqttDevicePointDic.TryAdd(dp.BoxId + "分组1" + dp.EnglishName, dp); _mqttDevicePointNameMappingDic.TryAdd(dp.BoxId + "分组1" + dp.ChineseName, dp.BoxId + "分组1" + dp.EnglishName); } // 订阅消息主题 // MqttQualityOfServiceLevel: (QoS): 0 最多一次,接收者不确认收到消息,并且消息不被发送者存储和重新发送提供与底层 TCP 协议相同的保证。 // 1: 保证一条消息至少有一次会传递给接收方。发送方存储消息,直到它从接收方收到确认收到消息的数据包。一条消息可以多次发送或传递。 // 2: 保证每条消息仅由预期的收件人接收一次。级别2是最安全和最慢的服务质量级别,保证由发送方和接收方之间的至少两个请求/响应(四次握手)。 _mqttClient.SubscribeAsync(_mqttOptions.Theme, MqttQualityOfServiceLevel.AtLeastOnce); //topic_02 return Task.CompletedTask; } public class MqttDeviceData { public Dictionary data { set; get; } public string boxid { set; get; } } /// /// 收到消息事件 /// /// /// private static Task MqttClient_ApplicationMessageReceivedAsync(MqttApplicationMessageReceivedEventArgs arg) { Console.WriteLine(Encoding.UTF8.GetString(arg?.ApplicationMessage?.PayloadSegment.ToArray())); var payload = Encoding.UTF8.GetString(arg?.ApplicationMessage?.PayloadSegment.ToArray()); MqttDeviceData mqttDeviceData = JsonConvert.DeserializeObject(payload); if (mqttDeviceData != null && mqttDeviceData.data.Count > 0) { foreach (var key in mqttDeviceData.data.Keys) { MqttDevicePoint mqttDevicePoint = _mqttDevicePointDic[mqttDeviceData.boxid + "分组1" + key]; mqttDevicePoint.BoxStatus = "1"; mqttDevicePoint.DataType = "3"; mqttDevicePoint.UpLoadTime = DateTime.Now; mqttDevicePoint.Value = mqttDeviceData.data[key]; _mqttDevicePointDic[mqttDeviceData.boxid + "分组1" + key] = mqttDevicePoint;// mqttDeviceData.data[key]; } } //_memoryCache.Set(json["boxId"].ToString(), payload); //Console.WriteLine(Encoding.UTF8.GetString(arg?.ApplicationMessage?.PayloadSegment.ToArray())); //Console.WriteLine($"ApplicationMessageReceivedAsync:客户端ID=【{arg.ClientId}】接收到消息。 Topic主题=【{arg.ApplicationMessage.Topic}】 消息=【{Encoding.UTF8.GetString(arg.ApplicationMessage.Payload)}】 qos等级=【{arg.ApplicationMessage.QualityOfServiceLevel}】"); return Task.CompletedTask; } } }