package com.mvc.netty.dealconstant;

import com.mvc.JniCode;
import com.mvc.entity.TAiread;
import com.mvc.utils.DateUtil;
import com.mvc.utils.FileUtil;
import com.mvc.utils.NumberUtil;
import com.mvc.utils.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 观察数据标识符
 */
public enum MsgIdentifier {

    FF61("FF61", "当日应发数据", "[0-9]*"),
    FF71("FF71", "闸位1", "[0-9]*"),
    FF72("FF72", "闸位2", "[0-9]*"),
    FF73("FF73", "闸位3", "[0-9]*"),
    FF74("FF74", "闸位4", "[0-9]*"),
    FF75("FF75", "闸位5", "[0-9]*"),
    FF01("FF01", "水表累积水量", "[0-9]*"),
    FF02("FF02", "信号强度", "[0-9]*"),
    FF03("FF03", "图片高", "[0-9]*"),
    FF04("FF04", "图片宽", "[0-9]*"),
    FF05("FF05", "图片大小", "[0-9]*"),
    FF06("FF06", "图片内容", ""),
    I_001("001", "pH值", "[0-9]*"),
    I_011("011", "化学需氧量", "[0-9]*"),
    I_38("38", "电源电压", "[0-9]*"),
    I_060("060", "氨氮", "[0-9]*"),
    I_101("101", "总磷", "[0-9]*"),
    I_065("065", "总氮", "[0-9]*"),
    FF10("FF10", "温度", "[0-9]*"),
    FF11("FF11", "电量", "[0-9]*"),
    FF12("FF12", "运行次数", "[0-9]*"),
    FF13("FF13", "运行时间", "[0-9]*"),
    FF14("FF14", "信号质量", "[0-9]*"),
    FF15("FF15", "错误码", "[a-zA-Z0-9]*"),
    FF16("FF16", "软件版本", "[a-zA-Z0-9]*"),
    FF17("FF17", "通讯卡号", "[a-zA-Z0-9]*"),
    FF18("FF18", "信噪比", "[0-9]*"),
    FF19("FF19", "主板编号", "[a-zA-Z0-9]*"),
    FF07("FF07", "AI原始读书", "[a-zA-Z0-9]*"),
    FF22("FF22", "调试信息", "[a-zA-Z0-9]*"),
    FF08("FF08", "设备sn", "[a-zA-Z0-9]*"),
    FF09("FF09", "硬件版本号", "[a-zA-Z0-9]*"),
    FF21("FF21", "AI原始读书", "[a-zA-Z0-9]*"),
    FF23("FF23", "nb版本号", "[a-zA-Z0-9]*"),
    FF26("FF26", "AI软件版本", "[a-zA-Z0-9]*"),

    //2022-11-08 guozhao
    FF27("FF27", "IP1发送状态", "[a-zA-Z0-9]*"),
    FF35("FF35", "AI占用字节描述2", "[a-zA-Z0-9]*"),//0x10(或0x20) - 1字节数据字节 =10时只有压缩前的2字节
    FF36("FF36", "NB注册时位置信息", "[a-zA-Z0-9]*"),//0x10(或0x20) - 1字节数据字节 =10时只有压缩前的2字节
    FF37("FF37", "未分片段的图片区域坐标", "[a-zA-Z0-9]*"),//0x48

    FF38("FF38", "IMEI", "[0-9]*"),
    FF39("FF39", "CSQ(信号强度)", "[0-9]*"),
    FF41("FF41", "ECL(小区覆盖等级)", "[0-9]*"),

    FF43("FF43", "通信模块类型", "[0-9]*"),
    FF42("FF42", "矩阵网关下的子设备的BDA", "[0-9]*"),
    FF44("FF44", "尾数单位精度等级", "[0-9]*"),
    FF45("FF45", "投运类型", "[0-9]*"),
    FF46("FF46", "设备上报消息", "[0-9]*"),
    ;

    private final static Logger log = LoggerFactory.getLogger("MsgIdentifier");

