package com.mvc.netty;

import com.mvc.entity.TPackLog;
import com.mvc.entity.TTaskDown;
import com.mvc.entity.TaskReviewPush;
import com.mvc.model.TAicbqCfgModel;
import com.mvc.netty.dealconstant.*;
import com.mvc.service.aiiden.TaskReviewPushService;
import com.mvc.service.taizhang.TAicbqCfgService;
import com.mvc.service.taizhang.TTaskDownService;
import com.mvc.service.taizhang.TaskBatchRemoteDetailService;
import com.mvc.utils.BeanUtil;
import com.mvc.utils.DateUtil;
import com.mvc.utils.NumberUtil;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

import com.mvc.utils.StringUtil;

import java.util.Date;
import java.util.List;

@Component
public class PackageParse {
    @Resource
    private BodyLengthParse bodyLengthParse;

    @Resource
    private TTaskDownService tTaskDownService;

    @Resource
    private TAicbqCfgService tAicbqCfgService;

    @Resource
    private TaskReviewPushService taskReviewPushService;

    @Resource
    private TaskBatchRemoteDetailService taskBatchRemoteDetailService;

    private static Logger log = LoggerFactory.getLogger(RegularMessageParser.class);

    /**
     * 解析生成报文对象  定时报
     *
     * @param msg
     * @return
     */
    public TPackLog parse(String msg) {
        String target = msg;
        String value = null;
        TPackLog tPackage = new TPackLog();

        if (StringUtils.isEmpty(msg)) {
            return null;
        }

        // 帧起始符
        target = PackageOperateCode.SOH.parse(target);
        // 中心站地址
        target = PackageData.Central_Station_Code.parse(target);
        value = PackageData.Central_Station_Code.getValue();
        tPackage.setCentralCode(value);
        // 遥测站地址
        target = PackageData.Telemetry_Station_Code.parse(target);
        value = PackageData.Telemetry_Station_Code.getValue();
        tPackage.setStationCode(value);
        // 密码
        target = PackageData.Password.parse(target);
        value = PackageData.Password.getValue();
        tPackage.setRptPwd(value);
        // 功能码
        target = PackageData.Msg_Fun_Code.parse(target);
        value = PackageData.Msg_Fun_Code.getValue();
        tPackage.setFunCode(value);
        HandleMsgCode inst = HandleMsgCode.get(value);
        tPackage.setFunName(inst.getDesc());
        // 报文上下行标识及长度
        target = PackageData.Body_Length.parse(target);
        value = PackageData.Body_Length.getValue();

        bodyLengthParse.parse(value);

        tPackage.setUpordown(bodyLengthParse.getIdentifier() + "");
        tPackage.setRptLen(bodyLengthParse.getBodyLength());

        //报文起始符
        target = PackageOperateCode.STX.parse(target);

        // 流水号
        target = PackageData.Serial_Number.parse(target);
        value = PackageData.Serial_Number.getValue();
        tPackage.setSerialNum(value);
        // 报文时间
        target = PackageData.Send_Time.parse(target);
        value = PackageData.Send_Time.getValue();
        tPackage.setRptTime(DateUtil.parseTimeStr(value, "yyMMddHHmmss"));
        // 地址标识符
        target = PackageOperateCode.O_F1F1.parse(target);
        // 遥测站地址
        target = PackageData.Telemetry_Station_Code.parse(target);
        // 遥测站类型
        target = PackageData.Telemetry_Type_Code.parse(target);
        value = PackageData.Telemetry_Type_Code.getValue();
        tPackage.setStationType(value);
        // 观测时间标识符
        target = PackageOperateCode.O_F0F0.parse(target);
        // 数据时间
        target = PackageData.Data_Time.parse(target);
        value = PackageData.Data_Time.getValue();
        tPackage.setDatatime(DateUtil.parseTimeStr(value, "yyMMddHHmm"));
        // 验证码
        target = PackageData.Check_Num.parse(target);
        value = PackageData.Check_Num.getValue();
        tPackage.setCheckNum(value);

        // 报文结束符
        target = PackageData.Body_End_Pattern.parse(target);
        value = PackageData.Body_End_Pattern.getValue();

        //报文
        tPackage.setMessage(target);
        tPackage.setOriginmessage(msg);
        tPackage.setPackId(StringUtil.getUuid());
        tPackage.setPackGid(tPackage.getPackId());
        tPackage.setMultiPackFlag("0");
        tPackage.setRevTime(new Date());
        return tPackage;
    }


