package com.mvc.netty;

import com.mvc.entity.*;
import com.mvc.model.TAireadModel;
import com.mvc.model.TTaskDownModel;
import com.mvc.model.TaskRemoteCfgModel;
import com.mvc.model.TaskSpecialCfgModel;
import com.mvc.netty.dealconstant.HandleMsgCode;
import com.mvc.netty.dealconstant.PackageData;
import com.mvc.netty.dealconstant.PackageOperateCode;
import com.mvc.netty.dealconstant.TaskRemoteIdentifier;
import com.mvc.service.taizhang.TaskRemoteCfgService;
import com.mvc.utils.CRC16Util;
import com.mvc.utils.DateUtil;
import com.mvc.utils.NumberUtil;
import com.mvc.utils.StringUtil;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.nio.charset.StandardCharsets;
import java.util.Date;

@Component
public class SimpleResponse {

    @Resource
    private TaskRemoteCfgService taskRemoteCfgService;

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

    private static final String MSG_CENTER_STATION = "16";        //中心站地址
    private static final String MSG_PASSWORD = "2020";            //密码
    private static final String MSG_SERIAL_NUMBER = "0002";       //流水号(无实际应用,未动态生成)
    private static final int MSG_UP_FLAG = 0;                     //上行标识符
    private static final int MSG_DOWN_FLAG = 8;                   //下行标识符

    /**
     * 生成回复报文
     *
     * @param tPackLog
     * @return
     */
    public String generate(TPackLog tPackLog) {
        String message = "";
        String body = null;

        // 帧起始符
        message = PackageOperateCode.SOH.encode(message);
        // 遥测站地址
        message = PackageData.Telemetry_Station_Code.encode(message, tPackLog);
        // 中心站地址
        message = PackageData.Central_Station_Code.encode(message, tPackLog);
        // 密码
        message = PackageData.Password.encode(message, tPackLog);
        // 功能码
        message = PackageData.Msg_Fun_Code.encode(message, tPackLog);
        // 报文上下行标识及长度
        body = generateBody(tPackLog);
        message = PackageData.Body_Length.encode(message, tPackLog, body.length());
        // 报文起始符
        message = PackageOperateCode.STX.encode(message);
        // 报文正文
        message += body;
        // 报文结束符
        message = PackageOperateCode.EOT.encode(message);
        // 校验码
        //int crc = CRC16Util.calcCrc16(message.getBytes(StandardCharsets.UTF_8));
        int crc = CRC16Util.calcCrc16(NumberUtil.hexStringToBytes(message));
        message += StringUtil.leftPad(Integer.toHexString(crc), 4, '0');
        return message;
    }

    /**
     * 生成回复报文
     *
     * @param stationCode
     * @return
     */
    public String generateByStationCode(String stationCode) {
        TPackLog tPackLog = new TPackLog();
        tPackLog.setStationCode(stationCode);
        tPackLog.setCentralCode(MSG_CENTER_STATION);
        tPackLog.setRptPwd(MSG_PASSWORD);
        tPackLog.setFunCode(HandleMsgCode.H_32.getCode());
        tPackLog.setSerialNum(MSG_SERIAL_NUMBER);
        String message = "";
        String body = null;
        // 帧起始符
        message = PackageOperateCode.SOH.encode(message);
        // 遥测站地址
        message = PackageData.Telemetry_Station_Code.encode(message, tPackLog);
        // 中心站地址
        message = PackageData.Central_Station_Code.encode(message, tPackLog);
        // 密码
        message = PackageData.Password.encode(message, tPackLog);
        // 功能码
        message = PackageData.Msg_Fun_Code.encode(message, tPackLog);
        // 报文上下行标识及长度
        body = generateBody(tPackLog);
        message = PackageData.Body_Length.encode(message, tPackLog, body.length());
        // 报文起始符
        message = PackageOperateCode.STX.encode(message);
        // 报文正文
        message += body;
        // 报文结束符
        message = PackageOperateCode.EOT.encode(message);
        // 校验码
        //int crc = CRC16Util.calcCrc16(message.getBytes(StandardCharsets.UTF_8));
        int crc = CRC16Util.calcCrc16(NumberUtil.hexStringToBytes(message));
        message += StringUtil.leftPad(Integer.toHexString(crc), 4, '0');
        return message;
    }


    /**
     * 生成报文正文
     *
     * @param tPackage
     * @return
     */
    public String generateBody(TPackLog tPackage) {
        String message = "";
        // 流水号
        message = PackageData.Serial_Number.encode(message, tPackage);
        // 发报时间
        message = PackageData.Send_Time.encode(message, tPackage);

        return message;
    }

