From 59818722a892c51aa374db1bbfc25126b1ff52ca Mon Sep 17 00:00:00 2001 From: MysticBoy Date: Tue, 29 Mar 2022 01:17:25 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=A0=E9=99=A4=E4=B8=8D=E5=BF=85=E8=A6=81?= =?UTF-8?q?=E7=9A=84=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- IoTSharp.TaskAction/Air720Sx.cs | 30 --- IoTSharp.TaskAction/DeviceActionExcutor.cs | 33 +--- IoTSharp.TaskAction/ITaskAction.cs | 7 +- IoTSharp.TaskAction/MessagePullExcutor.cs | 108 +++-------- .../TelemetryArrayPullExcutor.cs | 183 +++++------------- 5 files changed, 81 insertions(+), 280 deletions(-) delete mode 100644 IoTSharp.TaskAction/Air720Sx.cs diff --git a/IoTSharp.TaskAction/Air720Sx.cs b/IoTSharp.TaskAction/Air720Sx.cs deleted file mode 100644 index 300a7670..00000000 --- a/IoTSharp.TaskAction/Air720Sx.cs +++ /dev/null @@ -1,30 +0,0 @@ -using System; -using System.Collections.Generic; -using System.ComponentModel; -using System.Dynamic; -using System.Linq; -using System.Threading.Tasks; -using IoTSharp.Data; -using IoTSharp.TaskAction; -using Newtonsoft.Json; -using Newtonsoft.Json.Linq; - -namespace IoTSharp.TaskAction -{ - [DisplayName("Air720S解析器")] - public class Air720Sx : ITaskAction - { - - public Air720Sx() - { - - } - - public override Task ExecuteAsync(TaskActionInput _input) - { - var msg = _input.DynamicInput; - var playload = System.Text.Encoding.Default.GetString( Convert.FromBase64String(msg.Payload)); - return Task.FromResult( new TaskActionOutput() { Output=playload, DeviceId=_input.DeviceId}); - } - } -} diff --git a/IoTSharp.TaskAction/DeviceActionExcutor.cs b/IoTSharp.TaskAction/DeviceActionExcutor.cs index 94ba141d..052f571c 100644 --- a/IoTSharp.TaskAction/DeviceActionExcutor.cs +++ b/IoTSharp.TaskAction/DeviceActionExcutor.cs @@ -1,25 +1,15 @@ -using System; -using System.Collections.Generic; +using Newtonsoft.Json; +using RestSharp; +using System; using System.ComponentModel; -using System.Linq; using System.Net; -using System.Text; using System.Threading.Tasks; -using Newtonsoft.Json; -using Newtonsoft.Json.Linq; -using RestSharp; namespace IoTSharp.TaskAction { [DisplayName("用于设备行为推送的执行器")] public class DeviceActionExcutor : ITaskAction { - public DeviceActionExcutor() - { - - - } - public override async Task ExecuteAsync(TaskActionInput input) { var config = JsonConvert.DeserializeObject(input.ExecutorConfig); @@ -32,12 +22,12 @@ namespace IoTSharp.TaskAction request.RequestFormat = DataFormat.Json; request.AddHeader("Content-Type", contentType); request.AddHeader("cache-control", "no-cache"); - request.AddJsonBody(new{ sosType="1", sosContent= input.Input, usingUserId= "" }); + request.AddJsonBody(new { sosType = "1", sosContent = input.Input, usingUserId = "" }); var response = await restclient.ExecutePostAsync(request); if (response.StatusCode == HttpStatusCode.OK) { var result = JsonConvert.DeserializeObject(response.Content); - if (result is {success: true}) + if (result is { success: true }) { return new TaskActionOutput() { ExecutionInfo = response.Content, ExecutionStatus = result.success, DynamicOutput = input.DynamicInput }; ; } @@ -50,12 +40,10 @@ namespace IoTSharp.TaskAction { return new TaskActionOutput() { ExecutionInfo = $"StatusCode:{response.StatusCode } StatusDescription:{response.StatusDescription} {response.ErrorMessage}", ExecutionStatus = false }; ; } - } - class DeviceActionResult - { - + private class DeviceActionResult + { public bool success { get; set; } public string message { get; set; } public string code { get; set; } @@ -63,11 +51,9 @@ namespace IoTSharp.TaskAction public long timestamp { get; set; } public dynamic result { get; set; } - - } - class ModelExecutorConfig + private class ModelExecutorConfig { public string Url { get; set; } public string BaseUrl { get; set; } @@ -75,7 +61,6 @@ namespace IoTSharp.TaskAction public string Passwoid { get; set; } public string UserName { get; set; } public string Token { get; set; } - } } -} +} \ No newline at end of file diff --git a/IoTSharp.TaskAction/ITaskAction.cs b/IoTSharp.TaskAction/ITaskAction.cs index eb7d0888..dce6a169 100644 --- a/IoTSharp.TaskAction/ITaskAction.cs +++ b/IoTSharp.TaskAction/ITaskAction.cs @@ -8,8 +8,7 @@ namespace IoTSharp.TaskAction { public abstract class ITaskAction { - public abstract Task ExecuteAsync(TaskActionInput _input); - + public abstract Task ExecuteAsync(TaskActionInput _input); public IServiceProvider ServiceProvider { get; set; } } @@ -22,6 +21,7 @@ namespace IoTSharp.TaskAction public Guid DeviceId { get; set; } public bool ExecutionStatus { get; set; } public string ExecutionInfo { get; set; } + public dynamic DynamicOutput { get @@ -54,8 +54,9 @@ namespace IoTSharp.TaskAction private dynamic _DynamicOutput; private string _value; private readonly ExpandoObjectConverter expConverter = new(); - public Guid DeviceId { get; set; } + public Guid DeviceId { get; set; } public String ExecutorConfig { get; set; } + public dynamic DynamicInput { get diff --git a/IoTSharp.TaskAction/MessagePullExcutor.cs b/IoTSharp.TaskAction/MessagePullExcutor.cs index 78765aa6..6e76ad4a 100644 --- a/IoTSharp.TaskAction/MessagePullExcutor.cs +++ b/IoTSharp.TaskAction/MessagePullExcutor.cs @@ -1,64 +1,35 @@ -using System; -using System.Collections.Generic; +using Newtonsoft.Json; +using Newtonsoft.Json.Linq; +using RestSharp; +using System; using System.ComponentModel; -using System.Dynamic; using System.Linq; using System.Net; using System.Threading.Tasks; -using Castle.Components.DictionaryAdapter; -using IoTSharp.Data; -using IoTSharp.TaskAction; -using Microsoft.Extensions.Caching.Memory; -using Newtonsoft.Json; -using Newtonsoft.Json.Linq; -using RestSharp; namespace IoTSharp.TaskAction { [DisplayName("用于消息推送的执行器")] public class MessagePullExecutor : ITaskAction { - - public MessagePullExecutor() - { - - - } - public override Task ExecuteAsync(TaskActionInput input) { try { - // var cache = this.ServiceProvider.GetService(typeof(IMemoryCache)) as IMemoryCache; - - return SendData(input); - + return SendData(input); } catch (Exception ex) { - return Task.FromResult( new TaskActionOutput() { ExecutionInfo = ex.Message, ExecutionStatus = false }); + return Task.FromResult(new TaskActionOutput() { ExecutionInfo = ex.Message, ExecutionStatus = false }); } } - - private void Login(ModelExecutorConfig config) - { - - - - - } - - - private async Task SendData(TaskActionInput input) + private async Task SendData(TaskActionInput input) { try { - if (input.Input.Contains("ValueKind")) { - - JObject o = JsonConvert.DeserializeObject(input.Input) as JObject; var config = JsonConvert.DeserializeObject(input.ExecutorConfig); var dd = o.Properties().Select(c => new ParamObject { keyName = c.Name, value = JPropertyToObject(c.Value.First as JProperty) }).ToList(); @@ -75,7 +46,7 @@ namespace IoTSharp.TaskAction if (response.StatusCode == HttpStatusCode.OK) { var result = JsonConvert.DeserializeObject(response.Content); - if (result is {success: true}) + if (result is { success: true }) { return new TaskActionOutput() { ExecutionInfo = response.Content, ExecutionStatus = result.success, DynamicOutput = input.DynamicInput }; ; } @@ -88,9 +59,6 @@ namespace IoTSharp.TaskAction { return new TaskActionOutput() { ExecutionInfo = response.ErrorMessage, ExecutionStatus = false }; ; } - - - } else { @@ -106,12 +74,12 @@ namespace IoTSharp.TaskAction request.AddHeader("cache-control", "no-cache"); request.AddHeader("Content-Type", "application/json"); request.AddJsonBody(dd); - - var response = await restclient.ExecutePostAsync(request); + + var response = await restclient.ExecutePostAsync(request); if (response.StatusCode == HttpStatusCode.OK) { var result = JsonConvert.DeserializeObject(response.Content); - if (result is {success: true}) + if (result is { success: true }) { return new TaskActionOutput() { ExecutionInfo = response.Content, ExecutionStatus = result.success, DynamicOutput = input.DynamicInput }; ; } @@ -122,14 +90,9 @@ namespace IoTSharp.TaskAction } else { - return new TaskActionOutput() { ExecutionInfo =$"StatusCode:{response.StatusCode } StatusDescription:{response.StatusDescription} {response.ErrorMessage}" , ExecutionStatus = false }; ; + return new TaskActionOutput() { ExecutionInfo = $"StatusCode:{response.StatusCode } StatusDescription:{response.StatusDescription} {response.ErrorMessage}", ExecutionStatus = false }; ; } - } - - - - } catch (Exception ex) { @@ -137,7 +100,6 @@ namespace IoTSharp.TaskAction } } - public static object JPropertyToObject(JProperty property) { object obj = null; @@ -146,30 +108,39 @@ namespace IoTSharp.TaskAction case JTokenType.Integer: obj = property.Value.ToObject(); break; + case JTokenType.Float: obj = property.Value.ToObject(); break; + case JTokenType.String: obj = property.Value.ToObject(); break; + case JTokenType.Boolean: obj = property.Value.ToObject(); break; + case JTokenType.Date: obj = property.Value.ToObject(); break; + case JTokenType.Bytes: obj = property.Value.ToObject(); break; + case JTokenType.Guid: obj = property.Value.ToObject(); break; + case JTokenType.Uri: obj = property.Value.ToObject(); break; + case JTokenType.TimeSpan: obj = property.Value.ToObject(); break; + default: obj = property.Value; break; @@ -177,28 +148,8 @@ namespace IoTSharp.TaskAction return obj; } - class MessagePullParam + private class MessagePullResult { - - - public double param1 { get; set; } - public double param2 { get; set; } - public double param3 { get; set; } - public int param4 { get; set; } - - public long param5 { get; set; } - - - public ParamObject param6 { get; set; } - - public string param9 { get; set; } - } - - - class MessagePullResult - { - - public bool success { get; set; } public string message { get; set; } public string code { get; set; } @@ -206,25 +157,15 @@ namespace IoTSharp.TaskAction public long timestamp { get; set; } public dynamic result { get; set; } - - - } - - - public class LoginParam - { - - } public class ParamObject { - public string keyName { get; set; } public dynamic value { get; set; } } - class ModelExecutorConfig + private class ModelExecutorConfig { public string Url { get; set; } public string BaseUrl { get; set; } @@ -232,7 +173,6 @@ namespace IoTSharp.TaskAction public string Password { get; set; } public string UserName { get; set; } public string Token { get; set; } - } } -} +} \ No newline at end of file diff --git a/IoTSharp.TaskAction/TelemetryArrayPullExcutor.cs b/IoTSharp.TaskAction/TelemetryArrayPullExcutor.cs index d440b933..eb46a7b9 100644 --- a/IoTSharp.TaskAction/TelemetryArrayPullExcutor.cs +++ b/IoTSharp.TaskAction/TelemetryArrayPullExcutor.cs @@ -1,165 +1,87 @@ -using System; -using System.Collections.Generic; +using Newtonsoft.Json; +using RestSharp; +using System; using System.ComponentModel; - -using System.Linq; using System.Net; -using System.Text; using System.Threading.Tasks; -using Newtonsoft.Json; -using Newtonsoft.Json.Linq; -using RestSharp; namespace IoTSharp.TaskAction { - - - [DisplayName("用于遥测数组推送的执行器")] public class TelemetryArrayPullExcutor : ITaskAction { - public TelemetryArrayPullExcutor() { - - } public override Task ExecuteAsync(TaskActionInput input) { try { - // var cache = this.ServiceProvider.GetService(typeof(IMemoryCache)) as IMemoryCache; - return SendData(input); - } catch (Exception ex) { - return Task.FromResult(new TaskActionOutput() {ExecutionInfo = ex.Message, ExecutionStatus = false}); + return Task.FromResult(new TaskActionOutput() { ExecutionInfo = ex.Message, ExecutionStatus = false }); } } - - private void Login(ModelExecutorConfig config) - { - - - - - } - - private async Task SendData(TaskActionInput input) { try { - - - - var config = JsonConvert.DeserializeObject(input.ExecutorConfig); - - var restclient = new RestClient(config.BaseUrl); - restclient.AddDefaultHeader(KnownHeaders.Accept, "*/*"); - var request = - new RestRequest(config.Url + (input.DeviceId == Guid.Empty ? "" : "/" + input.DeviceId)); - request.AddHeader("X-Access-Token", - config.Token); - request.RequestFormat = DataFormat.Json; - request.AddHeader("cache-control", "no-cache"); - request.AddHeader("Content-Type", "application/json"); - request.AddJsonBody(JsonConvert.DeserializeObject(input.Input)); - - var response = await restclient.ExecutePostAsync(request); - if (response.StatusCode == HttpStatusCode.OK) + var config = JsonConvert.DeserializeObject(input.ExecutorConfig); + + var restclient = new RestClient(config.BaseUrl); + restclient.AddDefaultHeader(KnownHeaders.Accept, "*/*"); + var request = + new RestRequest(config.Url + (input.DeviceId == Guid.Empty ? "" : "/" + input.DeviceId)); + request.AddHeader("X-Access-Token", + config.Token); + request.RequestFormat = DataFormat.Json; + request.AddHeader("cache-control", "no-cache"); + request.AddHeader("Content-Type", "application/json"); + request.AddJsonBody(JsonConvert.DeserializeObject(input.Input)); + + var response = await restclient.ExecutePostAsync(request); + if (response.StatusCode == HttpStatusCode.OK) + { + var result = JsonConvert.DeserializeObject(response.Content); + if (result is { success: true }) { - var result = JsonConvert.DeserializeObject(response.Content); - if (result is {success: true}) - { - return new TaskActionOutput() - { - ExecutionInfo = response.Content, ExecutionStatus = result.success, - DynamicOutput = input.DynamicInput - }; - ; - } - else + return new TaskActionOutput() { - return new TaskActionOutput() {ExecutionInfo = response.Content, ExecutionStatus = false}; - ; - } + ExecutionInfo = response.Content, + ExecutionStatus = result.success, + DynamicOutput = input.DynamicInput + }; + ; } else { - return new TaskActionOutput() - { - ExecutionInfo = - $"StatusCode:{response.StatusCode} StatusDescription:{response.StatusDescription} {response.ErrorMessage}", - ExecutionStatus = false - }; - + return new TaskActionOutput() { ExecutionInfo = response.Content, ExecutionStatus = false }; + ; } - - - - - - + } + else + { + return new TaskActionOutput() + { + ExecutionInfo = + $"StatusCode:{response.StatusCode} StatusDescription:{response.StatusDescription} {response.ErrorMessage}", + ExecutionStatus = false + }; + } } catch (Exception ex) { - return new TaskActionOutput() {ExecutionInfo = ex.Message, ExecutionStatus = false}; + return new TaskActionOutput() { ExecutionInfo = ex.Message, ExecutionStatus = false }; ; } - - - - } - public static object JPropertyToObject(JProperty property) + private class MessagePullResult { - object obj = null; - switch (property.Value.Type) - { - case JTokenType.Integer: - obj = property.Value.ToObject(); - break; - case JTokenType.Float: - obj = property.Value.ToObject(); - break; - case JTokenType.String: - obj = property.Value.ToObject(); - break; - case JTokenType.Boolean: - obj = property.Value.ToObject(); - break; - case JTokenType.Date: - obj = property.Value.ToObject(); - break; - case JTokenType.Bytes: - obj = property.Value.ToObject(); - break; - case JTokenType.Guid: - obj = property.Value.ToObject(); - break; - case JTokenType.Uri: - obj = property.Value.ToObject(); - break; - case JTokenType.TimeSpan: - obj = property.Value.ToObject(); - break; - default: - obj = property.Value; - break; - } - return obj; - } - - class MessagePullResult - { - - public bool success { get; set; } public string message { get; set; } public string code { get; set; } @@ -167,25 +89,9 @@ namespace IoTSharp.TaskAction public long timestamp { get; set; } public dynamic result { get; set; } - - - } - - - public class LoginParam - { - - } - public class ParamObject - { - - public string keyName { get; set; } - public dynamic value { get; set; } - } - - class ModelExecutorConfig + private class ModelExecutorConfig { public string Url { get; set; } public string BaseUrl { get; set; } @@ -193,7 +99,6 @@ namespace IoTSharp.TaskAction public string Password { get; set; } public string UserName { get; set; } public string Token { get; set; } - } } -} +} \ No newline at end of file -- GitLab