    public TPackLog parse_DT(String msg) {
        String target = msg;
        String value = null;
        TPackLog tPackage = new TPackLog();

        if (StringUtils.isEmpty(msg)) {
            return null;
        }
        // 帧起始符  7e7e
        target = PackageOperateCode.SOH.parse(target);
        // 中心站地址  01
        target = PackageData.Central_Station_Code.parse(target);
        value = PackageData.Central_Station_Code.getValue();
        tPackage.setCentralCode(value);
        // 遥测站地址   99 99 99 99 17
        target = PackageData.Telemetry_Station_Code.parse(target);
        value = PackageData.Telemetry_Station_Code.getValue();
        tPackage.setStationCode(value);
        // 密码   2020
        target = PackageData.Password.parse(target);
        value = PackageData.Password.getValue();
        tPackage.setRptPwd(value);


        // 功能码   36
        target = PackageData.Msg_Fun_Code.parse(target);
        value = PackageData.Msg_Fun_Code.getValue();
        tPackage.setFunCode(value);
        HandleMsgCode inst = HandleMsgCode.get(value);
        tPackage.setFunName(inst.getDesc());

        // 报文上下行标识及长度
        target = PackageData.Body_Length.parse(target);
        value = PackageData.Body_Length.getValue();

        bodyLengthParse.parse(value);

        tPackage.setUpordown(bodyLengthParse.getIdentifier() + "");//上下行标识
        tPackage.setRptLen(bodyLengthParse.getBodyLength());//长度

        //去掉报文起始符
        target = target.substring(2);

        //总包数及当前包序
        String binarystr = NumberUtil.hexStringToBinaryString(target.substring(0, 6));
        int baonum = NumberUtil.binaryToDecimal(binarystr.substring(0, 12));
        int baoindex = NumberUtil.binaryToDecimal(binarystr.substring(12));
        target = target.substring(6);
        tPackage.setPackCount(baonum);
        tPackage.setPackIndex(baoindex);
        if (baoindex == 1) {
            // 流水号
            target = PackageData.Serial_Number.parse(target);
            value = PackageData.Serial_Number.getValue();
            tPackage.setSerialNum(value);
            // 发送报文时间
            target = PackageData.Send_Time.parse(target);
            value = PackageData.Send_Time.getValue();
            tPackage.setRptTime(DateUtil.parseTimeStr(value, "yyMMddHHmmss"));
            // 地址标识符
            target = PackageOperateCode.O_F1F1.parse(target);
            // 遥测站地址
            target = PackageData.Telemetry_Station_Code.parse(target);
            // 遥测站类型
            target = PackageData.Telemetry_Type_Code.parse(target);
            value = PackageData.Telemetry_Type_Code.getValue();
            tPackage.setStationType(value);
            // 观测时间标识符
            target = PackageOperateCode.O_F0F0.parse(target);
            // 数据时间
            target = PackageData.Data_Time.parse(target);
            value = PackageData.Data_Time.getValue();
            tPackage.setDatatime(DateUtil.parseTimeStr(value, "yyMMddHHmm"));
            //去除图片引导符
            target = target.substring(4);
            // 验证码
            target = PackageData.Check_Num.parse(target);
            value = PackageData.Check_Num.getValue();
            tPackage.setCheckNum(value);

            // 报文结束符
            target = PackageData.Body_End_Pattern.parse(target);
            value = PackageData.Body_End_Pattern.getValue();

            // 报文
            tPackage.setMessage(target);
        } else {
            //设置默认流水号
            tPackage.setSerialNum("0000");
            //设置默认发报时间
            tPackage.setRptTime(new Date());

            //验证码
            target = PackageData.Check_Num.parse(target);
            value = PackageData.Check_Num.getValue();
            tPackage.setCheckNum(value);
            // 报文结束符
            target = PackageData.Body_End_Pattern.parse(target);
            value = PackageData.Body_End_Pattern.getValue();

            //报文
            tPackage.setMessage(target);
        }
        tPackage.setPackId(StringUtil.getUuid());
        tPackage.setOriginmessage(msg);
        tPackage.setMultiPackFlag("1");
        tPackage.setRevTime(new Date());
        return tPackage;
    }

