Skip to content

Commit

Permalink
清理旧的状态处理。
Browse files Browse the repository at this point in the history
  • Loading branch information
maikebing committed Oct 9, 2022
1 parent c7ab0d6 commit f013701
Show file tree
Hide file tree
Showing 14 changed files with 10 additions and 168 deletions.
12 changes: 1 addition & 11 deletions IoTSharp.Contracts/Enums.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,7 @@ public enum ApiCode : int
Empty = 10019,
}

[System.Text.Json.Serialization.JsonConverter(typeof(System.Text.Json.Serialization.JsonStringEnumConverter))]
[JsonConverter(typeof(StringEnumConverter))]
public enum DeviceStatus
{
Deleted=-1,
UnKnow = 0,
Good = 1,
PartGood = 2,
Bad = 3

}


[System.Text.Json.Serialization.JsonConverter(typeof(System.Text.Json.Serialization.JsonStringEnumConverter))]
[JsonConverter(typeof(StringEnumConverter))]
Expand Down
1 change: 0 additions & 1 deletion IoTSharp.Data/PlayloadData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ namespace IoTSharp.Data
public class PlayloadData
{
public DateTime ts { get; set; } = DateTime.Now;
public DeviceStatus DeviceStatus { get; set; } = DeviceStatus.Good;
public Guid DeviceId { get; set; }
public Dictionary<string, object> MsgBody { get; set; }
public DataSide DataSide { get; set; }
Expand Down
6 changes: 1 addition & 5 deletions IoTSharp.EventBus.CAP/CapPublisher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,7 @@ public async Task PublishTelemetryData( PlayloadData msg)
{
await _queue.PublishAsync("iotsharp.services.datastream.telemetrydata", msg);
}

public async Task PublishDeviceStatus( Guid devid, DeviceStatus devicestatus)
{
await _queue.PublishAsync("iotsharp.services.datastream.devicestatus", new PlayloadData { DeviceId = devid, DeviceStatus = devicestatus });
}


public async Task PublishDeviceAlarm( CreateAlarmDto alarmDto)
{
Expand Down
6 changes: 1 addition & 5 deletions IoTSharp.EventBus.CAP/CapSubscriber.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,7 @@ public async Task alarm(CreateAlarmDto alarmDto)
}


[CapSubscribe("iotsharp.services.datastream.devicestatus")]
public async Task devicestatus(PlayloadData status)
{
await DeviceStatusEvent(status);
}


[CapSubscribe("iotsharp.services.datastream.telemetrydata")]
public async Task telemetrydata(PlayloadData msg)
Expand Down
6 changes: 1 addition & 5 deletions IoTSharp.EventBus.Shashlik/Events.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,7 @@ public class TelemetryDataEvent : ShashlikEvent<PlayloadData>
{

}
public class DeviceStatusEvent : IEvent
{
public Guid DeviceId { get; set; }
public DeviceStatus DeviceStatus { get; set; }
}

public class CreateDeviceEvent : IEvent
{
public Guid DeviceId { get; set; }
Expand Down
5 changes: 1 addition & 4 deletions IoTSharp.EventBus.Shashlik/ShashlikPublisher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,7 @@ public async Task PublishTelemetryData( PlayloadData msg)
await _queue.PublishAsync((TelemetryDataEvent)msg, null);
}

public async Task PublishDeviceStatus( Guid devid, DeviceStatus devicestatus)
{
await _queue.PublishAsync(new DeviceStatusEvent() { DeviceId = devid, DeviceStatus = devicestatus },null);
}


public async Task PublishDeviceAlarm( CreateAlarmDto alarmDto)
{
Expand Down
15 changes: 1 addition & 14 deletions IoTSharp.EventBus.Shashlik/ShashlikSubscriber.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,20 +67,7 @@ public async Task Execute(AlarmEvent @event, IDictionary<string, string> items)
}
}

public class DeviceStatusEventHandler : IEventHandler<DeviceStatusEvent>
{
private readonly ISubscriber _subscriber;

public DeviceStatusEventHandler(ISubscriber subscriber)
{
_subscriber = subscriber;
}
public async Task Execute(DeviceStatusEvent @event, IDictionary<string, string> items)
{
await _subscriber.DeviceStatusEvent(new PlayloadData() { DeviceId = @event.DeviceId, DeviceStatus = @event.DeviceStatus });
}
}


public class CreateDeviceEventHandler : IEventHandler<CreateDeviceEvent>
{
private readonly ISubscriber _subscriber;
Expand Down
10 changes: 0 additions & 10 deletions IoTSharp.EventBus/EventBusPublisher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,6 @@ public static void PublishTelemetryData(this IPublisher _queue, Device device, D
{
_queue.PublishTelemetryData(new PlayloadData() { DeviceId = device.Id, MsgBody = keyValues, DataSide = DataSide.ClientSide, DataCatalog = DataCatalog.TelemetryData });
}
/// <summary>
/// 发布设备状态到属性当中。
/// </summary>
/// <param name="_queue"></param>
/// <param name="device"></param>
/// <param name="status"></param>
public static void PublishStatus(this IPublisher _queue, Device device, DeviceStatus status)
{
_queue.PublishAttributeData(device, msg => msg.MsgBody.Add("Status", status));
}

}
}
39 changes: 0 additions & 39 deletions IoTSharp.EventBus/EventBusSubscriber.cs
Original file line number Diff line number Diff line change
Expand Up @@ -111,45 +111,6 @@ public async Task OccurredAlarm(CreateAlarmDto alarmDto)
}
}