    private final static char[] picHead = {
            0xFF, 0xD8, 0xFF, 0xDB, 0x00, 0x84, 0x00, 0x17, 0x10, 0x12, 0x15, 0x12, 0x0F, 0x17, 0x15, 0x13,
            0x15, 0x1A, 0x19, 0x17, 0x1C, 0x23, 0x3B, 0x26, 0x23, 0x20, 0x20, 0x23, 0x48, 0x33, 0x36, 0x2A,
            0x3B, 0x55, 0x4B, 0x59, 0x58, 0x53, 0x4B, 0x52, 0x51, 0x5E, 0x69, 0x87, 0x72, 0x5E, 0x64, 0x7F,
            0x65, 0x51, 0x52, 0x75, 0xA0, 0x77, 0x7F, 0x8B, 0x90, 0x97, 0x98, 0x97, 0x5B, 0x71, 0xA6, 0xB1,
            0xA4, 0x92, 0xB0, 0x87, 0x94, 0x97, 0x91, 0x01, 0x19, 0x1A, 0x1A, 0x23, 0x1F, 0x23, 0x45, 0x26,
            0x26, 0x45, 0x91, 0x61, 0x52, 0x61, 0x91, 0x91, 0x91, 0x91, 0x91, 0x91, 0x91, 0x91, 0x91, 0x91,
            0x91, 0x91, 0x91, 0x91, 0x91, 0x91, 0x91, 0x91, 0x91, 0x91, 0x91, 0x91, 0x91, 0x91, 0x91, 0x91,
            0x91, 0x91, 0x91, 0x91, 0x91, 0x91, 0x91, 0x91, 0x91, 0x91, 0x91, 0x91, 0x91, 0x91, 0x91, 0x91,
            0x91, 0x91, 0x91, 0x91, 0x91, 0x91, 0x91, 0x91, 0xFF, 0xC4, 0x01, 0xA2, 0x00, 0x00, 0x01, 0x05,
            0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x02,
            0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x01, 0x00, 0x03, 0x01, 0x01, 0x01, 0x01,
            0x01, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05,
            0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x10, 0x00, 0x02, 0x01, 0x03, 0x03, 0x02, 0x04, 0x03, 0x05,
            0x05, 0x04, 0x04, 0x00, 0x00, 0x01, 0x7D, 0x01, 0x02, 0x03, 0x00, 0x04, 0x11, 0x05, 0x12, 0x21,
            0x31, 0x41, 0x06, 0x13, 0x51, 0x61, 0x07, 0x22, 0x71, 0x14, 0x32, 0x81, 0x91, 0xA1, 0x08, 0x23,
            0x42, 0xB1, 0xC1, 0x15, 0x52, 0xD1, 0xF0, 0x24, 0x33, 0x62, 0x72, 0x82, 0x09, 0x0A, 0x16, 0x17,
            0x18, 0x19, 0x1A, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2A, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3A,
            0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4A, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5A,
            0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6A, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7A,
            0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8A, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99,
            0x9A, 0xA2, 0xA3, 0xA4, 0xA5, 0xA6, 0xA7, 0xA8, 0xA9, 0xAA, 0xB2, 0xB3, 0xB4, 0xB5, 0xB6, 0xB7,
            0xB8, 0xB9, 0xBA, 0xC2, 0xC3, 0xC4, 0xC5, 0xC6, 0xC7, 0xC8, 0xC9, 0xCA, 0xD2, 0xD3, 0xD4, 0xD5,
            0xD6, 0xD7, 0xD8, 0xD9, 0xDA, 0xE1, 0xE2, 0xE3, 0xE4, 0xE5, 0xE6, 0xE7, 0xE8, 0xE9, 0xEA, 0xF1,
            0xF2, 0xF3, 0xF4, 0xF5, 0xF6, 0xF7, 0xF8, 0xF9, 0xFA, 0x11, 0x00, 0x02, 0x01, 0x02, 0x04, 0x04,
            0x03, 0x04, 0x07, 0x05, 0x04, 0x04, 0x00, 0x01, 0x02, 0x77, 0x00, 0x01, 0x02, 0x03, 0x11, 0x04,
            0x05, 0x21, 0x31, 0x06, 0x12, 0x41, 0x51, 0x07, 0x61, 0x71, 0x13, 0x22, 0x32, 0x81, 0x08, 0x14,
            0x42, 0x91, 0xA1, 0xB1, 0xC1, 0x09, 0x23, 0x33, 0x52, 0xF0, 0x15, 0x62, 0x72, 0xD1, 0x0A, 0x16,
            0x24, 0x34, 0xE1, 0x25, 0xF1, 0x17, 0x18, 0x19, 0x1A, 0x26, 0x27, 0x28, 0x29, 0x2A, 0x35, 0x36,
            0x37, 0x38, 0x39, 0x3A, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4A, 0x53, 0x54, 0x55, 0x56,
            0x57, 0x58, 0x59, 0x5A, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6A, 0x73, 0x74, 0x75, 0x76,
            0x77, 0x78, 0x79, 0x7A, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8A, 0x92, 0x93, 0x94,
            0x95, 0x96, 0x97, 0x98, 0x99, 0x9A, 0xA2, 0xA3, 0xA4, 0xA5, 0xA6, 0xA7, 0xA8, 0xA9, 0xAA, 0xB2,
            0xB3, 0xB4, 0xB5, 0xB6, 0xB7, 0xB8, 0xB9, 0xBA, 0xC2, 0xC3, 0xC4, 0xC5, 0xC6, 0xC7, 0xC8, 0xC9,
            0xCA, 0xD2, 0xD3, 0xD4, 0xD5, 0xD6, 0xD7, 0xD8, 0xD9, 0xDA, 0xE2, 0xE3, 0xE4, 0xE5, 0xE6, 0xE7,
            0xE8, 0xE9, 0xEA, 0xF2, 0xF3, 0xF4, 0xF5, 0xF6, 0xF7, 0xF8, 0xF9, 0xFA, 0xFF, 0xC0, 0x00, 0x11,
            0x08, 0x00, 0x32, 0x00, 0xF0, 0x03, 0x01, 0x21, 0x00, 0x02, 0x11, 0x01, 0x03, 0x11, 0x01, 0xFF,
            0xDA, 0x00, 0x0C, 0x03, 0x01, 0x00, 0x02, 0x11, 0x03, 0x11, 0x00, 0x3F, 0x00
    };

