代码优化
This commit is contained in:
@ -0,0 +1,12 @@
|
||||
package com.fuyuanshen.global.mqtt.constants;
|
||||
|
||||
public class DeviceRedisKeyConstants {
|
||||
// 将设备上报状态
|
||||
public static final String DEVICE_STATUS_KEY_PREFIX = "device:status:";
|
||||
// 在线状态
|
||||
public static final String DEVICE_ONLINE_STATUS_KEY_PREFIX = "device:onlineStatus:";
|
||||
// 将设备状态信息存储到Redis中
|
||||
public static final String DEVICE_LOCATION_KEY_PREFIX = "device:location:";
|
||||
// 存储到一个列表中,保留历史位置信息
|
||||
public static final String DEVICE_LOCATION_HISTORY_KEY_PREFIX = "device:location:history:";
|
||||
}
|
@ -1,10 +1,12 @@
|
||||
package com.fuyuanshen.global.mqtt.rule;
|
||||
|
||||
import com.fuyuanshen.common.core.constant.GlobalConstants;
|
||||
import com.fuyuanshen.common.json.utils.JsonUtils;
|
||||
import com.fuyuanshen.common.redis.utils.RedisUtils;
|
||||
import com.fuyuanshen.global.mqtt.base.MqttMessageRule;
|
||||
import com.fuyuanshen.global.mqtt.base.MqttRuleContext;
|
||||
import com.fuyuanshen.global.mqtt.config.MqttGateway;
|
||||
import com.fuyuanshen.global.mqtt.constants.DeviceRedisKeyConstants;
|
||||
import com.fuyuanshen.global.mqtt.constants.LightingCommandTypeConstants;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
@ -78,11 +80,14 @@ public class ActiveReportingDeviceDataRule implements MqttMessageRule {
|
||||
deviceInfo.put("timestamp", System.currentTimeMillis());
|
||||
|
||||
// 将设备状态信息存储到Redis中
|
||||
String deviceRedisKey = "device:status:" + deviceImei;
|
||||
String deviceRedisKey = GlobalConstants.GLOBAL_REDIS_KEY+DeviceRedisKeyConstants.DEVICE_STATUS_KEY_PREFIX + deviceImei;
|
||||
String deviceInfoJson = JsonUtils.toJsonString(deviceInfo);
|
||||
|
||||
// 存储到Redis,设置过期时间(例如24小时)
|
||||
RedisUtils.setCacheObject(deviceRedisKey, deviceInfoJson, Duration.ofSeconds(24 * 60 * 60));
|
||||
RedisUtils.setCacheObject(deviceRedisKey, deviceInfoJson);
|
||||
//在线状态
|
||||
String deviceOnlineStatusRedisKey = GlobalConstants.GLOBAL_REDIS_KEY+DeviceRedisKeyConstants.DEVICE_ONLINE_STATUS_KEY_PREFIX + deviceImei;
|
||||
RedisUtils.setCacheObject(deviceOnlineStatusRedisKey, "1");
|
||||
|
||||
log.info("设备状态信息已异步发送到Redis: device={}, mainLightMode={}, laserLightMode={}, batteryPercentage={}",
|
||||
deviceImei, mainLightMode, laserLightMode, batteryPercentage);
|
||||
|
@ -1,5 +1,6 @@
|
||||
package com.fuyuanshen.global.mqtt.rule;
|
||||
|
||||
import com.fuyuanshen.common.core.constant.GlobalConstants;
|
||||
import com.fuyuanshen.common.core.utils.ImageToCArrayConverter;
|
||||
import com.fuyuanshen.common.core.utils.StringUtils;
|
||||
import com.fuyuanshen.common.json.utils.JsonUtils;
|
||||
@ -26,13 +27,13 @@ import static com.fuyuanshen.common.core.utils.ImageToCArrayConverter.convertHex
|
||||
@Component
|
||||
@RequiredArgsConstructor
|
||||
@Slf4j
|
||||
public class PersonnelInfoRule implements MqttMessageRule {
|
||||
public class DeviceBootLogoRule implements MqttMessageRule {
|
||||
|
||||
private final MqttGateway mqttGateway;
|
||||
|
||||
@Override
|
||||
public String getCommandType() {
|
||||
return LightingCommandTypeConstants.PERSONNEL_INFO;
|
||||
return LightingCommandTypeConstants.BOOT_LOGO;
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -43,7 +44,7 @@ public class PersonnelInfoRule implements MqttMessageRule {
|
||||
return;
|
||||
}
|
||||
|
||||
String data = RedisUtils.getCacheObject("894078:app_logo_data:" + context.getDeviceImei());
|
||||
String data = RedisUtils.getCacheObject(GlobalConstants.GLOBAL_REDIS_KEY+"app_logo_data:" + context.getDeviceImei());
|
||||
if (StringUtils.isEmpty(data)) {
|
||||
return;
|
||||
}
|
@ -1,5 +1,6 @@
|
||||
package com.fuyuanshen.global.mqtt.rule;
|
||||
|
||||
import com.fuyuanshen.common.core.constant.GlobalConstants;
|
||||
import com.fuyuanshen.common.core.utils.StringUtils;
|
||||
import com.fuyuanshen.common.json.utils.JsonUtils;
|
||||
import com.fuyuanshen.common.redis.utils.RedisUtils;
|
||||
@ -8,6 +9,7 @@ import com.fuyuanshen.equipment.utils.map.LngLonUtil;
|
||||
import com.fuyuanshen.global.mqtt.base.MqttMessageRule;
|
||||
import com.fuyuanshen.global.mqtt.base.MqttRuleContext;
|
||||
import com.fuyuanshen.global.mqtt.config.MqttGateway;
|
||||
import com.fuyuanshen.global.mqtt.constants.DeviceRedisKeyConstants;
|
||||
import com.fuyuanshen.global.mqtt.constants.LightingCommandTypeConstants;
|
||||
import com.fuyuanshen.global.mqtt.constants.MqttConstants;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
@ -118,12 +120,16 @@ public class LocationDataRule implements MqttMessageRule {
|
||||
|
||||
|
||||
// 将位置信息存储到Redis中
|
||||
String redisKey = "device:location:" + deviceImei;
|
||||
String redisKey = GlobalConstants.GLOBAL_REDIS_KEY+DeviceRedisKeyConstants.DEVICE_LOCATION_KEY_PREFIX + deviceImei;
|
||||
String locationJson = JsonUtils.toJsonString(locationInfo);
|
||||
|
||||
// 存储到Redis
|
||||
RedisUtils.setCacheObject(redisKey, locationJson, Duration.ofSeconds(24 * 60 * 60));
|
||||
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));
|
||||
log.info("位置信息已异步发送到Redis: device={}, lat={}, lon={}", deviceImei, latitude, longitude);
|
||||
} catch (Exception e) {
|
||||
log.error("异步发送位置信息到Redis时出错: device={}, error={}", deviceImei, e.getMessage(), e);
|
||||
@ -138,9 +144,11 @@ public class LocationDataRule implements MqttMessageRule {
|
||||
ArrayList<Integer> intData = new ArrayList<>();
|
||||
intData.add(11);
|
||||
intData.add(Integer.parseInt(latArr[0]));
|
||||
intData.add(Integer.parseInt(latArr[1]));
|
||||
String str1 = latArr[1];
|
||||
intData.add(Integer.parseInt(str1.substring(0,4)));
|
||||
String str2 = lonArr[1];
|
||||
intData.add(Integer.parseInt(lonArr[0]));
|
||||
intData.add(Integer.parseInt(lonArr[1]));
|
||||
intData.add(Integer.parseInt(str2.substring(0,4)));
|
||||
|
||||
Map<String, Object> map = new HashMap<>();
|
||||
map.put("instruct", intData);
|
||||
|
Reference in New Issue
Block a user