Skip to content

Commit

Permalink
定时检查活动状态, 如果超过 Timeout 设置的时间, 则认为非活动状态。
Browse files Browse the repository at this point in the history
  • Loading branch information
maikebing committed Oct 6, 2022
1 parent eafa682 commit 1de6d83
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 22 deletions.
17 changes: 17 additions & 0 deletions IoTSharp.Contracts/Constants.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace IoTSharp.Contracts
{
public static class Constants
{
public const string _Active = "Active";
public const string _LastActivityDateTime = "LastActivityDateTime";
public const string _InactivityAlarmDateTime = "InactivityAlarmDateTime ";
public const string _LastConnectDateTime = "LastConnectDateTime";
public const string _LastDisconnectDateTime = "LastDisconnectDateTime";
}
}
7 changes: 4 additions & 3 deletions IoTSharp.EventBus/EventBusSubscriber.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ namespace IoTSharp.EventBus

public class EventBusSubscriber
{

private readonly ILogger _logger;
private readonly IServiceScopeFactory _scopeFactor;
private readonly IStorage _storage;
Expand Down Expand Up @@ -176,8 +177,8 @@ public async Task Active(Guid devid, ActivityStatus activity)
msg.DataCatalog = DataCatalog.AttributeData;
msg.DataSide = DataSide.ServerSide;
msg.MsgBody = new Dictionary<string, object>();
msg.MsgBody.Add(activity == ActivityStatus.Activity ? "LastActivityDateTime" : "InactivityAlarmDateTime ", DateTime.Now);
msg.MsgBody.Add("Active", activity == ActivityStatus.Activity);
msg.MsgBody.Add(activity == ActivityStatus.Activity ? Constants._LastActivityDateTime : Constants._InactivityAlarmDateTime, DateTime.Now);
msg.MsgBody.Add(Constants._Active, activity == ActivityStatus.Activity);
await StoreAttributeData(msg, activity == ActivityStatus.Activity ? MountType.Activity : MountType.Inactivity);
}

Expand All @@ -188,7 +189,7 @@ public async Task Connect(Guid devid, ConnectStatus devicestatus)
msg.DataCatalog = DataCatalog.AttributeData;
msg.DataSide = DataSide.ServerSide;
msg.MsgBody = new Dictionary<string, object>();
msg.MsgBody.Add(devicestatus == ConnectStatus.Connected ? "LastConnectDateTime" : "LastDisconnectDateTime", DateTime.Now);
msg.MsgBody.Add(devicestatus == ConnectStatus.Connected ? Constants._LastConnectDateTime : Constants._LastDisconnectDateTime, DateTime.Now);
await StoreAttributeData(msg, devicestatus == ConnectStatus.Connected ? MountType.Connected : MountType.Disconnected);
}

Expand Down
39 changes: 20 additions & 19 deletions IoTSharp/Jobs/CheckDevices.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,27 +42,28 @@ public async Task Execute(IJobExecutionContext context)
using (var scope = _scopeFactor.CreateScope())
using (var _dbContext = scope.ServiceProvider.GetRequiredService<ApplicationDbContext>())
{
var clientstatus = await _serverEx.GetClientsAsync();
var onlinedev = from client in clientstatus select client.Session.Items[nameof(Device)] as Device;
//把超时时间小于1的都设置为300秒
var tfx = from d in _dbContext.Device where d.Timeout < 1 select d;
tfx.ToList().ForEach(d => d.Timeout = 300);

//所有在线且活跃时间超时的设备如果不在已连接客户端内, 则认为是离线。
//这里的自身我们认为是 有链接的设备, 而不是无连接的。
try
{
var sf = from d in _dbContext.Device.Include(d=>d.Owner) where d.Owner == null && !d.Deleted select d;
await sf.LoadAsync();
sf.ToList().ForEach(d =>
{
//if (!onlinedev.Any(dev => dev.Id != d.Id) && DateTime.Now.Subtract(d.LastActive).TotalSeconds >d.Timeout )
//{

//}
});
var saveresult = await _dbContext.SaveChangesAsync();
_logger.LogInformation($"设备检查程序已经处理{saveresult}调数据");
var sf = from d in _dbContext.AttributeLatest where (d.KeyName == Constants._Active && d.Value_Boolean == true) select d.DeviceId;
if (sf.Any())
{
var devids = await sf.ToListAsync();
foreach (var id in devids)
{
var dev = await _dbContext.Device.FirstOrDefaultAsync(d=>d.Id== id);
var ladt = from d in _dbContext.AttributeLatest where d.DeviceId == id && d.DataSide == DataSide.ServerSide && d.KeyName == Constants._LastActivityDateTime select d.Value_DateTime;
var __LastActivityDateTime = await ladt.FirstOrDefaultAsync();
if (dev != null && __LastActivityDateTime!=null)
{

if (DateTime.Now.Subtract(__LastActivityDateTime.GetValueOrDefault()).TotalSeconds > dev.Timeout)
{
_logger.LogInformation($"设备{dev.Name}({dev.Id})现在置非活跃状态,上次活跃时间为{__LastActivityDateTime},超时时间{dev.Timeout}秒");
await _queue.PublishActive(id, ActivityStatus.Inactivity);
}
}
}
}
}
catch (Exception ex)
{
Expand Down

0 comments on commit 1de6d83

Please sign in to comment.