25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 

153 satır
6.3 KiB

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