    private static byte[] headBytes;

    private String key;
    private String desc;
    private String rule;
    private String value;

    static {
        headBytes = NumberUtil.charsToBytes(picHead);
    }

    private MsgIdentifier(String key, String desc, String rule) {
        this.key = key;
        this.desc = desc;
        this.rule = rule;
    }

    public String getKey() {
        return key;
    }

    public void setKey(String key) {
        this.key = key;
    }

    public String getDesc() {
        return desc;
    }

    public void setDesc(String desc) {
        this.desc = desc;
    }

    /**
     * 获取key集合
     *
     * @return
     */
    public static List<String> keys() {
        List<String> keyList = new ArrayList<>();
        for (MsgIdentifier msg : MsgIdentifier.values()) {
            keyList.add(msg.key);
        }
        return keyList;
    }

    /**
     * 解析报文
     *
     * @param msg
     * @return
     */
    public String parse(String msg) {
        String result = msg;
        StringBuilder stringBuilder = new StringBuilder();
        String key = this.key;
        result = handleIdentifier(result);
        String deinfer = result.substring(0, 2);
        result = result.substring(2);
        String binaryStr = NumberUtil.hexStringToBinaryString(deinfer);
        //确定小数位
        String redixStr = binaryStr.substring(binaryStr.length() - 3);
        int radix = NumberUtil.binaryToDecimal(redixStr);
        // 确定数字位数
        String numStr = binaryStr.substring(0, binaryStr.length() - 3);
        int num = NumberUtil.binaryToDecimal(numStr);
        String dataStr = result.substring(0, num * 2);
        if (key.equals("FF01") && dataStr.substring(0, 2).equals("ff")) {
            //取得实践数字字符串
            dataStr = getFactData(dataStr.substring(2));
            result = result.substring(dataStr.length() + 2);
            stringBuilder.append("-" + dataStr);
            if (radix != 0) {
                stringBuilder.insert(dataStr.length() + 1 - radix, '.');
            }
            this.value = stringBuilder.toString();
            return result;
        } else {
            //取得实践数字字符串
            /** 记一个特殊情况,部分设备传FF14时,长度表示符是18(3字节),是错误的,实际是2个字节,但由于getFactData中使用正则进行处理,将下一个引导符FF过滤了,反而变成了2字符
             * 导致看起来是正确的,暂时不作调整 2024-04-12 guozhao
             */
            dataStr = getFactData(dataStr);
            result = result.substring(dataStr.length());
            stringBuilder.append(dataStr);
            if (radix != 0) {
                stringBuilder.insert(dataStr.length() - radix, '.');
            }
            this.value = stringBuilder.toString();
            return result;
        }
    }

    /**
     * 取得实践数字字符串
     *
     * @param data
     * @return
     */
    private String getFactData(String data) {
        String result = data;
        if (!StringUtil.regularCheck(result, this.rule)) {
            result = data.substring(0, data.length() - 2);
            result = getFactData(result);
        }
        return result;
    }

