puzhibing
5 天以前 8c6849d98fc168369d6de2d94d2f42a6d67cd9c5
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package com.ruoyi.dataInterchange.util.mqtt;
 
import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.dataInterchange.server.WarnMsgService;
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 javax.annotation.Resource;
 
/**
 * @author zhibing.pu
 * @Date 2025/5/23 17:30
 */
@Slf4j
@Component
public class MqttReceiverMessageHandler implements MessageHandler {
    
    @Resource
    private WarnMsgService warnMsgService;
    
    @Override
    public void handleMessage(Message<?> message) throws MessagingException {
        MessageHeaders headers = message.getHeaders();
        log.error("线程名称:{},收到消息,主题:{},消息:{}", Thread.currentThread().getName(), headers.get("mqtt_receivedTopic").toString(), message.getPayload());
        // log.info("收到消息主题:{}", headers.get("mqtt_receivedTopic").toString());
        // log.info("收到消息:{}", message.getPayload());
        String s = message.getPayload().toString();
        JSONObject jsonObject = JSONObject.parseObject(s);
        String method = jsonObject.getString("method");
        JSONObject params = jsonObject.getJSONObject("params");
        String ability = params.getString("ability");
        switch (ability){
            case "event_msa_alarm":
                warnMsgService.saveWarnMsgService(jsonObject);
                break;
            case "event_msa_pic":
                warnMsgService.saveWarnMsgPicService(jsonObject);
                break;
        }
    }
}