ProtobufConvert.java 2.93 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);

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

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


    /**
     * 回家离家转换
     *
     * @param message 入参
     * @return 返回值
     */
    public static Event convertLeaveHome(byte[] message) {
        Fence.TriggerDeviceFamilyInfo protobufFlameOut;
        try {
            protobufFlameOut = Fence.TriggerDeviceFamilyInfo.parseFrom(message);
        } catch (Exception e) {
            e.printStackTrace();
            protobufFlameOut = null;
        }
        if (null == protobufFlameOut) {
            return null;
        }
        Event event = new Event();
        LOGGER.info("离家数据推送,设备号{}"+protobufFlameOut.getSn());
        event.setSn(String.valueOf(protobufFlameOut.getSn()));
        event.setDeviceType(Constants.MACHINE_LEAVE_ORDER);
        event.setTime(DateUtils.parseDate(DateUtils.parseTimeStampDate(protobufFlameOut.getTriggerTime())));
        if(("1").equals((protobufFlameOut.getStatus()+""))) {
            event.setValue("true");
        }else{
            event.setValue("false");
        }
        return event;
    }

}