    //生成远程配置下行报文
    //添加参数rawStationCode(原始遥测站编码)来源于ttaskdown, 2022-06-27 guozhao
    //添加参数isReviewPush 是否有审核值要推送(有则结束符是1B) 2022-08-31 guozhao
    public String generateResponse_remotecfg(TaskRemoteCfgModel cfg, String rawStationCode, boolean isReviewPush) {
        String message = "";
        String hexaskRemoteCfg = taskRemoteCfgService.getHexaskRemoteCfg(cfg);
        int length = hexaskRemoteCfg.length() / 2 + 10;
        // 帧起始符
        message = PackageOperateCode.SOH.encode(message);
        //遥测站站点(帧头时使用原始的遥测站编码,因为有修改遥测站编码的情况)
        //message+=cfg.getStationCode();
        message += rawStationCode;
        //中心站点
        message += MSG_CENTER_STATION;
        //密码
        if (StringUtils.isNotEmpty(cfg.getDeviceType()) && cfg.getDeviceType().equals("2")) {
            message += "2120";                    //密码首位代表设备类型21=压力表
        } else {
            message += MSG_PASSWORD;
        }

        //功能码
        message += HandleMsgCode.H_E0.getCode();

        //上下行标识符及长度
        int xiaxing = MSG_DOWN_FLAG;
        int biaoshi = xiaxing << 12 | length;
        message += NumberUtil.numToHex16(biaoshi);
        //报文起始符02
        message += PackageOperateCode.STX.getKey();
        //流水号0002
        message += MSG_SERIAL_NUMBER;
        //发报时间
        String fabaotime = DateUtil.formateDate(new Date(), "yyMMddHHmmss");
        message += fabaotime;
        //数据内容
        message += hexaskRemoteCfg;
        //下行结束符
        if (isReviewPush) {
            message = PackageOperateCode.ESC.encode(message);
        } else {
            message = PackageOperateCode.ENQ.encode(message);
        }

        int crc = CRC16Util.calcCrc16(NumberUtil.hexStringToBytes(message));
        message += StringUtil.leftPad(Integer.toHexString(crc), 4, '0');
        return message;
    }

    /**
     * 生成远程配置下行报文
     *
     * @param tTaskDownModel
     * @param isReviewPush   是否有审核值要推送(有则结束符是1B) 2022-08-31 guozhao
     * @return
     */
    public String generateResponse_zhaocecfg(TTaskDownModel tTaskDownModel, boolean isReviewPush) {
        String message = "";
        int length = 10;
        // 帧起始符
        message = PackageOperateCode.SOH.encode(message);
        //遥测站站点
        message += tTaskDownModel.getStationCode();
        //中心站点
        message += MSG_CENTER_STATION;
        //密码
        message += MSG_PASSWORD;
        //功能码
        message += HandleMsgCode.H_E1.getCode();//注意注意:此处应该是E1 2023-10-31 guozhao

        //上下行标识符及长度
        int biaoshi = MSG_DOWN_FLAG << 12 | length;
        message += NumberUtil.numToHex16(biaoshi);
        //报文起始符02
        message += PackageOperateCode.STX.getKey();
        //流水号0002
        message += MSG_SERIAL_NUMBER;
        //发报时间
        String fabaotime = DateUtil.formateDate(new Date(), "yyMMddHHmmss");
        message += fabaotime;
        //数据内容
        //下行结束符
        if (isReviewPush) {
            message = PackageOperateCode.ESC.encode(message);
        } else {
            message = PackageOperateCode.ENQ.encode(message);
        }
        int crc = CRC16Util.calcCrc16(NumberUtil.hexStringToBytes(message));
        message += StringUtil.leftPad(Integer.toHexString(crc), 4, '0');
        return message;
    }

