|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166 |
- 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;
- }
-
- }
- }
|