唐召明
authored
|
1
|
using HHECS.BllModel;
|
唐召明
authored
|
2
|
using HHECS.DAQClient.Dto;
|
唐召明
authored
|
3
|
using HHECS.DAQClient.Model;
|
唐召明
authored
|
4
5
|
using System.IO.Compression;
using System.IO;
|
唐召明
authored
|
6
7
8
|
using System.Net.Http;
using System.Text;
using System.Text.Json;
|
唐召明
authored
|
9
|
using System.Net.Http.Headers;
|
唐召明
authored
|
10
|
|
唐召明
authored
|
11
|
namespace HHECS.DAQClient.Services
|
唐召明
authored
|
12
13
14
15
16
|
{
internal class HttpService
{
private readonly HttpClient _httpClient;
|
唐召明
authored
|
17
|
private readonly JsonSerializerOptions jsonSerializeOptions = new JsonSerializerOptions
|
唐召明
authored
|
18
19
|
{
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
|
唐召明
authored
|
20
21
22
23
24
25
|
PropertyNameCaseInsensitive = true
};
private readonly JsonSerializerOptions jsonDeserializeOptions = new JsonSerializerOptions
{
PropertyNameCaseInsensitive = true
|
唐召明
authored
|
26
27
28
29
30
31
32
33
34
35
|
};
public HttpService(HttpClient httpClient)
{
_httpClient = httpClient;
}
/// <summary>
/// 推送设备实时数据
/// </summary>
|
唐召明
authored
|
36
|
/// <param name="equipmentDataQueues"></param>
|
唐召明
authored
|
37
|
public BllResult SendEquipmentData(IEnumerable<EquipmentDataQueue> equipmentDataQueues)
|
唐召明
authored
|
38
|
{
|
唐召明
authored
|
39
|
try
|
唐召明
authored
|
40
|
{
|
唐召明
authored
|
41
42
|
var data = equipmentDataQueues.Select(x => new EquipmentDataDto
{
|
唐召明
authored
|
43
|
Plmeid = x.Id == Guid.Empty ? Guid.NewGuid() : x.Id,
|
唐召明
authored
|
44
45
46
47
48
49
50
|
EquipmentSN = x.EquipmentCode,
Reported = JsonSerializer.Deserialize<List<TagItem>>(x.Reported),
Version = x.Version,
Timestamp = x.SourceTimestamp,
}).ToList();
var json = JsonSerializer.Serialize(data, jsonSerializeOptions);
|
唐召明
authored
|
51
52
53
54
55
56
|
var compressedData = CompressString(json);
var content = new ByteArrayContent(compressedData);
// 设置请求头,指明内容是GZip压缩的
content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
content.Headers.ContentEncoding.Add("gzip");
var result = _httpClient.PostAsync("Equipment/SendEquipmentData", content).Result;
|
唐召明
authored
|
57
58
59
|
var resultContent = result.Content.ReadAsStringAsync().Result;
if (result.IsSuccessStatusCode)
{
|
唐召明
authored
|
60
|
return JsonSerializer.Deserialize<BllResult>(resultContent, jsonDeserializeOptions);
|
唐召明
authored
|
61
|
}
|
唐召明
authored
|
62
63
|
if (string.IsNullOrEmpty(resultContent))
|
唐召明
authored
|
64
|
{
|
唐召明
authored
|
65
|
resultContent = result.ReasonPhrase;
|
唐召明
authored
|
66
|
}
|
唐召明
authored
|
67
|
return BllResultFactory.Error($"推送失败:{resultContent}");
|
唐召明
authored
|
68
69
70
71
|
}
catch (Exception ex)
{
return BllResultFactory.Error(ex.Message);
|
唐召明
authored
|
72
73
|
}
}
|
唐召明
authored
|
74
|
|
唐召明
authored
|
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
|
/// <summary>
/// 推送某一时间段的数据
/// </summary>
/// <param name="equipmentDataQueues"></param>
/// <returns></returns>
public BllResult SendEquipmentDataV2(IEnumerable<EquipmentDataQueue> equipmentDataQueues)
{
try
{
var data = equipmentDataQueues.Select(x => new EquipmentDataV2Dto
{
Plmeid = x.Id,
EquipmentSN = x.EquipmentCode,
Reported = JsonSerializer.Deserialize<List<TagItem>>(x.Reported),
Version = x.Version,
TimestampStart = x.SourceTimestamp,
TimestampEnd = (long)(x.SourceTimestamp + (x.Updated.Value - x.Created.Value).TotalMilliseconds),
});
var json = JsonSerializer.Serialize(data, jsonSerializeOptions);
|
唐召明
authored
|
94
95
96
97
98
99
|
var compressedData = CompressString(json);
var content = new ByteArrayContent(compressedData);
// 设置请求头,指明内容是GZip压缩的
content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
content.Headers.ContentEncoding.Add("gzip");
var result = _httpClient.PostAsync("Equipment/SendEquipmentDataV2", content).Result;
|
唐召明
authored
|
100
101
102
103
104
|
var resultContent = result.Content.ReadAsStringAsync().Result;
if (result.IsSuccessStatusCode)
{
return JsonSerializer.Deserialize<BllResult>(resultContent, jsonDeserializeOptions);
}
|
唐召明
authored
|
105
106
|
if (string.IsNullOrEmpty(resultContent))
|
唐召明
authored
|
107
|
{
|
唐召明
authored
|
108
|
resultContent = result.ReasonPhrase;
|
唐召明
authored
|
109
|
}
|
唐召明
authored
|
110
|
return BllResultFactory.Error($"推送失败:{resultContent}");
|
唐召明
authored
|
111
112
113
114
115
116
117
|
}
catch (Exception ex)
{
return BllResultFactory.Error(ex.Message);
}
}
|
唐召明
authored
|
118
119
120
121
122
123
124
125
126
|
public BllResult UpdateClientStatus(Guid clientId)
{
try
{
var data = new
{
ClientId = clientId,
};
var json = JsonSerializer.Serialize(data, jsonSerializeOptions);
|
唐召明
authored
|
127
128
129
130
131
132
133
|
var compressedData = CompressString(json);
var temp = DecompressString(compressedData);
var content = new ByteArrayContent(compressedData);
// 设置请求头,指明内容是GZip压缩的
content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
content.Headers.ContentEncoding.Add("gzip");
var result = _httpClient.PostAsync("Equipment/UpdateClientStatus", content).Result;
|
唐召明
authored
|
134
135
136
137
138
|
var resultContent = result.Content.ReadAsStringAsync().Result;
if (result.IsSuccessStatusCode)
{
return JsonSerializer.Deserialize<BllResult>(resultContent, jsonDeserializeOptions);
}
|
唐召明
authored
|
139
140
|
if (string.IsNullOrEmpty(resultContent))
|
唐召明
authored
|
141
|
{
|
唐召明
authored
|
142
|
resultContent = result.ReasonPhrase;
|
唐召明
authored
|
143
|
}
|
唐召明
authored
|
144
|
return BllResultFactory.Error($"推送失败:{resultContent}");
|
唐召明
authored
|
145
146
147
148
149
150
|
}
catch (Exception ex)
{
return BllResultFactory.Error(ex.Message);
}
}
|
唐召明
authored
|
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
|
/// <summary>
/// 数据压缩
/// </summary>
/// <param name="text"></param>
/// <returns></returns>
public static byte[] CompressString(string text)
{
byte[] buffer = Encoding.UTF8.GetBytes(text);
using var memoryStream = new MemoryStream();
using (var gZipStream = new GZipStream(memoryStream, CompressionMode.Compress, true))
{
gZipStream.Write(buffer, 0, buffer.Length);
}
return memoryStream.ToArray();
}
public static string DecompressString(byte[] gzip)
{
using var memoryStream = new MemoryStream(gzip);
using var gZipStream = new GZipStream(memoryStream, CompressionMode.Decompress);
using var streamReader = new StreamReader(gZipStream, Encoding.UTF8);
return streamReader.ReadToEnd();
}
|
唐召明
authored
|
175
176
|
}
}
|