    //定时报多包处理协议
    public TPackLog parse_duobao(String msg) {
        String target = msg;
        String value = null;
        TPackLog tPackage = new TPackLog();

        if (StringUtils.isEmpty(msg)) {
            return null;
        }
        // 帧起始符
        target = PackageOperateCode.SOH.parse(target);
        // 中心站地址
        target = PackageData.Central_Station_Code.parse(target);
        value = PackageData.Central_Station_Code.getValue();
        tPackage.setCentralCode(value);
        // 遥测站地址
        target = PackageData.Telemetry_Station_Code.parse(target);
        value = PackageData.Telemetry_Station_Code.getValue();
        tPackage.setStationCode(value);
        // 密码
        target = PackageData.Password.parse(target);
        value = PackageData.Password.getValue();
        tPackage.setRptPwd(value);
        // 功能码
        target = PackageData.Msg_Fun_Code.parse(target);
        value = PackageData.Msg_Fun_Code.getValue();
        tPackage.setFunCode(value);
        HandleMsgCode inst = HandleMsgCode.get(value);

        tPackage.setFunName(inst.getDesc());
        // 报文上下行标识及长度
        target = PackageData.Body_Length.parse(target);
        value = PackageData.Body_Length.getValue();

        bodyLengthParse.parse(value);

        tPackage.setUpordown(bodyLengthParse.getIdentifier() + "");
        tPackage.setRptLen(bodyLengthParse.getBodyLength());
        //报文起始符
        target = PackageOperateCode.SYN.parse(target);
        //总包数及当前包序
        String binarystr = NumberUtil.hexStringToBinaryString(target.substring(0, 6));
        int baonum = NumberUtil.binaryToDecimal(binarystr.substring(0, 12));
        int baoindex = NumberUtil.binaryToDecimal(binarystr.substring(12));
        target = target.substring(6);
        tPackage.setPackCount(baonum);
        tPackage.setPackIndex(baoindex);

        boolean isPart = tPackage.getRptPwd().startsWith("70");
        int partIndex = Integer.valueOf(tPackage.getRptPwd().substring(3));
        //if(baoindex==1){ 兼容多片段处理(第二片段开始是从FF03) 2023-01-13 guozhao
        if ((baoindex == 1 && !isPart) || (isPart && partIndex == 1 && baoindex == 1)) {
            // 流水号
            target = PackageData.Serial_Number.parse(target);
            value = PackageData.Serial_Number.getValue();
            tPackage.setSerialNum(value);
            // 报文时间
            target = PackageData.Send_Time.parse(target);
            value = PackageData.Send_Time.getValue();
            tPackage.setRptTime(DateUtil.parseTimeStr(value, "yyMMddHHmmss"));
            // 地址标识符
            target = PackageOperateCode.O_F1F1.parse(target);
            // 遥测站地址
            target = PackageData.Telemetry_Station_Code.parse(target);
            // 遥测站类型
            target = PackageData.Telemetry_Type_Code.parse(target);
            value = PackageData.Telemetry_Type_Code.getValue();
            tPackage.setStationType(value);
            // 观测时间标识符
            target = PackageOperateCode.O_F0F0.parse(target);
            // 数据时间
            target = PackageData.Data_Time.parse(target);
            value = PackageData.Data_Time.getValue();
            tPackage.setDatatime(DateUtil.parseTimeStr(value, "yyMMddHHmm"));
            // 验证码
            target = PackageData.Check_Num.parse(target);
            value = PackageData.Check_Num.getValue();
            tPackage.setCheckNum(value);

            // 报文结束符
            target = PackageData.Body_End_Pattern.parse(target);
            value = PackageData.Body_End_Pattern.getValue();
            // 报文
            tPackage.setMessage(target);
        } else {
            //设置默认流水号
            tPackage.setSerialNum("0000");
            //设置默认发报时间
            tPackage.setRptTime(new Date());
            // 验证码
            target = PackageData.Check_Num.parse(target);
            value = PackageData.Check_Num.getValue();
            tPackage.setCheckNum(value);
            // 报文结束符
            target = PackageData.Body_End_Pattern.parse(target);
            value = PackageData.Body_End_Pattern.getValue();
            // 报文
            tPackage.setMessage(target);
        }
        tPackage.setPackId(StringUtil.getUuid());
        tPackage.setOriginmessage(msg);
        tPackage.setMultiPackFlag("1");
        tPackage.setRevTime(new Date());
        return tPackage;
    }

    //处理远程配置上行报文
    public TPackLog parse_remoteCfg(String msg, String taskDownId) {
        TPackLog tPackLog = new TPackLog();
        TAicbqCfgModel tAicbqCfgModel = new TAicbqCfgModel();
        String target = msg;
        String value = null;

        if (StringUtils.isEmpty(msg) || taskDownId == null) {
            return null;
        }
        // 帧起始符
        target = PackageOperateCode.SOH.parse(target);
        // 中心站地址????和遥测站谁在前
        target = PackageData.Central_Station_Code.parse(target);
        String centralcode = PackageData.Central_Station_Code.getValue();
        tPackLog.setCentralCode(centralcode);

        // 遥测站地址
        target = PackageData.Telemetry_Station_Code.parse(target);
        String stationcode = PackageData.Telemetry_Station_Code.getValue();
        tPackLog.setStationCode(stationcode);

        // 密码
        target = PackageData.Password.parse(target);
        String password = PackageData.Password.getValue();
        tPackLog.setRptPwd(password);

        tAicbqCfgModel.setDeviceType(password.startsWith("21") ? "2" : "1");  //设备类型,密码首字节=21代表压力表,20代表水表 2022-09-13 guozhao

        // 功能码
        target = PackageData.Msg_Fun_Code.parse(target);
        String funcode = PackageData.Msg_Fun_Code.getValue();
        tPackLog.setFunCode(funcode);

        HandleMsgCode inst = HandleMsgCode.get(funcode);
        tPackLog.setFunName(inst.getDesc());

        // 报文上下行标识及长度
        target = PackageData.Body_Length.parse(target);
        value = PackageData.Body_Length.getValue();
        bodyLengthParse.parse(value);

        String biaoshi = bodyLengthParse.getIdentifier() + "";
        Integer length = bodyLengthParse.getBodyLength();
        tPackLog.setUpordown(biaoshi);
        tPackLog.setRptLen(length);
        //报文起始符
        target = PackageOperateCode.STX.parse(target);
        // 流水号
        target = PackageData.Serial_Number.parse(target);
        String SerialNum = PackageData.Serial_Number.getValue();
        tPackLog.setSerialNum(SerialNum);
        //报文时间
        target = PackageData.Send_Time.parse(target);
        String sendtime = PackageData.Send_Time.getValue();
        tPackLog.setRptTime(DateUtil.parseTimeStr(sendtime, "yyMMddHHmmss"));
        tPackLog.setPackId(StringUtil.getUuid());
        tPackLog.setOriginmessage(msg);
        //解析报文
        List<String> keys = TaskRemoteIdentifier.keys();
        String temp = target.substring(0, target.length() - 6);
        boolean isError = false;
        while (StringUtils.isNotEmpty(temp)) {
            boolean blnFind = false;
            for (String key : keys) {
                if (temp.toUpperCase().startsWith(key)) {
                    TaskRemoteIdentifier identifier = TaskRemoteIdentifier.getElement(key);
                    try {
                        temp = identifier.parse(temp);
                        value = identifier.getValue();
                        BeanUtil.fillTAicbqCfg(tAicbqCfgModel, value, identifier);
                        log.info("配置项:" + identifier.getDesc() + ":" + value + "[" + identifier.getLen() + "]");
                        blnFind = true;
                    } catch (Exception e) {
                        //已解析了了一部分后,后面有错误报文(配置任务会显示完成的,用PackLog-flag-44来区分) 记录下 2024-04-26 guozhao
                        isError = true;
                        log.error(e.getMessage());
                        blnFind = false;
                    }
                    break;
                }
            }
            if (blnFind == false) {
                break;
            }
        }

        //结束符
        target = PackageData.Body_End_Pattern.parse(target);
        //校验码
        target = PackageData.Check_Num.parse(target);
        tPackLog.setCheckNum(PackageData.Check_Num.getValue());

        //更新taskdown 远程配置任务表
        TTaskDown tTaskDown = tTaskDownService.updateTTaskDownAndRelateTask(taskDownId);
        //跟新抄表器参数配置表
        if (io.netty.util.internal.StringUtil.isNullOrEmpty(tAicbqCfgModel.getDeviceSn())) {
            tAicbqCfgModel.setDeviceSn(tTaskDown.getDeviceSn());
        }

        //下行任务的类型与抄表器配置表的数据来源要匹配 2022-07-05 guozhao
        tAicbqCfgModel.setDataFrom(tTaskDown.getTaskType());
        tAicbqCfgModel.setTaskDownId(tTaskDown.getTaskDownId());
        TAicbqCfgModel cfgResModel = tAicbqCfgService.saveTAicbqCfgAndupdate(tAicbqCfgModel);

        taskBatchRemoteDetailService.updateTaskBatchRemoteDetailByTaskDownId(tTaskDown.getTaskDownId(), cfgResModel.getCfgId());

        tPackLog.setPackGid(tPackLog.getPackId());
        tPackLog.setMultiPackFlag("0");
        tPackLog.setPackIndex(1);
        tPackLog.setPackCount(1);
        tPackLog.setRevTime(new Date());
        tPackLog.setFlag(isError ? "44" : "1");         //扩展的,用31代表配置报文已经解析
        return tPackLog;
    }

