DeviceActionExcutor.cs 2.8 KB
Newer Older
W
wq1234wq 已提交
1 2
using System;
using System.Collections.Generic;
W
wq1234wq 已提交
3
using System.ComponentModel;
W
wq1234wq 已提交
4
using System.Linq;
W
wq1234wq 已提交
5
using System.Net;
W
wq1234wq 已提交
6 7
using System.Text;
using System.Threading.Tasks;
W
wq1234wq 已提交
8 9 10
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using RestSharp;
W
wq1234wq 已提交
11 12 13

namespace IoTSharp.TaskAction
{
W
wq1234wq 已提交
14 15
    [DisplayName("用于设备行为推送的执行器")]
    public class DeviceActionExcutor : ITaskAction
W
wq1234wq 已提交
16
    {
W
wq1234wq 已提交
17 18 19 20 21 22
        public DeviceActionExcutor()
        {


        }

23
        public override async Task<TaskActionOutput> ExecuteAsync(TaskActionInput input)
W
wq1234wq 已提交
24 25 26 27
        {
            var config = JsonConvert.DeserializeObject<ModelExecutorConfig>(input.ExecutorConfig);
            string contentType = "application/json";
            var restclient = new RestClient(config.BaseUrl);
28 29
            restclient.AddDefaultHeader(KnownHeaders.Accept, "*/*");
            var request = new RestRequest(config.Url + (input.DeviceId == Guid.Empty ? "" : "/" + input.DeviceId));
W
wq1234wq 已提交
30 31 32
            request.AddHeader("X-Access-Token",
                config.Token);
            request.RequestFormat = DataFormat.Json;
W
wq1234wq 已提交
33
            request.AddHeader("Content-Type", contentType);
W
wq1234wq 已提交
34
            request.AddHeader("cache-control", "no-cache");
35
            request.AddJsonBody(new{ sosType="1", sosContent= input.Input, usingUserId= "" });
36
            var response = await restclient.ExecutePostAsync(request);
W
wq1234wq 已提交
37 38 39
            if (response.StatusCode == HttpStatusCode.OK)
            {
                var result = JsonConvert.DeserializeObject<DeviceActionResult>(response.Content);
W
wq1234wq 已提交
40
                if (result is {success: true})
W
wq1234wq 已提交
41
                {
W
wq1234wq 已提交
42
                    return new TaskActionOutput() { ExecutionInfo = response.Content, ExecutionStatus = result.success, DynamicOutput = input.DynamicInput }; ;
W
wq1234wq 已提交
43 44 45
                }
                else
                {
W
wq1234wq 已提交
46
                    return new TaskActionOutput() { ExecutionInfo = response.Content, ExecutionStatus = false }; ;
W
wq1234wq 已提交
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
                }
            }
            else
            {
                return new TaskActionOutput() { ExecutionInfo = $"StatusCode:{response.StatusCode  } StatusDescription:{response.StatusDescription}  {response.ErrorMessage}", ExecutionStatus = false }; ;
            }

        }
        class DeviceActionResult
        {


            public bool success { get; set; }
            public string message { get; set; }
            public string code { get; set; }

            public long timestamp { get; set; }

            public dynamic result { get; set; }


        }

        class ModelExecutorConfig
        {
            public string Url { get; set; }
            public string BaseUrl { get; set; }
            public string Method { get; set; }
            public string Passwoid { get; set; }
            public string UserName { get; set; }
            public string Token { get; set; }

        }
W
wq1234wq 已提交
80 81
    }
}