private async Task <Device> FoundDevice(string clientid)
        {
            Device device  = null;
            var    clients = await _serverEx.GetClientsAsync();

            var client = clients.FirstOrDefault(c => c.Id == clientid);

            if (client != null)
            {
                device = client.Session.Items[nameof(Device)] as Device;
                if (device == null)
                {
                    if (clientid != _mcsetting.MqttBroker)
                    {
                        _logger.LogWarning($"未能找到客户端{clientid  }回话附加的设备信息,现在断开此链接。 ");
                        await client.DisconnectAsync();
                    }
                }
            }
            else
            {
                _logger.LogWarning($"未能找到客户端{clientid  }上下文信息");
            }
            return(device);
        }
Exemple #2
0
        public static async Task   PublishAsync(this MqttServer mqtt, string SenderClientId, MqttApplicationMessage message)
        {
            var clients = await mqtt.GetClientsAsync();

            var client = clients.FirstOrDefault(c => c.Id == SenderClientId);
            await client.Session.EnqueueApplicationMessageAsync(message);
        }
Exemple #3
0
 public Task Execute(IJobExecutionContext context)
 {
     return(Task.Run(async() =>
     {
         //如果中断在mqtt服务器列表中, 则取得最后一次收到消息的时间戳,
         using (var scope = _scopeFactor.CreateScope())
             using (var _dbContext = scope.ServiceProvider.GetRequiredService <ApplicationDbContext>())
             {
                 var clientstatus = await _serverEx.GetClientsAsync();
                 clientstatus.ToList().ForEach(cs =>
                 {
                     try
                     {
                         var _device = cs.Session.Items[nameof(Device)] as Device;
                         if (_device != null)
                         {
                             var d = _dbContext.Device.FirstOrDefault(d => d.Id == _device.Id);
                             if (d != null)
                             {
                                 if (!d.Online && DateTime.Now.Subtract(d.LastActive).TotalSeconds > d.Timeout)
                                 {
                                     Task.Run(cs.DisconnectAsync);
                                 }
                             }
                         }
                     }
                     catch (Exception ex)
                     {
                         _logger.LogInformation($"检查设备{cs.Id}-{cs.Endpoint}) 时遇到异常{ex.Message}{ex.InnerException?.Message}  发送消息:{cs.SentApplicationMessagesCount}({cs.BytesSent}kb)  收到{cs.ReceivedApplicationMessagesCount}({cs.BytesReceived / 1024}KB )  ");
                     }
                 });
                 var tfx = from d in _dbContext.Device where d.Timeout < 1 select d;
                 tfx.ToList().ForEach(d => d.Timeout = 300);
                 //当前时间减去最后活跃时间如果小于超时时间, 则为在线, 否则就是离线
                 _dbContext.Device.ToList().ForEach(d =>
                 {
                     if (d.Online && DateTime.Now.Subtract(d.LastActive).TotalSeconds > d.Timeout)
                     {
                         d.Online = false;
                     }
                 });
                 var saveresult = await _dbContext.SaveChangesAsync();
                 _logger.LogInformation($"设备检查程序已经处理{saveresult}调数据");
             }
     }));
 }