调用平台设备服务

This commit is contained in:
ChenYi 2026-03-17 16:23:51 +08:00
parent 919f58d6c5
commit 6da12cbf2d
5 changed files with 156 additions and 5 deletions

View File

@ -0,0 +1,26 @@
using JiShe.ServicePro.Core;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Text;
namespace JiShe.IoT.DeviceAggregation
{
/// <summary>
/// 调用设备服务入参
/// </summary>
public class CallDeviceServiceForApiInput : IdInput
{
/// <summary>
/// 服务名称
/// </summary>
[Required]
public string ServiceName { get; set; }
/// <summary>
/// 服务参数
/// </summary>
[Required]
public Dictionary<string, object> ServiceParams { get; set; }
}
}

View File

@ -106,5 +106,12 @@ namespace JiShe.IoT.DeviceAggregation
/// <param name="input"></param> /// <param name="input"></param>
/// <returns></returns> /// <returns></returns>
Task<bool> BindingDeviceThingModel(BindingDeviceThingModelInput input); Task<bool> BindingDeviceThingModel(BindingDeviceThingModelInput input);
/// <summary>
/// 调用OneNET平台设备服务
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
Task<Dictionary<string, object>> CallDeviceServiceToOneNETForApiAsync(CallDeviceServiceForApiInput input);
} }
} }

View File

@ -638,6 +638,60 @@ namespace JiShe.IoT.DeviceAggregation
} }
/// <summary>
/// 调用OneNET平台设备服务
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
public async Task<Dictionary<string, object>> CallDeviceServiceToOneNETForApiAsync(CallDeviceServiceForApiInput input)
{
try
{
if (string.IsNullOrWhiteSpace(input.ServiceName) || input.ServiceParams == null || input.ServiceParams.Count <= 0)
{
throw new UserFriendlyException($"服务名称参数异常");
}
var deviceInfo = await deviceAppService.FindByIdAsync(input);
if (deviceInfo == null)
{
throw new UserFriendlyException($"设备不存在");
}
//数据写入遥测任务数据存储通道
var commandRequest = new OpenApiRequest()
{
Message = input.Serialize(),
};
var packetTaskInfo = GetDeviceTelemetryPacketTaskInfo(ioTDBOptions, commandRequest, deviceInfo.Adapt<DeviceCacheInfos>(), input.Serialize());
packetTaskInfo.TelemetryType = (int)DeviceTelemetryCommandTypeEnum.GetAttributeData;
if (deviceInfo.IoTPlatform == IoTPlatformTypeEnum.OneNET)
{
await ioTDBDataChannelManageService.DeviceTelemetryTaskWriterAsync(DataChannelManage.DeviceTelemetryTaskDataChannel.Writer, (DistributedMessageCenterConst.OneNETUpgradeCommandIssuedEventName, packetTaskInfo));
return await CallDeviceServiceToOneNET(deviceInfo, input);
}
else if (deviceInfo.IoTPlatform == IoTPlatformTypeEnum.CTWing)
{
//await redisPubSubService.PublishReliableAsync(DistributedMessageCenterConst.CTWingAepCommandIssuedEventName,commandRequest);
//return true;
throw new UserFriendlyException($"发送设备指令信息失败CTWing暂未实现。");
}
else
{
throw new UserFriendlyException($"发送设备指令信息失败,未找到对应的产品配置信息。");
}
}
catch (Exception)
{
throw;
}
}
#region CTWing #region CTWing
/// <summary> /// <summary>
/// CTWing 设备创建 /// CTWing 设备创建

View File

@ -34,12 +34,12 @@ namespace JiShe.IoT
{ {
protected readonly ILogger<IoTDeviceBasicAppService> logger; protected readonly ILogger<IoTDeviceBasicAppService> logger;
protected readonly IDeviceAppService deviceAppService; protected readonly IDeviceAppService deviceAppService;
protected readonly IOneNETProductService oneNETProductService;
protected readonly IOneNETDeviceService oneNETDeviceService; protected readonly IOneNETDeviceService oneNETDeviceService;
protected readonly IReliableRedisPubSubService redisPubSubService; protected readonly IReliableRedisPubSubService redisPubSubService;
protected readonly IIoTDBDataChannelManageService ioTDBDataChannelManageService; protected readonly IIoTDBDataChannelManageService ioTDBDataChannelManageService;
protected readonly IoTDBOptions ioTDBOptions; protected readonly IoTDBOptions ioTDBOptions;
protected readonly ServerApplicationOptions serverApplicationOptions; protected readonly ServerApplicationOptions serverApplicationOptions;
protected readonly IOneNETProductService oneNETProductService;
protected readonly IDeviceThingModelManagementAppService deviceThingModelService; protected readonly IDeviceThingModelManagementAppService deviceThingModelService;
protected readonly IIoTPlatformThingModelInfoAppService platformThingModelInfoAppService; protected readonly IIoTPlatformThingModelInfoAppService platformThingModelInfoAppService;
protected readonly IDeviceUpgradeRecordService deviceUpgradeRecordService; protected readonly IDeviceUpgradeRecordService deviceUpgradeRecordService;
@ -802,6 +802,70 @@ namespace JiShe.IoT
throw; throw;
} }
} }
/// <summary>
/// 调用OneNET平台设备服务
/// </summary>
/// <param name="deviceInfo"></param>
/// <param name="input"></param>
/// <returns></returns>
/// <exception cref="UserFriendlyException"></exception>
protected async Task<Dictionary<string, object>> CallDeviceServiceToOneNET(DeviceManagementInfoDto deviceInfo, CallDeviceServiceForApiInput input)
{
try
{
//检查下设备是否在线
var deviceOnlineStatus = await oneNETDeviceService.DeviceInfoDetailAsync(new DeviceInfoDetailInput()
{
DeviceName = deviceInfo.IoTPlatformDeviceOpenInfo,
OneNETAccountId = deviceInfo.IoTPlatformAccountId,
ProductId = deviceInfo.IoTPlatformProductId,
});
if (deviceOnlineStatus == null || deviceOnlineStatus.Code != ResponeResultEnum.Success)
{
throw new UserFriendlyException("获取平台设备信息失败");
}
if (deviceOnlineStatus.Data.Status != 1)
{
throw new UserFriendlyException("设备不在线");
}
var deviceDataResult = await oneNETDeviceService.CallDeviceService(new CallDeviceServiceRequestInput()
{
DeviceName = deviceInfo.IoTPlatformDeviceOpenInfo,
OneNETAccountId = deviceInfo.IoTPlatformAccountId,
ProductId = deviceInfo.IoTPlatformProductId,
ThingModelIdentifier = input.ServiceName,
ServiceParams = input.ServiceParams
});
if (deviceDataResult == null || deviceDataResult.Success == false || deviceDataResult.Data == null)
{
throw new UserFriendlyException($"设备{deviceInfo.DeviceName}获取数据失败");
}
//调用平台设备服务
var platformThingModelInfo = await platformThingModelInfoAppService.FindByPlatformProductIdAsync(new IdInput<string>() { Id = deviceInfo.IoTPlatformProductId });
if (platformThingModelInfo == null || platformThingModelInfo.Count <= 0)
{
return deviceDataResult.Data;
}
List<string> updateKeys = new List<string>()
{
ThingModelFixedTypeConst.FIRMWARE_VERSION.ToLowerInvariant(),
ThingModelFixedTypeConst.ReadingMode.ToLowerInvariant()
};
return deviceDataResult.Data;
}
catch (Exception ex)
{
throw;
}
}
#endregion #endregion
#region CTWing #region CTWing

