设备日志
This commit is contained in:
@ -0,0 +1,56 @@
|
||||
package com.fuyuanshen.web.config;
|
||||
|
||||
import cn.hutool.core.lang.UUID;
|
||||
import com.fuyuanshen.global.mqtt.config.MqttPropertiesConfig;
|
||||
import com.fuyuanshen.web.handler.mqtt.DeviceReceiverMessageHandler;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.integration.annotation.ServiceActivator;
|
||||
import org.springframework.integration.channel.DirectChannel;
|
||||
import org.springframework.integration.core.MessageProducer;
|
||||
import org.springframework.integration.mqtt.core.MqttPahoClientFactory;
|
||||
import org.springframework.integration.mqtt.inbound.MqttPahoMessageDrivenChannelAdapter;
|
||||
import org.springframework.integration.mqtt.support.DefaultPahoMessageConverter;
|
||||
import org.springframework.messaging.MessageChannel;
|
||||
import org.springframework.messaging.MessageHandler;
|
||||
|
||||
/**
|
||||
* @author: 默苍璃
|
||||
* @date: 2025-08-0110:46
|
||||
*/
|
||||
@Configuration
|
||||
public class CustomMqttInboundConfiguration {
|
||||
@Autowired
|
||||
private MqttPropertiesConfig mqttPropertiesConfig;
|
||||
@Autowired
|
||||
private MqttPahoClientFactory mqttPahoClientFactory;
|
||||
@Autowired
|
||||
private DeviceReceiverMessageHandler deviceReceiverMessageHandler;
|
||||
|
||||
@Bean
|
||||
public MessageChannel customMqttChannel(){
|
||||
return new DirectChannel();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public MessageProducer customMessageProducer(){
|
||||
String clientId = "custom_client_" + UUID.fastUUID();
|
||||
MqttPahoMessageDrivenChannelAdapter adapter = new MqttPahoMessageDrivenChannelAdapter(
|
||||
mqttPropertiesConfig.getUrl(),
|
||||
clientId,
|
||||
mqttPahoClientFactory,
|
||||
"A/#", "B/#" // 直接指定这两个主题
|
||||
);
|
||||
adapter.setQos(1);
|
||||
adapter.setConverter(new DefaultPahoMessageConverter());
|
||||
adapter.setOutputChannel(customMqttChannel());
|
||||
return adapter;
|
||||
}
|
||||
|
||||
@Bean
|
||||
@ServiceActivator(inputChannel = "customMqttChannel")
|
||||
public MessageHandler customMessageHandler(){
|
||||
return deviceReceiverMessageHandler;
|
||||
}
|
||||
}
|
@ -0,0 +1,42 @@
|
||||
package com.fuyuanshen.web.enums;
|
||||
|
||||
/**
|
||||
* @author: 默苍璃
|
||||
* @date: 2025-08-0114:14
|
||||
*/
|
||||
public enum InstructType6170 {
|
||||
|
||||
EQUIPMENT_REPORTING(0, "设备上报"),
|
||||
LIGHT_MODE(1, "灯光模式"),
|
||||
UNIT_INFO(2, "单位/姓名/职位"),
|
||||
BOOT_IMAGE(3, "开机图片"),
|
||||
LASER_LIGHT(4, "激光灯"),
|
||||
BRIGHTNESS(5, "亮度调节"),
|
||||
LOCATION_DATA(11, "定位数据");
|
||||
|
||||
private final int code;
|
||||
private final String description;
|
||||
|
||||
InstructType6170(int code, String description) {
|
||||
this.code = code;
|
||||
this.description = description;
|
||||
}
|
||||
|
||||
public int getCode() {
|
||||
return code;
|
||||
}
|
||||
|
||||
public String getDescription() {
|
||||
return description;
|
||||
}
|
||||
|
||||
public static InstructType6170 fromCode(int code) {
|
||||
for (InstructType6170 type : values()) {
|
||||
if (type.getCode() == code) {
|
||||
return type;
|
||||
}
|
||||
}
|
||||
throw new IllegalArgumentException("未知的指令类型代码: " + code);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,45 @@
|
||||
package com.fuyuanshen.web.enums;
|
||||
|
||||
/**
|
||||
* @author: 默苍璃
|
||||
* @date: 2025-08-0114:30
|
||||
*/
|
||||
public enum LightModeEnum6170 {
|
||||
|
||||
OFF(0, "关灯"),
|
||||
HIGH_BEAM(1, "强光模式"),
|
||||
LOW_BEAM(2, "弱光模式"),
|
||||
STROBE(3, "爆闪模式"),
|
||||
FLOOD(4, "泛光模式"),
|
||||
|
||||
UNKNOWN(-1, "未知的灯光模式");
|
||||
|
||||
|
||||
private final int code;
|
||||
private final String description;
|
||||
|
||||
|
||||
LightModeEnum6170(int code, String description) {
|
||||
this.code = code;
|
||||
this.description = description;
|
||||
}
|
||||
|
||||
public int getCode() {
|
||||
return code;
|
||||
}
|
||||
|
||||
public String getDescription() {
|
||||
return description;
|
||||
}
|
||||
|
||||
public static LightModeEnum6170 fromCode(int code) {
|
||||
for (LightModeEnum6170 mode : values()) {
|
||||
if (mode.getCode() == code) {
|
||||
return mode;
|
||||
}
|
||||
}
|
||||
// throw new IllegalArgumentException("未知的灯光模式代码: " + code);
|
||||
return UNKNOWN;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,236 @@
|
||||
package com.fuyuanshen.web.handler.mqtt;
|
||||
|
||||
import cn.hutool.core.collection.CollectionUtil;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fuyuanshen.app.domain.APPDevice;
|
||||
import com.fuyuanshen.app.enums.UserType;
|
||||
import com.fuyuanshen.equipment.domain.Device;
|
||||
import com.fuyuanshen.equipment.domain.DeviceLog;
|
||||
import com.fuyuanshen.equipment.mapper.DeviceLogMapper;
|
||||
import com.fuyuanshen.equipment.mapper.DeviceMapper;
|
||||
import com.fuyuanshen.web.enums.InstructType6170;
|
||||
import com.fuyuanshen.web.enums.LightModeEnum6170;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.messaging.Message;
|
||||
import org.springframework.messaging.MessageHandler;
|
||||
import org.springframework.messaging.MessageHeaders;
|
||||
import org.springframework.messaging.MessagingException;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 定义监听主题消息的处理器
|
||||
*
|
||||
* @author: 默苍璃
|
||||
* @date: 2025-08-0110:19
|
||||
*/
|
||||
@Component
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
@Slf4j
|
||||
public class DeviceReceiverMessageHandler implements MessageHandler {
|
||||
|
||||
private final DeviceMapper deviceMapper;
|
||||
private final DeviceLogMapper deviceLogMapper;
|
||||
|
||||
// 使用Jackson解析JSON
|
||||
private static final ObjectMapper objectMapper = new ObjectMapper();
|
||||
|
||||
|
||||
/**
|
||||
* 处理接收的消息
|
||||
*
|
||||
* @param message
|
||||
* @throws MessagingException
|
||||
*/
|
||||
@Override
|
||||
public void handleMessage(Message<?> message) throws MessagingException {
|
||||
// System.out.println("接收到的消息:" + message.getPayload());
|
||||
MessageHeaders headers = message.getHeaders();
|
||||
String receivedTopicName = (String) headers.get("mqtt_receivedTopic");
|
||||
System.out.println("消息来自主题:" + receivedTopicName);
|
||||
|
||||
String payload = message.getPayload().toString();
|
||||
|
||||
if (receivedTopicName != null) {
|
||||
// 1. 提取设备ID (从主题中获取)
|
||||
String deviceImei = extractDeviceId(receivedTopicName);
|
||||
Device device = deviceMapper.selectOne(new QueryWrapper<Device>().eq("device_imei", deviceImei));
|
||||
if (device == null) {
|
||||
log.info("不存在的设备IMEI: {}", deviceImei);
|
||||
} else {
|
||||
|
||||
try {
|
||||
JsonNode root = objectMapper.readTree(payload);
|
||||
|
||||
// 2. 处理instruct消息
|
||||
if (root.has("instruct")) {
|
||||
JsonNode instructNode = root.get("instruct");
|
||||
if (instructNode.isArray()) {
|
||||
DeviceLog record = parseInstruct(device, instructNode);
|
||||
// 手动设置租户ID
|
||||
record.setTenantId(device.getTenantId()); // 从设备信息中获取租户ID
|
||||
// 设备ID
|
||||
record.setDeviceId(device.getId());
|
||||
|
||||
// 根据不同主题进行不同处理
|
||||
if (receivedTopicName.startsWith("A/")) {
|
||||
// 处理A主题的消息(设备上传)
|
||||
record.setDataSource("设备上报");
|
||||
} else if (receivedTopicName.startsWith("B/")) {
|
||||
// 处理B主题的消息 (手动上传)
|
||||
record.setDataSource("客户端操作");
|
||||
}
|
||||
|
||||
deviceLogMapper.insert(record);
|
||||
}
|
||||
}
|
||||
// 3. 处理state消息
|
||||
// else if (root.has("state")) {
|
||||
// JsonNode stateNode = root.get("state");
|
||||
// if (stateNode.isArray()) {
|
||||
// StateRecord record = parseState(device, stateNode);
|
||||
// stateRepo.save(record);
|
||||
// }
|
||||
// }
|
||||
} catch (Exception e) {
|
||||
log.error("消息解析失败: {}", payload, e);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 从主题中提取设备ID(IMEI)
|
||||
*
|
||||
* @param topic
|
||||
* @return
|
||||
*/
|
||||
private String extractDeviceId(String topic) {
|
||||
// 处理 A/# 或 B/# 格式的主题,例如 B/861556078765285 或 A/861556078765285
|
||||
String[] segments = topic.split("/");
|
||||
if (segments.length >= 2) {
|
||||
// 返回第二个段,即 / 后面的部分
|
||||
return segments[1];
|
||||
}
|
||||
// 如果格式不符合预期,返回原主题
|
||||
return topic;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 解析instruct消息
|
||||
*
|
||||
* @param device
|
||||
* @param array
|
||||
* @return
|
||||
*/
|
||||
private DeviceLog parseInstruct(Device device, JsonNode array) {
|
||||
DeviceLog record = new DeviceLog();
|
||||
record.setDeviceName(device.getDeviceName());
|
||||
// 设备行为
|
||||
record.setDeviceAction(InstructType6170.fromCode(array.get(0).asInt()).getDescription());
|
||||
|
||||
switch (array.get(0).asInt()) {
|
||||
case 1: // 灯光模式
|
||||
LightModeEnum6170 lightModeEnum6170 = LightModeEnum6170.fromCode(array.get(1).asInt());
|
||||
record.setContent(lightModeEnum6170.getDescription());
|
||||
break;
|
||||
|
||||
case 2: // 单位/姓名/职位
|
||||
byte[] unitBytes = new byte[480];
|
||||
for (int i = 1; i <= 480; i++) {
|
||||
unitBytes[i - 1] = (byte) array.get(i).asInt();
|
||||
}
|
||||
// record.setUnitData(unitBytes);
|
||||
break;
|
||||
|
||||
case 3: // 开机图片
|
||||
// record.setImagePage(array.get(1).asInt());
|
||||
byte[] imageBytes = new byte[512];
|
||||
for (int i = 2; i <= 513; i++) {
|
||||
imageBytes[i - 2] = (byte) array.get(i).asInt();
|
||||
}
|
||||
// record.setImageData(imageBytes);
|
||||
break;
|
||||
|
||||
case 4: // 激光灯
|
||||
// record.setLaserLight(array.get(1).asInt() == 1);
|
||||
break;
|
||||
|
||||
case 5: // 亮度调节
|
||||
// record.setBrightness(array.get(1).asInt());
|
||||
break;
|
||||
|
||||
case 11: // 定位数据
|
||||
// record.setLatitudeDeg(array.get(1).asInt());
|
||||
// record.setLatitudeMin(new BigDecimal(array.get(2).asDouble()));
|
||||
// record.setLongitudeDeg(array.get(3).asInt());
|
||||
// record.setLongitudeMin(new BigDecimal(array.get(4).asDouble()));
|
||||
break;
|
||||
}
|
||||
return record;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 解析state消息
|
||||
*
|
||||
* @param device
|
||||
* @param array
|
||||
* @return
|
||||
*/
|
||||
// private StateRecord parseState(Device device, JsonNode array) {
|
||||
// StateRecord record = new StateRecord();
|
||||
// record.setDevice(device);
|
||||
// record.setStateType(array.get(0).asInt());
|
||||
//
|
||||
// switch (record.getStateType()) {
|
||||
// case 1: // 灯光状态
|
||||
// record.setLightMode(array.get(1).asInt());
|
||||
// record.setBrightness(array.get(2).asInt());
|
||||
// break;
|
||||
//
|
||||
// case 2: // 设置结果
|
||||
// record.setSetResult(array.get(1).asInt() == 1);
|
||||
// break;
|
||||
//
|
||||
// case 3: // 图片更新状态
|
||||
// record.setImagePage(array.get(1).asInt());
|
||||
// break;
|
||||
//
|
||||
// case 4: // 激光灯状态
|
||||
// record.setLaserStatus(array.get(1).asInt() == 1);
|
||||
// break;
|
||||
//
|
||||
// case 5: // 亮度状态
|
||||
// record.setBrightness(array.get(1).asInt());
|
||||
// break;
|
||||
//
|
||||
// case 11: // 定位上报
|
||||
// record.setLatitude(array.get(1).asDouble());
|
||||
// record.setLongitude(array.get(2).asDouble());
|
||||
// break;
|
||||
//
|
||||
// case 12: // 设备状态
|
||||
// record.setMainLightGear(array.get(1).asInt());
|
||||
// record.setLaserLightGear(array.get(2).asInt());
|
||||
// record.setBattery(array.get(3).asInt());
|
||||
// record.setChargeStatus(array.get(4).asInt());
|
||||
// record.setDuration(array.get(5).asInt());
|
||||
// break;
|
||||
// }
|
||||
// return record;
|
||||
// }
|
||||
|
||||
|
||||
}
|
@ -303,6 +303,6 @@ mqtt:
|
||||
password: #YtvpSfCNG
|
||||
url: tcp://47.120.79.150:2883
|
||||
subClientId: fys_subClient
|
||||
subTopic: A/#,worker/location/#
|
||||
subTopic: A/#,B/#,worker/location/#
|
||||
pubTopic: B/#
|
||||
pubClientId: fys_pubClient
|
@ -82,6 +82,10 @@ public class Device extends TenantEntity {
|
||||
@Schema(name = "蓝牙名称")
|
||||
private String bluetoothName;
|
||||
|
||||
/**
|
||||
* 设备IMEI
|
||||
* device_imei
|
||||
*/
|
||||
@Schema(name = "设备IMEI")
|
||||
private String deviceImei;
|
||||
|
||||
|
@ -2,6 +2,7 @@ package com.fuyuanshen.equipment.domain;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.*;
|
||||
import com.fuyuanshen.common.mybatis.core.domain.BaseEntity;
|
||||
import com.fuyuanshen.common.tenant.core.TenantEntity;
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
|
||||
@ -16,7 +17,7 @@ import java.io.Serial;
|
||||
@Data
|
||||
@EqualsAndHashCode(callSuper = true)
|
||||
@TableName("device_log")
|
||||
public class DeviceLog extends BaseEntity {
|
||||
public class DeviceLog extends TenantEntity {
|
||||
|
||||
@Serial
|
||||
private static final long serialVersionUID = 1L;
|
||||
@ -27,10 +28,16 @@ public class DeviceLog extends BaseEntity {
|
||||
@TableId(value = "id")
|
||||
private Long id;
|
||||
|
||||
/**
|
||||
* 设备ID
|
||||
*/
|
||||
private Long deviceId;
|
||||
|
||||
/**
|
||||
* 设备行为
|
||||
*/
|
||||
private String deviceAction;
|
||||
// private Integer deviceActionInt;
|
||||
|
||||
/**
|
||||
* 设备名称
|
||||
@ -47,5 +54,4 @@ public class DeviceLog extends BaseEntity {
|
||||
*/
|
||||
private String content;
|
||||
|
||||
|
||||
}
|
||||
|
@ -1,5 +1,7 @@
|
||||
package com.fuyuanshen.equipment.domain.vo;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.FieldFill;
|
||||
import com.baomidou.mybatisplus.annotation.TableField;
|
||||
import com.fuyuanshen.equipment.domain.DeviceLog;
|
||||
import cn.idev.excel.annotation.ExcelIgnoreUnannotated;
|
||||
import cn.idev.excel.annotation.ExcelProperty;
|
||||
@ -58,5 +60,10 @@ public class DeviceLogVo implements Serializable {
|
||||
@ExcelProperty(value = "内容")
|
||||
private String content;
|
||||
|
||||
/**
|
||||
* 创建时间
|
||||
*/
|
||||
@TableField(fill = FieldFill.INSERT)
|
||||
private Date createTime;
|
||||
|
||||
}
|
||||
|
@ -73,14 +73,15 @@ public class DeviceLogServiceImpl implements IDeviceLogService {
|
||||
private LambdaQueryWrapper<DeviceLog> buildQueryWrapper(DeviceLogBo bo) {
|
||||
Map<String, Object> params = bo.getParams();
|
||||
LambdaQueryWrapper<DeviceLog> lqw = Wrappers.lambdaQuery();
|
||||
lqw.orderByAsc(DeviceLog::getId);
|
||||
lqw.eq(StringUtils.isNotBlank(bo.getDeviceAction()), DeviceLog::getDeviceAction, bo.getDeviceAction());
|
||||
lqw.orderByDesc(DeviceLog::getCreateTime);
|
||||
lqw.like(StringUtils.isNotBlank(bo.getDeviceAction()), DeviceLog::getDeviceAction, bo.getDeviceAction());
|
||||
lqw.like(StringUtils.isNotBlank(bo.getDeviceName()), DeviceLog::getDeviceName, bo.getDeviceName());
|
||||
lqw.eq(StringUtils.isNotBlank(bo.getDataSource()), DeviceLog::getDataSource, bo.getDataSource());
|
||||
lqw.eq(StringUtils.isNotBlank(bo.getContent()), DeviceLog::getContent, bo.getContent());
|
||||
lqw.like(StringUtils.isNotBlank(bo.getContent()), DeviceLog::getContent, bo.getContent());
|
||||
return lqw;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 新增设备日志
|
||||
*
|
||||
|
Reference in New Issue
Block a user