    //生成水利所上行报文
    public String generatefor_sls_send(TAireadModel tAiread, String content) {
        //没有有效值(大图或报文异常情况下),不做推送 2022-09-04 guozhao
        if (tAiread==null || tAiread.getIdentValue() == null || tAiread.getVoltage() == null) {
            log.info("+生成水利所报文出现无效读数记录generatefor_sls_send");
            return "";
        }
        String message = "";
        int length = content.length() / 2 + 2;
        // 帧起始符
        message = PackageOperateCode.SOH.encode(message);
        //中心站点
        message += MSG_CENTER_STATION;
        //遥测站站点
        message += tAiread.getStationCode();
        //密码
        message += MSG_PASSWORD;
        //功能码
        message += HandleMsgCode.H_32.getCode();
        //上下行标识符及长度
        int xiaxing = MSG_UP_FLAG;
        int biaoshi = xiaxing << 12 | length;
        message += NumberUtil.numToHex16(biaoshi);
        //报文起始符02
        message += PackageOperateCode.STX.getKey();
        //流水号0002
        message += MSG_SERIAL_NUMBER;
        //数据内容
        message += content;
        //报文结束符
        message = PackageOperateCode.ETX.encode(message);
        int crc = CRC16Util.calcCrc16(NumberUtil.hexStringToBytes(message));
        message += StringUtil.leftPad(Integer.toHexString(crc), 4, '0');
        //通过tcp 发送水利所
        log.info("发给水利所的小图报文:" + message);
        //return tcpClient.SendtoServer(NumberUtil.hexStringToBytes(message), tAiread.getTaskReviewId());
        return message;
    }

