sean.zhou
2022-03-28 7d0facde2378bf5e3709306ed0dfbd9f59967d48
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
package com.dji.sample.manage.handler;
 
import com.dji.sample.component.mqtt.model.TopicStateReceiver;
import com.dji.sample.manage.model.enums.StateDataEnum;
import com.dji.sample.manage.model.receiver.CapacityDeviceReceiver;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
 
/**
 * @author sean
 * @version 0.3
 * @date 2022/2/21
 */
@Service
public class StateLiveCapacityHandler extends AbstractStateTopicHandler {
 
    private static final String DEVICE_LIST = "device_list";
 
    protected StateLiveCapacityHandler(@Autowired @Qualifier("stateDefaultHandler") AbstractStateTopicHandler handler) {
        super(handler);
    }
 
    @Override
    public TopicStateReceiver handleState(JsonNode dataNode, TopicStateReceiver stateReceiver, String sn) throws JsonProcessingException {
        String name = dataNode.fieldNames().next();
        JsonNode childNode = dataNode.findPath(name);
        // Determine if it is live capacity data based on name.
        if (name.equals(StateDataEnum.LIVE_CAPACITY.getDesc())) {
            JsonNode deviceNode = childNode.findPath(DEVICE_LIST);
            stateReceiver.setData(
                    mapper.treeToValue(deviceNode.get(0), CapacityDeviceReceiver.class));
            return stateReceiver;
        }
        return handler.handleState(dataNode, stateReceiver, sn);
    }
}