设备升级结果更新通知

This commit is contained in:
ChenYi 2026-01-08 17:29:54 +08:00
parent 7ca6f529e8
commit 777e1303a9
3 changed files with 113 additions and 5 deletions

View File

@ -14,10 +14,23 @@ namespace JiShe.ServicePro.OneNETManagement.Subscribers
Task ServiceCommunicationDeviceStatusSubscriber(); Task ServiceCommunicationDeviceStatusSubscriber();
/// <summary> /// <summary>
/// 设备状态更新 /// 设备在线状态更新
/// </summary> /// </summary>
/// <param name="input"></param> /// <param name="input"></param>
/// <returns></returns> /// <returns></returns>
Task DeviceStatusUpdate(DeviceStatusMessage input); Task DeviceOnlineStatusUpdate(DeviceStatusMessage input);
/// <summary>
/// 设备升级结果通知 Redis 消息订阅
/// </summary>
/// <returns></returns>
Task ServiceCommunicationDeviceUpgradeSubscriber();
/// <summary>
/// 设备升级结果更新
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
Task DeviceUpgradeResultUpdate(UpgradeResultMessage input);
} }
} }

View File

@ -61,6 +61,7 @@ namespace JiShe.IoT
var serviceCommunicationChannelSubscriberService = context.ServiceProvider.GetRequiredService<IServiceCommunicationChannelSubscriberService>(); var serviceCommunicationChannelSubscriberService = context.ServiceProvider.GetRequiredService<IServiceCommunicationChannelSubscriberService>();
serviceCommunicationChannelSubscriberService.ServiceCommunicationDeviceStatusSubscriber(); serviceCommunicationChannelSubscriberService.ServiceCommunicationDeviceStatusSubscriber();
serviceCommunicationChannelSubscriberService.ServiceCommunicationDeviceUpgradeSubscriber();
} }
} }
} }

View File