    //处理紧急域名报文
    public TPackLog parse_DomainContact(String msg) {
        TPackLog tPackLog = new TPackLog();
        String target = msg;
        String value = null;

        if (StringUtils.isEmpty(msg)) {
            return null;
        }

        // 帧起始符
        target = PackageOperateCode.SOH.parse(target);

        target = PackageData.Central_Station_Code.parse(target);
        String centralcode = PackageData.Central_Station_Code.getValue();
        tPackLog.setCentralCode(centralcode);

        // 遥测站地址
        target = PackageData.Telemetry_Station_Code.parse(target);
        String stationcode = PackageData.Telemetry_Station_Code.getValue();
        tPackLog.setStationCode(stationcode);

        // 密码
        target = PackageData.Password.parse(target);
        String password = PackageData.Password.getValue();

        // 功能码
        target = PackageData.Msg_Fun_Code.parse(target);
        String funcode = PackageData.Msg_Fun_Code.getValue();
        tPackLog.setFunCode(funcode);

        HandleMsgCode inst = HandleMsgCode.get(funcode);
        tPackLog.setFunName(inst.getDesc());

        // 报文上下行标识及长度
        target = PackageData.Body_Length.parse(target);
        value = PackageData.Body_Length.getValue();
        bodyLengthParse.parse(value);

        String biaoshi = bodyLengthParse.getIdentifier() + "";
        Integer length = bodyLengthParse.getBodyLength();
        tPackLog.setUpordown(biaoshi);
        tPackLog.setRptLen(length);
        //报文起始符
        target = PackageOperateCode.STX.parse(target);
        // 流水号
        target = PackageData.Serial_Number.parse(target);
        String SerialNum = PackageData.Serial_Number.getValue();
        tPackLog.setSerialNum(SerialNum);
        //发报时间
        target = PackageData.Send_Time.parse(target);
        String sendtime = PackageData.Send_Time.getValue();
        tPackLog.setRptTime(DateUtil.parseTimeStr(sendtime, "yyMMddHHmmss"));

        List<String> keys = DomainContactIdentifier.keys();
        String temp = target.substring(0, target.length() - 6);
        while (StringUtils.isNotEmpty(temp)) {
            boolean blnFind = false;
            for (String key : keys) {
                if (temp.toUpperCase().startsWith(key)) {
                    DomainContactIdentifier identifier = DomainContactIdentifier.getElement(key);
                    try {
                        temp = identifier.parse(temp);
                        value = identifier.getValue();
                        log.info("紧急域名报文:" + identifier.getDesc() + ":" + value + "[" + identifier.getLen() + "]");
                        blnFind = true;
                    } catch (Exception e) {
                        log.error(e.getMessage());
                        blnFind = false;
                    }
                    break;
                }
            }
            if (blnFind == false) {
                break;
            }
        }
        tPackLog.setOriginmessage(msg);
        return tPackLog;
    }


