|
@@ -1,8 +1,12 @@
|
|
|
package com.szwl.event;
|
|
|
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
+import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
|
|
import com.szwl.model.entity.MqttMsg;
|
|
|
+import com.szwl.model.entity.TEquipment;
|
|
|
import com.szwl.service.MqttMsgService;
|
|
|
+import com.szwl.service.TEquipmentService;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.context.event.EventListener;
|
|
@@ -17,6 +21,9 @@ public class MqttMessageEventListener {
|
|
|
@Autowired
|
|
|
private MqttMsgService mqttMsgService;
|
|
|
|
|
|
+ @Autowired
|
|
|
+ private TEquipmentService equipmentService;
|
|
|
+
|
|
|
/**
|
|
|
* 监听主题:/connected,/disconnected,作用:监听设备上线、下线
|
|
|
*/
|
|
@@ -27,24 +34,28 @@ public class MqttMessageEventListener {
|
|
|
String payload = event.getPayload();
|
|
|
MqttMsg mqttMsg = new MqttMsg();
|
|
|
mqttMsg.setCreateDate(new Date());
|
|
|
- if (result.equals("connected")) {
|
|
|
- mqttMsg.setTopic(result);
|
|
|
- mqttMsg.setMessage("设备上线");
|
|
|
- JSONObject jsonObject = JSONObject.parseObject(payload);
|
|
|
- String clientId = jsonObject.getString("clientid");
|
|
|
- mqttMsg.setClientId(clientId);
|
|
|
- } else if (result.equals("disconnected")) {
|
|
|
- mqttMsg.setTopic(result);
|
|
|
- mqttMsg.setMessage("设备离线");
|
|
|
+ if (result.equals("connected") || result.equals("disconnected")) {
|
|
|
JSONObject jsonObject = JSONObject.parseObject(payload);
|
|
|
String clientId = jsonObject.getString("clientid");
|
|
|
- mqttMsg.setClientId(clientId);
|
|
|
+ LambdaQueryWrapper<TEquipment> query = Wrappers.lambdaQuery();
|
|
|
+ query.eq(TEquipment::getClientId, clientId);
|
|
|
+ TEquipment equipment = equipmentService.getOne(query);
|
|
|
+ if (equipment != null) {
|
|
|
+ if (result.equals("connected")) {
|
|
|
+ // 上线
|
|
|
+ equipment.setEqeStatus(1);
|
|
|
+ } else {
|
|
|
+ // 下线
|
|
|
+ equipment.setEqeStatus(0);
|
|
|
+ }
|
|
|
+ equipmentService.updateById(equipment);
|
|
|
+ }
|
|
|
} else {
|
|
|
log.info("主题:{},消息:{}", topic, payload);
|
|
|
mqttMsg.setTopic(topic);
|
|
|
mqttMsg.setMessage(payload);
|
|
|
+ mqttMsgService.save(mqttMsg);
|
|
|
}
|
|
|
- mqttMsgService.save(mqttMsg);
|
|
|
}
|
|
|
|
|
|
// 更多主题的处理逻辑
|