@ -2,6 +2,7 @@
using JiShe.ServicePro.Core; using JiShe.ServicePro.Core;
using JiShe.ServicePro.DeviceManagement.DeviceInfos; using JiShe.ServicePro.DeviceManagement.DeviceInfos;
using JiShe.ServicePro.Dto; using JiShe.ServicePro.Dto;
using JiShe.ServicePro.Enums;
using JiShe.ServicePro.FreeRedisProvider; using JiShe.ServicePro.FreeRedisProvider;
using JiShe.ServicePro.FreeSqlProvider; using JiShe.ServicePro.FreeSqlProvider;
using Mapster; using Mapster;
@ -54,7 +55,7 @@ namespace JiShe.ServicePro.OneNETManagement.Subscribers
var scopedServiceCommunicationChannelSubscriberService = sp.GetRequiredService<IServiceCommunicationChannelSubscriberService>(); var scopedServiceCommunicationChannelSubscriberService = sp.GetRequiredService<IServiceCommunicationChannelSubscriberService>();
var scopedLogger = sp.GetRequiredService<ILogger<ServiceCommunicationChannelSubscriberService>>(); var scopedLogger = sp.GetRequiredService<ILogger<ServiceCommunicationChannelSubscriberService>>();
scopedLogger.LogError($"{nameof(ServiceCommunicationDeviceStatusSubscriber)}收到设备{message.DeviceAddress}上下线状态更新通知,开始更新{message.Status}状态处理..."); scopedLogger.LogError($"{nameof(ServiceCommunicationDeviceStatusSubscriber)}收到设备{message.DeviceAddress}上下线状态更新通知,开始更新{message.Status}状态处理...");
await scopedServiceCommunicationChannelSubscriberService.DeviceStatusUpdate(message); await scopedServiceCommunicationChannelSubscriberService.DeviceOnlineStatusUpdate(message);
scopedLogger.LogError($"{nameof(ServiceCommunicationDeviceStatusSubscriber)}设备{message.DeviceAddress}上下线状态更新通知,结束更新{message.Status}状态处理..."); scopedLogger.LogError($"{nameof(ServiceCommunicationDeviceStatusSubscriber)}设备{message.DeviceAddress}上下线状态更新通知,结束更新{message.Status}状态处理...");
} }
return true; return true;
@ -73,7 +74,7 @@ namespace JiShe.ServicePro.OneNETManagement.Subscribers
/// <param name="input"></param> /// <param name="input"></param>
/// <returns></returns> /// <returns></returns>
/// <exception cref="UserFriendlyException"></exception> /// <exception cref="UserFriendlyException"></exception>
public async Task DeviceStatusUpdate(DeviceStatusMessage input) public async Task DeviceOnlineStatusUpdate(DeviceStatusMessage input)
{ {
try try
{ {
@ -120,9 +121,102 @@ namespace JiShe.ServicePro.OneNETManagement.Subscribers
} }
catch (Exception ex) catch (Exception ex)
{ {
_logger.LogError($"{nameof(DeviceStatusUpdate)} 设备在线状态更新发生异常:{ex.Message}"); _logger.LogError($"{nameof(DeviceOnlineStatusUpdate)} 设备在线状态更新发生异常:{ex.Message}");
throw ; throw ;
} }
} }
/// <summary>
/// 设备升级结果通知 Redis 消息订阅
/// </summary>
/// <returns></returns>
public async Task ServiceCommunicationDeviceUpgradeSubscriber()
{
try
{
// 订阅频道
await _reliableRedisPubSubService.SubscribeReliableAsync<UpgradeResultMessage>(DistributedMessageCenterConst.ServiceCommunicationDeviceUpgradeEventName, async (message) =>
{
using (var scope = _serviceScopeFactory.CreateScope())
{
var sp = scope.ServiceProvider;
var scopedServiceCommunicationChannelSubscriberService = sp.GetRequiredService<IServiceCommunicationChannelSubscriberService>();
var scopedLogger = sp.GetRequiredService<ILogger<ServiceCommunicationChannelSubscriberService>>();
scopedLogger.LogError($"{nameof(ServiceCommunicationDeviceUpgradeSubscriber)}收到设备{message.DeviceAddress} 升级结果更新通知,开始更新{message.ResultType}状态处理...");
await scopedServiceCommunicationChannelSubscriberService.DeviceUpgradeResultUpdate(message);
scopedLogger.LogError($"{nameof(ServiceCommunicationDeviceUpgradeSubscriber)}设备{message.DeviceAddress} 升级结果更新通知,结束更新{message.ResultType}状态处理...");
}
return true;
});
}
catch (Exception)
{
throw;
}
}
/// <summary>
/// 设备升级结果更新
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
/// <exception cref="UserFriendlyException"></exception>
public async Task DeviceUpgradeResultUpdate(UpgradeResultMessage input)
{
try
{
if (input == null || input.DeviceAddress.IsNullOrWhiteSpace())
{
throw new UserFriendlyException($"设备升级结果消息处理失败,参数为空");
}
var deviceUpgradeRecordEntity = await FreeSqlDbContext.Instance.Select<DeviceUpgradeRecord>()
.Where(d => d.UpgradeIdentifier == input.UpgradeIdentifier && d.DeviceAddress == input.DeviceAddress)
.FirstAsync();
if (deviceUpgradeRecordEntity == null)
{
throw new UserFriendlyException($"设备 {input.DeviceAddress} 的标识符{input.UpgradeIdentifier}升级记录不存在");
}
if (input.ResultType == DeviceUpgradeResultTypeEnum.Success)
{
deviceUpgradeRecordEntity.UpgradeResult = input.ResultType;
deviceUpgradeRecordEntity.UpgradeStatus = DeviceUpgradeStatusTypeEnum.UpgradeSuccess;
deviceUpgradeRecordEntity.UpgradeDate = TimestampHelper.ConvertToDateTime(input.ReceivedTime, TimestampUnit.Milliseconds, DateTimeKind.Local);
}
else
{
deviceUpgradeRecordEntity.UpgradeResult = input.ResultType;
deviceUpgradeRecordEntity.UpgradeStatus = DeviceUpgradeStatusTypeEnum.UpgradeFailed;
deviceUpgradeRecordEntity.UpgradeDate = TimestampHelper.ConvertToDateTime(input.ReceivedTime, TimestampUnit.Milliseconds, DateTimeKind.Local);
}
var updateResult = await FreeSqlDbContext.Instance.Update<DeviceUpgradeRecord>()
.SetSource(deviceUpgradeRecordEntity)
.UpdateColumns(a => new { a.UpgradeResult, a.UpgradeStatus, a.UpgradeDate })
.ExecuteAffrowsAsync();
if (updateResult <= 0)
{
_logger.LogError($"{nameof(ServiceCommunicationDeviceStatusSubscriber)} 设备状态更新失败{input.Serialize()}");
throw new UserFriendlyException($"设备状态更新失败");
}
//更新设备数据缓存到Redis
DeviceCacheInfos deviceCacheInfos = deviceUpgradeRecordEntity.Adapt<DeviceCacheInfos>();
deviceCacheInfos.PlatformPassword = null;
FreeRedisProvider.Instance.HSet<DeviceCacheInfos>(RedisConst.CacheAllDeviceInfoHashKey, deviceUpgradeRecordEntity.DeviceAddress, deviceCacheInfos);
}
catch (Exception ex)
{
_logger.LogError($"{nameof(DeviceUpgradeResultUpdate)} 设备升级结果状态更新发生异常:{ex.Message}");
throw;
}
}
} }
} }