View File

@ -35,7 +35,7 @@ namespace JiShe.IoT.IoTPlatformAggregation
private readonly ICTWingAccountService _ctwingAccountService; private readonly ICTWingAccountService _ctwingAccountService;
private readonly IOneNETAccountService _oneNETAccountService; private readonly IOneNETAccountService _oneNETAccountService;
private readonly IIoTPlatformThingModelInfoAppService _ioTPlatformThingModelInfoAppService; private readonly IIoTPlatformThingModelInfoAppService _ioTPlatformThingModelInfoAppService;
private readonly IOneNETSubscriberBasic _oneNETSubscriberBasicService; private readonly IOneNETPulsarServiceSubscriptionService _oneNETPulsarServiceSubscriptionService;
private readonly ITableModelService _tableModelService; private readonly ITableModelService _tableModelService;
@ -45,7 +45,7 @@ namespace JiShe.IoT.IoTPlatformAggregation
IOneNETProductService oneNetProductService, IOneNETProductService oneNetProductService,
ICTWingAccountService ctwingAccountService, ICTWingAccountService ctwingAccountService,
IOneNETAccountService oneNETAccountService, IOneNETAccountService oneNETAccountService,
IOneNETSubscriberBasic oneNETSubscriberBasicService, IOneNETPulsarServiceSubscriptionService oneNETPulsarServiceSubscriptionService,
ITableModelService tableModelService, ITableModelService tableModelService,
IIoTPlatformThingModelInfoAppService ioTPlatformThingModelInfoAppService) IIoTPlatformThingModelInfoAppService ioTPlatformThingModelInfoAppService)
{ {
@ -55,7 +55,7 @@ namespace JiShe.IoT.IoTPlatformAggregation
_ctwingAccountService = ctwingAccountService; _ctwingAccountService = ctwingAccountService;
_oneNETAccountService = oneNETAccountService; _oneNETAccountService = oneNETAccountService;
_ioTPlatformThingModelInfoAppService = ioTPlatformThingModelInfoAppService; _ioTPlatformThingModelInfoAppService = ioTPlatformThingModelInfoAppService;
_oneNETSubscriberBasicService = oneNETSubscriberBasicService; _oneNETPulsarServiceSubscriptionService = oneNETPulsarServiceSubscriptionService;
_tableModelService = tableModelService; _tableModelService = tableModelService;
} }
@ -383,7 +383,7 @@ namespace JiShe.IoT.IoTPlatformAggregation
{ {
string topicName = string.Equals(item.MessageType, IoTDBDataTypeConst.Data, StringComparison.OrdinalIgnoreCase) ? DistributedMessageCenterConst.OneNETThingModelPropertyChangeReceivedEventName : DistributedMessageCenterConst.OneNETThingModelEventChangeReceivedEventName; string topicName = string.Equals(item.MessageType, IoTDBDataTypeConst.Data, StringComparison.OrdinalIgnoreCase) ? DistributedMessageCenterConst.OneNETThingModelPropertyChangeReceivedEventName : DistributedMessageCenterConst.OneNETThingModelEventChangeReceivedEventName;
var tempOneNETReceiveBasicModel = item.RawMessage.Deserialize<OneNETReceiveBasicModel>(); var tempOneNETReceiveBasicModel = item.RawMessage.Deserialize<OneNETReceiveBasicModel>();
await _oneNETSubscriberBasicService.OneNETReceiveThingModelHandlerAsync(topicName, item.IoTDataType, tempOneNETReceiveBasicModel, true); await _oneNETPulsarServiceSubscriptionService.OneNETReceiveThingModelHandlerAsync(topicName, item.IoTDataType, tempOneNETReceiveBasicModel, true);
} }
} }
catch (Exception) catch (Exception)