Blame view

Services/IotService.cs 3.75 KB
唐召明 authored
1
using DataAcquisition.Models;
唐召明 authored
2
using DataAcquisition.ViewModels.IOT;
唐召明 authored
3
using HHECS.BllModel;
唐召明 authored
4
5
using System.Text;
using System.Text.Json;
唐召明 authored
6
using System.Text.Json.Serialization;
唐召明 authored
7
唐召明 authored
8
namespace DataAcquisition.Services
唐召明 authored
9
10
11
12
13
14
{
    public class IotService
    {
        private readonly IHttpClientFactory _httpClientFactory;

        private readonly string identity = "iot";
唐召明 authored
15
        private readonly Guid _clientId;
唐召明 authored
16
唐召明 authored
17
        public IotService(IHttpClientFactory httpClientFactory, IConfiguration configuration)
唐召明 authored
18
19
        {
            _httpClientFactory = httpClientFactory;
唐召明 authored
20
            _clientId = configuration.GetValue<Guid>("ClientId");
唐召明 authored
21
22
23
24
25
        }

        /// <summary>
        /// 设备数据
        /// </summary>
唐召明 authored
26
        /// <param name="queues"></param>
唐召明 authored
27
        /// <returns></returns>
唐召明 authored
28
        public BllResult SendEquipmentData(IEnumerable<EquipmentDataQueue> queues)
唐召明 authored
29
        {
唐召明 authored
30
            var data = queues.Select(x => new IotEquipmentDataDto
唐召明 authored
31
            {
唐召明 authored
32
                Plmeid = x.Id == Guid.Empty ? Guid.NewGuid() : x.Id,
唐召明 authored
33
34
35
36
37
38
                EquipmentSN = x.EquipmentCode,
                Reported = JsonSerializer.Deserialize<List<TagItem>>(x.Reported) ?? new List<TagItem>(),
                Timestamp = x.SourceTimestamp,
                Version = x.Version,
            }).ToList();
            return Post("Equipment/SendEquipmentData", data);
唐召明 authored
39
40
        }
唐召明 authored
41
42
43
44
45
46
47
48
49
        public BllResult UpdateClientStatus()
        {
            var data = new
            {
                ClientId = _clientId
            };
            return Post("Equipment/UpdateClientStatus", data);
        }
唐召明 authored
50
51
52
53
54
        /// <summary>
        /// 设备报警
        /// </summary>
        /// <param name="equipmentAlarm"></param>
        /// <returns></returns>
唐召明 authored
55
        public BllResult SendEquipmentAlarm(IotEquipmentAlarm equipmentAlarm)
唐召明 authored
56
57
58
59
60
61
62
63
64
        {
            return Post("", equipmentAlarm);
        }

        /// <summary>
        /// 设备运行参数
        /// </summary>
        /// <param name="equipmmentRunParameter"></param>
        /// <returns></returns>
唐召明 authored
65
        public BllResult SendEquipmentRunParameter(IotEquipmmentRunParameter equipmmentRunParameter)
唐召明 authored
66
67
68
69
70
71
72
73
74
        {
            return Post("", equipmmentRunParameter);
        }

        /// <summary>
        /// 设备状态
        /// </summary>
        /// <param name="equipmentStatus"></param>
        /// <returns></returns>
唐召明 authored
75
        public BllResult SendEquipmentStatus(IotEquipmentStatus equipmentStatus)
唐召明 authored
76
77
78
79
        {
            return Post("", equipmentStatus);
        }
唐召明 authored
80
        private BllResult Post<T>(string requestUri, T data)
唐召明 authored
81
82
83
84
        {
            try
            {
                var client = _httpClientFactory.CreateClient(identity);
唐召明 authored
85
86
                var json = JsonSerializer.Serialize(data, JsonSerializeOptions);
                var responseMessage = client.PostAsync(requestUri, new StringContent(json, Encoding.UTF8, "application/json")).GetAwaiter().GetResult();
唐召明 authored
87
88
                if (!responseMessage.IsSuccessStatusCode)
                {
唐召明 authored
89
                    return BllResultFactory.Error(responseMessage.ReasonPhrase ?? string.Empty);
唐召明 authored
90
91
                }
                var responseContent = responseMessage.Content.ReadAsStringAsync().GetAwaiter().GetResult();
唐召明 authored
92
                return JsonSerializer.Deserialize<BllResult>(responseContent, JsonDeserializeOptions)!;
唐召明 authored
93
94
95
            }
            catch (Exception ex)
            {
唐召明 authored
96
                return BllResultFactory.Error(ex.Message);
唐召明 authored
97
98
            }
        }
唐召明 authored
99
唐召明 authored
100
        private static readonly JsonSerializerOptions JsonSerializeOptions = new JsonSerializerOptions
唐召明 authored
101
        {
唐召明 authored
102
103
            ReferenceHandler = ReferenceHandler.IgnoreCycles,
        };
唐召明 authored
104
唐召明 authored
105
        private static readonly JsonSerializerOptions JsonDeserializeOptions = new JsonSerializerOptions
唐召明 authored
106
        {
唐召明 authored
107
            PropertyNameCaseInsensitive = true,
唐召明 authored
108
        };
唐召明 authored
109
110
    }
}