|
...
|
...
|
@@ -19,6 +19,7 @@ import org.apache.http.impl.client.HttpClients; |
|
19
|
19
|
import org.apache.http.util.EntityUtils;
|
|
20
|
20
|
import org.springframework.beans.factory.annotation.Value;
|
|
21
|
21
|
import org.springframework.data.redis.core.RedisTemplate;
|
|
|
22
|
+import org.springframework.jdbc.core.JdbcTemplate;
|
|
22
|
23
|
import org.springframework.stereotype.Service;
|
|
23
|
24
|
import org.springframework.util.CollectionUtils;
|
|
24
|
25
|
import org.springframework.util.LinkedMultiValueMap;
|
|
...
|
...
|
@@ -28,6 +29,7 @@ import org.springframework.web.util.UriComponentsBuilder; |
|
28
|
29
|
import java.io.ByteArrayOutputStream;
|
|
29
|
30
|
import java.io.IOException;
|
|
30
|
31
|
import java.io.InputStream;
|
|
|
32
|
+import java.text.ParseException;
|
|
31
|
33
|
import java.text.SimpleDateFormat;
|
|
32
|
34
|
import java.util.*;
|
|
33
|
35
|
import java.util.concurrent.TimeUnit;
|
|
...
|
...
|
@@ -47,20 +49,91 @@ public class DevicePullService { |
|
47
|
49
|
private String deviceInfoUrl;
|
|
48
|
50
|
@Value("${device.detail.url}")
|
|
49
|
51
|
private String deviceDetailUrl;
|
|
|
52
|
+ @Value("${device.snRate.url}")
|
|
|
53
|
+ private String deviceSnRateUrl;
|
|
50
|
54
|
@Value("${device.energyInfo.url}")
|
|
51
|
55
|
private String energyInfoUrl;
|
|
|
56
|
+ @Value("${device.db.corpCode}")
|
|
|
57
|
+ private String deviceCorpCode;
|
|
|
58
|
+ @Value("${device.db.tableName}")
|
|
|
59
|
+ private String deviceTableName;
|
|
52
|
60
|
|
|
53
|
61
|
@Resource
|
|
54
|
62
|
private RedisTemplate<String, String> redisTemplate;
|
|
|
63
|
+ @Resource
|
|
|
64
|
+ private JdbcTemplate jdbcTemplate;
|
|
55
|
65
|
|
|
56
|
66
|
final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
|
57
|
67
|
|
|
58
|
|
- public void pullDeviceAndPushToIot() {
|
|
|
68
|
+ public void pullDeviceAndPushToIot() throws ParseException {
|
|
59
|
69
|
String deviceResult = getDeviceInfo();
|
|
60
|
70
|
Map<String, Object> deviceInfos = JSON.parseObject(deviceResult, new TypeReference<>() {
|
|
61
|
71
|
});
|
|
62
|
72
|
|
|
63
|
73
|
JSONArray deviceInfoList = (JSONArray) deviceInfos.get("data");
|
|
|
74
|
+ for (Object o : deviceInfoList) {
|
|
|
75
|
+ JSONObject deviceInfoJson = (JSONObject) o;
|
|
|
76
|
+
|
|
|
77
|
+ String projectState = deviceInfoJson.getString("projectState");
|
|
|
78
|
+ String projectType = deviceInfoJson.getString("projectType");
|
|
|
79
|
+ String deviceName = deviceInfoJson.getString("deviceName");
|
|
|
80
|
+ String dtuId = deviceInfoJson.getString("dtuId");
|
|
|
81
|
+ String deviceId = deviceInfoJson.getString("deviceId");
|
|
|
82
|
+ String dtuSn = deviceInfoJson.getString("dtuSn");
|
|
|
83
|
+
|
|
|
84
|
+ String deviceInfoDetails = getDeviceInfoDetail(dtuSn);
|
|
|
85
|
+ if (StringUtils.isBlank(deviceInfoDetails)) {
|
|
|
86
|
+ return;
|
|
|
87
|
+ }
|
|
|
88
|
+
|
|
|
89
|
+ Map<String, Object> deviceInfoDetailMap = JSON.parseObject(deviceInfoDetails, new TypeReference<>() {
|
|
|
90
|
+ });
|
|
|
91
|
+
|
|
|
92
|
+ JSONArray deviceInfoDetailList = (JSONArray) deviceInfoDetailMap.get("data");
|
|
|
93
|
+ if (CollectionUtils.isEmpty(deviceInfoDetailList)) {
|
|
|
94
|
+ return;
|
|
|
95
|
+ }
|
|
|
96
|
+
|
|
|
97
|
+ JSONObject deviceInfoDetailJson = (JSONObject) deviceInfoDetailList.get(0);
|
|
|
98
|
+
|
|
|
99
|
+ String lampState = deviceInfoDetailJson.getString("lampState");
|
|
|
100
|
+ String startTime = deviceInfoDetailJson.getString("startTime");
|
|
|
101
|
+
|
|
|
102
|
+ long startTimestamp = 0;
|
|
|
103
|
+ if (StringUtils.isNotBlank(startTime)) {
|
|
|
104
|
+ startTimestamp = dateFormat.parse(startTime).getTime();
|
|
|
105
|
+ }
|
|
|
106
|
+
|
|
|
107
|
+ long currentTimestamp = System.currentTimeMillis();
|
|
|
108
|
+ long diffMillis = currentTimestamp - startTimestamp;
|
|
|
109
|
+ long hours = diffMillis / (1000 * 60 * 60);
|
|
|
110
|
+ long minutes = (diffMillis % (1000 * 60 * 60)) / (1000 * 60);
|
|
|
111
|
+ long seconds = (diffMillis % (1000 * 60)) / 1000;
|
|
|
112
|
+ StringBuilder durationBuilder = new StringBuilder();
|
|
|
113
|
+ if (hours > 0) {
|
|
|
114
|
+ durationBuilder.append(hours).append("时");
|
|
|
115
|
+ }
|
|
|
116
|
+
|
|
|
117
|
+ if (minutes > 0) {
|
|
|
118
|
+ durationBuilder.append(minutes).append("分");
|
|
|
119
|
+ }
|
|
|
120
|
+
|
|
|
121
|
+ durationBuilder.append(seconds).append("秒");
|
|
|
122
|
+ String duration = durationBuilder.toString();
|
|
|
123
|
+
|
|
|
124
|
+ // 获取稼动率
|
|
|
125
|
+ String utilizationRate = getUtilizationRate(dtuSn);
|
|
|
126
|
+
|
|
|
127
|
+ log.info("设备数据汇总 - projectState:{}, projectType:{}, deviceName:{}, dtuId:{}, deviceId:{}, dtuSn:{}, " +
|
|
|
128
|
+ "lampState:{}, duration:{}, utilizationRate:{}",
|
|
|
129
|
+ projectState, projectType, deviceName, dtuId, deviceId, dtuSn,
|
|
|
130
|
+ lampState, duration, utilizationRate);
|
|
|
131
|
+
|
|
|
132
|
+ // 保存或更新数据库
|
|
|
133
|
+ saveOrUpdateDevice(projectState, projectType, deviceName, dtuId, deviceId, dtuSn,
|
|
|
134
|
+ lampState, startTime, duration, utilizationRate);
|
|
|
135
|
+ }
|
|
|
136
|
+
|
|
64
|
137
|
|
|
65
|
138
|
}
|
|
66
|
139
|
|
|
...
|
...
|
@@ -99,13 +172,100 @@ public class DevicePullService { |
|
99
|
172
|
return deviceInfoDetail;
|
|
100
|
173
|
}
|
|
101
|
174
|
|
|
|
175
|
+ public String getDtuSnRateOfAction(String dtuSn) {
|
|
|
176
|
+ String accessToken = getAccessToken();
|
|
|
177
|
+ Map<String, String> headerMap = new HashMap<>(1);
|
|
|
178
|
+ headerMap.put("Authorization", "Bearer " + accessToken);
|
|
|
179
|
+
|
|
|
180
|
+ Map<String, String> paramsMap = new HashMap<>();
|
|
|
181
|
+ String today = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
|
|
|
182
|
+ paramsMap.put("startDate", today);
|
|
|
183
|
+ paramsMap.put("endDate", today);
|
|
|
184
|
+ paramsMap.put("dtuSn", dtuSn);
|
|
|
185
|
+
|
|
|
186
|
+ String rateResult = sendRequestGet(deviceSnRateUrl, paramsMap, headerMap);
|
|
|
187
|
+ if (StringUtils.isBlank(rateResult)) {
|
|
|
188
|
+ return null;
|
|
|
189
|
+ }
|
|
|
190
|
+
|
|
|
191
|
+ Map<String, Object> rateMap = JSON.parseObject(rateResult, new TypeReference<>() {});
|
|
|
192
|
+ Integer rateCode = (Integer) rateMap.get("code");
|
|
|
193
|
+ if (rateCode != 200) {
|
|
|
194
|
+ return null;
|
|
|
195
|
+ }
|
|
|
196
|
+
|
|
|
197
|
+ return rateResult;
|
|
|
198
|
+ }
|
|
|
199
|
+
|
|
|
200
|
+ public String getUtilizationRate(String dtuSn) {
|
|
|
201
|
+ String rateResult = getDtuSnRateOfAction(dtuSn);
|
|
|
202
|
+ if (StringUtils.isBlank(rateResult)) {
|
|
|
203
|
+ return "0%";
|
|
|
204
|
+ }
|
|
|
205
|
+
|
|
|
206
|
+ Map<String, Object> rateMap = JSON.parseObject(rateResult, new TypeReference<>() {});
|
|
|
207
|
+ JSONArray dataList = (JSONArray) rateMap.get("data");
|
|
|
208
|
+ if (CollectionUtils.isEmpty(dataList)) {
|
|
|
209
|
+ return "0%";
|
|
|
210
|
+ }
|
|
|
211
|
+
|
|
|
212
|
+ JSONObject todayData = (JSONObject) dataList.get(0);
|
|
|
213
|
+ JSONArray realRateList = todayData.getJSONArray("realRate");
|
|
|
214
|
+ if (CollectionUtils.isEmpty(realRateList)) {
|
|
|
215
|
+ return "0%";
|
|
|
216
|
+ }
|
|
|
217
|
+
|
|
|
218
|
+ JSONObject rateObj = (JSONObject) realRateList.get(0);
|
|
|
219
|
+ long state0 = rateObj.getLongValue("0");
|
|
|
220
|
+ long state1 = rateObj.getLongValue("1");
|
|
|
221
|
+ long state2 = rateObj.getLongValue("2");
|
|
|
222
|
+ long state3 = rateObj.getLongValue("3");
|
|
|
223
|
+ long state4 = rateObj.getLongValue("4");
|
|
|
224
|
+ long state5 = rateObj.getLongValue("5");
|
|
|
225
|
+ long totalTime = state0 + state1 + state2 + state3 + state4 + state5;
|
|
|
226
|
+ if (totalTime == 0) {
|
|
|
227
|
+ return "0%";
|
|
|
228
|
+ }
|
|
|
229
|
+
|
|
|
230
|
+ double rate = (double) state3 / totalTime * 100;
|
|
|
231
|
+ return String.format("%.2f%%", rate);
|
|
|
232
|
+ }
|
|
|
233
|
+
|
|
|
234
|
+ public void saveOrUpdateDevice(String projectState, String projectType, String deviceName, String dtuId,
|
|
|
235
|
+ String deviceId, String dtuSn, String lampState, String startTime,
|
|
|
236
|
+ String duration, String utilizationRate) {
|
|
|
237
|
+ // 查询是否已存在(按公司+dtuSn判断)
|
|
|
238
|
+ List<Map<String, Object>> existList = jdbcTemplate.queryForList(
|
|
|
239
|
+ "SELECT id FROM " + deviceTableName + " WHERE corp_code = ? AND dtuSn = ?", deviceCorpCode, dtuSn);
|
|
|
240
|
+
|
|
|
241
|
+ Date now = new Date();
|
|
|
242
|
+ if (!existList.isEmpty()) {
|
|
|
243
|
+ // 更新
|
|
|
244
|
+ jdbcTemplate.update("UPDATE " + deviceTableName + " SET projectState = ?, projectType = ?, deviceName = ?, " +
|
|
|
245
|
+ "dtuId = ?, deviceId = ?, lampState = ?, startTime = ?, duration = ?, utilizationRate = ?, updated_at = ? WHERE dtuSn = ?",
|
|
|
246
|
+ projectState, projectType, deviceName, dtuId, deviceId, lampState,
|
|
|
247
|
+ startTime, duration, utilizationRate, now, dtuSn);
|
|
|
248
|
+ log.info("设备数据更新成功 - dtuSn:{}", dtuSn);
|
|
|
249
|
+ } else {
|
|
|
250
|
+ // 新增
|
|
|
251
|
+ String id = UUID.randomUUID().toString().replace("-", "");
|
|
|
252
|
+ jdbcTemplate.update("INSERT INTO " + deviceTableName + " (id, corp_code, created_at, created_by, updated_at, updated_by, " +
|
|
|
253
|
+ "deviceName, projectType, projectState, dtuSn, dtuId, deviceId, lampState, startTime, duration, utilizationRate) " +
|
|
|
254
|
+ "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
|
|
|
255
|
+ id, deviceCorpCode, now, "system", now, "system",
|
|
|
256
|
+ deviceName, projectType, projectState, dtuSn, dtuId, deviceId, lampState,
|
|
|
257
|
+ startTime, duration, utilizationRate);
|
|
|
258
|
+ log.info("设备数据新增成功 - dtuSn:{}", dtuSn);
|
|
|
259
|
+ }
|
|
|
260
|
+ }
|
|
|
261
|
+
|
|
102
|
262
|
public String getEnergyInfo() {
|
|
103
|
263
|
String accessToken = getAccessToken();
|
|
104
|
264
|
Map<String, String> headerMap = new HashMap<>(1);
|
|
105
|
265
|
headerMap.put("Authorization", "Bearer " + accessToken);
|
|
106
|
266
|
|
|
107
|
267
|
Map<String, String> paramsMap = new HashMap<>();
|
|
108
|
|
- paramsMap.put("groupName", "一期工厂");
|
|
|
268
|
+ paramsMap.put("groupName", "SHC");
|
|
109
|
269
|
|
|
110
|
270
|
String energyInfoResult = sendRequestGet(energyInfoUrl, paramsMap, headerMap);
|
|
111
|
271
|
if (StringUtils.isBlank(energyInfoResult)) {
|
|
...
|
...
|
@@ -155,7 +315,7 @@ public class DevicePullService { |
|
155
|
315
|
headerMap.put("Authorization", "Bearer " + accessToken);
|
|
156
|
316
|
|
|
157
|
317
|
Map<String, String> paramsMap = new HashMap<>();
|
|
158
|
|
- paramsMap.put("groupName", "一期工厂");
|
|
|
318
|
+ paramsMap.put("groupName", "SHC");
|
|
159
|
319
|
|
|
160
|
320
|
// 第一次请求设备信息
|
|
161
|
321
|
String deviceResult = sendRequestGet(deviceInfoUrl, paramsMap, headerMap);
|
...
|
...
|
|