package com.keystar.plane.inspection.mqtt;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.keystar.mqtt.annotation.MqttHandlerType;
import com.keystar.mqtt.annotation.MqttSubScribe;
import com.keystar.mqtt.handler.AbstractMqttHandler;
import com.keystar.plane.inspection.utils.RedisUtils;
import lombok.extern.slf4j.Slf4j;
import org.eclipse.paho.client.mqttv3.MqttMessage;

import java.io.IOException;
import java.util.Map;

@MqttHandlerType(
        {
                @MqttSubScribe(topic = "/70ddc6e4-1b3e-4da9-9952-16c04ec5c415/status/base", qos = 0)
        }
)
@Slf4j
public class NestMqttHandler implements AbstractMqttHandler {

    private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();


    @Override
    public void handle(MqttMessage message) throws IOException {

        String messageContent = new String(message.getPayload());

        Map<String, String> map;
        try{
            map = OBJECT_MAPPER.readValue(messageContent,Map.class);
        }catch (IOException e){
            return;
        }

        String key = "PlaneInspection:nestStatus";

        if (map.containsKey("nestStateConstant")){
            String nestState = map.get("nestStateConstant");
            RedisUtils.set(key,nestState);
        }


    }
}
