From 294c999591b039cbbed3aaefff5036bc30d89b2e Mon Sep 17 00:00:00 2001 From: fyf Date: Mon, 23 May 2022 11:30:59 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8F=90=E4=BA=A4=EF=BC=9A1.=E5=A2=9E=E5=8A=A0?= =?UTF-8?q?=E6=A0=B9=E6=8D=AE=E8=AE=BE=E5=A4=87=E5=90=8D=E7=A7=B0=E6=9F=A5?= =?UTF-8?q?=E8=AF=A2=E6=97=A5=E5=BF=97=E6=96=87=E4=BB=B6=202.=E5=A2=9E?= =?UTF-8?q?=E5=8A=A0=E6=8E=A5=E5=8F=A3=E8=80=8D=E7=95=8C=E9=9D=A2=E5=BA=97?= =?UTF-8?q?=E9=93=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- DataVAPI.Tools/IOT/IOTDevServer.cs | 13 +++ DataVAPI/Controllers/DeviceController.cs | 18 ++++ DataVAPI/Controllers/LogController.cs | 28 ++++++ DataVAPI/DataVAPI.csproj | 5 ++ DataVAPI/UpAndDown/ProcessServer.cs | 15 ++-- DataVAPI/UpAndDown/Sample.cs | 105 +++++++++++++++++++++++ 6 files changed, 176 insertions(+), 8 deletions(-) create mode 100644 DataVAPI/UpAndDown/Sample.cs diff --git a/DataVAPI.Tools/IOT/IOTDevServer.cs b/DataVAPI.Tools/IOT/IOTDevServer.cs index 22b21a6..08c25af 100644 --- a/DataVAPI.Tools/IOT/IOTDevServer.cs +++ b/DataVAPI.Tools/IOT/IOTDevServer.cs @@ -276,6 +276,17 @@ namespace DataVAPI.Tool.IOT } client.Subscribe(new string[] { topic }, new byte[] { 0 }); } + + public void SetSub(List str) + { + str?.ForEach(par => + { + if (SubTopicList.Contains(par)) + { + SubTopicList.Add(par); + } + }); + } #endregion #region 私有函数 @@ -358,6 +369,8 @@ namespace DataVAPI.Tool.IOT SubTopicList.ForEach(par => { client.Subscribe(new string[] { par }, new byte[] { 0 }); + UNConnectMqtt?.Invoke($"订阅主题{par}!"); + }); UNConnectMqtt?.Invoke("重新连接阿里云MQTT成功!"); } diff --git a/DataVAPI/Controllers/DeviceController.cs b/DataVAPI/Controllers/DeviceController.cs index 36b0241..f457189 100644 --- a/DataVAPI/Controllers/DeviceController.cs +++ b/DataVAPI/Controllers/DeviceController.cs @@ -1,6 +1,7 @@ using DataVAPI.Model; using DataVAPI.ModelDataBus; using DataVAPI.ServerDB.MongoDB; +using DataVAPI.UpAndDown; using Microsoft.AspNetCore.Mvc; using System; using System.Collections.Generic; @@ -198,5 +199,22 @@ namespace DataVAPI.Controllers return JsonMsg.Error(null, st, ex.Message); } } + /// + /// 刷新设备 + /// + [HttpGet] + public JsonMsg FlushedDevice() + { + st = System.Reflection.MethodBase.GetCurrentMethod().Name; + try + { + ProcessServer.Instance.LoadingShopInformation(); + return JsonMsg.OK("success!", st); + } + catch (Exception ex) + { + return JsonMsg.Error(null, st, ex.Message); + } + } } } diff --git a/DataVAPI/Controllers/LogController.cs b/DataVAPI/Controllers/LogController.cs index dc9cb4f..ce65d4e 100644 --- a/DataVAPI/Controllers/LogController.cs +++ b/DataVAPI/Controllers/LogController.cs @@ -2,6 +2,7 @@ using DataVAPI.ModelDataBus; using DataVAPI.ServerDB.MongoDB; using DataVAPI.Tool.IOT; +using DataVAPI.UpAndDown; using Microsoft.AspNetCore.Mvc; using System; using System.Collections.Generic; @@ -256,5 +257,32 @@ namespace DataVAPI.Controllers return JsonMsg.Error(null, st, ex.Message); } } + + /// + /// 查询设备所有文件 + /// + /// + /// + [HttpGet] + public JsonMsg QueryLogFile(string DeviceName) + { + st = System.Reflection.MethodBase.GetCurrentMethod().Name; + try + { + if (string.IsNullOrEmpty(DeviceName)) + { + return JsonMsg.Error(null, st, "设备名称不能为空"); + } + else + { + return JsonMsg.OK(Sample.FileQueryALL(DeviceName), st); + } + } + catch (Exception ex) + { + return JsonMsg.Error(null, st, ex.Message); + } + } + } } diff --git a/DataVAPI/DataVAPI.csproj b/DataVAPI/DataVAPI.csproj index 97aadc5..369e489 100644 --- a/DataVAPI/DataVAPI.csproj +++ b/DataVAPI/DataVAPI.csproj @@ -13,12 +13,17 @@ + + + + + diff --git a/DataVAPI/UpAndDown/ProcessServer.cs b/DataVAPI/UpAndDown/ProcessServer.cs index f622797..451753c 100644 --- a/DataVAPI/UpAndDown/ProcessServer.cs +++ b/DataVAPI/UpAndDown/ProcessServer.cs @@ -61,14 +61,14 @@ namespace DataVAPI.UpAndDown LoadingShopInformation(); //MQTT 数据接收处理 ConsoleHelper.WriteInfoLine("尝试连接阿里云."); + //设置变量 + IOTDevServer.GetInstance().Set(DataBus.ProductKey, DataBus.DeviceName, DataBus.DeviceSecret); + IOTDevServer.GetInstance().SetSub(new List { IOTDevServer.HeartbeatSubTopic, IOTDevServer.BroadcastTopic }); + if (IOTDevServer.GetInstance().CreateLinks(DataBus.ProductKey, DataBus.DeviceName, DataBus.DeviceSecret)) { - //属性状态变xin - //Subscribe(IOTDevServer.TargetStatusSubTopic); - //订阅上下线 Subscribe(IOTDevServer.HeartbeatSubTopic); - //订阅主播消息 Subscribe(IOTDevServer.BroadcastTopic); ConsoleHelper.WriteSuccessLine($"阿里云【Transit】连接成功"); @@ -104,18 +104,17 @@ namespace DataVAPI.UpAndDown if (IOTDevServer.GetInstance().CreateLinks(DataBus.ProductKey, DataBus.DeviceName, DataBus.DeviceSecret)) { - ConsoleHelper.WriteSuccessLine($"阿里云【Transit】连接成功"); + ConsoleHelper.WriteSuccessLine($"阿里云【Transit】重新连接成功"); //订阅上下线 Subscribe(IOTDevServer.HeartbeatSubTopic); - //订阅主播消息 Subscribe(IOTDevServer.BroadcastTopic); } else { - ConsoleHelper.WriteSuccessLine($"阿里云【Transit】连接失败"); + ConsoleHelper.WriteSuccessLine($"阿里云【Transit】重新连接失败"); } - Thread.Sleep(10000); + Thread.Sleep(360000); } } diff --git a/DataVAPI/UpAndDown/Sample.cs b/DataVAPI/UpAndDown/Sample.cs new file mode 100644 index 0000000..eb7d10c --- /dev/null +++ b/DataVAPI/UpAndDown/Sample.cs @@ -0,0 +1,105 @@ +using DataVAPI.ModelDataBus; +using DataVAPI.Tool.IOT; +using System.Collections.Generic; +using Tea; +using static AlibabaCloud.SDK.Iot20180120.Models.QueryDeviceFileResponseBody; + +namespace DataVAPI.UpAndDown +{ + public class Sample + { + + /** + * 使用AK&SK初始化账号Client + * @param accessKeyId + * @param accessKeySecret + * @return Client + * @throws Exception + */ + public static AlibabaCloud.SDK.Iot20180120.Client CreateClient(string accessKeyId, string accessKeySecret) + { + AlibabaCloud.OpenApiClient.Models.Config config = new AlibabaCloud.OpenApiClient.Models.Config + { + // 您的AccessKey ID + AccessKeyId = accessKeyId, + // 您的AccessKey Secret + AccessKeySecret = accessKeySecret, + }; + // 访问的域名 + config.Endpoint = "iot.cn-shanghai.aliyuncs.com"; + return new AlibabaCloud.SDK.Iot20180120.Client(config); + } + + public static List FileQueryALL(string DeviceName) + { + List retdata=new List(); + AlibabaCloud.SDK.Iot20180120.Client client = CreateClient("LTAI5tDbUDuCvqU5hP8ZpHHV", "SvNIRQuz3BQFEaC3PFSJdgpeOKWFRi"); + AlibabaCloud.SDK.Iot20180120.Models.QueryDeviceFileListRequest queryDeviceFileListRequest = new AlibabaCloud.SDK.Iot20180120.Models.QueryDeviceFileListRequest + { + IotInstanceId = "iot-06z00g9pf3kwtxp", + ProductKey = DataBus.ProductKey, + DeviceName = DeviceName, + }; + AlibabaCloud.TeaUtil.Models.RuntimeOptions runtime = new AlibabaCloud.TeaUtil.Models.RuntimeOptions(); + AlibabaCloud.SDK.Iot20180120.Models.QueryDeviceFileListResponse resp = client.QueryDeviceFileListWithOptions(queryDeviceFileListRequest, runtime); + //AlibabaCloud.TeaConsole.Client.Log(); + //; + AlibabaCloudRes cloudRes= Tools.JsonToObjectTools(AlibabaCloud.TeaUtil.Common.ToJSONString(resp.ToMap())); + cloudRes?.body?.Data?.FileSummary?.ForEach(fileSummary => + { + QueryDeviceFileResponseBodyData _data= FileQuery(DeviceName,fileSummary.FileId); + if (_data != null) + { + retdata.Add(_data); + } + }); + return retdata; + } + + public static QueryDeviceFileResponseBodyData FileQuery(string DeviceName,string FileId) + { + AlibabaCloud.SDK.Iot20180120.Client client = CreateClient("LTAI5tDbUDuCvqU5hP8ZpHHV", "SvNIRQuz3BQFEaC3PFSJdgpeOKWFRi"); + AlibabaCloud.SDK.Iot20180120.Models.QueryDeviceFileRequest queryDeviceFileRequest = new AlibabaCloud.SDK.Iot20180120.Models.QueryDeviceFileRequest + { + IotInstanceId = "iot-06z00g9pf3kwtxp", + DeviceName = DeviceName, + ProductKey = DataBus.ProductKey, + FileId = FileId, + }; + AlibabaCloud.TeaUtil.Models.RuntimeOptions runtime = new AlibabaCloud.TeaUtil.Models.RuntimeOptions(); + // 复制代码运行请自行打印 API 的返回值 + QueryDeviceFileResponseBodyData queryDeviceFile= client.QueryDeviceFileWithOptions(queryDeviceFileRequest, runtime).Body.Data; + return queryDeviceFile; + } + + } + + public class AlibabaCloudRes + { + public object headers { get; set; } + public StrRes body { get; set; } + } + public class StrRes + { + public string code { get; set; } + public string CurrentPage { get; set; } + public ResData Data { get; set; } + public string ErrorMessage { get; set; } + public int PageCount { get; set; } + public int PageSize { get; set; } + public string RequestId { get; set; } + public bool Success { get; set; } + public int Total { get; set; } + } + public class ResData + { + public List FileSummary { get; set; } + } + public class summary + { + public string UtcCreatedOn { get; set; } + public string Size { get; set; } + public string FileId { get; set; } + public string Name { get; set; } + } +}