    /**
     * 处理设备端对审核值下发的回执报文
     *
     * @param msg
     * @return
     */
    public TPackLog parse_TaskReviewPush(String msg, TaskReviewPush taskReviewPush) {
        TPackLog tPackLog = new TPackLog();
        tPackLog.setOriginmessage(msg);
        String target = msg;
        String value = null;

        if (StringUtils.isEmpty(msg)) {
            return null;
        }

        // 帧起始符
        target = PackageOperateCode.SOH.parse(target);

        target = PackageData.Central_Station_Code.parse(target);
        String centralcode = PackageData.Central_Station_Code.getValue();
        tPackLog.setCentralCode(centralcode);

        // 遥测站地址
        target = PackageData.Telemetry_Station_Code.parse(target);
        String stationcode = PackageData.Telemetry_Station_Code.getValue();
        tPackLog.setStationCode(stationcode);

        // 密码
        target = PackageData.Password.parse(target);
        String password = PackageData.Password.getValue();
        tPackLog.setRptPwd(password);

        // 功能码
        target = PackageData.Msg_Fun_Code.parse(target);
        String funcode = PackageData.Msg_Fun_Code.getValue();
        tPackLog.setFunCode(funcode);

        HandleMsgCode inst = HandleMsgCode.get(funcode);
        tPackLog.setFunName(inst.getDesc());

        // 报文上下行标识及长度
        target = PackageData.Body_Length.parse(target);
        value = PackageData.Body_Length.getValue();
        bodyLengthParse.parse(value);

        String biaoshi = bodyLengthParse.getIdentifier() + "";
        Integer length = bodyLengthParse.getBodyLength();
        tPackLog.setUpordown(biaoshi);
        tPackLog.setRptLen(length);
        //报文起始符
        target = PackageOperateCode.STX.parse(target);
        // 流水号
        target = PackageData.Serial_Number.parse(target);
        String SerialNum = PackageData.Serial_Number.getValue();
        tPackLog.setSerialNum(SerialNum);
        //发报时间
        target = PackageData.Send_Time.parse(target);
        String sendtime = PackageData.Send_Time.getValue();
        tPackLog.setRptTime(DateUtil.parseTimeStr(sendtime, "yyMMddHHmmss"));

        //观测时间
        if (target.startsWith("f0f0")) {
            target = target.substring(4);
            target = PackageData.Data_Time.parse(target);
            String simTime = PackageData.Data_Time.getValue();
            tPackLog.setDatatime(DateUtil.parseTimeStr(simTime, "yyMMddHHmm"));
        }

        //结束符
        target = PackageData.Body_End_Pattern.parse(target);
        //校验码
        target = PackageData.Check_Num.parse(target);
        tPackLog.setCheckNum(PackageData.Check_Num.getValue());

        //更新审核下发任务的状态
        if (taskReviewPush != null) {
            taskReviewPush.setReplyMsg(msg);
            taskReviewPush.setFinishedFlag("1");
            taskReviewPush.setFinishedTime(new Date());
            taskReviewPushService.updateTaskReviewPush(taskReviewPush);
            log.info("审核任务推送状态已更新");
        }

        tPackLog.setPackId(StringUtil.getUuid());
        tPackLog.setPackGid(tPackLog.getPackId());
        tPackLog.setMultiPackFlag("0");
        tPackLog.setPackCount(1);
        tPackLog.setPackIndex(1);
        tPackLog.setFlag("1");
        tPackLog.setRevTime(new Date());
        return tPackLog;
    }

