64 lines
2.3 KiB
C#
64 lines
2.3 KiB
C#
using Infrastructure.Cache;
|
|
using Infrastructure.CloudSdk.mqttmessagecenter;
|
|
using OpenAuth.App.ServiceApp;
|
|
using OpenAuth.Repository.Domain;
|
|
using System.Text.Json.Nodes;
|
|
|
|
namespace OpenAuth.WebApi.Model.mqtt
|
|
{
|
|
//机载盒子
|
|
public class ThingOnboardCaseHandler : IMqttMessageHandler
|
|
{
|
|
private readonly ICacheContext _cache;
|
|
public ThingOnboardCaseHandler(AirportMaintenanceApp app, ICacheContext cache)
|
|
{
|
|
_cache = cache;
|
|
}
|
|
public bool CanHandle(string topic)
|
|
{
|
|
return topic.Contains("/onboardcase");
|
|
}
|
|
public async Task HandleAsync(string topic, string payload)
|
|
{
|
|
var root = JsonNode.Parse(payload)?.AsObject();
|
|
if (root == null || root["type"]?.ToString() != "status_update")
|
|
return;
|
|
|
|
var deviceList = root["devices"]?.AsArray();
|
|
if (deviceList == null || deviceList.Count == 0)
|
|
return;
|
|
|
|
foreach (var item in deviceList)
|
|
{
|
|
var deviceObj = item?.AsObject();
|
|
if (deviceObj == null) continue;
|
|
|
|
var deviceId = deviceObj["device_id"]?.ToString();
|
|
if (string.IsNullOrEmpty(deviceId)) continue;
|
|
|
|
var deviceStatus = new
|
|
{
|
|
device_id = deviceId,
|
|
is_input_available = deviceObj["is_input_available"]?.GetValue<bool>() ?? false,
|
|
is_forwarding = deviceObj["is_forwarding"]?.GetValue<bool>() ?? false,
|
|
srs_rtmp_push_url = deviceObj["srs_rtmp_push_url"]?.ToString(),
|
|
last_check_time = DateTime.TryParse(deviceObj["last_check_time"]?.ToString(),
|
|
out var dt) ? dt : DateTime.Now
|
|
};
|
|
_cache.Set(deviceId, deviceStatus, DateTime.Now.AddSeconds(20));
|
|
}
|
|
// var root = JsonNode.Parse(payload)?.AsObject();
|
|
//if (root != null && root["type"]?.ToString() == "status_update")
|
|
//{
|
|
// var deviceList = root["devices"];
|
|
// foreach (var item in deviceList)
|
|
// {
|
|
// var onboardcase = _cache.Set(item["device_id"]?.ToString(), payload, DateTime.Now.AddSeconds(20));
|
|
// }
|
|
|
|
//}
|
|
|
|
}
|
|
}
|
|
}
|