From 88650c3d9f9b59eed84ca8599ea1b55f0e44d495 Mon Sep 17 00:00:00 2001 From: chenyouting <514333061@qq.com> Date: Wed, 14 Jan 2026 18:36:23 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BB=A3=E7=A0=81=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../bjq/BjqActiveReportingDeviceDataRule.java | 42 +++++----- .../mqtt/rule/bjq/BjqLocationDataRule.java | 76 +++++++++---------- 2 files changed, 56 insertions(+), 62 deletions(-) diff --git a/fys-admin/src/main/java/com/fuyuanshen/global/mqtt/rule/bjq/BjqActiveReportingDeviceDataRule.java b/fys-admin/src/main/java/com/fuyuanshen/global/mqtt/rule/bjq/BjqActiveReportingDeviceDataRule.java index bdc82373..50fd65a0 100644 --- a/fys-admin/src/main/java/com/fuyuanshen/global/mqtt/rule/bjq/BjqActiveReportingDeviceDataRule.java +++ b/fys-admin/src/main/java/com/fuyuanshen/global/mqtt/rule/bjq/BjqActiveReportingDeviceDataRule.java @@ -67,31 +67,29 @@ public class BjqActiveReportingDeviceDataRule implements MqttMessageRule { */ public void asyncSendDeviceDataToRedisWithFuture(String deviceImei, String mainLightMode, String laserLightMode, String batteryPercentage, String chargeState, String batteryRemainingTime) { - CompletableFuture.runAsync(() -> { - try { - // 构造设备状态信息对象 - Map deviceInfo = new LinkedHashMap<>(); - deviceInfo.put("deviceImei", deviceImei); - deviceInfo.put("mainLightMode", mainLightMode); - deviceInfo.put("laserLightMode", laserLightMode); - deviceInfo.put("batteryPercentage", batteryPercentage); - deviceInfo.put("chargeState", chargeState); - deviceInfo.put("batteryRemainingTime", batteryRemainingTime); - deviceInfo.put("timestamp", System.currentTimeMillis()); + try { + // 构造设备状态信息对象 + Map deviceInfo = new LinkedHashMap<>(); + deviceInfo.put("deviceImei", deviceImei); + deviceInfo.put("mainLightMode", mainLightMode); + deviceInfo.put("laserLightMode", laserLightMode); + deviceInfo.put("batteryPercentage", batteryPercentage); + deviceInfo.put("chargeState", chargeState); + deviceInfo.put("batteryRemainingTime", batteryRemainingTime); + deviceInfo.put("timestamp", System.currentTimeMillis()); - // 将设备状态信息存储到Redis中 - String deviceRedisKey = GlobalConstants.GLOBAL_REDIS_KEY+DeviceRedisKeyConstants.DEVICE_KEY_PREFIX + deviceImei + DEVICE_STATUS_KEY_PREFIX; - String deviceInfoJson = JsonUtils.toJsonString(deviceInfo); + // 将设备状态信息存储到Redis中 + String deviceRedisKey = GlobalConstants.GLOBAL_REDIS_KEY+DeviceRedisKeyConstants.DEVICE_KEY_PREFIX + deviceImei + DEVICE_STATUS_KEY_PREFIX; + String deviceInfoJson = JsonUtils.toJsonString(deviceInfo); - // 存储到Redis - RedisUtils.setCacheObject(deviceRedisKey, deviceInfoJson); + // 存储到Redis + RedisUtils.setCacheObject(deviceRedisKey, deviceInfoJson); - log.info("设备状态信息已异步发送到Redis: device={}, mainLightMode={}, laserLightMode={}, batteryPercentage={}", - deviceImei, mainLightMode, laserLightMode, batteryPercentage); - } catch (Exception e) { - log.error("异步发送设备信息到Redis时出错: device={}, error={}", deviceImei, e.getMessage(), e); - } - }); + log.info("设备状态信息已异步发送到Redis: device={}, mainLightMode={}, laserLightMode={}, batteryPercentage={}", + deviceImei, mainLightMode, laserLightMode, batteryPercentage); + } catch (Exception e) { + log.error("异步发送设备信息到Redis时出错: device={}, error={}", deviceImei, e.getMessage(), e); + } } diff --git a/fys-admin/src/main/java/com/fuyuanshen/global/mqtt/rule/bjq/BjqLocationDataRule.java b/fys-admin/src/main/java/com/fuyuanshen/global/mqtt/rule/bjq/BjqLocationDataRule.java index 5fac7459..e4377e42 100644 --- a/fys-admin/src/main/java/com/fuyuanshen/global/mqtt/rule/bjq/BjqLocationDataRule.java +++ b/fys-admin/src/main/java/com/fuyuanshen/global/mqtt/rule/bjq/BjqLocationDataRule.java @@ -121,16 +121,15 @@ public class BjqLocationDataRule implements MqttMessageRule { * @param longitude 经度 */ public void asyncSendLocationToRedisWithFuture(String deviceImei, String latitude, String longitude) { - CompletableFuture.runAsync(() -> { - try { - if (StringUtils.isBlank(latitude) || StringUtils.isBlank(longitude)) { - return; - } + try { + if (StringUtils.isBlank(latitude) || StringUtils.isBlank(longitude)) { + return; + } // String[] latArr = latitude.split("\\."); // String[] lonArr = longitude.split("\\."); // // 将位置信息存储到Redis中 - String redisKey = GlobalConstants.GLOBAL_REDIS_KEY + DEVICE_KEY_PREFIX + deviceImei + DEVICE_LOCATION_KEY_PREFIX; + String redisKey = GlobalConstants.GLOBAL_REDIS_KEY + DEVICE_KEY_PREFIX + deviceImei + DEVICE_LOCATION_KEY_PREFIX; // String redisObj = RedisUtils.getCacheObject(redisKey); // JSONObject jsonOBj = JSONObject.parseObject(redisObj); // if(jsonOBj != null){ @@ -150,34 +149,33 @@ public class BjqLocationDataRule implements MqttMessageRule { // } // } - // 构造位置信息对象 - Map locationInfo = new LinkedHashMap<>(); - double[] doubles = LngLonUtil.gps84_To_Gcj02(Double.parseDouble(latitude), Double.parseDouble(longitude)); - locationInfo.put("deviceImei", deviceImei); - locationInfo.put("latitude", doubles[0]); - locationInfo.put("longitude", doubles[1]); - locationInfo.put("wgs84_latitude", latitude); - locationInfo.put("wgs84_longitude", longitude); - String address = GetAddressFromLatUtil.getAdd(String.valueOf(doubles[1]), String.valueOf(doubles[0])); - locationInfo.put("address", address); - locationInfo.put("timestamp", System.currentTimeMillis()); + // 构造位置信息对象 + Map locationInfo = new LinkedHashMap<>(); + double[] doubles = LngLonUtil.gps84_To_Gcj02(Double.parseDouble(latitude), Double.parseDouble(longitude)); + locationInfo.put("deviceImei", deviceImei); + locationInfo.put("latitude", doubles[0]); + locationInfo.put("longitude", doubles[1]); + locationInfo.put("wgs84_latitude", latitude); + locationInfo.put("wgs84_longitude", longitude); + String address = GetAddressFromLatUtil.getAdd(String.valueOf(doubles[1]), String.valueOf(doubles[0])); + locationInfo.put("address", address); + locationInfo.put("timestamp", System.currentTimeMillis()); - String locationJson = JsonUtils.toJsonString(locationInfo); + String locationJson = JsonUtils.toJsonString(locationInfo); - // 存储到Redis - RedisUtils.setCacheObject(redisKey, locationJson); + // 存储到Redis + RedisUtils.setCacheObject(redisKey, locationJson); - // 存储到一个列表中,保留历史位置信息 + // 存储到一个列表中,保留历史位置信息 // String locationHistoryKey = GlobalConstants.GLOBAL_REDIS_KEY+DeviceRedisKeyConstants.DEVICE_LOCATION_HISTORY_KEY_PREFIX + deviceImei; // RedisUtils.addCacheList(locationHistoryKey, locationJson); // RedisUtils.expire(locationHistoryKey, Duration.ofDays(90)); - storeDeviceTrajectoryWithSortedSet(deviceImei, locationJson); - log.info("位置信息已异步发送到Redis: device={}, lat={}, lon={}", deviceImei, latitude, longitude); - } catch (Exception e) { - log.error("异步发送位置信息到Redis时出错: device={}, error={}", deviceImei, e.getMessage(), e); - } - }); + storeDeviceTrajectoryWithSortedSet(deviceImei, locationJson); + log.info("位置信息已异步发送到Redis: device={}, lat={}, lon={}", deviceImei, latitude, longitude); + } catch (Exception e) { + log.error("异步发送位置信息到Redis时出错: device={}, error={}", deviceImei, e.getMessage(), e); + } } @@ -189,20 +187,18 @@ public class BjqLocationDataRule implements MqttMessageRule { * @param longitude 经度 */ public void asyncSaveLocationToMySQLWithFuture(String deviceImei, String latitude, String longitude) { - CompletableFuture.runAsync(() -> { - try { - if (StringUtils.isBlank(latitude) || StringUtils.isBlank(longitude)) { - return; - } - - // 调用服务层方法更新设备位置信息 - deviceService.updateDeviceLocationByImei(deviceImei, longitude, latitude); - - log.info("位置信息已异步保存到MySQL: device={}, lat={}, lon={}", deviceImei, latitude, longitude); - } catch (Exception e) { - log.error("异步保存位置信息到MySQL时出错: device={}, error={}", deviceImei, e.getMessage(), e); + try { + if (StringUtils.isBlank(latitude) || StringUtils.isBlank(longitude)) { + return; } - }); + + // 调用服务层方法更新设备位置信息 + deviceService.updateDeviceLocationByImei(deviceImei, longitude, latitude); + + log.info("位置信息已异步保存到MySQL: device={}, lat={}, lon={}", deviceImei, latitude, longitude); + } catch (Exception e) { + log.error("异步保存位置信息到MySQL时出错: device={}, error={}", deviceImei, e.getMessage(), e); + } }