IotService.cs
5.33 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
using DataAcquisition.Common.Enums;
using DataAcquisition.Models;
using DataAcquisition.ViewModels.IOT;
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";
public IotService(IHttpClientFactory httpClientFactory)
{
_httpClientFactory = httpClientFactory;
}
/// <summary>
/// 设备数据
/// </summary>
/// <param name="equipments"></param>
/// <returns></returns>
public IotResult<string> SendEquipmentData(IEnumerable<Equipment> equipments)
{
var data = new IotEquipmentDataDto
{
Equipments = equipments.Where(x => EquipmentCodes.Contains(x.Code)).Select(x => new EquipmentItem
{
EquipmentSN = x.EquipmentSN,
Tags = x.EquipmentProperties.Select(p => new TagItem
{
Tag = p.DataAddress,
Value = p.Value ?? string.Empty,
SourceTimestamp = ConvertToTimestamp(p.UpdateTime),
}).ToList(),
}).ToList(),
Version = 1,
Timestamp = ConvertToTimestamp(DateTime.Now)
};
return Post("api/Equipment/SendEquipmentData", data);
}
/// <summary>
/// 设备报警
/// </summary>
/// <param name="equipmentAlarm"></param>
/// <returns></returns>
public IotResult<string> SendEquipmentAlarm(IotEquipmentAlarm equipmentAlarm)
{
return Post("", equipmentAlarm);
}
/// <summary>
/// 设备运行参数
/// </summary>
/// <param name="equipmmentRunParameter"></param>
/// <returns></returns>
public IotResult<string> SendEquipmentRunParameter(IotEquipmmentRunParameter equipmmentRunParameter)
{
return Post("", equipmmentRunParameter);
}
/// <summary>
/// 设备状态
/// </summary>
/// <param name="equipmentStatus"></param>
/// <returns></returns>
public IotResult<string> SendEquipmentStatus(IotEquipmentStatus equipmentStatus)
{
return Post("", equipmentStatus);
}
public IotResult<string> SyncEquipment(IEnumerable<Equipment> equipments)
{
var data = equipments.Where(x => EquipmentCodes.Contains(x.Code)).Select(x => new IotEquipmentDto
{
Code = x.Code,
Name = x.Name,
EquipmentSN = x.EquipmentSN,
Type = x.EquipmentType.ToString(),
Remark = x.Remark ?? string.Empty,
EquipmentProperties = x.EquipmentProperties.Select(p => new EquipmentPropertyDto
{
Code = p.Code,
Name = p.Name,
Tag = p.DataAddress,
Value = p.Value,
Remark = p.Remark,
}).ToList(),
WarehouseCode = x.WarehouseCode,
}).OrderBy(x => x.Code).ToList();
return Post("api/Equipment/SyncEquipment", data);
}
private IotResult<string> Post<T>(string requestUri, T data)
{
try
{
var client = _httpClientFactory.CreateClient(identity);
var json = JsonSerializer.Serialize(data, JsonSerializerOptions);
var responseMessage = client.PostAsync(requestUri, new StringContent(json, Encoding.UTF8)).GetAwaiter().GetResult();
if (!responseMessage.IsSuccessStatusCode)
{
return new IotResult<string>
{
Code = (int)responseMessage.StatusCode,
Status = false,
Message = responseMessage.ReasonPhrase ?? string.Empty,
};
}
var responseContent = responseMessage.Content.ReadAsStringAsync().GetAwaiter().GetResult();
return JsonSerializer.Deserialize<IotResult<string>>(responseContent)!;
}
catch (Exception ex)
{
return new IotResult<string>
{
Code = 500,
Message = ex.Message,
Status = false,
};
}
}
private static long ConvertToTimestamp(DateTime? dateTime)
{
if (dateTime == null)
{
return 0;
}
return Convert.ToInt64(((DateTime)dateTime - new DateTime(1970, 1, 1)).TotalSeconds);
}
private static List<string> EquipmentCodes => new List<EquipmentConst>
{
EquipmentConst.Kuka_1,
EquipmentConst.Kuka_2,
EquipmentConst.Kuka_3,
EquipmentConst.Kuka_4,
EquipmentConst.Efort_1,
EquipmentConst.Fanuc_1,
}.Select(x => x.ToString()).ToList();
private static readonly JsonSerializerOptions JsonSerializerOptions = new JsonSerializerOptions
{
ReferenceHandler = ReferenceHandler.IgnoreCycles,
};
}
}