xieb
2024-04-25 47ff10d84efb1f08768ba2793d4f06020cd71b57
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
package com.dji.sample.component.mqtt.handler;
 
import com.dji.sample.component.mqtt.model.ChannelName;
import com.dji.sample.component.mqtt.model.CommonTopicReceiver;
import com.dji.sample.manage.model.receiver.StatusGatewayReceiver;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.integration.annotation.MessageEndpoint;
import org.springframework.integration.annotation.Router;
import org.springframework.integration.annotation.ServiceActivator;
import org.springframework.integration.mqtt.support.MqttHeaders;
import org.springframework.messaging.Message;
import org.springframework.util.CollectionUtils;
 
import static com.dji.sample.component.mqtt.model.TopicConst.*;
 
/**
 *
 * @author sean.zhou
 * @date 2021/11/12
 * @version 0.1
 */
@MessageEndpoint
public class StatusRouter {
 
    @Autowired
    private ObjectMapper mapper;
 
    /**
     * Converts the status data sent by the gateway device into an object.
     * @param message
     * @return
     */
    @ServiceActivator(inputChannel = ChannelName.INBOUND_STATUS, outputChannel = ChannelName.INBOUND_STATUS_ROUTER)
    public CommonTopicReceiver<StatusGatewayReceiver> resolveStatus(Message<?> message) {
        CommonTopicReceiver<StatusGatewayReceiver> statusReceiver = new CommonTopicReceiver<>();
        try {
            statusReceiver = mapper.readValue(
                    (byte[])message.getPayload(),
                    new TypeReference<CommonTopicReceiver<StatusGatewayReceiver>>() {});
 
            String topic = message.getHeaders().get(MqttHeaders.RECEIVED_TOPIC).toString();
 
            // set gateway's sn
            statusReceiver.getData().setSn(
                    topic.substring((BASIC_PRE + PRODUCT).length(),
                            topic.indexOf(STATUS_SUF)));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return statusReceiver;
    }
 
    /**
     * Handles the routing of status topic messages. Depending on the data, it is assigned to different channels for handling.
     * @param receiver
     * @return
     */
    @Router(inputChannel = ChannelName.INBOUND_STATUS_ROUTER)
    public String resolveStatusRouter(CommonTopicReceiver<StatusGatewayReceiver> receiver) {
        // Determine whether the drone is online or offline according to whether the data of the sub-device is empty.
        return CollectionUtils.isEmpty(receiver.getData().getSubDevices()) ?
                ChannelName.INBOUND_STATUS_OFFLINE : ChannelName.INBOUND_STATUS_ONLINE;
    }
 
}