|
- using GpsCardGatewayPosition.Common;
- using GpsCardGatewayPosition.Model.Config;
- using Microsoft.Extensions.Logging;
- using Microsoft.Extensions.Options;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
-
- namespace GpsCardGatewayPosition.Service.Biz.Iot
- {
- public class DeviceIotOpenService
- {
- private readonly IotConfig _configIot;
- private readonly ServiceAccessConfig _configServiceAccess;
-
- private readonly ILogger<DeviceIotOpenService> _logger;
-
- private readonly AlibabaCloud.SDK.Iot20180120.Client _client;
-
- public string Message { get; private set; }
-
- public DeviceIotOpenService(IOptions<IotConfig> optConfigIot, IOptions<ServiceAccessConfig> optConfigServiceAccess,
- ILogger<DeviceIotOpenService> logger)
- {
- _configIot = optConfigIot.Value;
- _configServiceAccess = optConfigServiceAccess.Value;
- _logger = logger;
-
- AlibabaCloud.OpenApiClient.Models.Config config = new AlibabaCloud.OpenApiClient.Models.Config
- {
- // 您的AccessKey ID
- AccessKeyId = _configIot.AccessKey,
- // 您的AccessKey Secret
- AccessKeySecret = _configIot.AccessSecret,
- // 访问的域名
- Endpoint = _configIot.Endpoint,
- };
-
- _client = new AlibabaCloud.SDK.Iot20180120.Client(config);
- }
-
- /// <summary>
- /// 调用设备服务方法
- /// </summary>
- /// <param name="deviceName">DeviceName</param>
- /// <param name="serviceName">ServiceName</param>
- /// <param name="args">JSON String{"Status":1,"Data":"Hello World"}</param>
- /// <returns>bool true/false</returns>
- public async Task<bool> InvokeThingServiceAsync(string deviceName, string serviceName, string args)
- {
- bool success = false;
- if (!_configServiceAccess.EnableIotService) return success;
-
- using (var sw = new CustomizeStopWatch("InvokeThingService", _logger))
- {
- var request = new AlibabaCloud.SDK.Iot20180120.Models.InvokeThingServiceRequest
- {
- ProductKey = _configIot.ProductKey,
- DeviceName = deviceName,
- Identifier = serviceName,
- Args = args
- };
- var now = DateTime.Now;
-
- try
- {
- var response = await _client.InvokeThingServiceAsync(request).ConfigureAwait(false);
-
- sw.Content = $"RequestId: {response.Body.RequestId}, 调用时间: {now.ToString("yyyy-MM-dd HH:mm:ss:fff")}, 完成时间: {DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff")}";
- _logger.LogInformation($"Invoke Service /Identifier {serviceName} /Args: {request.Args}");
- success = response.Body.Success ?? false;
-
- //if ((serviceName.Equals("LocationDownload") || serviceName.Equals("getGeoLocation") == false))
- //{
- // //服务守护
- // _serviceGuardMq.Send(request, response);
- //}
- }
- catch (Exception ex)
- {
- _logger.LogError($"InvokeThingService error, Message: {ex.Message}");
- }
- }
- return success;
- }
-
- /// <summary>
- /// 下发指令设置设备期望属性
- /// </summary>
- /// <param name="deviceName">DeviceName</param>
- /// <param name="items">JSON String{"Status":1,"Data":"Hello World"}</param>
- /// <returns>bool true/false</returns>
- public async Task<bool> SetDeviceDesiredPropertyAsync(string deviceName, string items)
- {
- bool success = false;
- if (!_configServiceAccess.EnableIotService) return success;
-
- using (var sw = new CustomizeStopWatch("InvokeThingService", _logger))
- {
- var request = new AlibabaCloud.SDK.Iot20180120.Models.SetDeviceDesiredPropertyRequest
- {
- ProductKey = _configIot.ProductKey,
- DeviceName = deviceName,
- Versions = "{}",
- Items = items
- };
- var now = DateTime.Now;
-
- try
- {
- var response = await _client.SetDeviceDesiredPropertyAsync(request).ConfigureAwait(false);
-
- sw.Content = $"RequestId: {response.Body.RequestId}, 调用时间: {now.ToString("yyyy-MM-dd HH:mm:ss:fff")}, 完成时间: {DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff")}";
- _logger.LogInformation($"Set Device /DesiredProperty {deviceName} /Items: {request.Items}");
- success = response.Body.Success ?? false;
- }
- catch (Exception ex)
- {
- _logger.LogError($"SetDeviceDesiredProperty error, Message: {ex.Message}");
- }
- }
- return success;
- }
-
-
- /// <summary>
- /// 下发指令设置设备属性
- /// </summary>
- /// <param name="deviceName">DeviceName</param>
- /// <param name="items">JSON String{"Status":1,"Data":"Hello World"}</param>
- /// <returns>bool true/false</returns>
- public async Task<bool> SetDevicePropertyAsync(string deviceName, string items)
- {
- bool success = false;
- if (!_configServiceAccess.EnableIotService) return success;
-
- using (var sw = new CustomizeStopWatch("InvokeThingService", _logger))
- {
- var request = new AlibabaCloud.SDK.Iot20180120.Models.SetDevicePropertyRequest
- {
- ProductKey = _configIot.ProductKey,
- DeviceName = deviceName,
- Items = items
- };
- var now = DateTime.Now;
-
- try
- {
- var response = await _client.SetDevicePropertyAsync(request).ConfigureAwait(false);
-
- sw.Content = $"RequestId: {response.Body.RequestId}, 调用时间: {now.ToString("yyyy-MM-dd HH:mm:ss:fff")}, 完成时间: {DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff")}";
- _logger.LogInformation($"Set Device /Property {deviceName} /Items: {request.Items}");
- success = response.Body.Success ?? false;
- }
- catch (Exception ex)
- {
- _logger.LogError($"SetDevicePropert error, Message: {ex.Message}");
- }
- }
- return success;
- }
-
- }
- }
|