唐召明
authored
|
1
|
using HHECS.BllModel;
|
唐召明
authored
|
2
|
using HHECS.DAQClient.Dto;
|
唐召明
authored
|
3
|
using HHECS.DAQClient.Model;
|
唐召明
authored
|
4
5
6
7
8
|
using System.Configuration;
using System.Net.Http;
using System.Text;
using System.Text.Json;
|
唐召明
authored
|
9
|
namespace HHECS.DAQClient.Services
|
唐召明
authored
|
10
11
12
13
14
|
{
internal class HttpService
{
private readonly HttpClient _httpClient;
|
唐召明
authored
|
15
|
private readonly JsonSerializerOptions jsonSerializeOptions = new JsonSerializerOptions
|
唐召明
authored
|
16
17
|
{
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
|
唐召明
authored
|
18
19
20
21
22
23
|
PropertyNameCaseInsensitive = true
};
private readonly JsonSerializerOptions jsonDeserializeOptions = new JsonSerializerOptions
{
PropertyNameCaseInsensitive = true
|
唐召明
authored
|
24
25
26
27
|
};
public HttpService(HttpClient httpClient)
{
|
唐召明
authored
|
28
29
30
31
32
|
//HttpClientHandler clientHandler = new HttpClientHandler();
//clientHandler.ServerCertificateCustomValidationCallback += (sender, cert, chain, sslPolicyErrors) => { return true; };
//clientHandler.SslProtocols = SslProtocols.None;
//httpClient = new HttpClient(clientHandler);
|
唐召明
authored
|
33
34
|
httpClient.BaseAddress = new Uri(ConfigurationManager.AppSettings["API"]!);
httpClient.DefaultRequestHeaders.Add("Accept", "application/json");
|
唐召明
authored
|
35
|
httpClient.DefaultRequestHeaders.Add("User-Agent", "HHECS.DAQClient");
|
唐召明
authored
|
36
37
38
39
40
41
42
|
_httpClient = httpClient;
}
/// <summary>
/// 推送设备实时数据
/// </summary>
/// <param name="entity"></param>
|
唐召明
authored
|
43
|
public BllResult SendEquipmentData(IEnumerable<EquipmentDataQueue> equipmentDataQueues)
|
唐召明
authored
|
44
|
{
|
唐召明
authored
|
45
|
try
|
唐召明
authored
|
46
|
{
|
唐召明
authored
|
47
48
|
var data = equipmentDataQueues.Select(x => new EquipmentDataDto
{
|
唐召明
authored
|
49
|
Plmeid = x.Id == Guid.Empty ? Guid.NewGuid() : x.Id,
|
唐召明
authored
|
50
51
52
53
54
55
56
|
EquipmentSN = x.EquipmentCode,
Reported = JsonSerializer.Deserialize<List<TagItem>>(x.Reported),
Version = x.Version,
Timestamp = x.SourceTimestamp,
}).ToList();
var json = JsonSerializer.Serialize(data, jsonSerializeOptions);
|
唐召明
authored
|
57
|
var stringContent = new StringContent(json, Encoding.UTF8, "application/json");
|
唐召明
authored
|
58
|
var result = _httpClient.PostAsync("Equipment/SendEquipmentData", stringContent).Result;
|
唐召明
authored
|
59
60
61
|
var resultContent = result.Content.ReadAsStringAsync().Result;
if (result.IsSuccessStatusCode)
{
|
唐召明
authored
|
62
|
return JsonSerializer.Deserialize<BllResult>(resultContent, jsonDeserializeOptions);
|
唐召明
authored
|
63
64
|
}
else
|
唐召明
authored
|
65
|
{
|
唐召明
authored
|
66
67
68
69
70
|
if (string.IsNullOrEmpty(resultContent))
{
resultContent = result.ReasonPhrase;
}
return BllResultFactory.Error($"推送失败:{resultContent}");
|
唐召明
authored
|
71
|
}
|
唐召明
authored
|
72
73
74
75
|
}
catch (Exception ex)
{
return BllResultFactory.Error(ex.Message);
|
唐召明
authored
|
76
77
78
79
|
}
}
}
}
|