baoshiwei
2025-06-19 be00ddc83f86599916eb8d0f581f448aa74c9d51
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package com.zhitan.handler;
 
import com.fasterxml.jackson.databind.ObjectMapper;
import com.zhitan.config.mqtt.MqttTopic;
import com.zhitan.model.entity.DeviceData;
import com.zhitan.model.entity.ElectricPower;
import com.zhitan.model.entity.KtDataEntity;
import com.zhitan.model.entity.PowerEntity;
import com.zhitan.service.IDataService;
import com.zhitan.util.KtDataMapper;
import com.zhitan.util.PowerDataMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.integration.mqtt.support.MqttHeaders;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageHandler;
import org.springframework.messaging.MessagingException;
 
import java.util.ArrayList;
import java.util.List;
 
/**
 * mqtt 消息处理类
 */
@Slf4j
public class MqttMessageHandler implements MessageHandler {
    private final IDataService dataService;
 
    public MqttMessageHandler(IDataService dataService) {
        this.dataService = dataService;
    }
 
    @Override
    public void handleMessage(Message<?> message) throws MessagingException {
        String topic = (String) message.getHeaders().get(MqttHeaders.RECEIVED_TOPIC);
        String payload = (String) message.getPayload();
        System.out.println("Received message from topic " + topic + ": " + payload);
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            // 将 JSON 字符串转换为 SensorData 对象
            //ElectricPower electricPower = objectMapper.readValue(payload, ElectricPower.class);
            DeviceData data = objectMapper.readValue(payload, DeviceData.class);
            if (topic != null) {
                switch ( topic) {
                    case MqttTopic.DEFAULT_TOPIC:
                        handleDeviceUpMessage(data);
                        break;
                    case MqttTopic.DEVICE_KT_UP:
                        handleDeviceKtUpMessage(data);
                        break;
                    default:
                        log.error("Invalid topic: " + topic);
                }
            }
        } catch (Exception e) {
            log.error(e.getMessage());
        }
 
 
 
    }
 
    private void handleDeviceKtUpMessage(DeviceData data) {
        try {
            KtDataEntity ktDataEntity = KtDataMapper.mapToEntity(data);
            dataService.writeTimeSeriesData(ktDataEntity);
            // dataService.writeTimeSeriesData(electricPower)
        } catch (Exception e) {
            log.error(e.getMessage());
        }
 
    }
 
    private void handleDeviceUpMessage(DeviceData data) {
        try {
            List<PowerEntity> powerMeters = PowerDataMapper.mapToEntities(data);
            for (PowerEntity powerMeter : powerMeters) {
                dataService.writeTimeSeriesData(powerMeter);
            }
            // dataService.writeTimeSeriesData(electricPower)
        } catch (Exception e) {
            log.error(e.getMessage());
        }
    }
}