You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

DeviceIotOpenService.cs 6.7KB

7 kuukautta sitten
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. using GpsCardGatewayPosition.Common;
  2. using GpsCardGatewayPosition.Model.Config;
  3. using Microsoft.Extensions.Logging;
  4. using Microsoft.Extensions.Options;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. namespace GpsCardGatewayPosition.Service.Biz.Iot
  11. {
  12. public class DeviceIotOpenService
  13. {
  14. private readonly IotConfig _configIot;
  15. private readonly ServiceAccessConfig _configServiceAccess;
  16. private readonly ILogger<DeviceIotOpenService> _logger;
  17. private readonly AlibabaCloud.SDK.Iot20180120.Client _client;
  18. public string Message { get; private set; }
  19. public DeviceIotOpenService(IOptions<IotConfig> optConfigIot, IOptions<ServiceAccessConfig> optConfigServiceAccess,
  20. ILogger<DeviceIotOpenService> logger)
  21. {
  22. _configIot = optConfigIot.Value;
  23. _configServiceAccess = optConfigServiceAccess.Value;
  24. _logger = logger;
  25. AlibabaCloud.OpenApiClient.Models.Config config = new AlibabaCloud.OpenApiClient.Models.Config
  26. {
  27. // 您的AccessKey ID
  28. AccessKeyId = _configIot.AccessKey,
  29. // 您的AccessKey Secret
  30. AccessKeySecret = _configIot.AccessSecret,
  31. // 访问的域名
  32. Endpoint = _configIot.Endpoint,
  33. };
  34. _client = new AlibabaCloud.SDK.Iot20180120.Client(config);
  35. }
  36. /// <summary>
  37. /// 调用设备服务方法
  38. /// </summary>
  39. /// <param name="deviceName">DeviceName</param>
  40. /// <param name="serviceName">ServiceName</param>
  41. /// <param name="args">JSON String{"Status":1,"Data":"Hello World"}</param>
  42. /// <returns>bool true/false</returns>
  43. public async Task<bool> InvokeThingServiceAsync(string deviceName, string serviceName, string args)
  44. {
  45. bool success = false;
  46. if (!_configServiceAccess.EnableIotService) return success;
  47. using (var sw = new CustomizeStopWatch("InvokeThingService", _logger))
  48. {
  49. var request = new AlibabaCloud.SDK.Iot20180120.Models.InvokeThingServiceRequest
  50. {
  51. ProductKey = _configIot.ProductKey,
  52. DeviceName = deviceName,
  53. Identifier = serviceName,
  54. Args = args
  55. };
  56. var now = DateTime.Now;
  57. try
  58. {
  59. var response = await _client.InvokeThingServiceAsync(request).ConfigureAwait(false);
  60. 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")}";
  61. _logger.LogInformation($"Invoke Service /Identifier {serviceName} /Args: {request.Args}");
  62. success = response.Body.Success ?? false;
  63. //if ((serviceName.Equals("LocationDownload") || serviceName.Equals("getGeoLocation") == false))
  64. //{
  65. // //服务守护
  66. // _serviceGuardMq.Send(request, response);
  67. //}
  68. }
  69. catch (Exception ex)
  70. {
  71. _logger.LogError($"InvokeThingService error, Message: {ex.Message}");
  72. }
  73. }
  74. return success;
  75. }
  76. /// <summary>
  77. /// 下发指令设置设备期望属性
  78. /// </summary>
  79. /// <param name="deviceName">DeviceName</param>
  80. /// <param name="items">JSON String{"Status":1,"Data":"Hello World"}</param>
  81. /// <returns>bool true/false</returns>
  82. public async Task<bool> SetDeviceDesiredPropertyAsync(string deviceName, string items)
  83. {
  84. bool success = false;
  85. if (!_configServiceAccess.EnableIotService) return success;
  86. using (var sw = new CustomizeStopWatch("InvokeThingService", _logger))
  87. {
  88. var request = new AlibabaCloud.SDK.Iot20180120.Models.SetDeviceDesiredPropertyRequest
  89. {
  90. ProductKey = _configIot.ProductKey,
  91. DeviceName = deviceName,
  92. Versions = "{}",
  93. Items = items
  94. };
  95. var now = DateTime.Now;
  96. try
  97. {
  98. var response = await _client.SetDeviceDesiredPropertyAsync(request).ConfigureAwait(false);
  99. 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")}";
  100. _logger.LogInformation($"Set Device /DesiredProperty {deviceName} /Items: {request.Items}");
  101. success = response.Body.Success ?? false;
  102. }
  103. catch (Exception ex)
  104. {
  105. _logger.LogError($"SetDeviceDesiredProperty error, Message: {ex.Message}");
  106. }
  107. }
  108. return success;
  109. }
  110. /// <summary>
  111. /// 下发指令设置设备属性
  112. /// </summary>
  113. /// <param name="deviceName">DeviceName</param>
  114. /// <param name="items">JSON String{"Status":1,"Data":"Hello World"}</param>
  115. /// <returns>bool true/false</returns>
  116. public async Task<bool> SetDevicePropertyAsync(string deviceName, string items)
  117. {
  118. bool success = false;
  119. if (!_configServiceAccess.EnableIotService) return success;
  120. using (var sw = new CustomizeStopWatch("InvokeThingService", _logger))
  121. {
  122. var request = new AlibabaCloud.SDK.Iot20180120.Models.SetDevicePropertyRequest
  123. {
  124. ProductKey = _configIot.ProductKey,
  125. DeviceName = deviceName,
  126. Items = items
  127. };
  128. var now = DateTime.Now;
  129. try
  130. {
  131. var response = await _client.SetDevicePropertyAsync(request).ConfigureAwait(false);
  132. 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")}";
  133. _logger.LogInformation($"Set Device /Property {deviceName} /Items: {request.Items}");
  134. success = response.Body.Success ?? false;
  135. }
  136. catch (Exception ex)
  137. {
  138. _logger.LogError($"SetDevicePropert error, Message: {ex.Message}");
  139. }
  140. }
  141. return success;
  142. }
  143. }
  144. }