|
|
@@ -23,305 +23,305 @@ import java.util.concurrent.TimeUnit;
|
|
23
|
23
|
@Component
|
|
24
|
24
|
public class VehicleSyncTask {
|
|
25
|
25
|
|
|
26
|
|
- private static final Logger log = LoggerFactory.getLogger(VehicleSyncTask.class);
|
|
27
|
|
-
|
|
28
|
|
- private final SysCarService sysCarService;
|
|
29
|
|
- private final SysDeviceService sysDeviceService;
|
|
30
|
|
- private final StringRedisTemplate stringRedisTemplate;
|
|
31
|
|
- private final SysrealtimeService sysrealtimeService;
|
|
32
|
|
- private final SysDeviceVoService sysDeviceVoService;
|
|
33
|
|
- private final SysDeviceControlService sysDeviceControlService;
|
|
34
|
|
- private final RestTemplate restTemplate;
|
|
35
|
|
- private final IotProperties iotProperties;
|
|
36
|
|
-
|
|
37
|
|
- public VehicleSyncTask(SysCarService sysCarService,
|
|
38
|
|
- SysDeviceService sysDeviceService,
|
|
39
|
|
- StringRedisTemplate stringRedisTemplate,
|
|
40
|
|
- SysrealtimeService sysrealtimeService,
|
|
41
|
|
- SysDeviceVoService sysDeviceVoService,
|
|
42
|
|
- SysDeviceControlService sysDeviceControlService,
|
|
43
|
|
- RestTemplate restTemplate,
|
|
44
|
|
- IotProperties iotProperties) {
|
|
45
|
|
- this.sysCarService = sysCarService;
|
|
46
|
|
- this.sysDeviceService = sysDeviceService;
|
|
47
|
|
- this.stringRedisTemplate = stringRedisTemplate;
|
|
48
|
|
- this.sysrealtimeService = sysrealtimeService;
|
|
49
|
|
- this.sysDeviceVoService = sysDeviceVoService;
|
|
50
|
|
- this.sysDeviceControlService = sysDeviceControlService;
|
|
51
|
|
- this.restTemplate = restTemplate;
|
|
52
|
|
- this.iotProperties = iotProperties;
|
|
53
|
|
- }
|
|
54
|
|
-
|
|
55
|
|
- private boolean tryLock(String lockKey, long expireSeconds) {
|
|
56
|
|
- Boolean acquired = stringRedisTemplate.opsForValue().setIfAbsent(lockKey, "1", expireSeconds, TimeUnit.SECONDS);
|
|
57
|
|
- return Boolean.TRUE.equals(acquired);
|
|
58
|
|
- }
|
|
59
|
|
-
|
|
60
|
|
- private void unlock(String lockKey) {
|
|
61
|
|
- Boolean deleted = stringRedisTemplate.delete(lockKey);
|
|
62
|
|
- if (!Boolean.TRUE.equals(deleted)) {
|
|
63
|
|
- log.warn("分布式锁释放失败: {}", lockKey);
|
|
64
|
|
- }
|
|
65
|
|
- }
|
|
66
|
|
-
|
|
67
|
|
- /**
|
|
68
|
|
- * 更新车辆的控制器信息
|
|
69
|
|
- * 30秒更新一次
|
|
70
|
|
- */
|
|
71
|
|
- @Scheduled(fixedDelay = 30000)
|
|
72
|
|
- public void updateSysCar() {
|
|
73
|
|
- String lockKey = "lock:vehicle-sync:updateSysCar";
|
|
74
|
|
- if (!tryLock(lockKey, 60)) {
|
|
75
|
|
- log.debug("获取锁失败,跳过本次执行: {}", lockKey);
|
|
76
|
|
- return;
|
|
77
|
|
- }
|
|
78
|
|
- try {
|
|
79
|
|
- doUpdateSysCar();
|
|
80
|
|
- } finally {
|
|
81
|
|
- unlock(lockKey);
|
|
82
|
|
- }
|
|
83
|
|
- }
|
|
84
|
|
-
|
|
85
|
|
- private void doUpdateSysCar() {
|
|
86
|
|
- List<SysCar> sysCarList = sysCarService.selectcontrollerId();
|
|
87
|
|
- for (SysCar sysCar : sysCarList) {
|
|
88
|
|
- try {
|
|
89
|
|
- if (sysCar.getControllerId() == null || sysCar.getControllerId().isEmpty()) {
|
|
90
|
|
- continue;
|
|
91
|
|
- }
|
|
92
|
|
- SysDevice latitude = sysDeviceService.selectsysdevice(sysCar.getControllerId(), "纬度");
|
|
93
|
|
- SysDevice longitude = sysDeviceService.selectsysdevice(sysCar.getControllerId(), "经度");
|
|
94
|
|
-
|
|
95
|
|
- String redisKeyPattern = "workorder:coordinate:" + sysCar.getControllerId() + ":*";
|
|
96
|
|
- Set<String> keys = scanKeys(redisKeyPattern);
|
|
97
|
|
-
|
|
98
|
|
- if (keys == null || keys.isEmpty()) {
|
|
99
|
|
- updateCarPosition(sysCar, latitude, longitude);
|
|
100
|
|
- } else {
|
|
101
|
|
- for (String key : keys) {
|
|
102
|
|
- Map<Object, Object> coordinateMap = stringRedisTemplate.opsForHash().entries(key);
|
|
103
|
|
- Object cachedLat = coordinateMap.get("latitude");
|
|
104
|
|
- Object cachedLon = coordinateMap.get("longitude");
|
|
105
|
|
- if (cachedLat != null && cachedLat.equals(latitude.getV()) && cachedLon != null && cachedLon.equals(longitude.getV())) {
|
|
106
|
|
- continue;
|
|
107
|
|
- }
|
|
108
|
|
- updateCarPosition(sysCar, latitude, longitude);
|
|
109
|
|
- }
|
|
110
|
|
- }
|
|
111
|
|
- } catch (DataAccessException e) {
|
|
112
|
|
- log.error("更新车辆位置失败 carId={}: {}", sysCar.getCarId(), e.getMessage(), e);
|
|
113
|
|
- } catch (Exception e) {
|
|
114
|
|
- log.error("更新车辆位置异常 carId={}: {}", sysCar.getCarId(), e.getMessage(), e);
|
|
115
|
|
- }
|
|
116
|
|
- }
|
|
117
|
|
- }
|
|
118
|
|
-
|
|
119
|
|
- private void updateCarPosition(SysCar sysCar, SysDevice latitude, SysDevice longitude) {
|
|
120
|
|
- String redisKey = "workorder:coordinate:" + sysCar.getControllerId();
|
|
121
|
|
- stringRedisTemplate.opsForHash().put(redisKey, "latitude", latitude.getV());
|
|
122
|
|
- stringRedisTemplate.opsForHash().put(redisKey, "longitude", longitude.getV());
|
|
123
|
|
- stringRedisTemplate.persist(redisKey);
|
|
124
|
|
-
|
|
125
|
|
- String position = latitude.getV() + "," + longitude.getV();
|
|
126
|
|
- sysCarService.updatecarposition(position, sysCar.getCarId());
|
|
127
|
|
- String url = iotProperties.getMqtt().getVehicleTriggerUrl() + "?carId=" + sysCar.getCarId();
|
|
128
|
|
- try {
|
|
129
|
|
- restTemplate.postForObject(url, null, String.class);
|
|
130
|
|
- } catch (RestClientException e) {
|
|
131
|
|
- log.warn("触发webhook失败 carId={}: {}", sysCar.getCarId(), e.getMessage());
|
|
132
|
|
- }
|
|
133
|
|
- }
|
|
134
|
|
-
|
|
135
|
|
- private Set<String> scanKeys(String pattern) {
|
|
136
|
|
- Set<String> keys = new HashSet<>();
|
|
137
|
|
- ScanOptions options = ScanOptions.scanOptions().match(pattern).count(100).build();
|
|
138
|
|
- try {
|
|
139
|
|
- stringRedisTemplate.execute((RedisCallback<Void>) connection -> {
|
|
140
|
|
- org.springframework.data.redis.core.Cursor<byte[]> cursor = connection.scan(options);
|
|
141
|
|
- while (cursor.hasNext()) {
|
|
142
|
|
- keys.add(new String(cursor.next()));
|
|
143
|
|
- }
|
|
144
|
|
- cursor.close();
|
|
145
|
|
- return null;
|
|
146
|
|
- });
|
|
147
|
|
- } catch (RedisConnectionFailureException e) {
|
|
148
|
|
- log.warn("Redis SCAN 连接失败 pattern={}: {}", pattern, e.getMessage());
|
|
149
|
|
- } catch (Exception e) {
|
|
150
|
|
- log.error("Redis SCAN 失败 pattern={}: {}", pattern, e.getMessage(), e);
|
|
151
|
|
- }
|
|
152
|
|
- return keys;
|
|
153
|
|
- }
|
|
154
|
|
-
|
|
155
|
|
- @Scheduled(fixedDelay = 30000)
|
|
156
|
|
- public void insertDevice() {
|
|
157
|
|
- String lockKey = "lock:vehicle-sync:insertDevice";
|
|
158
|
|
- if (!tryLock(lockKey, 60)) {
|
|
159
|
|
- log.debug("获取锁失败,跳过本次执行: {}", lockKey);
|
|
160
|
|
- return;
|
|
161
|
|
- }
|
|
162
|
|
- try {
|
|
163
|
|
- doInsertDevice();
|
|
164
|
|
- } finally {
|
|
165
|
|
- unlock(lockKey);
|
|
166
|
|
- }
|
|
167
|
|
- }
|
|
168
|
|
-
|
|
169
|
|
- private void doInsertDevice() {
|
|
170
|
|
- Set<String> activeKeys;
|
|
171
|
|
- try {
|
|
172
|
|
- activeKeys = stringRedisTemplate.opsForSet().members("DSB:active:devices");
|
|
173
|
|
- } catch (Exception e) {
|
|
174
|
|
- log.warn("redis中无数据");
|
|
175
|
|
- return;
|
|
176
|
|
- }
|
|
177
|
|
- try {
|
|
178
|
|
- if (activeKeys == null || activeKeys.isEmpty()) {
|
|
179
|
|
- log.info("redis中无数据");
|
|
180
|
|
- return;
|
|
181
|
|
- }
|
|
182
|
|
- List<SysDeviceControl> sysDeviceControlList = sysDeviceControlService.selectdevice("device");
|
|
183
|
|
- for (String redisKey : activeKeys) {
|
|
184
|
|
- Map<Object, Object> dataMap = stringRedisTemplate.opsForHash().entries(redisKey);
|
|
185
|
|
- if (dataMap == null || dataMap.isEmpty()) {
|
|
186
|
|
- stringRedisTemplate.opsForSet().remove("DSB:active:devices", redisKey);
|
|
187
|
|
- continue;
|
|
188
|
|
- }
|
|
189
|
|
- String[] parts = redisKey.split(":", 3);
|
|
190
|
|
- if (parts.length != 3 || !"DSB".equals(parts[0])) {
|
|
191
|
|
- log.warn("跳过非法 key: {}", redisKey);
|
|
192
|
|
- continue;
|
|
193
|
|
- }
|
|
194
|
|
- String controllerId = parts[1];
|
|
195
|
|
- Integer count = sysDeviceVoService.selectcount(controllerId);
|
|
196
|
|
- if (count != null && count > 0) {
|
|
197
|
|
- StringBuilder keyvalue = new StringBuilder();
|
|
198
|
|
- for (int i = 0; i < sysDeviceControlList.size(); i++) {
|
|
199
|
|
- for (Map.Entry<Object, Object> entry : dataMap.entrySet()) {
|
|
200
|
|
- String fieldKey = entry.getKey().toString();
|
|
201
|
|
- String fieldvalue = entry.getValue().toString();
|
|
202
|
|
- if (sysDeviceControlList.get(i).getControllerName().equals(fieldKey)) {
|
|
203
|
|
- keyvalue.append(fieldKey).append("=/'").append(fieldvalue).append("/'");
|
|
204
|
|
- if (i < sysDeviceControlList.size() - 1) {
|
|
205
|
|
- keyvalue.append(",");
|
|
206
|
|
- }
|
|
207
|
|
- }
|
|
208
|
|
- }
|
|
209
|
|
- }
|
|
210
|
|
- boolean updated = sysDeviceVoService.updatesysdevice(keyvalue.toString(), controllerId);
|
|
211
|
|
- if (!updated) {
|
|
212
|
|
- log.warn("更新设备配置失败: controllerId={}", controllerId);
|
|
213
|
|
- }
|
|
214
|
|
- } else {
|
|
215
|
|
- StringBuilder key = new StringBuilder();
|
|
216
|
|
- StringBuilder value = new StringBuilder();
|
|
217
|
|
- for (int i = 0; i < sysDeviceControlList.size(); i++) {
|
|
218
|
|
- for (Map.Entry<Object, Object> entry : dataMap.entrySet()) {
|
|
219
|
|
- String fieldKey = entry.getKey().toString();
|
|
220
|
|
- String fieldvalue = entry.getValue().toString();
|
|
221
|
|
- if (sysDeviceControlList.get(i).getControllerName().equals(fieldKey)) {
|
|
222
|
|
- key.append(fieldKey);
|
|
223
|
|
- value.append(fieldvalue);
|
|
224
|
|
- if (i < sysDeviceControlList.size() - 1) {
|
|
225
|
|
- key.append(",");
|
|
226
|
|
- value.append(",");
|
|
227
|
|
- }
|
|
228
|
|
- }
|
|
229
|
|
- }
|
|
230
|
|
- }
|
|
231
|
|
- boolean inserted = sysDeviceVoService.insertdevice(key.toString(), value.toString());
|
|
232
|
|
- if (!inserted) {
|
|
233
|
|
- log.warn("插入设备配置失败: controllerId={}", controllerId);
|
|
234
|
|
- }
|
|
235
|
|
- }
|
|
236
|
|
- }
|
|
237
|
|
- } catch (RedisConnectionFailureException e) {
|
|
238
|
|
- log.warn("Redis 连接失败,跳过本次同步: {}", e.getMessage());
|
|
239
|
|
- } catch (DataAccessException e) {
|
|
240
|
|
- log.error("数据库操作失败: {}", e.getMessage(), e);
|
|
241
|
|
- } catch (Exception e) {
|
|
242
|
|
- log.error("同步设备配置失败: {}", e.getMessage(), e);
|
|
243
|
|
- }
|
|
244
|
|
- }
|
|
245
|
|
-
|
|
246
|
|
- /**
|
|
247
|
|
- * 更新数据库实时数据
|
|
248
|
|
- */
|
|
249
|
|
- @Scheduled(fixedDelay = 30000)
|
|
250
|
|
- public void syncRedisToMySQL() {
|
|
251
|
|
- String lockKey = "lock:vehicle-sync:syncRedisToMySQL";
|
|
252
|
|
- if (!tryLock(lockKey, 60)) {
|
|
253
|
|
- log.debug("获取锁失败,跳过本次执行: {}", lockKey);
|
|
254
|
|
- return;
|
|
255
|
|
- }
|
|
256
|
|
- try {
|
|
257
|
|
- doSyncRedisToMySQL();
|
|
258
|
|
- } finally {
|
|
259
|
|
- unlock(lockKey);
|
|
260
|
|
- }
|
|
261
|
|
- }
|
|
262
|
|
-
|
|
263
|
|
- private void doSyncRedisToMySQL() {
|
|
264
|
|
- Set<String> activeKeys = stringRedisTemplate.opsForSet().members("DSB:active:devices");
|
|
265
|
|
- if (activeKeys == null || activeKeys.isEmpty()) return;
|
|
266
|
|
-
|
|
267
|
|
- for (String redisKey : activeKeys) {
|
|
268
|
|
- try {
|
|
269
|
|
- Map<Object, Object> dataMap = stringRedisTemplate.opsForHash().entries(redisKey);
|
|
270
|
|
- if (dataMap == null || dataMap.isEmpty()) {
|
|
271
|
|
- stringRedisTemplate.opsForSet().remove("DSB:active:devices", redisKey);
|
|
272
|
|
- continue;
|
|
273
|
|
- }
|
|
274
|
|
-
|
|
275
|
|
- String[] parts = redisKey.split(":", 3);
|
|
276
|
|
- if (parts.length != 3 || !"DSB".equals(parts[0])) {
|
|
277
|
|
- log.warn("跳过非法 key: {}", redisKey);
|
|
278
|
|
- continue;
|
|
279
|
|
- }
|
|
280
|
|
- String controllerId = parts[1];
|
|
281
|
|
-
|
|
282
|
|
- try {
|
|
283
|
|
- sysrealtimeService.createrealtime(controllerId);
|
|
284
|
|
- } catch (Exception e) {
|
|
285
|
|
- log.error("创建表失败: {} | {}", controllerId, e.getMessage(), e);
|
|
286
|
|
- continue;
|
|
287
|
|
- }
|
|
288
|
|
-
|
|
289
|
|
- String createTime = getStringValue(dataMap, "createTime");
|
|
290
|
|
- String timestamp = getStringValue(dataMap, "timestamp");
|
|
291
|
|
- String deviceId = getStringValue(dataMap, "device_id");
|
|
292
|
|
- if (createTime == null || timestamp == null || deviceId == null) {
|
|
293
|
|
- continue;
|
|
294
|
|
- }
|
|
295
|
|
-
|
|
296
|
|
- List<String> existingKeys = sysrealtimeService.selectAllKeys(controllerId);
|
|
297
|
|
- Set<String> existingKeySet = existingKeys != null ? new HashSet<>(existingKeys) : Collections.emptySet();
|
|
298
|
|
-
|
|
299
|
|
- for (Map.Entry<Object, Object> entry : dataMap.entrySet()) {
|
|
300
|
|
- String fieldKey = entry.getKey().toString();
|
|
301
|
|
- if ("createTime".equals(fieldKey) || "timestamp".equals(fieldKey) || "device_id".equals(fieldKey)) {
|
|
302
|
|
- continue;
|
|
303
|
|
- }
|
|
304
|
|
- String fieldValue = getStringValue(dataMap, fieldKey);
|
|
305
|
|
- if (fieldValue == null) continue;
|
|
306
|
|
-
|
|
307
|
|
- if (existingKeySet.contains(fieldKey)) {
|
|
308
|
|
- sysrealtimeService.updatetables(controllerId, createTime, fieldValue, timestamp, fieldKey, deviceId);
|
|
309
|
|
- } else {
|
|
310
|
|
- sysrealtimeService.inserttables(controllerId, createTime, deviceId, timestamp, fieldKey, fieldValue);
|
|
311
|
|
- }
|
|
312
|
|
- }
|
|
313
|
|
- } catch (RedisConnectionFailureException e) {
|
|
314
|
|
- log.error("Redis 连接失败: {} | {}", redisKey, e.getMessage());
|
|
315
|
|
- } catch (DataAccessException e) {
|
|
316
|
|
- log.error("数据库操作失败: {} | {}", redisKey, e.getMessage(), e);
|
|
317
|
|
- } catch (Exception e) {
|
|
318
|
|
- log.error("同步设备失败: {} | {}", redisKey, e.getMessage(), e);
|
|
319
|
|
- }
|
|
320
|
|
- }
|
|
321
|
|
- }
|
|
322
|
|
-
|
|
323
|
|
- private String getStringValue(Map<Object, Object> map, String key) {
|
|
324
|
|
- Object val = map.get(key);
|
|
325
|
|
- return val == null ? null : val.toString().trim();
|
|
326
|
|
- }
|
|
|
26
|
+// private static final Logger log = LoggerFactory.getLogger(VehicleSyncTask.class);
|
|
|
27
|
+//
|
|
|
28
|
+// private final SysCarService sysCarService;
|
|
|
29
|
+// private final SysDeviceService sysDeviceService;
|
|
|
30
|
+// private final StringRedisTemplate stringRedisTemplate;
|
|
|
31
|
+// private final SysrealtimeService sysrealtimeService;
|
|
|
32
|
+// private final SysDeviceVoService sysDeviceVoService;
|
|
|
33
|
+// private final SysDeviceControlService sysDeviceControlService;
|
|
|
34
|
+// private final RestTemplate restTemplate;
|
|
|
35
|
+// private final IotProperties iotProperties;
|
|
|
36
|
+//
|
|
|
37
|
+// public VehicleSyncTask(SysCarService sysCarService,
|
|
|
38
|
+// SysDeviceService sysDeviceService,
|
|
|
39
|
+// StringRedisTemplate stringRedisTemplate,
|
|
|
40
|
+// SysrealtimeService sysrealtimeService,
|
|
|
41
|
+// SysDeviceVoService sysDeviceVoService,
|
|
|
42
|
+// SysDeviceControlService sysDeviceControlService,
|
|
|
43
|
+// RestTemplate restTemplate,
|
|
|
44
|
+// IotProperties iotProperties) {
|
|
|
45
|
+// this.sysCarService = sysCarService;
|
|
|
46
|
+// this.sysDeviceService = sysDeviceService;
|
|
|
47
|
+// this.stringRedisTemplate = stringRedisTemplate;
|
|
|
48
|
+// this.sysrealtimeService = sysrealtimeService;
|
|
|
49
|
+// this.sysDeviceVoService = sysDeviceVoService;
|
|
|
50
|
+// this.sysDeviceControlService = sysDeviceControlService;
|
|
|
51
|
+// this.restTemplate = restTemplate;
|
|
|
52
|
+// this.iotProperties = iotProperties;
|
|
|
53
|
+// }
|
|
|
54
|
+//
|
|
|
55
|
+// private boolean tryLock(String lockKey, long expireSeconds) {
|
|
|
56
|
+// Boolean acquired = stringRedisTemplate.opsForValue().setIfAbsent(lockKey, "1", expireSeconds, TimeUnit.SECONDS);
|
|
|
57
|
+// return Boolean.TRUE.equals(acquired);
|
|
|
58
|
+// }
|
|
|
59
|
+//
|
|
|
60
|
+// private void unlock(String lockKey) {
|
|
|
61
|
+// Boolean deleted = stringRedisTemplate.delete(lockKey);
|
|
|
62
|
+// if (!Boolean.TRUE.equals(deleted)) {
|
|
|
63
|
+// log.warn("分布式锁释放失败: {}", lockKey);
|
|
|
64
|
+// }
|
|
|
65
|
+// }
|
|
|
66
|
+//
|
|
|
67
|
+// /**
|
|
|
68
|
+// * 更新车辆的控制器信息
|
|
|
69
|
+// * 30秒更新一次
|
|
|
70
|
+// */
|
|
|
71
|
+// @Scheduled(fixedDelay = 30000)
|
|
|
72
|
+// public void updateSysCar() {
|
|
|
73
|
+// String lockKey = "lock:vehicle-sync:updateSysCar";
|
|
|
74
|
+// if (!tryLock(lockKey, 60)) {
|
|
|
75
|
+// log.debug("获取锁失败,跳过本次执行: {}", lockKey);
|
|
|
76
|
+// return;
|
|
|
77
|
+// }
|
|
|
78
|
+// try {
|
|
|
79
|
+// doUpdateSysCar();
|
|
|
80
|
+// } finally {
|
|
|
81
|
+// unlock(lockKey);
|
|
|
82
|
+// }
|
|
|
83
|
+// }
|
|
|
84
|
+//
|
|
|
85
|
+// private void doUpdateSysCar() {
|
|
|
86
|
+// List<SysCar> sysCarList = sysCarService.selectcontrollerId();
|
|
|
87
|
+// for (SysCar sysCar : sysCarList) {
|
|
|
88
|
+// try {
|
|
|
89
|
+// if (sysCar.getControllerId() == null || sysCar.getControllerId().isEmpty()) {
|
|
|
90
|
+// continue;
|
|
|
91
|
+// }
|
|
|
92
|
+// SysDevice latitude = sysDeviceService.selectsysdevice(sysCar.getControllerId(), "纬度");
|
|
|
93
|
+// SysDevice longitude = sysDeviceService.selectsysdevice(sysCar.getControllerId(), "经度");
|
|
|
94
|
+//
|
|
|
95
|
+// String redisKeyPattern = "workorder:coordinate:" + sysCar.getControllerId() + ":*";
|
|
|
96
|
+// Set<String> keys = scanKeys(redisKeyPattern);
|
|
|
97
|
+//
|
|
|
98
|
+// if (keys == null || keys.isEmpty()) {
|
|
|
99
|
+// updateCarPosition(sysCar, latitude, longitude);
|
|
|
100
|
+// } else {
|
|
|
101
|
+// for (String key : keys) {
|
|
|
102
|
+// Map<Object, Object> coordinateMap = stringRedisTemplate.opsForHash().entries(key);
|
|
|
103
|
+// Object cachedLat = coordinateMap.get("latitude");
|
|
|
104
|
+// Object cachedLon = coordinateMap.get("longitude");
|
|
|
105
|
+// if (cachedLat != null && cachedLat.equals(latitude.getV()) && cachedLon != null && cachedLon.equals(longitude.getV())) {
|
|
|
106
|
+// continue;
|
|
|
107
|
+// }
|
|
|
108
|
+// updateCarPosition(sysCar, latitude, longitude);
|
|
|
109
|
+// }
|
|
|
110
|
+// }
|
|
|
111
|
+// } catch (DataAccessException e) {
|
|
|
112
|
+// log.error("更新车辆位置失败 carId={}: {}", sysCar.getCarId(), e.getMessage(), e);
|
|
|
113
|
+// } catch (Exception e) {
|
|
|
114
|
+// log.error("更新车辆位置异常 carId={}: {}", sysCar.getCarId(), e.getMessage(), e);
|
|
|
115
|
+// }
|
|
|
116
|
+// }
|
|
|
117
|
+// }
|
|
|
118
|
+//
|
|
|
119
|
+// private void updateCarPosition(SysCar sysCar, SysDevice latitude, SysDevice longitude) {
|
|
|
120
|
+// String redisKey = "workorder:coordinate:" + sysCar.getControllerId();
|
|
|
121
|
+// stringRedisTemplate.opsForHash().put(redisKey, "latitude", latitude.getV());
|
|
|
122
|
+// stringRedisTemplate.opsForHash().put(redisKey, "longitude", longitude.getV());
|
|
|
123
|
+// stringRedisTemplate.persist(redisKey);
|
|
|
124
|
+//
|
|
|
125
|
+// String position = latitude.getV() + "," + longitude.getV();
|
|
|
126
|
+// sysCarService.updatecarposition(position, sysCar.getCarId());
|
|
|
127
|
+// String url = iotProperties.getMqtt().getVehicleTriggerUrl() + "?carId=" + sysCar.getCarId();
|
|
|
128
|
+// try {
|
|
|
129
|
+// restTemplate.postForObject(url, null, String.class);
|
|
|
130
|
+// } catch (RestClientException e) {
|
|
|
131
|
+// log.warn("触发webhook失败 carId={}: {}", sysCar.getCarId(), e.getMessage());
|
|
|
132
|
+// }
|
|
|
133
|
+// }
|
|
|
134
|
+//
|
|
|
135
|
+// private Set<String> scanKeys(String pattern) {
|
|
|
136
|
+// Set<String> keys = new HashSet<>();
|
|
|
137
|
+// ScanOptions options = ScanOptions.scanOptions().match(pattern).count(100).build();
|
|
|
138
|
+// try {
|
|
|
139
|
+// stringRedisTemplate.execute((RedisCallback<Void>) connection -> {
|
|
|
140
|
+// org.springframework.data.redis.core.Cursor<byte[]> cursor = connection.scan(options);
|
|
|
141
|
+// while (cursor.hasNext()) {
|
|
|
142
|
+// keys.add(new String(cursor.next()));
|
|
|
143
|
+// }
|
|
|
144
|
+// cursor.close();
|
|
|
145
|
+// return null;
|
|
|
146
|
+// });
|
|
|
147
|
+// } catch (RedisConnectionFailureException e) {
|
|
|
148
|
+// log.warn("Redis SCAN 连接失败 pattern={}: {}", pattern, e.getMessage());
|
|
|
149
|
+// } catch (Exception e) {
|
|
|
150
|
+// log.error("Redis SCAN 失败 pattern={}: {}", pattern, e.getMessage(), e);
|
|
|
151
|
+// }
|
|
|
152
|
+// return keys;
|
|
|
153
|
+// }
|
|
|
154
|
+//
|
|
|
155
|
+//// @Scheduled(fixedDelay = 30000)
|
|
|
156
|
+//// public void insertDevice() {
|
|
|
157
|
+//// String lockKey = "lock:vehicle-sync:insertDevice";
|
|
|
158
|
+//// if (!tryLock(lockKey, 60)) {
|
|
|
159
|
+//// log.debug("获取锁失败,跳过本次执行: {}", lockKey);
|
|
|
160
|
+//// return;
|
|
|
161
|
+//// }
|
|
|
162
|
+//// try {
|
|
|
163
|
+//// doInsertDevice();
|
|
|
164
|
+//// } finally {
|
|
|
165
|
+//// unlock(lockKey);
|
|
|
166
|
+//// }
|
|
|
167
|
+//// }
|
|
|
168
|
+////
|
|
|
169
|
+//// private void doInsertDevice() {
|
|
|
170
|
+//// Set<String> activeKeys;
|
|
|
171
|
+//// try {
|
|
|
172
|
+//// activeKeys = stringRedisTemplate.opsForSet().members("DSB:active:devices");
|
|
|
173
|
+//// } catch (Exception e) {
|
|
|
174
|
+//// log.warn("redis中无数据");
|
|
|
175
|
+//// return;
|
|
|
176
|
+//// }
|
|
|
177
|
+//// try {
|
|
|
178
|
+//// if (activeKeys == null || activeKeys.isEmpty()) {
|
|
|
179
|
+//// log.info("redis中无数据");
|
|
|
180
|
+//// return;
|
|
|
181
|
+//// }
|
|
|
182
|
+//// List<SysDeviceControl> sysDeviceControlList = sysDeviceControlService.selectdevice("device");
|
|
|
183
|
+//// for (String redisKey : activeKeys) {
|
|
|
184
|
+//// Map<Object, Object> dataMap = stringRedisTemplate.opsForHash().entries(redisKey);
|
|
|
185
|
+//// if (dataMap == null || dataMap.isEmpty()) {
|
|
|
186
|
+//// stringRedisTemplate.opsForSet().remove("DSB:active:devices", redisKey);
|
|
|
187
|
+//// continue;
|
|
|
188
|
+//// }
|
|
|
189
|
+//// String[] parts = redisKey.split(":", 3);
|
|
|
190
|
+//// if (parts.length != 3 || !"DSB".equals(parts[0])) {
|
|
|
191
|
+//// log.warn("跳过非法 key: {}", redisKey);
|
|
|
192
|
+//// continue;
|
|
|
193
|
+//// }
|
|
|
194
|
+//// String controllerId = parts[1];
|
|
|
195
|
+//// Integer count = sysDeviceVoService.selectcount(controllerId);
|
|
|
196
|
+//// if (count != null && count > 0) {
|
|
|
197
|
+//// StringBuilder keyvalue = new StringBuilder();
|
|
|
198
|
+//// for (int i = 0; i < sysDeviceControlList.size(); i++) {
|
|
|
199
|
+//// for (Map.Entry<Object, Object> entry : dataMap.entrySet()) {
|
|
|
200
|
+//// String fieldKey = entry.getKey().toString();
|
|
|
201
|
+//// String fieldvalue = entry.getValue().toString();
|
|
|
202
|
+//// if (sysDeviceControlList.get(i).getControllerName().equals(fieldKey)) {
|
|
|
203
|
+//// keyvalue.append(fieldKey).append("=/'").append(fieldvalue).append("/'");
|
|
|
204
|
+//// if (i < sysDeviceControlList.size() - 1) {
|
|
|
205
|
+//// keyvalue.append(",");
|
|
|
206
|
+//// }
|
|
|
207
|
+//// }
|
|
|
208
|
+//// }
|
|
|
209
|
+//// }
|
|
|
210
|
+//// boolean updated = sysDeviceVoService.updatesysdevice(keyvalue.toString(), controllerId);
|
|
|
211
|
+//// if (!updated) {
|
|
|
212
|
+//// log.warn("更新设备配置失败: controllerId={}", controllerId);
|
|
|
213
|
+//// }
|
|
|
214
|
+//// } else {
|
|
|
215
|
+//// StringBuilder key = new StringBuilder();
|
|
|
216
|
+//// StringBuilder value = new StringBuilder();
|
|
|
217
|
+//// for (int i = 0; i < sysDeviceControlList.size(); i++) {
|
|
|
218
|
+//// for (Map.Entry<Object, Object> entry : dataMap.entrySet()) {
|
|
|
219
|
+//// String fieldKey = entry.getKey().toString();
|
|
|
220
|
+//// String fieldvalue = entry.getValue().toString();
|
|
|
221
|
+//// if (sysDeviceControlList.get(i).getControllerName().equals(fieldKey)) {
|
|
|
222
|
+//// key.append(fieldKey);
|
|
|
223
|
+//// value.append(fieldvalue);
|
|
|
224
|
+//// if (i < sysDeviceControlList.size() - 1) {
|
|
|
225
|
+//// key.append(",");
|
|
|
226
|
+//// value.append(",");
|
|
|
227
|
+//// }
|
|
|
228
|
+//// }
|
|
|
229
|
+//// }
|
|
|
230
|
+//// }
|
|
|
231
|
+//// boolean inserted = sysDeviceVoService.insertdevice(key.toString(), value.toString());
|
|
|
232
|
+//// if (!inserted) {
|
|
|
233
|
+//// log.warn("插入设备配置失败: controllerId={}", controllerId);
|
|
|
234
|
+//// }
|
|
|
235
|
+//// }
|
|
|
236
|
+//// }
|
|
|
237
|
+//// } catch (RedisConnectionFailureException e) {
|
|
|
238
|
+//// log.warn("Redis 连接失败,跳过本次同步: {}", e.getMessage());
|
|
|
239
|
+//// } catch (DataAccessException e) {
|
|
|
240
|
+//// log.error("数据库操作失败: {}", e.getMessage(), e);
|
|
|
241
|
+//// } catch (Exception e) {
|
|
|
242
|
+//// log.error("同步设备配置失败: {}", e.getMessage(), e);
|
|
|
243
|
+//// }
|
|
|
244
|
+//// }
|
|
|
245
|
+//
|
|
|
246
|
+// /**
|
|
|
247
|
+// * 更新数据库实时数据
|
|
|
248
|
+// */
|
|
|
249
|
+// @Scheduled(fixedDelay = 30000)
|
|
|
250
|
+// public void syncRedisToMySQL() {
|
|
|
251
|
+// String lockKey = "lock:vehicle-sync:syncRedisToMySQL";
|
|
|
252
|
+// if (!tryLock(lockKey, 60)) {
|
|
|
253
|
+// log.debug("获取锁失败,跳过本次执行: {}", lockKey);
|
|
|
254
|
+// return;
|
|
|
255
|
+// }
|
|
|
256
|
+// try {
|
|
|
257
|
+// doSyncRedisToMySQL();
|
|
|
258
|
+// } finally {
|
|
|
259
|
+// unlock(lockKey);
|
|
|
260
|
+// }
|
|
|
261
|
+// }
|
|
|
262
|
+//
|
|
|
263
|
+// private void doSyncRedisToMySQL() {
|
|
|
264
|
+// Set<String> activeKeys = stringRedisTemplate.opsForSet().members("DSB:active:devices");
|
|
|
265
|
+// if (activeKeys == null || activeKeys.isEmpty()) return;
|
|
|
266
|
+//
|
|
|
267
|
+// for (String redisKey : activeKeys) {
|
|
|
268
|
+// try {
|
|
|
269
|
+// Map<Object, Object> dataMap = stringRedisTemplate.opsForHash().entries(redisKey);
|
|
|
270
|
+// if (dataMap == null || dataMap.isEmpty()) {
|
|
|
271
|
+// stringRedisTemplate.opsForSet().remove("DSB:active:devices", redisKey);
|
|
|
272
|
+// continue;
|
|
|
273
|
+// }
|
|
|
274
|
+//
|
|
|
275
|
+// String[] parts = redisKey.split(":", 3);
|
|
|
276
|
+// if (parts.length != 3 || !"DSB".equals(parts[0])) {
|
|
|
277
|
+// log.warn("跳过非法 key: {}", redisKey);
|
|
|
278
|
+// continue;
|
|
|
279
|
+// }
|
|
|
280
|
+// String controllerId = parts[1];
|
|
|
281
|
+//
|
|
|
282
|
+// try {
|
|
|
283
|
+// sysrealtimeService.createrealtime(controllerId);
|
|
|
284
|
+// } catch (Exception e) {
|
|
|
285
|
+// log.error("创建表失败: {} | {}", controllerId, e.getMessage(), e);
|
|
|
286
|
+// continue;
|
|
|
287
|
+// }
|
|
|
288
|
+//
|
|
|
289
|
+// String createTime = getStringValue(dataMap, "createTime");
|
|
|
290
|
+// String timestamp = getStringValue(dataMap, "timestamp");
|
|
|
291
|
+// String deviceId = getStringValue(dataMap, "device_id");
|
|
|
292
|
+// if (createTime == null || timestamp == null || deviceId == null) {
|
|
|
293
|
+// continue;
|
|
|
294
|
+// }
|
|
|
295
|
+//
|
|
|
296
|
+// List<String> existingKeys = sysrealtimeService.selectAllKeys(controllerId);
|
|
|
297
|
+// Set<String> existingKeySet = existingKeys != null ? new HashSet<>(existingKeys) : Collections.emptySet();
|
|
|
298
|
+//
|
|
|
299
|
+// for (Map.Entry<Object, Object> entry : dataMap.entrySet()) {
|
|
|
300
|
+// String fieldKey = entry.getKey().toString();
|
|
|
301
|
+// if ("createTime".equals(fieldKey) || "timestamp".equals(fieldKey) || "device_id".equals(fieldKey)) {
|
|
|
302
|
+// continue;
|
|
|
303
|
+// }
|
|
|
304
|
+// String fieldValue = getStringValue(dataMap, fieldKey);
|
|
|
305
|
+// if (fieldValue == null) continue;
|
|
|
306
|
+//
|
|
|
307
|
+// if (existingKeySet.contains(fieldKey)) {
|
|
|
308
|
+// sysrealtimeService.updatetables(controllerId, createTime, fieldValue, timestamp, fieldKey, deviceId);
|
|
|
309
|
+// } else {
|
|
|
310
|
+// sysrealtimeService.inserttables(controllerId, createTime, deviceId, timestamp, fieldKey, fieldValue);
|
|
|
311
|
+// }
|
|
|
312
|
+// }
|
|
|
313
|
+// } catch (RedisConnectionFailureException e) {
|
|
|
314
|
+// log.error("Redis 连接失败: {} | {}", redisKey, e.getMessage());
|
|
|
315
|
+// } catch (DataAccessException e) {
|
|
|
316
|
+// log.error("数据库操作失败: {} | {}", redisKey, e.getMessage(), e);
|
|
|
317
|
+// } catch (Exception e) {
|
|
|
318
|
+// log.error("同步设备失败: {} | {}", redisKey, e.getMessage(), e);
|
|
|
319
|
+// }
|
|
|
320
|
+// }
|
|
|
321
|
+// }
|
|
|
322
|
+//
|
|
|
323
|
+// private String getStringValue(Map<Object, Object> map, String key) {
|
|
|
324
|
+// Object val = map.get(key);
|
|
|
325
|
+// return val == null ? null : val.toString().trim();
|
|
|
326
|
+// }
|
|
327
|
327
|
}
|