public async Task DeviceStatusEvent(PlayloadData status)
{
try
{
using (var _scope = _scopeFactor.CreateScope())
{
using (var _dbContext = _scope.ServiceProvider.GetRequiredService<ApplicationDbContext>())
{
var dev = _dbContext.Device.FirstOrDefault(d => d.Id == status.DeviceId);
if (dev != null)
{
if (status.DeviceStatus != DeviceStatus.Good)
{
await RunRules(dev.Id, status, EventType.Disconnected);
//真正离线
}
else if (status.DeviceStatus == DeviceStatus.Good)
{
await RunRules(dev.Id, status, EventType.Connected);
//真正掉线
}
var result2 = await _dbContext.SaveAsync<AttributeLatest>(status.ToDictionary(), dev.Id, DataSide.ServerSide);
_dbContext.SaveChanges();
}
else
{
_logger.LogWarning($"未找到设备{status.DeviceId} ,因此无法处理设备状态");
}
}
}
}
catch (Exception ex)
{
_logger.LogError(ex, $"处理{status.DeviceId} 的状态{status.DeviceStatus} 时遇到异常:{ex.Message}");

}
}

public async Task StoreTelemetryData(PlayloadData msg)
{
var result = await _storage.StoreTelemetryAsync(msg);
Expand Down
3 changes: 1 addition & 2 deletions IoTSharp.EventBus/IPublisher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ public interface IPublisher

public Task PublishAttributeData(PlayloadData msg);
public Task PublishTelemetryData(PlayloadData msg);
[Obsolete]
public Task PublishDeviceStatus(Guid devid, DeviceStatus devicestatus);

public Task PublishConnect(Guid devid, ConnectStatus devicestatus);
public Task PublishActive(Guid devid, ActivityStatus activity);
public Task PublishDeviceAlarm(CreateAlarmDto alarmDto);
Expand Down
1 change: 0 additions & 1 deletion IoTSharp.EventBus/ISubscriber.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ public interface ISubscriber
{
public Task StoreAttributeData(PlayloadData msg);
public Task OccurredAlarm(CreateAlarmDto alarmDto);
public Task DeviceStatusEvent(PlayloadData status);
public Task StoreTelemetryData(PlayloadData msg);
public Task DeleteDevice(Guid deviceId);
public Task CreateDevice(Guid deviceId);
Expand Down
3 changes: 1 addition & 2 deletions IoTSharp/Dtos/GatewayPlayload.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ public class GatewayPlayload
{
[JsonProperty(PropertyName = "ts")]
public long Ticks { get; set; } = DateTime.Now.Ticks;
[JsonProperty(PropertyName = "deviceStatus")]
public DeviceStatus DeviceStatus { get; set; } = DeviceStatus.Good;

[JsonProperty(PropertyName = "values")]
public Dictionary<string, object> Values { get; set; } = new();
}
Expand Down
4 changes: 2 additions & 2 deletions IoTSharp/Services/MQTTControllers/GatewayController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public async Task telemetry()
_logger.LogInformation($"{ClientId}的网关数据正在处理设备{dev}, 设备ID为{_dev?.Id}");
plst.ForEach(p =>
{
_queue.PublishTelemetryData(new PlayloadData() { DeviceId = device.Id, DeviceStatus = p.DeviceStatus, ts = new DateTime(p.Ticks), MsgBody = p.Values, DataSide = DataSide.ClientSide, DataCatalog = DataCatalog.TelemetryData });
_queue.PublishTelemetryData(new PlayloadData() { DeviceId = device.Id, ts = new DateTime(p.Ticks), MsgBody = p.Values, DataSide = DataSide.ClientSide, DataCatalog = DataCatalog.TelemetryData });
});
_logger.LogInformation($"{ClientId}的网关数据处理完成,设备{dev}ID为{device?.Id}共计{plst.Count}条");
});
Expand All @@ -87,7 +87,7 @@ public async Task Attributes()
_logger.LogInformation($"{ClientId}的网关数据正在处理设备{dev}, 设备ID为{device?.Id}");
plst.ForEach(async p =>
{
await _queue.PublishAttributeData(new PlayloadData() { DeviceId = device.Id, DeviceStatus = p.DeviceStatus, ts = new DateTime(p.Ticks), MsgBody = p.Values, DataSide = DataSide.ClientSide, DataCatalog = DataCatalog.TelemetryData });
await _queue.PublishAttributeData(new PlayloadData() { DeviceId = device.Id, ts = new DateTime(p.Ticks), MsgBody = p.Values, DataSide = DataSide.ClientSide, DataCatalog = DataCatalog.TelemetryData });
});
_logger.LogInformation($"{ClientId}的网关数据处理完成,设备{dev}ID为{device?.Id}共计{plst.Count}条");
});
Expand Down
67 changes: 0 additions & 67 deletions IoTSharp/Services/MQTTControllers/StatusController.cs

This file was deleted.

0 comments on commit f013701

Please sign in to comment.