设备mqtt收发数据
This commit is contained in:
@ -9,7 +9,7 @@ public interface MqttMessageRule {
|
||||
* 获取命令类型
|
||||
* @return 命令类型
|
||||
*/
|
||||
Integer getCommandType();
|
||||
String getCommandType();
|
||||
/**
|
||||
* 执行处理
|
||||
* @param context 处理上下文
|
||||
|
@ -13,11 +13,12 @@ import java.util.List;
|
||||
public class MqttRuleEngine {
|
||||
|
||||
|
||||
private final LinkedHashMap<Integer, MqttMessageRule> rulesMap = new LinkedHashMap<>();
|
||||
private final LinkedHashMap<String, MqttMessageRule> rulesMap = new LinkedHashMap<>();
|
||||
public MqttRuleEngine(List<MqttMessageRule> rules) {
|
||||
// 按优先级排序
|
||||
rules.sort(Comparator.comparing(MqttMessageRule::getPriority));
|
||||
rules.forEach(rule -> rulesMap.put(rule.getCommandType(), rule));
|
||||
rules.forEach(rule -> rulesMap.put(rule.getCommandType(), rule)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -27,7 +28,7 @@ public class MqttRuleEngine {
|
||||
*/
|
||||
public boolean executeRule(MqttRuleContext context) {
|
||||
int commandType = context.getCommandType();
|
||||
MqttMessageRule mqttMessageRule = rulesMap.get(commandType);
|
||||
MqttMessageRule mqttMessageRule = rulesMap.get("Light_"+commandType);
|
||||
if (mqttMessageRule != null) {
|
||||
mqttMessageRule.execute(context);
|
||||
return true;
|
||||
|
@ -1,78 +0,0 @@
|
||||
package com.fuyuanshen.global.mqtt.constants;
|
||||
|
||||
/**
|
||||
* 设备命令类型常量
|
||||
* Device Command Type Constants
|
||||
*/
|
||||
public class DeviceCommandTypeConstants {
|
||||
|
||||
/**
|
||||
* 灯光模式 (Light Mode)
|
||||
*/
|
||||
public static final int LIGHT_MODE = 1;
|
||||
|
||||
/**
|
||||
* 人员信息 (Personnel Information)
|
||||
*/
|
||||
public static final int PERSONNEL_INFO = 2;
|
||||
|
||||
/**
|
||||
* 开机LOGO (Boot Logo)
|
||||
*/
|
||||
public static final int BOOT_LOGO = 3;
|
||||
|
||||
/**
|
||||
* 激光灯 (Laser Light)
|
||||
*/
|
||||
public static final int LASER_LIGHT = 4;
|
||||
|
||||
/**
|
||||
* 主灯亮度 (Main Light Brightness)
|
||||
*/
|
||||
public static final int MAIN_LIGHT_BRIGHTNESS = 5;
|
||||
|
||||
/**
|
||||
* 定位数据 (Location Data)
|
||||
*/
|
||||
public static final int LOCATION_DATA = 11;
|
||||
|
||||
/**
|
||||
* 获取命令类型描述
|
||||
*
|
||||
* @param commandType 命令类型
|
||||
* @return 命令类型描述
|
||||
*/
|
||||
public static String getCommandTypeDescription(int commandType) {
|
||||
switch (commandType) {
|
||||
case LIGHT_MODE:
|
||||
return "灯光模式 (Light Mode)";
|
||||
case PERSONNEL_INFO:
|
||||
return "人员信息 (Personnel Information)";
|
||||
case BOOT_LOGO:
|
||||
return "开机LOGO (Boot Logo)";
|
||||
case LASER_LIGHT:
|
||||
return "激光灯 (Laser Light)";
|
||||
case MAIN_LIGHT_BRIGHTNESS:
|
||||
return "主灯亮度 (Main Light Brightness)";
|
||||
case LOCATION_DATA:
|
||||
return "定位数据 (Location Data)";
|
||||
default:
|
||||
return "未知命令类型 (Unknown Command Type)";
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查是否为有效命令类型
|
||||
*
|
||||
* @param commandType 命令类型
|
||||
* @return 是否有效
|
||||
*/
|
||||
public static boolean isValidCommandType(int commandType) {
|
||||
return commandType == LIGHT_MODE ||
|
||||
commandType == PERSONNEL_INFO ||
|
||||
commandType == BOOT_LOGO ||
|
||||
commandType == LASER_LIGHT ||
|
||||
commandType == MAIN_LIGHT_BRIGHTNESS ||
|
||||
commandType == LOCATION_DATA;
|
||||
}
|
||||
}
|
@ -0,0 +1,76 @@
|
||||
package com.fuyuanshen.global.mqtt.constants;
|
||||
|
||||
/**
|
||||
* 设备命令类型常量
|
||||
* Device Command Type Constants
|
||||
*/
|
||||
public class LightingCommandTypeConstants {
|
||||
|
||||
/**
|
||||
* 灯光模式 (Light Mode)
|
||||
*/
|
||||
public static final String LIGHT_MODE = "Light_1";
|
||||
|
||||
/**
|
||||
* 人员信息 (Personnel Information)
|
||||
*/
|
||||
public static final String PERSONNEL_INFO = "Light_2";
|
||||
|
||||
/**
|
||||
* 开机LOGO (Boot Logo)
|
||||
*/
|
||||
public static final String BOOT_LOGO = "Light_3";
|
||||
|
||||
/**
|
||||
* 激光灯 (Laser Light)
|
||||
*/
|
||||
public static final String LASER_LIGHT = "Light_4";
|
||||
|
||||
/**
|
||||
* 主灯亮度 (Main Light Brightness)
|
||||
*/
|
||||
public static final String MAIN_LIGHT_BRIGHTNESS = "Light_5";
|
||||
|
||||
/**
|
||||
* 定位数据 (Location Data)
|
||||
*/
|
||||
public static final String LOCATION_DATA = "Light_11";
|
||||
|
||||
/**
|
||||
* 主动上报设备数据 (Active Reporting Device Data)
|
||||
*/
|
||||
public static final String ACTIVE_REPORTING_DEVICE_DATA = "Light_12";
|
||||
|
||||
/**
|
||||
* 获取命令类型描述
|
||||
*
|
||||
* @param commandType 命令类型
|
||||
* @return 命令类型描述
|
||||
*/
|
||||
public static String getCommandTypeDescription(String commandType) {
|
||||
return switch (commandType) {
|
||||
case LIGHT_MODE -> "灯光模式 (Light Mode)";
|
||||
case PERSONNEL_INFO -> "人员信息 (Personnel Information)";
|
||||
case BOOT_LOGO -> "开机LOGO (Boot Logo)";
|
||||
case LASER_LIGHT -> "激光灯 (Laser Light)";
|
||||
case MAIN_LIGHT_BRIGHTNESS -> "主灯亮度 (Main Light Brightness)";
|
||||
case LOCATION_DATA -> "定位数据 (Location Data)";
|
||||
default -> "未知命令类型 (Unknown Command Type)";
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查是否为有效命令类型
|
||||
*
|
||||
* @param commandType 命令类型
|
||||
* @return 是否有效
|
||||
*/
|
||||
public static boolean isValidCommandType(String commandType) {
|
||||
return commandType.equals(LIGHT_MODE) ||
|
||||
commandType.equals(PERSONNEL_INFO) ||
|
||||
commandType.equals(BOOT_LOGO) ||
|
||||
commandType.equals(LASER_LIGHT) ||
|
||||
commandType.equals(MAIN_LIGHT_BRIGHTNESS) ||
|
||||
commandType.equals(LOCATION_DATA);
|
||||
}
|
||||
}
|
@ -0,0 +1,95 @@
|
||||
package com.fuyuanshen.global.mqtt.rule;
|
||||
|
||||
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.LightingCommandTypeConstants;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.time.Duration;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
|
||||
/**
|
||||
* 主动上报设备数据命令处理
|
||||
* "第1位为12表示设备主动上报设备硬件状态,第2为表示当时设备主灯档位,第3位表示当时激光灯档位,第4位电量百分比,第5位为充电状态(0没有充电,1正在充电,2为已充满)
|
||||
* 第6位200代表电池剩余续航时间200分钟"
|
||||
*/
|
||||
@Component
|
||||
@RequiredArgsConstructor
|
||||
@Slf4j
|
||||
public class ActiveReportingDeviceDataRule implements MqttMessageRule {
|
||||
|
||||
private final MqttGateway mqttGateway;
|
||||
|
||||
|
||||
@Override
|
||||
public String getCommandType() {
|
||||
return LightingCommandTypeConstants.ACTIVE_REPORTING_DEVICE_DATA;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void execute(MqttRuleContext context) {
|
||||
try {
|
||||
Object[] convertArr = context.getConvertArr();
|
||||
// Latitude, longitude
|
||||
//主灯档位,激光灯档位,电量百分比,充电状态,电池剩余续航时间
|
||||
String mainLightMode = convertArr[1].toString();
|
||||
String laserLightMode = convertArr[2].toString();
|
||||
String batteryPercentage = convertArr[3].toString();
|
||||
String chargeState = convertArr[4].toString();
|
||||
String batteryRemainingTime = convertArr[5].toString();
|
||||
|
||||
// 异步发送设备状态和位置信息到Redis
|
||||
asyncSendDeviceDataToRedisWithFuture(context.getDeviceImei(), mainLightMode, laserLightMode,
|
||||
batteryPercentage, chargeState, batteryRemainingTime);
|
||||
} catch (Exception e) {
|
||||
log.error("处理定位数据命令时出错", e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 异步发送设备状态信息和位置信息到Redis(使用CompletableFuture)
|
||||
*
|
||||
* @param deviceImei 设备IMEI
|
||||
* @param mainLightMode 主灯档位
|
||||
* @param laserLightMode 激光灯档位
|
||||
* @param batteryPercentage 电量百分比
|
||||
* @param chargeState 充电状态
|
||||
* @param batteryRemainingTime 电池剩余续航时间
|
||||
*/
|
||||
public void asyncSendDeviceDataToRedisWithFuture(String deviceImei, String mainLightMode, String laserLightMode,
|
||||
String batteryPercentage, String chargeState, String batteryRemainingTime) {
|
||||
CompletableFuture.runAsync(() -> {
|
||||
try {
|
||||
// 构造设备状态信息对象
|
||||
Map<String, Object> 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 = "device:status:" + deviceImei;
|
||||
String deviceInfoJson = JsonUtils.toJsonString(deviceInfo);
|
||||
|
||||
// 存储到Redis,设置过期时间(例如24小时)
|
||||
RedisUtils.setCacheObject(deviceRedisKey, deviceInfoJson, Duration.ofSeconds(24 * 60 * 60));
|
||||
|
||||
log.info("设备状态信息已异步发送到Redis: device={}, mainLightMode={}, laserLightMode={}, batteryPercentage={}",
|
||||
deviceImei, mainLightMode, laserLightMode, batteryPercentage);
|
||||
} catch (Exception e) {
|
||||
log.error("异步发送设备信息到Redis时出错: device={}, error={}", deviceImei, e.getMessage(), e);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
}
|
@ -1,12 +1,14 @@
|
||||
package com.fuyuanshen.global.mqtt.rule;
|
||||
|
||||
import com.fuyuanshen.common.core.utils.StringUtils;
|
||||
import com.fuyuanshen.common.json.utils.JsonUtils;
|
||||
import com.fuyuanshen.common.redis.utils.RedisUtils;
|
||||
import com.fuyuanshen.equipment.utils.c.map.GetAddressFromLatUtil;
|
||||
import com.fuyuanshen.equipment.utils.c.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.DeviceCommandTypeConstants;
|
||||
import com.fuyuanshen.global.mqtt.constants.LightingCommandTypeConstants;
|
||||
import com.fuyuanshen.global.mqtt.constants.MqttConstants;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
@ -31,8 +33,8 @@ public class LocationDataRule implements MqttMessageRule {
|
||||
|
||||
|
||||
@Override
|
||||
public Integer getCommandType() {
|
||||
return DeviceCommandTypeConstants.LOCATION_DATA;
|
||||
public String getCommandType() {
|
||||
return LightingCommandTypeConstants.LOCATION_DATA;
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -101,12 +103,16 @@ public class LocationDataRule implements MqttMessageRule {
|
||||
public void asyncSendLocationToRedisWithFuture(String deviceImei, String latitude, String longitude) {
|
||||
CompletableFuture.runAsync(() -> {
|
||||
try {
|
||||
if(StringUtils.isNotBlank(latitude) || StringUtils.isNotBlank(longitude)){
|
||||
return;
|
||||
}
|
||||
// 构造位置信息对象
|
||||
Map<String, Object> locationInfo = new LinkedHashMap<>();
|
||||
double[] doubles = LngLonUtil.gps84_To_Gcj02(Double.parseDouble(latitude), Double.parseDouble(longitude));
|
||||
locationInfo.put("deviceImei", deviceImei);
|
||||
locationInfo.put("latitude", latitude);
|
||||
locationInfo.put("longitude", longitude);
|
||||
String address = GetAddressFromLatUtil.getAdd(longitude, latitude);
|
||||
locationInfo.put("latitude", doubles[0]);
|
||||
locationInfo.put("longitude", doubles[1]);
|
||||
String address = GetAddressFromLatUtil.getAdd(String.valueOf(doubles[1]), String.valueOf(doubles[0]));
|
||||
locationInfo.put("address", address);
|
||||
locationInfo.put("timestamp", System.currentTimeMillis());
|
||||
|
||||
@ -130,7 +136,7 @@ public class LocationDataRule implements MqttMessageRule {
|
||||
String[] lonArr = longitude.split("\\.");
|
||||
|
||||
ArrayList<Integer> intData = new ArrayList<>();
|
||||
intData.add(DeviceCommandTypeConstants.LOCATION_DATA);
|
||||
intData.add(11);
|
||||
intData.add(Integer.parseInt(latArr[0]));
|
||||
intData.add(Integer.parseInt(latArr[1]));
|
||||
intData.add(Integer.parseInt(lonArr[0]));
|
||||
|
@ -7,7 +7,7 @@ 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.DeviceCommandTypeConstants;
|
||||
import com.fuyuanshen.global.mqtt.constants.LightingCommandTypeConstants;
|
||||
import com.fuyuanshen.global.mqtt.constants.MqttConstants;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
@ -31,8 +31,8 @@ public class PersonnelInfoRule implements MqttMessageRule {
|
||||
private final MqttGateway mqttGateway;
|
||||
|
||||
@Override
|
||||
public Integer getCommandType() {
|
||||
return DeviceCommandTypeConstants.PERSONNEL_INFO;
|
||||
public String getCommandType() {
|
||||
return LightingCommandTypeConstants.PERSONNEL_INFO;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
Reference in New Issue
Block a user