    //处理运行日志 2022.10.07 guozhao
    public TPackLog parse_RunLogs(String msg) {
        String target = msg;
        String value = null;
        TPackLog tPackage = new TPackLog();

        if (StringUtils.isEmpty(msg)) {
            return null;
        }
        // 帧起始符  7e7e
        target = PackageOperateCode.SOH.parse(target);
        // 中心站地址  01
        target = PackageData.Central_Station_Code.parse(target);
        value = PackageData.Central_Station_Code.getValue();
        tPackage.setCentralCode(value);
        // 遥测站地址   99 99 99 99 17
        target = PackageData.Telemetry_Station_Code.parse(target);
        value = PackageData.Telemetry_Station_Code.getValue();
        tPackage.setStationCode(value);
        // 密码   2020
        target = PackageData.Password.parse(target);
        value = PackageData.Password.getValue();
        tPackage.setRptPwd(value);

        // 功能码
        target = PackageData.Msg_Fun_Code.parse(target);
        value = PackageData.Msg_Fun_Code.getValue();
        tPackage.setFunCode(value);
        HandleMsgCode inst = HandleMsgCode.get(value);
        tPackage.setFunName(inst.getDesc());

        // 报文上下行标识及长度
        target = PackageData.Body_Length.parse(target);
        value = PackageData.Body_Length.getValue();

        bodyLengthParse.parse(value);

        tPackage.setUpordown(bodyLengthParse.getIdentifier() + "");//上下行标识
        tPackage.setRptLen(bodyLengthParse.getBodyLength());//长度

        //去掉报文起始符
        target = target.substring(2);

        //总包数及当前包序
        String binarystr = NumberUtil.hexStringToBinaryString(target.substring(0, 6));
        int baonum = NumberUtil.binaryToDecimal(binarystr.substring(0, 12));
        int baoindex = NumberUtil.binaryToDecimal(binarystr.substring(12));
        target = target.substring(6);
        tPackage.setPackCount(baonum);
        tPackage.setPackIndex(baoindex);
        if (baoindex == 1) {
            // 流水号
            target = PackageData.Serial_Number.parse(target);
            value = PackageData.Serial_Number.getValue();
            tPackage.setSerialNum(value);
            // 发送报文时间
            target = PackageData.Send_Time.parse(target);
            value = PackageData.Send_Time.getValue();
            tPackage.setRptTime(DateUtil.parseTimeStr(value, "yyMMddHHmmss"));
            // 地址标识符
            target = PackageOperateCode.O_F1F1.parse(target);
            // 遥测站地址
            target = PackageData.Telemetry_Station_Code.parse(target);
            // 遥测站类型
            target = PackageData.Telemetry_Type_Code.parse(target);
            value = PackageData.Telemetry_Type_Code.getValue();
            tPackage.setStationType(value);
            // 观测时间标识符
            target = PackageOperateCode.O_F0F0.parse(target);
            // 数据时间
            target = PackageData.Data_Time.parse(target);
            value = PackageData.Data_Time.getValue();
            tPackage.setDatatime(DateUtil.parseTimeStr(value, "yyMMddHHmm"));

            //设备SN(运行日志专用)
            if (target.substring(0, 4).equals("ff08")) {
                int pLen = Integer.valueOf(target.substring(4, 6), 16);
                int len = pLen >> 3;
                value = target.substring(6, 6 + len * 2);
                tPackage.setDeviceSn(value.substring(0, 11));
                target = target.substring(6 + len * 2);
            }

            //日志属性
            if (target.substring(0, 4).equals("ff30")) {
                int pLen = Integer.valueOf(target.substring(4, 6), 16);
                int len = pLen >> 3;
                value = target.substring(6, 6 + len * 2);
                //借用字段PackGid,此处存储的是日志属性(没有特意在表中添加字段)
                tPackage.setPackGid(value);
                target = target.substring(6 + len * 2);
            }
            //错误日志的采样时间
            if (target.substring(0, 4).equals("fafa")) {
                value = target.substring(4, 14);
                tPackage.setPackId(value);
                target = target.substring(14);
            }

            //去除内容引导符
            target = target.substring(4);
            // 验证码
            target = PackageData.Check_Num.parse(target);
            value = PackageData.Check_Num.getValue();
            tPackage.setCheckNum(value);

            // 报文结束符
            target = PackageData.Body_End_Pattern.parse(target);
            value = PackageData.Body_End_Pattern.getValue();

            // 报文
            tPackage.setMessage(target);
        } else {
            //设置默认流水号
            tPackage.setSerialNum("0000");
            //设置默认发报时间
            tPackage.setRptTime(new Date());

            //验证码
            target = PackageData.Check_Num.parse(target);
            value = PackageData.Check_Num.getValue();
            tPackage.setCheckNum(value);
            // 报文结束符
            target = PackageData.Body_End_Pattern.parse(target);
            value = PackageData.Body_End_Pattern.getValue();

            //报文
            tPackage.setMessage(target);
        }
        tPackage.setOriginmessage(msg);
        tPackage.setMultiPackFlag("1");
        tPackage.setRevTime(new Date());
        return tPackage;
    }

