IotService.cs
3.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
using DataAcquisition.Models;
using DataAcquisition.ViewModels.IOT;
using HHECS.BllModel;
using System.Text;
using System.Text.Json;
using System.Text.Json.Serialization;
namespace DataAcquisition.Services
{
public class IotService
{
private readonly IHttpClientFactory _httpClientFactory;
private readonly string identity = "iot";
private readonly Guid _clientId;
public IotService(IHttpClientFactory httpClientFactory, IConfiguration configuration)
{
_httpClientFactory = httpClientFactory;
_clientId = configuration.GetValue<Guid>("ClientId");
}
/// <summary>
/// 设备数据
/// </summary>
/// <param name="queues"></param>
/// <returns></returns>
public BllResult SendEquipmentData(IEnumerable<EquipmentDataQueue> queues)
{
var data = queues.Select(x => new IotEquipmentDataDto
{
Plmeid = x.Id == Guid.Empty ? Guid.NewGuid() : x.Id,
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);
}
public BllResult UpdateClientStatus()
{
var data = new
{
ClientId = _clientId
};
return Post("Equipment/UpdateClientStatus", data);
}
/// <summary>
/// 设备报警
/// </summary>
/// <param name="equipmentAlarm"></param>
/// <returns></returns>
public BllResult SendEquipmentAlarm(IotEquipmentAlarm equipmentAlarm)
{
return Post("", equipmentAlarm);
}
/// <summary>
/// 设备运行参数
/// </summary>
/// <param name="equipmmentRunParameter"></param>
/// <returns></returns>
public BllResult SendEquipmentRunParameter(IotEquipmmentRunParameter equipmmentRunParameter)
{
return Post("", equipmmentRunParameter);
}
/// <summary>
/// 设备状态
/// </summary>
/// <param name="equipmentStatus"></param>
/// <returns></returns>
public BllResult SendEquipmentStatus(IotEquipmentStatus equipmentStatus)
{
return Post("", equipmentStatus);
}
private BllResult Post<T>(string requestUri, T data)
{
try
{
var client = _httpClientFactory.CreateClient(identity);
var json = JsonSerializer.Serialize(data, JsonSerializeOptions);
var responseMessage = client.PostAsync(requestUri, new StringContent(json, Encoding.UTF8, "application/json")).GetAwaiter().GetResult();
if (!responseMessage.IsSuccessStatusCode)
{
return BllResultFactory.Error(responseMessage.ReasonPhrase ?? string.Empty);
}
var responseContent = responseMessage.Content.ReadAsStringAsync().GetAwaiter().GetResult();
return JsonSerializer.Deserialize<BllResult>(responseContent, JsonDeserializeOptions)!;
}
catch (Exception ex)
{
return BllResultFactory.Error(ex.Message);
}
}
private static readonly JsonSerializerOptions JsonSerializeOptions = new JsonSerializerOptions
{
ReferenceHandler = ReferenceHandler.IgnoreCycles,
};
private static readonly JsonSerializerOptions JsonDeserializeOptions = new JsonSerializerOptions
{
PropertyNameCaseInsensitive = true,
};
}
}