终端一体化运控平台
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.
 
 
 

153 line
6.4 KiB

  1. using DataVAPI.Tool.IOT;
  2. using System;
  3. using System.Collections.Generic;
  4. using Tea;
  5. using static AlibabaCloud.SDK.Iot20180120.Models.QueryDeviceFileResponseBody;
  6. namespace BPASmartClient.IoT.Model
  7. {
  8. public class Sample
  9. {
  10. /**
  11. * 使用AK&SK初始化账号Client
  12. * @param accessKeyId
  13. * @param accessKeySecret
  14. * @return Client
  15. * @throws Exception
  16. */
  17. public static AlibabaCloud.SDK.Iot20180120.Client CreateClient(string accessKeyId, string accessKeySecret)
  18. {
  19. AlibabaCloud.OpenApiClient.Models.Config config = new AlibabaCloud.OpenApiClient.Models.Config
  20. {
  21. // 您的AccessKey ID
  22. AccessKeyId = accessKeyId,
  23. // 您的AccessKey Secret
  24. AccessKeySecret = accessKeySecret,
  25. };
  26. // 访问的域名
  27. config.Endpoint = "iot.cn-shanghai.aliyuncs.com";
  28. return new AlibabaCloud.SDK.Iot20180120.Client(config);
  29. }
  30. public static List<QueryDeviceFileResponseBodyData> FileQueryALL(string DeviceName)
  31. {
  32. List<QueryDeviceFileResponseBodyData> retdata = new List<QueryDeviceFileResponseBodyData>();
  33. AlibabaCloud.SDK.Iot20180120.Client client = CreateClient("LTAI5tDbUDuCvqU5hP8ZpHHV", "SvNIRQuz3BQFEaC3PFSJdgpeOKWFRi");
  34. AlibabaCloud.SDK.Iot20180120.Models.QueryDeviceFileListRequest queryDeviceFileListRequest = new AlibabaCloud.SDK.Iot20180120.Models.QueryDeviceFileListRequest
  35. {
  36. IotInstanceId = "iot-06z00g9pf3kwtxp",
  37. DeviceName = DataVClient.GetInstance().DeviceName,
  38. ProductKey = DataVClient.GetInstance().ProductKey,
  39. };
  40. AlibabaCloud.TeaUtil.Models.RuntimeOptions runtime = new AlibabaCloud.TeaUtil.Models.RuntimeOptions();
  41. AlibabaCloud.SDK.Iot20180120.Models.QueryDeviceFileListResponse resp = client.QueryDeviceFileListWithOptions(queryDeviceFileListRequest, runtime);
  42. AlibabaCloudRes cloudRes = Tools.JsonToObjectTools<AlibabaCloudRes>(AlibabaCloud.TeaUtil.Common.ToJSONString(resp.ToMap()));
  43. cloudRes?.body?.Data?.FileSummary?.ForEach(fileSummary =>
  44. {
  45. QueryDeviceFileResponseBodyData _data = FileQuery(DeviceName, fileSummary.FileId);
  46. if (_data != null)
  47. {
  48. _data.UtcCreatedOn = DateTime.Parse(_data.UtcCreatedOn).AddHours(8).ToString("yyyy-MM-dd HH:mm:ss");
  49. _data.DownloadUrl = _data.DownloadUrl.Replace("http://", "https://");
  50. retdata.Add(_data);
  51. }
  52. });
  53. return retdata;
  54. }
  55. /// <summary>
  56. /// 替换字符串起始位置(开头)中指定的字符串
  57. /// </summary>
  58. /// <param name="s">源串</param>
  59. /// <param name="searchStr">查找的串</param>
  60. /// <param name="replaceStr">替换的目标串</param>
  61. /// <returns></returns>
  62. public static string CutStarStr(string s, string searchStr, string replaceStr)
  63. {
  64. var result = s;
  65. try
  66. {
  67. if (string.IsNullOrEmpty(result))
  68. {
  69. return result;
  70. }
  71. if (s.Length < searchStr.Length)
  72. {
  73. return result;
  74. }
  75. if (s.IndexOf(searchStr, 0, searchStr.Length, StringComparison.Ordinal) > -1)
  76. {
  77. result = s.Substring(searchStr.Length);
  78. }
  79. return result;
  80. }
  81. catch (Exception e)
  82. {
  83. return result;
  84. }
  85. }
  86. public static QueryDeviceFileResponseBodyData FileQuery(string DeviceName, string FileId)
  87. {
  88. AlibabaCloud.SDK.Iot20180120.Client client = CreateClient("LTAI5tDbUDuCvqU5hP8ZpHHV", "SvNIRQuz3BQFEaC3PFSJdgpeOKWFRi");
  89. AlibabaCloud.SDK.Iot20180120.Models.QueryDeviceFileRequest queryDeviceFileRequest = new AlibabaCloud.SDK.Iot20180120.Models.QueryDeviceFileRequest
  90. {
  91. IotInstanceId = "iot-06z00g9pf3kwtxp",
  92. DeviceName = DataVClient.GetInstance().DeviceName,
  93. ProductKey = DataVClient.GetInstance().ProductKey,
  94. FileId = FileId,
  95. };
  96. AlibabaCloud.TeaUtil.Models.RuntimeOptions runtime = new AlibabaCloud.TeaUtil.Models.RuntimeOptions();
  97. // 复制代码运行请自行打印 API 的返回值
  98. QueryDeviceFileResponseBodyData queryDeviceFile = client.QueryDeviceFileWithOptions(queryDeviceFileRequest, runtime).Body.Data;
  99. return queryDeviceFile;
  100. }
  101. public static bool? DeleteFile(string DeviceName, string FileId)
  102. {
  103. AlibabaCloud.SDK.Iot20180120.Client client = CreateClient("LTAI5tDbUDuCvqU5hP8ZpHHV", "SvNIRQuz3BQFEaC3PFSJdgpeOKWFRi");
  104. AlibabaCloud.SDK.Iot20180120.Models.DeleteDeviceFileRequest deleteDeviceFileRequest = new AlibabaCloud.SDK.Iot20180120.Models.DeleteDeviceFileRequest
  105. {
  106. IotInstanceId = "iot-06z00g9pf3kwtxp",
  107. DeviceName = DataVClient.GetInstance().DeviceName,
  108. ProductKey = DataVClient.GetInstance().ProductKey,
  109. FileId = FileId,
  110. };
  111. AlibabaCloud.TeaUtil.Models.RuntimeOptions runtime = new AlibabaCloud.TeaUtil.Models.RuntimeOptions();
  112. // 复制代码运行请自行打印 API 的返回值
  113. return client.DeleteDeviceFileWithOptions(deleteDeviceFileRequest, runtime).Body.Success;
  114. }
  115. }
  116. public class AlibabaCloudRes
  117. {
  118. public object headers { get; set; }
  119. public StrRes body { get; set; }
  120. }
  121. public class StrRes
  122. {
  123. public string code { get; set; }
  124. public string CurrentPage { get; set; }
  125. public ResData Data { get; set; }
  126. public string ErrorMessage { get; set; }
  127. public int PageCount { get; set; }
  128. public int PageSize { get; set; }
  129. public string RequestId { get; set; }
  130. public bool Success { get; set; }
  131. public int Total { get; set; }
  132. }
  133. public class ResData
  134. {
  135. public List<summary> FileSummary { get; set; }
  136. }
  137. public class summary
  138. {
  139. public string UtcCreatedOn { get; set; }
  140. public string Size { get; set; }
  141. public string FileId { get; set; }
  142. public string Name { get; set; }
  143. }
  144. }