    //处理CNN数据(网络输出数据报文) 2022-11-08 guozhao
    public TPackLog parse_CnnData(String msg) {
        String target = msg;
        String value = null;
        TPackLog tPackage = new TPackLog();

        if (StringUtils.isEmpty(msg)) {
            return null;
        }
        // 帧起始符  7e7e
        target = PackageOperateCode.SOH.parse(target);
        // 中心站地址  01
        target = PackageData.Central_Station_Code.parse(target);
        value = PackageData.Central_Station_Code.getValue();
        tPackage.setCentralCode(value);
        // 遥测站地址   99 99 99 99 17
        target = PackageData.Telemetry_Station_Code.parse(target);
        value = PackageData.Telemetry_Station_Code.getValue();
        tPackage.setStationCode(value);
        // 密码   2020
        target = PackageData.Password.parse(target);
        value = PackageData.Password.getValue();
        tPackage.setRptPwd(value);

        // 功能码
        target = PackageData.Msg_Fun_Code.parse(target);
        value = PackageData.Msg_Fun_Code.getValue();
        tPackage.setFunCode(value);
        HandleMsgCode inst = HandleMsgCode.get(value);
        tPackage.setFunName(inst.getDesc());

        // 报文上下行标识及长度
        target = PackageData.Body_Length.parse(target);
        value = PackageData.Body_Length.getValue();

        bodyLengthParse.parse(value);

        tPackage.setUpordown(bodyLengthParse.getIdentifier() + "");//上下行标识
        tPackage.setRptLen(bodyLengthParse.getBodyLength());//长度

        //去掉报文起始符
        target = target.substring(2);

        //总包数及当前包序
        String binarystr = NumberUtil.hexStringToBinaryString(target.substring(0, 6));
        int baonum = NumberUtil.binaryToDecimal(binarystr.substring(0, 12));
        int baoindex = NumberUtil.binaryToDecimal(binarystr.substring(12));
        target = target.substring(6);
        tPackage.setPackCount(baonum);
        tPackage.setPackIndex(baoindex);
        if (baoindex == 1) {
            // 流水号
            target = PackageData.Serial_Number.parse(target);
            value = PackageData.Serial_Number.getValue();
            tPackage.setSerialNum(value);
            // 发送报文时间
            target = PackageData.Send_Time.parse(target);
            value = PackageData.Send_Time.getValue();
            tPackage.setRptTime(DateUtil.parseTimeStr(value, "yyMMddHHmmss"));
            // 地址标识符
            target = PackageOperateCode.O_F1F1.parse(target);
            // 遥测站地址
            target = PackageData.Telemetry_Station_Code.parse(target);
            // 遥测站类型
            target = PackageData.Telemetry_Type_Code.parse(target);
            value = PackageData.Telemetry_Type_Code.getValue();
            tPackage.setStationType(value);
            // 观测时间标识符
            target = PackageOperateCode.O_F0F0.parse(target);
            // 观测时间
            target = PackageData.Data_Time.parse(target);
            value = PackageData.Data_Time.getValue();
            tPackage.setDatatime(DateUtil.parseTimeStr(value, "yyMMddHHmm"));

            //设备SN
            if (target.substring(0, 4).equals("ff08")) {
                int pLen = Integer.valueOf(target.substring(4, 6), 16);
                int len = pLen >> 3;
                value = target.substring(6, 6 + len * 2);
                tPackage.setDeviceSn(value.substring(0, 11));
                target = target.substring(6 + len * 2);
            }

            //数据第一维的长度
            if (target.substring(0, 4).equals("ff29")) {
                int pLen = Integer.valueOf(target.substring(4, 6), 16);
                int len = pLen >> 3;
                value = target.substring(6, 6 + len * 2);

                tPackage.setFileName(value.substring(0, 1));//是否压缩 1压,0不压
                value = value.substring(1);

                //借用字段PackGid,此处存储的是 数据第一维的长度(没有特意在表中添加字段)
                tPackage.setPackGid(value);
                target = target.substring(6 + len * 2);
            }
            //压缩后网路传输数据的总长度(借用)
            if (target.substring(0, 4).equals("ff30")) {
                int pLen = Integer.valueOf(target.substring(4, 6), 16);
                int len = pLen >> 3;
                value = target.substring(6, 6 + len * 2);
                //借用字段PackId,此处存储的是压缩后网路传输数据的总长度(没有特意在表中添加字段)
                tPackage.setPackId(value);
                target = target.substring(6 + len * 2);
            }

            //去除内容引导符
            target = target.substring(4);
            // 验证码
            target = PackageData.Check_Num.parse(target);
            value = PackageData.Check_Num.getValue();
            tPackage.setCheckNum(value);

            // 报文结束符
            target = PackageData.Body_End_Pattern.parse(target);
            value = PackageData.Body_End_Pattern.getValue();

            // 报文
            tPackage.setMessage(target);
        } else {
            //设置默认流水号
            tPackage.setSerialNum("0000");
            //设置默认发报时间
            tPackage.setRptTime(new Date());

            //验证码
            target = PackageData.Check_Num.parse(target);
            value = PackageData.Check_Num.getValue();
            tPackage.setCheckNum(value);
            // 报文结束符
            target = PackageData.Body_End_Pattern.parse(target);
            value = PackageData.Body_End_Pattern.getValue();

            //报文
            tPackage.setMessage(target);
        }
        tPackage.setOriginmessage(msg);
        tPackage.setMultiPackFlag("1");
        tPackage.setRevTime(new Date());
        return tPackage;
    }

    /**
     * 解析查询服务地址的报文
     *
     * @param msg
     * @return
     */
    public TPackLog parse_QueryAddressMsg(String msg) {
        String value = null;
        TPackLog tPackLog = parse_FrameHeader(msg);
        String target = tPackLog.getMessage();
        // 流水号
        /*target = PackageData.Serial_Number.parse(target);
        value = PackageData.Serial_Number.getValue();
        tPackLog.setSerialNum(value);
        // 发送报文时间
        target = PackageData.Send_Time.parse(target);
        value = PackageData.Send_Time.getValue();
        tPackLog.setRptTime(DateUtil.parseTimeStr(value, "yyMMddHHmmss"));*/

        //设备SN
        if (target.startsWith("ff08")) {
            int pLen = Integer.valueOf(target.substring(4, 6), 16);
            int len = pLen >> 3;
            value = target.substring(6, 6 + len * 2);
            tPackLog.setDeviceSn(value.substring(0, 11));
            target = target.substring(6 + len * 2);
        }

        // 验证码
        /*target = PackageData.Check_Num.parse(target);
        value = PackageData.Check_Num.getValue();
        tPackLog.setCheckNum(value);

        // 报文结束符
        target = PackageData.Body_End_Pattern.parse(target);
        value = PackageData.Body_End_Pattern.getValue();*/

        return tPackLog;
    }