    //处理调试信息
    public String parseDebugInfo(String msg) {
        String result = msg;
        result = handleIdentifier(result);

//        result = result.substring(2);
//        String shuju=result.substring(0,40);
//        result=result.substring(40);

        //兼容新旧设备调试信息(原来读取长度是写死的,现在改成根据协议读取长度)
        String len = result.substring(0, 2);
        int iLen = Integer.valueOf(len, 16);
        iLen = iLen >> 3;

        result = result.substring(2);
        String shuju = result.substring(0, iLen * 2);
        result = result.substring(iLen * 2);

        //处理数据
        StringBuilder stringBuilder = new StringBuilder();
        //改为按长度取  2022-07-05 guozhao (原来是写死20,-是按9判断的)
        for (int i = 0; i < iLen; i++) {
            String hex = shuju.substring(0, 2);
            String s = NumberUtil.hex2DecString(hex);
            if (i == iLen / 2 - 1) {
                stringBuilder.append(s).append("-");
            } else {
                stringBuilder.append(s).append(",");
            }
            shuju = shuju.substring(2);
        }
        this.value = stringBuilder.substring(0, stringBuilder.length() - 1);
        return result;
    }

    /**
     * 处理图片
     *
     * @param highStr
     * @param widthStr
     * @param sizeStr
     * @param msg
     * @return
     */
    public String parsePic(String highStr, String widthStr, String sizeStr, String msg, TAiread rawdata, String picsize) {
        String result = msg;

        result = handleIdentifier(result);
        //图片头编号
        String picbianhao = result.substring(0, 2);

        if (picbianhao.equals("99")) {//0x99代表BMP图 2022-11-08 guozhao
            return parsePic_Bmp(sizeStr, msg, rawdata, picsize);
        }

        //result = PackageOperateCode.PIC.parse(result);
        //去除FF 06 后面的2个字节  主要有(00 01 02 03 04 10 11 12 13 14)这几个值
        result = result.substring(2);
        int size = Integer.parseInt(sizeStr);
        String data = result.substring(0, size * 2);
        if ("".equals(data)) {
            return data;
        }
        result = result.substring(data.length());

        byte[] high = NumberUtil.intTo2bytes(Integer.valueOf(highStr));
        byte[] width = NumberUtil.intTo2bytes(Integer.valueOf(widthStr));

        // pic 图片内容
        byte[] pic = NumberUtil.hexStringToBytes(data);
        //组装图片头信息
        char[] headchararr = PicHeaderInfo.gethearbyindex(picbianhao);
        byte[] headbytearr = NumberUtil.charsToBytes(headchararr);
        // head
        /*byte[] head = new byte[headBytes.length];
        System.arraycopy(headBytes, 0, head, 0, headBytes.length);*/
        byte[] head = new byte[headbytearr.length];
        System.arraycopy(headbytearr, 0, head, 0, headbytearr.length);

        if (picbianhao.equals("aa")) {
            //彩色头更换高度宽度
            head[561] = high[0];
            head[562] = high[1];
            head[563] = width[0];
            head[564] = width[1];
        } else if (picbianhao.indexOf("a") >= 0 || picbianhao.indexOf("b") >= 0 || picbianhao.indexOf("c") >= 0 || picbianhao.indexOf("d") >= 0 || picbianhao.indexOf("e") >= 0) {
            //灰色头更换高度宽度
            head[496] = high[0];
            head[497] = high[1];
            head[498] = width[0];
            head[499] = width[1];
        } else {
            //彩色头更换高度宽度
            head[561] = high[0];
            head[562] = high[1];
            head[563] = width[0];
            head[564] = width[1];
        }
        byte[] byteAll = combine(head, pic);
        try {
            log.info("图片文件保存---" + rawdata.getAireadId());
            String nowtime = DateUtil.formateDate(new Date(), "yyyyMMddHHmmssSSS");

            //统一SN为11位 2022-12-01 guozhao
            String deviceSn = rawdata.getDeviceSn().length() == 12 ? rawdata.getDeviceSn().substring(0, 11) : rawdata.getDeviceSn();
            String filename = deviceSn + "_" + nowtime + "_" + picsize + ".jpg";
            FileUtil.writeBytesFile(rawdata.getBigJpgUrl(), filename, byteAll);
            rawdata.setBigJpgUrl(null);
            rawdata.setSmallJpgUrl(rawdata.getSmallJpgUrl() + filename);
            rawdata.setPicType(0);
        } catch (IOException e) {
            log.error("图片文件保存失败---" + rawdata.getAireadId());
        }
        return result;
    }

