Commit 66424764 by 姚冰

[add] 设备离线添加处理入库

1 parent cdf7edc0
package vion.constant;
public enum EventStatusEnum {
UNKNOWN((short) -1, "未知"),
SUCCESS((short) 1, "成功"),
FAIL((short) 0, "失败"),
;
private final Short code;
private final String desc;
EventStatusEnum(Short code, String desc) {
this.code = code;
this.desc = desc;
}
public Short getCode() {
return code;
}
public String getDesc() {
return desc;
}
}
...@@ -19,7 +19,7 @@ public enum MqttMessageType { ...@@ -19,7 +19,7 @@ public enum MqttMessageType {
public static MqttMessageType getEnumByType(String type) { public static MqttMessageType getEnumByType(String type) {
return Arrays.stream(values()) return Arrays.stream(values())
.filter(e -> e.type == type) .filter(e -> e.type.equals(type))
.findFirst() .findFirst()
.orElse(null); .orElse(null);
} }
......
package vion.dto;
import lombok.Data;
@Data
public class OfflineDevice {
private String mallUid;
private String devices;
}
package vion.event.mqtt; package vion.event.mqtt;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.fasterxml.jackson.databind.JsonNode;
import net.dreamlu.iot.mqtt.codec.MqttPublishMessage; import net.dreamlu.iot.mqtt.codec.MqttPublishMessage;
import net.dreamlu.iot.mqtt.core.client.IMqttClientMessageListener; import net.dreamlu.iot.mqtt.core.client.IMqttClientMessageListener;
import net.dreamlu.iot.mqtt.spring.client.MqttClientSubscribe; import net.dreamlu.iot.mqtt.spring.client.MqttClientSubscribe;
import org.dromara.hutool.json.JSON; import org.dromara.hutool.core.bean.BeanUtil;
import org.json.JSONArray;
import org.json.JSONObject;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.tio.core.ChannelContext; import org.tio.core.ChannelContext;
import vion.constant.EventStatusEnum;
import vion.constant.MqttMessageType; import vion.constant.MqttMessageType;
import vion.dto.OfflineDevice;
import vion.model.monitor.EventRecord; import vion.model.monitor.EventRecord;
import vion.service.monitor.IEventRecordService;
import vion.utils.JsonUtil; import vion.utils.JsonUtil;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.util.List;
import static vion.constant.MqttMessageType.WHOLEDAYA_ANALYZE; import static vion.constant.MqttMessageType.WHOLEDAYA_ANALYZE;
...@@ -28,21 +33,26 @@ import static vion.constant.MqttMessageType.WHOLEDAYA_ANALYZE; ...@@ -28,21 +33,26 @@ import static vion.constant.MqttMessageType.WHOLEDAYA_ANALYZE;
public class MqttClientMessageListener implements IMqttClientMessageListener { public class MqttClientMessageListener implements IMqttClientMessageListener {
private static final Logger logger = LoggerFactory.getLogger(MqttClientMessageListener.class); private static final Logger logger = LoggerFactory.getLogger(MqttClientMessageListener.class);
@Autowired
private IEventRecordService recordService;
@Override @Override
public void onMessage(ChannelContext context, String topic, MqttPublishMessage message, byte[] payload) { public void onMessage(ChannelContext context, String topic, MqttPublishMessage message, byte[] payload) {
String payloadStr = new String(payload, StandardCharsets.UTF_8); String payloadStr = new String(payload, StandardCharsets.UTF_8);
logger.info("topic:{} payload:{}", topic, payloadStr); logger.info("topic:{} payload:{}", topic, payloadStr);
try { try {
JSONObject object = new JSONObject(payloadStr); JsonNode object = JsonUtil.parseTree(payloadStr);
String type = object.getString("type"); String type = object.get("eventType").asText();
switch (MqttMessageType.getEnumByType(type)) { switch (MqttMessageType.getEnumByType(type)) {
case MqttMessageType.DEVICE_OFFLINE: case MqttMessageType.DEVICE_OFFLINE:
//设备离线 //设备离线
handleDeviceOffline(payloadStr);
break; break;
case MqttMessageType.WHOLEDAYA_ANALYZE: case MqttMessageType.WHOLEDAYA_ANALYZE:
//全天分析异常 //全天分析异常
handleWholeDayAnalyze(payloadStr);
break; break;
default: default:
logger.info("未定义的消息类型:{}, payload:{}", type, payloadStr); logger.info("未定义的消息类型:{}, payload:{}", type, payloadStr);
...@@ -56,7 +66,23 @@ public class MqttClientMessageListener implements IMqttClientMessageListener { ...@@ -56,7 +66,23 @@ public class MqttClientMessageListener implements IMqttClientMessageListener {
//设备离线上报 //设备离线上报
private void handleDeviceOffline(String payloadStr) { private void handleDeviceOffline(String payloadStr) {
EventRecord eventRecord = JsonUtil.parseObject(payloadStr, EventRecord.class); EventRecord eventRecord = JsonUtil.parseObject(payloadStr, EventRecord.class);
// JSONArray arrary = (JSONArray) eventRecord.getData(); String list = (String) eventRecord.getData();
List<OfflineDevice> deviceList = JsonUtil.parseArray(list, OfflineDevice.class);
for (int i = 0; i < deviceList.size(); i++) {
OfflineDevice device = deviceList.get(i);
EventRecord eventRecord1 = new EventRecord();
BeanUtil.copyProperties(eventRecord, eventRecord1);
eventRecord1.setEventUid(eventRecord.getEventUid());
eventRecord1.setData(JsonUtil.toJsonString(device));
eventRecord1.setMallUid(deviceList.get(i).getMallUid());
if (StringUtils.isEmpty(device.getDevices())) {
eventRecord1.setStatus(EventStatusEnum.SUCCESS.getCode());
} else {
eventRecord1.setStatus(EventStatusEnum.FAIL.getCode());
}
//入库
recordService.save(eventRecord1);
}
// logger.info("设备离线:{}", JSON.toJSONString(eventRecord)); // logger.info("设备离线:{}", JSON.toJSONString(eventRecord));
} }
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!