    /**
     * 解析查询任务的报文
     *
     * @param msg
     * @return
     */
    public TPackLog parse_QueryTaskMsg(String msg) {
        String value = null;
        TPackLog tPackLog = parse_FrameHeader(msg);
        String target = tPackLog.getMessage();
        // 流水号
        /*target = PackageData.Serial_Number.parse(target);
        value = PackageData.Serial_Number.getValue();
        tPackLog.setSerialNum(value);
        // 发送报文时间
        target = PackageData.Send_Time.parse(target);
        value = PackageData.Send_Time.getValue();
        tPackLog.setRptTime(DateUtil.parseTimeStr(value, "yyMMddHHmmss"));*/

        //设备SN
        if (target.startsWith("ff08")) {
            int pLen = Integer.valueOf(target.substring(4, 6), 16);
            int len = pLen >> 3;
            value = target.substring(6, 6 + len * 2);
            tPackLog.setDeviceSn(value.substring(0, 11));
            target = target.substring(6 + len * 2);
        }

        if (target.startsWith("fafa")) {//任务编号
            int pLen = Integer.valueOf(target.substring(4, 6), 16);
            int len = pLen >> 3;
            value = target.substring(6, 6 + len * 2);
            //借用字段PackId,此处存储的是压缩后网路传输数据的总长度(没有特意在表中添加字段)
            tPackLog.setPackId(value);
            target = target.substring(6 + len * 2);
        }

        // 验证码
        /*target = PackageData.Check_Num.parse(target);
        value = PackageData.Check_Num.getValue();
        tPackLog.setCheckNum(value);

        // 报文结束符
        target = PackageData.Body_End_Pattern.parse(target);
        value = PackageData.Body_End_Pattern.getValue();*/

        return tPackLog;
    }

    /**
     * 解析版本信息报文
     *
     * @param msg
     * @return
     */
    public TPackLog parse_VersionInfo(String msg) {
        String value = null;
        TPackLog tPackLog = parse_FrameHeader(msg);
        tPackLog.setMultiPackFlag("0");
        tPackLog.setOriginmessage(msg);
        tPackLog.setPackCount(1);
        String target = tPackLog.getMessage();
        // 流水号
        /*target = PackageData.Serial_Number.parse(target);
        value = PackageData.Serial_Number.getValue();
        tPackLog.setSerialNum(value);
        // 发送报文时间
        target = PackageData.Send_Time.parse(target);
        value = PackageData.Send_Time.getValue();
        tPackLog.setRptTime(DateUtil.parseTimeStr(value, "yyMMddHHmmss"));*/

        // 地址标识符
        target = PackageOperateCode.O_F1F1.parse(target);
        // 遥测站地址
        target = PackageData.Telemetry_Station_Code.parse(target);

        // 验证码
        /*target = PackageData.Check_Num.parse(target);
        value = PackageData.Check_Num.getValue();
        tPackLog.setCheckNum(value);

        // 报文结束符
        target = PackageData.Body_End_Pattern.parse(target);
        value = PackageData.Body_End_Pattern.getValue();*/
        tPackLog.setMessage(target);
        return tPackLog;
    }

    /**
     * 解析固定帧头
     * 上面代码有大量重复的内容需要整合
     *
     * @param target
     * @return
     */
    public TPackLog parse_FrameHeader(String target) {
        String value = null;
        TPackLog tPackage = new TPackLog();
        // 帧起始符  7e7e
        target = PackageOperateCode.SOH.parse(target);
        // 中心站地址  01
        target = PackageData.Central_Station_Code.parse(target);
        value = PackageData.Central_Station_Code.getValue();
        tPackage.setCentralCode(value);
        // 遥测站地址
        target = PackageData.Telemetry_Station_Code.parse(target);
        value = PackageData.Telemetry_Station_Code.getValue();
        tPackage.setStationCode(value);
        // 密码   2020
        target = PackageData.Password.parse(target);
        value = PackageData.Password.getValue();
        tPackage.setRptPwd(value);

        // 功能码
        target = PackageData.Msg_Fun_Code.parse(target);
        value = PackageData.Msg_Fun_Code.getValue();
        tPackage.setFunCode(value);
        HandleMsgCode inst = HandleMsgCode.get(value);
        tPackage.setFunName(inst.getDesc());

        // 报文上下行标识及长度
        target = PackageData.Body_Length.parse(target);
        value = PackageData.Body_Length.getValue();

        bodyLengthParse.parse(value);

        tPackage.setUpordown(bodyLengthParse.getIdentifier() + "");//上下行标识
        tPackage.setRptLen(bodyLengthParse.getBodyLength());//长度

        //去掉报文起始符
        target = target.substring(2);

        // 流水号
        target = PackageData.Serial_Number.parse(target);
        value = PackageData.Serial_Number.getValue();
        tPackage.setSerialNum(value);

        // 发送报文时间
        target = PackageData.Send_Time.parse(target);
        value = PackageData.Send_Time.getValue();
        tPackage.setRptTime(DateUtil.parseTimeStr(value, "yyMMddHHmmss"));

        // 验证码
        target = PackageData.Check_Num.parse(target);
        value = PackageData.Check_Num.getValue();
        tPackage.setCheckNum(value);

        // 报文结束符
        target = PackageData.Body_End_Pattern.parse(target);
        value = PackageData.Body_End_Pattern.getValue();

        tPackage.setMessage(target);
        return tPackage;
    }
}