    /**
     * 处理BMP格式图片
     *
     * @return
     */
    private String parsePic_Bmp(String sizeStr, String msg, TAiread rawdata, String picsize) {
        String result = msg;
        result = handleIdentifier(result);
        //图片头编号
        //String picbianhao=result.substring(0,2);
        result = result.substring(2);
        int size = Integer.parseInt(sizeStr);
        String strImage = result.substring(0, size * 2);
        byte[] imageBuffer = NumberUtil.hexStringToBytes(strImage);

        //解压图片
        byte[] decodedData = new byte[1024 * 100];
        int decodedLen = JniCode.decondeData(imageBuffer, decodedData, imageBuffer.length);
        byte[] data = new byte[decodedLen];
        System.arraycopy(decodedData, 0, data, 0, decodedLen);
        decodedData = null;
        result = result.substring(strImage.length());
        try {
            log.info("--BMP文件保存--压缩前:" + imageBuffer.length + ";压缩后:" + decodedLen + "压缩比:" + (imageBuffer.length - decodedLen) / imageBuffer.length + ";AireadId:" + rawdata.getAireadId());
            String nowtime = DateUtil.formateDate(new Date(), "yyyyMMddHHmmssSSS");
            //统一SN为11位 2022-12-01 guozhao
            String deviceSn = rawdata.getDeviceSn().length() == 12 ? rawdata.getDeviceSn().substring(0, 11) : rawdata.getDeviceSn();
            String bmpfilename = deviceSn + "_" + nowtime + "_" + picsize + ".bmp";
            //String bmpDir=rawdata.getBigJpgUrl().replace(ResourcePath.Rawdata.getPath(),ResourcePath.BmpPath.getPath());
            FileUtil.writeBytesFile(rawdata.getBigJpgUrl(), bmpfilename, data);

            //另存一份jpg
            //String jpgfilename=rawdata.getDeviceSn()+"_"+nowtime+"_"+picsize+".jpg";
            //FileUtil.bmp2Jpeg(rawdata.getBigJpgUrl()+bmpfilename,rawdata.getBigJpgUrl()+jpgfilename);

            rawdata.setBigJpgUrl(null);
            rawdata.setSmallJpgUrl(rawdata.getSmallJpgUrl() + bmpfilename);
            rawdata.setPicType(1);
        } catch (IOException e) {
            log.error("图片文件保存失败---" + rawdata.getAireadId());
        }
        data = null;
        return result;
    }

    /**
     * 处理片段式的图片
     *
     * @param highStr
     * @param widthStr
     * @param sizeStr
     * @param msg
     * @param rawdata
     * @param dirName
     * @param partIndex
     * @return
     */
    public String parsePicPart(String highStr, String widthStr, String sizeStr, String msg, TAiread rawdata, String dirName, int partIndex) {
        String result = msg;
        result = handleIdentifier(result);
        //图片头编号
        String picbianhao = result.substring(0, 2);
        if (picbianhao.equals("99") || picbianhao.equals("98")) {//0x99代表BMP图 2022-11-08 guozhao
            return parsePicPart_Bmp(sizeStr, msg, rawdata, dirName, partIndex, picbianhao.equals("99"));
        }
        //result = PackageOperateCode.PIC.parse(result);
        //去除FF 06 后面的2个字节  主要有(00 01 02 03 04 10 11 12 13 14)这几个值
        result = result.substring(2);
        int size = Integer.parseInt(sizeStr);
        String data = result.substring(0, size * 2);
        if ("".equals(data)) {
            return data;
        }
        result = result.substring(data.length());

        byte[] high = NumberUtil.intTo2bytes(Integer.valueOf(highStr));
        byte[] width = NumberUtil.intTo2bytes(Integer.valueOf(widthStr));

        // pic 图片内容
        byte[] pic = NumberUtil.hexStringToBytes(data);
        //组装图片头信息
        char[] headchararr = PicHeaderInfo.gethearbyindex(picbianhao);
        byte[] headbytearr = NumberUtil.charsToBytes(headchararr);
        // head
        /*byte[] head = new byte[headBytes.length];
        System.arraycopy(headBytes, 0, head, 0, headBytes.length);*/
        byte[] head = new byte[headbytearr.length];
        System.arraycopy(headbytearr, 0, head, 0, headbytearr.length);

        if (picbianhao.equals("aa")) {
            //彩色头更换高度宽度
            head[561] = high[0];
            head[562] = high[1];
            head[563] = width[0];
            head[564] = width[1];
        } else if (picbianhao.indexOf("a") >= 0 || picbianhao.indexOf("b") >= 0 || picbianhao.indexOf("c") >= 0 || picbianhao.indexOf("d") >= 0 || picbianhao.indexOf("e") >= 0) {
            //灰色头更换高度宽度
            head[496] = high[0];
            head[497] = high[1];
            head[498] = width[0];
            head[499] = width[1];
        } else {
            //彩色头更换高度宽度
            head[561] = high[0];
            head[562] = high[1];
            head[563] = width[0];
            head[564] = width[1];
        }
        byte[] byteAll = combine(head, pic);
        try {
            log.info("片段文件保存---" + partIndex);
            String filename = partIndex + ".jpg";//1.jpg  2.jpg

            String path = rawdata.getBigJpgUrl() + dirName + "/";
            File dir = new File(path);
            if (!dir.exists()) {
                dir.mkdirs();
            }

            FileUtil.writeBytesFile(path, filename, byteAll);
/*            rawdata.setBigJpgUrl(null);
            rawdata.setSmallJpgUrl(rawdata.getSmallJpgUrl()+filename);*/
            rawdata.setPicType(0);
        } catch (IOException e) {
            log.error("图片文件保存失败---" + rawdata.getAireadId());
        }
        return result;
    }