    //生成水利所内容部分
    public String generatefor_content_sls(TAireadModel tAiread) {
        String message = "";
        try {
            //发报时间
            String fabaotime = DateUtil.formateDate(tAiread.getRptTime(), "yyMMddHHmmss");
            message += fabaotime;
            //遥测站地址
            message += "F1F1";
            message += tAiread.getStationCode();
            //遥测站分类码
            message += "48";
            //观测时间
            message += "F0F0" + DateUtil.formateDate(tAiread.getSampleTime(), "yyMMddHHmm");
            //电压
            message += "3812";
            int v = tAiread.getVoltage() == null ? 366 : tAiread.getVoltage().multiply(new BigDecimal(100)).intValue();
            String value = "0000" + v;
            message += value.substring(value.length() - 4);
            //AI识别结果
            message += "FF0145";
            BigDecimal ai = tAiread.getIdentValue().multiply(new BigDecimal(100000));
            String aivalue = "0000000000000000" + ai.stripTrailingZeros().toPlainString();
            message += aivalue.substring(aivalue.length() - 16);
            //温度
            message += "FF1011";
            int temper = tAiread.getTemperature() == null ? 11 : tAiread.getTemperature().multiply(new BigDecimal(10)).intValue();
            String tempervalue = "0000" + temper;
            message += tempervalue.substring(tempervalue.length() - 4);
            //剩余电量百分比
            message += "FF1108";
            String socrate = "00" + (tAiread.getSocRate() == null ? "99" : tAiread.getSocRate());
            message += socrate.substring(socrate.length() - 2);
            //主板运行次数
            message += "FF1218";
            String runnum = "000000" + (tAiread.getMcuRunNum() == null ? "" : tAiread.getMcuRunNum());
            message += runnum.substring(runnum.length() - 6);
            //电池运行时间
            message += "FF1318";
            String runtime = "000000" + (tAiread.getBatRumTime() == null ? "" : tAiread.getBatRumTime());
            message += runtime.substring(runtime.length() - 6);
            //rsrp信号质量
            message += "FF1410";
            String rsrp = "0000" + (tAiread.getRsrp() == null ? "0000" : tAiread.getRsrp());
            message += rsrp.substring(rsrp.length() - 4);
            //错误码
            message += "FF1528";
            message += StringUtils.isEmpty(tAiread.getErrorCode()) ? "0818000000" : tAiread.getErrorCode();
            //软件版本
            message += "FF1630";
            //新程序的版本长度不一样,2024-09-04 guozhao
            message += (StringUtils.isEmpty(tAiread.getSoftVer()) || tAiread.getSoftVer().length() != 12) ? "240101000000" : tAiread.getSoftVer();
            //SIM卡号
            message += "FF1750";
            message += StringUtils.isEmpty(tAiread.getCcid()) ? "00000000000000000000" : tAiread.getCcid();
            //snr信造比
            message += "FF1808";
            String snr = "00" + (tAiread.getSnr() == null ? "00" : tAiread.getSnr().intValue());
            message += snr.substring(snr.length() - 2);
            //主板编号
            message += "FF1950";
            //发现有空的,给默认值  2024-09-04 guozhao
            message += StringUtils.isEmpty(tAiread.getHardwareCode()) ? "0A0B0C0D0E0F10111213" : tAiread.getHardwareCode();
            //AI原始读数
            message += "FF0745";
            String rawvalue = "0000000000000000" + tAiread.getRawValue().multiply(new BigDecimal(100000)).stripTrailingZeros().toPlainString();

            message += rawvalue.substring(rawvalue.length() - 16);
            //设备sn
            message += "FF0830";
            String sn = "0000" + tAiread.getDeviceSn();
            message += sn.substring(sn.length() - 12);
            //cpu硬件版本号
            message += "FF0910";
            message += StringUtils.isEmpty(tAiread.getHardVersion()) ? "0000" : tAiread.getHardVersion();

            //不再传图片部分(经测试可行) 2025-03-25
            //图片高
            /*message += "FF0310";
            String height = "0000" + (tAiread.getPicHeigth() == null ? 36 : tAiread.getPicHeigth().intValue());
            message += height.substring(height.length() - 4);

            //图片宽
            message += "FF0410";
            String width = "0000" + (tAiread.getPicWidth() == null ? 124 : tAiread.getPicWidth().intValue());
            message += width.substring(width.length() - 4);

            String picRaw = tAiread.getSmallRawData();

            //图片占用字节描述
            message += "FF0510";
            String picLen = "0000" + (picRaw.length() / 2);
            message += picLen.substring(picLen.length() - 4);

            //图片起始标志+类型
            message += "FF06";
            String type = "00" + (StringUtils.isEmpty(tAiread.getSmallPicType()) ? "02" : tAiread.getSmallPicType());
            message += type.substring(type.length() - 2);
            //图片内容
            message += picRaw;*/
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
        return message;
    }

    /**
     * 生成审核值推送报文(IP2,非水利所的)
     *
     * @param model
     * @return
     */
    public String generatefor_TaskReviewPushMsg(TaskReviewPush model) {
        String message = "";
        int length = 10;
        // 帧起始符
        message = PackageOperateCode.SOH.encode(message);
        //遥测站站点
        message += model.getStationCode();
        //中心站点
        message += MSG_CENTER_STATION;
        //密码
        message += MSG_PASSWORD;
        //功能码
        message += "E4";

        //上下行标识符及长度
        int biaoshi = MSG_DOWN_FLAG << 12 | length;
        message += NumberUtil.numToHex16(biaoshi);
        //报文起始符02
        message += PackageOperateCode.STX.getKey();
        //流水号0002
        message += MSG_SERIAL_NUMBER;
        //发报时间
        String fabaotime = DateUtil.formateDate(new Date(), "yyMMddHHmmss");
        message += fabaotime;
        //正文部分

        //采样时间
        message += "F0F0";
        String sampleTime = DateUtil.formateDate(model.getSampleTime(), "yyMMddHHmm");
        message += sampleTime;

        //审核结果值
        message += "FF0145";
        BigDecimal ai = model.getReviewValue().multiply(new BigDecimal(100000));
        String aivalue = "0000000000000000" + ai.stripTrailingZeros().toPlainString();
        message += aivalue.substring(aivalue.length() - 16, aivalue.length());
        //下行结束符

        message = PackageOperateCode.ENQ.encode(message);
        int crc = CRC16Util.calcCrc16(NumberUtil.hexStringToBytes(message));
        message += StringUtil.leftPad(Integer.toHexString(crc), 4, '0');
        return message;
    }

    /**
     * 根据tPackLog和报文内容生成报文
     *
     * @param tPackLog
     * @param body
     * @return
     */
    private String generateResponseMessage(TPackLog tPackLog, String body) {
        String message = "";

        // 帧起始符
        message = PackageOperateCode.SOH.encode(message);
        // 遥测站地址
        message = PackageData.Telemetry_Station_Code.encode(message, tPackLog);
        // 中心站地址
        message = PackageData.Central_Station_Code.encode(message, tPackLog);
        // 密码
        message = PackageData.Password.encode(message, tPackLog);
        // 功能码
        message = PackageData.Msg_Fun_Code.encode(message, tPackLog);
        // 报文上下行标识及长度
        message = PackageData.Body_Length.encode(message, tPackLog, body.length());
        // 报文起始符
        message = PackageOperateCode.STX.encode(message);
        // 报文正文
        message += body;
        // 报文结束符
        message = PackageOperateCode.ETX.encode(message);
        // 校验码
        //int crc = CRC16Util.calcCrc16(message.getBytes(StandardCharsets.UTF_8));
        int crc = CRC16Util.calcCrc16(NumberUtil.hexStringToBytes(message));
        message += StringUtil.leftPad(Integer.toHexString(crc), 4, '0');
        return message;
    }

    /**
     * 生成查询服务器IP的回执内容部分
     *
     * @param tPackLog
     * @param ip1port1
     * @param domain
     * @param taskNum
     * @return
     */
    public String generateQueryAddress(TPackLog tPackLog, String ip1port1, String domain, int taskNum) {
        String message = "";
        // 流水号
        message = PackageData.Serial_Number.encode(message, tPackLog);
        // 发报时间
        message = PackageData.Send_Time.encode(message, tPackLog);
        //服务器重定向标志
        message += "fb0108";
        message += StringUtils.isEmpty(ip1port1) && StringUtils.isEmpty(domain) ? "00" : "01";
        //服务器存在未执行的任务标志
        message += "fb0208";
        message += taskNum == -1 ? "99" : StringUtil.leftPad(taskNum + "", 2, '0');
        if (StringUtils.isNotEmpty(ip1port1)) {
            String[] split1 = ip1port1.split(":");
            String[] split2 = split1[0].split("\\.");
            String value1 = ("000" + split2[0]).substring(("000" + split2[0]).length() - 3);
            String value2 = ("000" + split2[1]).substring(("000" + split2[1]).length() - 3);
            String value3 = ("000" + split2[2]).substring(("000" + split2[2]).length() - 3);
            String value4 = ("000" + split2[3]).substring(("000" + split2[3]).length() - 3);
            String value5 = ("000000" + split1[1]).substring(("000000" + split1[1]).length() - 6);
            message += "fb035000" + value1 + value2 + value3 + value4 + value5;
        }
        if (StringUtils.isNotEmpty(domain)) {
            String[] arrDomain = domain.split(":");
            if (arrDomain.length == 2 && StringUtils.isNotEmpty(arrDomain[0]) && StringUtils.isNumeric(arrDomain[1])) {
                byte[] dz = arrDomain[0].getBytes(StandardCharsets.US_ASCII);
                byte[] dk = NumberUtil.intTo2bytes(Integer.parseInt(arrDomain[1]));
                byte[] bytes = new byte[30];
                System.arraycopy(dz, 0, bytes, 0, dz.length);
                System.arraycopy(dk, 0, bytes, 28, dk.length);
                message += "fb04f0" + NumberUtil.bytesToHexStr(bytes);
            }
        }

        return generateResponseMessage(tPackLog, message);
    }

    /**
     * 生成查询任务的回执内容部分
     *
     * @param tPackLog
     * @param handleMsgCode
     * @return
     */
    public String generateQueryTask(TPackLog tPackLog, HandleMsgCode handleMsgCode) {
        String message = "";
        // 流水号
        message = PackageData.Serial_Number.encode(message, tPackLog);
        // 发报时间
        message = PackageData.Send_Time.encode(message, tPackLog);
        return generateResponseMessage(tPackLog, message);
    }

    /**
     * 生成回执报文
     *
     * @param stationCode
     * @param funCode
     * @return
     */
    public String generateMsgByStationCode(String stationCode, HandleMsgCode funCode, String dataBody) {
        String message = "";
        message = PackageOperateCode.SOH.encode(message);
        // 遥测站地址
        message = PackageData.Telemetry_Station_Code.encode(message, stationCode);
        // 中心站地址
        message = PackageData.Central_Station_Code.encode(message, MSG_CENTER_STATION);
        // 密码
        message = PackageData.Password.encode(message, MSG_PASSWORD);
        // 功能码
        message = PackageData.Msg_Fun_Code.encode(message, funCode.getCode());
        // 报文上下行标识及长度
        String body = generateNormalBody() + dataBody;
        message = PackageData.Body_Length.encode(message, "", body.length());
        // 报文起始符
        message = PackageOperateCode.STX.encode(message);
        // 报文正文
        message += body;
        // 报文结束符
        message += funCode.getEndMark();
        // 校验码
        //int crc = CRC16Util.calcCrc16(message.getBytes(StandardCharsets.UTF_8));
        int crc = CRC16Util.calcCrc16(NumberUtil.hexStringToBytes(message));
        message += StringUtil.leftPad(Integer.toHexString(crc), 4, '0');
        return message;
    }

    /*public String generateSpecialCfgMessage(String imei,String deviceSn){

    }*/

    /**
     * 生成报文通用正文部分
     *
     * @return
     */
    private String generateNormalBody() {
        String message = "";
        // 流水号
        message = PackageData.Serial_Number.encode(message, MSG_SERIAL_NUMBER);
        // 发报时间
        message = PackageData.Send_Time.encode(message, "");

        return message;
    }

    /**
     * 生成报文
     *
     * @param msgCode     功能标识符
     * @param stationCode 站点编码
     * @param body        报文正文
     * @return
     */
    private String generateMessage(HandleMsgCode msgCode, String stationCode, String body) {
        String message = "";

        String fabaotime = DateUtil.formateDate(new Date(), "yyMMddHHmmss");

        int length = (body.length() + MSG_SERIAL_NUMBER.length() + fabaotime.length()) / 2;
        // 帧起始符
        message = PackageOperateCode.SOH.encode(message);
        //遥测站站点(帧头时使用原始的遥测站编码,因为有修改遥测站编码的情况)
        //message+=cfg.getStationCode();
        message += stationCode;
        //中心站点
        message += MSG_CENTER_STATION;

        //密码
        message += MSG_PASSWORD;

        //功能码
        message += msgCode.getCode();

        //上下行标识符及长度
        int xiaxing = MSG_DOWN_FLAG;
        int biaoshi = xiaxing << 12 | length;
        message += NumberUtil.numToHex16(biaoshi);
        //报文起始符02
        message += PackageOperateCode.STX.getKey();
        //流水号0002
        message += MSG_SERIAL_NUMBER;
        //发报时间

        message += fabaotime;
        //数据内容
        message += body;

        //结束符
        message += msgCode.getEndMark();

        int crc = CRC16Util.calcCrc16(NumberUtil.hexStringToBytes(message));
        message += StringUtil.leftPad(Integer.toHexString(crc), 4, '0');
        return message;
    }

    /**
     * 生成专项配置报文
     *
     * @param cfg
     * @return
     */
    public String generateSpecialCfgMessage(TaskSpecialCfgModel cfg) {
        StringBuilder sb = new StringBuilder();
        sb.append(TaskRemoteIdentifier.getIdentifierMsg(TaskRemoteIdentifier.FF38, cfg.getImei()));

        //目前默认只处理数据长度为零的情况,即只处理主设备(网关或带有独立远传模块的设备)-主设备时，数据长度为0
        if (StringUtils.isNotEmpty(cfg.getSubDeviceAddress())) {
            sb.append(TaskRemoteIdentifier.getIdentifierMsg(TaskRemoteIdentifier.FF42, cfg.getSubDeviceAddress()));
        } else {
            sb.append(TaskRemoteIdentifier.FF42.getKey());
            sb.append("00");
        }
        //SN 是后补0
        String deviceSn = StringUtils.rightPad(cfg.getDeviceSn(), 12, '0');
        sb.append(TaskRemoteIdentifier.getIdentifierMsg(TaskRemoteIdentifier.EE01, deviceSn));
        //此时StionCode并无实际意义
        return generateMessage(HandleMsgCode.H_E3, cfg.getDeviceSn().substring(1), sb.toString());
    }

    //生成水利所内容部分(仅温度监测使用)
    public String generatefor_content_sls_temperature(TAireadModel tAiread) {
        String message = "";
        try {
            //发报时间
            String fabaotime = DateUtil.formateDate(new Date(), "yyMMddHHmmss");
            message += fabaotime;
            //遥测站地址
            message += "F1F1";
            message += tAiread.getStationCode();
            //遥测站分类码
            message += "48";
            //观测时间
            message += "F0F0" + DateUtil.formateDate(tAiread.getSampleTime(), "yyMMddHHmm");
            //电压
            message += "3812";
            int v = tAiread.getVoltage() == null ? 366 : tAiread.getVoltage().multiply(new BigDecimal(100)).intValue();
            String value = "0000" + v;
            message += value.substring(value.length() - 4);
            //温度
            message += "FF1011";
            int temper = tAiread.getTemperature() == null ? 11 : tAiread.getTemperature().multiply(new BigDecimal(10)).intValue();
            //特别注意：感觉平台温度值会减去100,原因暂不明确(因此下面加了100,1000是因为上面已经乘10了) 2025-03-25
            temper = temper + 1000;
            String tempervalue = "0000" + temper;
            message += tempervalue.substring(tempervalue.length() - 4);

            //AI原始读数
            message += "FF0745";
            String rawvalue = "0000000000000000" + tAiread.getRawValue().multiply(new BigDecimal(100000)).stripTrailingZeros().toPlainString();
            message += rawvalue.substring(rawvalue.length() - 16);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
        return message;
    }
}
