package com.mvc.iot.model;

import com.mvc.entity.TPackLog;
import com.mvc.netty.FzBaowenModel;
import com.mvc.netty.WmNettyServerHandler_2;
import com.mvc.netty.dealconstant.HandleMsgCode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.Serializable;
import java.util.*;

public class IotDataModel implements Serializable {
    private static Logger logger = LoggerFactory.getLogger(WmNettyServerHandler_2.class);

    private String imei;
    private String deviceSn;//设备SN
    private String stationCode;//站点编码
    private Date sampleTime;//采样时间
    private Integer h32MessageCount;//32报文总包数
    private Integer h36MessageCount;//36报文总包数
    private List<String> h32Messages;//32报文列表
    private List<String> h36Messages;//36报文列表
    private Date h32MessagesLastRevTime;//32报文最后接收时间
    private Date h36MessagesLastRevTime;//36报文最后接收时间
    private Date initTime;//对象初始化时间(用来处理报文不完整,超时丢弃)

    public IotDataModel(String imei, TPackLog packLog, HandleMsgCode handleMsgCode) {
        Date now = new Date();
        this.imei = imei;
        this.deviceSn = packLog.getDeviceSn();
        this.stationCode = packLog.getStationCode();
        this.sampleTime = packLog.getDatatime();
        this.h32Messages = new ArrayList<>();
        this.h32MessageCount = 0;
        this.h36MessageCount = 0;

        if (handleMsgCode == HandleMsgCode.H_32) {
            this.h32MessageCount = packLog.getPackCount();
            this.h32Messages.add(packLog.getOriginmessage());
            this.h32MessagesLastRevTime = now;
        } else if (handleMsgCode == HandleMsgCode.H_36) {
            this.h36MessageCount = packLog.getPackCount();
            this.h36Messages.add(packLog.getOriginmessage());
            this.h36MessagesLastRevTime = now;
        }
        this.initTime = now;
    }

    /**
     * 转换为FzBaowenModel
     * 写在此类是为了减少对FzBaowenModel的影响
     *
     * @return
     */
    public FzBaowenModel convertToFzBaowenModel(HandleMsgCode handleMsgCode) {
        FzBaowenModel fzBaowenModel = new FzBaowenModel();
        fzBaowenModel.setImei(this.imei);
        fzBaowenModel.setSn(this.deviceSn);
        fzBaowenModel.setStationcode(this.stationCode);
        fzBaowenModel.setType(handleMsgCode);

        if (handleMsgCode == HandleMsgCode.H_32) {
            fzBaowenModel.setBaowen(this.h32Messages);
            fzBaowenModel.setLastRevTime(this.h32MessagesLastRevTime);
            fzBaowenModel.addLastRevTimeMap(handleMsgCode, this.h32MessagesLastRevTime);
        } else if (handleMsgCode == HandleMsgCode.H_36) {
            fzBaowenModel.setBaowen(this.h36Messages);
            fzBaowenModel.setLastRevTime(this.h36MessagesLastRevTime);
            fzBaowenModel.addLastRevTimeMap(handleMsgCode, this.h36MessagesLastRevTime);
        }

        return fzBaowenModel;
    }

    public void addMessage(HandleMsgCode handleMsgCode, TPackLog packLog) {
        if (handleMsgCode == HandleMsgCode.H_32) {
            if (packLog.getPackIndex() <= this.h32Messages.size()) {//相同时间的报文重复出现(扔掉旧的)
                logger.info("[IOT][{}]32报文重复出现,丢弃旧的:", this.getImei());
                this.h32Messages.clear();
                this.h32MessageCount = packLog.getPackCount();

            }
            this.h32Messages.add(packLog.getOriginmessage());
            this.h32MessagesLastRevTime = new Date();
        } else if (handleMsgCode == HandleMsgCode.H_36) {
            if (packLog.getPackIndex() <= h36MessageCount) {//相同时间的报文重复出现(扔掉旧的)
                logger.info("[IOT][{}]36报文重复出现,丢弃旧的:", this.getImei());
                this.h36Messages.clear();
                this.h36MessageCount = packLog.getPackCount();
            }
            this.h36Messages.add(packLog.getOriginmessage());
            this.h36MessagesLastRevTime = new Date();
        }
    }

    public String getImei() {
        return imei;
    }

    public void setImei(String imei) {
        this.imei = imei;
    }

    public String getDeviceSn() {
        return deviceSn;
    }

    public void setDeviceSn(String deviceSn) {
        this.deviceSn = deviceSn;
    }

    public String getStationCode() {
        return stationCode;
    }

    public void setStationCode(String stationCode) {
        this.stationCode = stationCode;
    }

    public Date getSampleTime() {
        return sampleTime;
    }

    public void setSampleTime(Date sampleTime) {
        this.sampleTime = sampleTime;
    }

    public Integer getH32MessageCount() {
        return h32MessageCount;
    }

    public void setH32MessageCount(Integer h32MessageCount) {
        this.h32MessageCount = h32MessageCount;
    }

    public Integer getH36MessageCount() {
        return h36MessageCount;
    }

    public void setH36MessageCount(Integer h36MessageCount) {
        this.h36MessageCount = h36MessageCount;
    }

    public List<String> getH32Messages() {
        return h32Messages;
    }

    public void setH32Messages(List<String> h32Messages) {
        this.h32Messages = h32Messages;
    }

    public List<String> getH36Messages() {
        return h36Messages;
    }

    public void setH36Messages(List<String> h36Messages) {
        this.h36Messages = h36Messages;
    }

    public Date getH32MessagesLastRevTime() {
        return h32MessagesLastRevTime;
    }

    public void setH32MessagesLastRevTime(Date h32MessagesLastRevTime) {
        this.h32MessagesLastRevTime = h32MessagesLastRevTime;
    }

    public Date getH36MessagesLastRevTime() {
        return h36MessagesLastRevTime;
    }

    public void setH36MessagesLastRevTime(Date h36MessagesLastRevTime) {
        this.h36MessagesLastRevTime = h36MessagesLastRevTime;
    }

    public Date getInitTime() {
        return initTime;
    }

    public void setInitTime(Date initTime) {
        this.initTime = initTime;
    }
}
