package cn.reghao.autodop.dmaster.mqttsub.impl;

import cn.reghao.autodop.common.machine.Machine;
import cn.reghao.autodop.common.msg.pub.clazz.NodePubClazz;
import cn.reghao.autodop.common.msg.pub.dto.node.NodeLogDTO;
import cn.reghao.autodop.dmaster.machine.service.NodePubClazzImpl;
import cn.reghao.jdkutil.serializer.JsonConverter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

/**
 * @author reghao
 * @date 2020-12-30 10:26:47
 */
@Slf4j
@Component
public class NodePubClazzHandler {
    private final NodePubClazzImpl nodePubClazz;

    public NodePubClazzHandler(NodePubClazzImpl nodePubClazz) {
        this.nodePubClazz = nodePubClazz;
    }

    public void process(String method, String jsonPayload) {
        switch (NodePubClazz.valueOf(method)) {
            case start:
                Machine.MachineDetail machineDetail = JsonConverter.jsonToObject(jsonPayload, Machine.MachineDetail.class);
                nodePubClazz.start(machineDetail);
                break;
            case shutdown:
                Machine.MachineStat machineStat = JsonConverter.jsonToObject(jsonPayload, Machine.MachineStat.class);
                nodePubClazz.shutdown(machineStat);
                break;
            case heartbeat:
                Machine.MachineStat machineStat1 = JsonConverter.jsonToObject(jsonPayload, Machine.MachineStat.class);
                nodePubClazz.heartbeat(machineStat1);
                break;
            case log:
                NodeLogDTO nodeLogDTO = JsonConverter.jsonToObject(jsonPayload, NodeLogDTO.class);
                nodePubClazz.log(nodeLogDTO);
                break;
            default:
                ;
        }
    }
}