    /**
     * 处理BMP格式片段式图片
     *
     * @return
     */
    private String parsePicPart_Bmp(String sizeStr, String msg, TAiread rawdata, String dirName, int partIndex, boolean isCompress) {
        String result = msg;
        result = handleIdentifier(result);
        //图片头编号
        //String picbianhao=result.substring(0,2);
        result = result.substring(2);
        int size = Integer.parseInt(sizeStr);
        String strImage = result.substring(0, size * 2);
        byte[] imageBuffer = NumberUtil.hexStringToBytes(strImage);


        result = result.substring(strImage.length());
        byte[] data = null;
        if (isCompress) {
            //解压图片
            byte[] decodedData = new byte[1024 * 100];
            int decodedLen = JniCode.decondeData(imageBuffer, decodedData, imageBuffer.length);
            data = new byte[decodedLen];
            System.arraycopy(decodedData, 0, data, 0, decodedLen);
            decodedData = null;
            log.info("--BMP片段保存--压缩前:" + imageBuffer.length + ";压缩后:" + decodedLen + "压缩比:" + (imageBuffer.length - decodedLen) / imageBuffer.length + ";AireadId:" + rawdata.getAireadId());
        } else {
            data = imageBuffer;
        }
        try {
            String path = rawdata.getBigJpgUrl() + dirName + "/";
            File dir = new File(path);
            if (!dir.exists()) {
                dir.mkdirs();
            }
            String bmpfilename = partIndex + ".bmp";
            FileUtil.writeBytesFile(path, bmpfilename, data);
            rawdata.setPicType(1);
        } catch (IOException e) {
            log.error("图片文件保存失败---" + rawdata.getAireadId());
        }
        data = null;
        return result;
    }


    /**
     * 处理图片占用字节描述2
     *
     * @param msg
     * @return
     */
    public String parseImageDescribe(String msg) {
        String result = msg;
        result = handleIdentifier(result);

        String len = result.substring(0, 2);
        int iLen = Integer.valueOf(len, 16);
        iLen = iLen >> 3;

        result = result.substring(2);
        String shuju = "";
        //0x10(或0x20) 现在先只取压缩后的长度,10是(0,4);20是(4,8)
        if (iLen == 2) {
            shuju = result.substring(0, iLen * 2);
        } else {
            shuju = result.substring(4, 8);
        }
        result = result.substring(iLen * 2);

        this.value = Integer.valueOf(shuju, 16).toString();
        return result;
    }

    private byte[] combine(byte[] head, byte[] pic) {
        byte[] result = new byte[head.length + pic.length];
        System.arraycopy(head, 0, result, 0, head.length);
        System.arraycopy(pic, 0, result, head.length, pic.length);
        return result;
    }

    /**
     * 处理表示符
     *
     * @param msg
     * @return
     */
    public String handleIdentifier(String msg) {
        String result = msg;
        result = result.substring(this.key.length());
        return result;
    }

    /**
     * 获取成员
     *
     * @param key
     * @return
     */
    public static MsgIdentifier getElement(String key) {
        for (MsgIdentifier msg : MsgIdentifier.values()) {
            if (msg.key.equals(key)) {
                return msg;
            }
        }
        return null;
    }

    public String getValue() {
        return value;
    }
}
