ProtobufConvert.java 3.3 KB
package cn.com.glsx.smartkafka.modules.utils;

import cn.com.glsx.smartkafka.commons.constants.Constants;
import cn.com.glsx.smartkafka.modules.model.pb.Event;
import cn.com.glsx.smartkafka.modules.model.pb.Fence;
import cn.com.glsx.smartkafka.modules.model.pb.kafkasvr;
import org.apache.log4j.Logger;

public class ProtobufConvert {

    protected final static Logger LOGGER = Logger.getLogger(ProtobufConvert.class);
    // 点火
    private static final String TOPIC_IGNITION = "ignition_ddbox";
    // 熄火
    private static final String TOPIC_FLAMEOUT = "flameout_ddbox";

    public static Object convert(String topic, byte[] message) {
        if (topic.contains(TOPIC_IGNITION)) {
            return convertIgnition(message);
        }
        if (topic.contains(TOPIC_FLAMEOUT)) {
            return convertFlameOut(message);
        }
        return null;
    }

    // 点火启动转换
    public static Event convertIgnition(byte[] message) {
        kafkasvr.EventStateReportReq protobufIgnition = null;
        try {
            protobufIgnition = kafkasvr.EventStateReportReq.parseFrom(message);
        } catch (Exception e) {
            e.printStackTrace();
            protobufIgnition = null;
        }
        if (null == protobufIgnition) {
            return null;
        }
        Event event = new Event();
        event.setSn(String.valueOf(protobufIgnition.getDevicesn().trim()));
        event.setTime(DateUtils.parseDate(DateUtils.parseTimeStampDate(protobufIgnition.getTime())));
        event.setDeviceType(Constants.MACHINE_STATUS_ORDER);
        event.setValue("true");
        return event;
    }

    // 熄火转换
    public static Event convertFlameOut(byte[] message) {
        kafkasvr.EventStateReportReq protobufFlameOut = null;
        try {
            protobufFlameOut = kafkasvr.EventStateReportReq.parseFrom(message);
        } catch (Exception e) {
            e.printStackTrace();
            protobufFlameOut = null;
        }
        if (null == protobufFlameOut) {
            return null;
        }
        Event event = new Event();
        event.setSn(String.valueOf(protobufFlameOut.getDevicesn().trim()));
        event.setTime(DateUtils.parseDate(DateUtils.parseTimeStampDate(protobufFlameOut.getTime())));
        event.setDeviceType(Constants.MACHINE_STATUS_ORDER);
        event.setValue("false");
        return event;
    }


    // 回家离家转换
    public static Event convertLeaveHome(byte[] message) {
        Fence.TriggerDeviceFamilyInfo protobufFlameOut = null;
        try {

            protobufFlameOut = Fence.TriggerDeviceFamilyInfo.parseFrom(message);
        } catch (Exception e) {
            e.printStackTrace();
            protobufFlameOut = null;
        }
        if (null == protobufFlameOut) {
            return null;
        }
        Event event = new Event();
        LOGGER.info("离家数据推送,设备号{}"+String.valueOf(protobufFlameOut.getSn()));
        event.setSn(String.valueOf(protobufFlameOut.getSn()));
        event.setDeviceType(Constants.MACHINE_LEAVE_ORDER);
        event.setTime(DateUtils.parseDate(DateUtils.parseTimeStampDate(protobufFlameOut.getTriggerTime())));
        if((protobufFlameOut.getStatus()+"").equals("1")) {
            event.setValue("true");
        }else{
            event.setValue("false");
        }
        return event;
    }

}