package com.zd.laboratory.socket.vo;

import cn.hutool.json.JSONUtil;
import com.zd.common.core.annotation.DeviceID;
import com.zd.common.core.annotation.FunctionMapper;
import org.apache.commons.lang3.StringUtils;

import java.math.BigDecimal;

/**
 * 传感器传输协议实体类
 * @author liubo
 */
public class TransmissionVo {

    /**
     * 帧头，固定为fedc
     */
    private String A1;

    /**
     * 固定01 版本号
     */
    private String A2;

    /**
     * 6个字节，此为设备唯一id
     */
    @DeviceID
    private String A3;

    /**
     * 会话控制，自动增加，不用管，session
     */
    private String A4;

    /**
     * 固定命令 03
     */
    private String A5;

    /**
     * 后面的内容的字节长度。从这两个字节以后的，每四个字节都是一条数据
     *
     * A7 和 A8 字节长度
     */
    private String A6;

    /**
     * 为16进制显示的数据
     *
     * 内容存在多条， 四个字节为一个值
     */
    private String A7;

    /**
     * 煤尘   没有小数位    mg/m3
     */
    @FunctionMapper( funNum = "fenchen")
    private String A7_1;

    /**
     * 温度   一个小数位    ℃
     */
    @FunctionMapper( funNum = "wendu")
    private String A7_2;

    /**
     * 湿度   一位小数位     %RH
     */
    @FunctionMapper( funNum = "shidu")
    private String A7_3;

    /**
     * 一氧化碳  一位小数位   ppm
     */
    @FunctionMapper( funNum = "co")
    private String A7_4;

    /**
     * 大气压   两位小数位   mbar
     */
    @FunctionMapper( funNum = "daqiya")
    private String A7_5;

    /**
     * 氧气浓度  一位小数位    %
     */
    @FunctionMapper( funNum = "o2")
    private String A7_6;

    /**
     * 烟雾报警  一位小数位
     */
    @FunctionMapper( isUnit = false, funNum = "yanwu",unit = "dB/m")
    private String A7_7;

    /**
     * 甲烷  一位小数位 %
     */
    @FunctionMapper( isUnit = false, funNum = "jiawan",unit = "%")
    private String A7_8;

    /**
     * voc
     */
    @FunctionMapper( isUnit = false, funNum = "voc",unit = "%")
    private String A7_9;

    /**
     * 二氧化硫
     */
    @FunctionMapper( isUnit = false, funNum = "so2",unit = "%")
    private String A7_10;

    /**
     * 一氧化氮
     */
    @FunctionMapper( isUnit = false, funNum = "no",unit = "%")
    private String A7_11;

    /**
     * 备用字段
     */
    @FunctionMapper( isUnit = false, funNum = "no")
    private String A7_12;

    /**
     * 备用字段
     */
    @FunctionMapper( isUnit = false, funNum = "no")
    private String A7_13;

    /**
     * 备用字段
     */
    @FunctionMapper( isUnit = false, funNum = "no")
    private String A7_14;

    /**
     * 备用字段
     */
    @FunctionMapper( isUnit = false, funNum = "no")
    private String A7_15;

    /**
     * 备用字段
     */
    @FunctionMapper( isUnit = false, funNum = "no")
    private String A7_16;

    /**
     * 同上一条
     */
    private String A8;

    /**
     * 校验位，一个字节
     */
    private String A9;

    public TransmissionVo() {
    }

    public TransmissionVo(String data) {
        // 传感器接收数据
        // fe dc
        // 01
        // 83 62 40 6e 6b 6b
        // 00 00 2a 08
        // 03
        // 00 20
        // 00 00 00 00
        // 00 00 00 f3
        // 00 00 01 8f
        // 00 00 00 00
        // 00 01 78 79
        // 00 00 00 d1 00 00 00 00 00 00 00 00
        // 00

        data = data.replaceAll(" ", "");

        int index = 0;
        this.A1 = data.substring(index, index+=4);
        this.A2 = data.substring(index, index+=2);
        this.A3 = data.substring(index, index+=12);
        this.A4 = data.substring(index, index+=8);
        this.A5 = data.substring(index, index+=2);

        this.A6 = data.substring(index, index+=4);
        int bytes = Integer.parseInt(this.A6, Character.FORMAT) * 2;

        this.setA7(data.substring(index, index += bytes), bytes);

        this.A8 = "";

        this.A9 = data.substring(index);

    }


    public void setA7(String a7, int bytes) {
        this.A7 = a7;
        int index = 0;
        if(bytes >= index + 8){
//            this.A7_1 = Integer.parseInt(this.A7.substring(index, index+=8), Character.FORMAT) + " mg/m3";
            this.A7_1 = percentage(this.A7.substring(index, index+=8));
        }
        if(bytes >= index + 8){
//            this.A7_2 = percentage(this.A7.substring(index, index+=8), 1) + " ℃";
            this.A7_2  = percentage(this.A7.substring(index, index+=8));
        }
        if(bytes >= index + 8){
//            this.A7_3 = percentage(this.A7.substring(index, index+=8), 1) + " %RH";
            this.A7_3 = percentage(this.A7.substring(index, index+=8));
        }
        if(bytes >= index + 8){
//            this.A7_4 = percentage(this.A7.substring(index, index+=8), 1) + " ppm";
            this.A7_4  = percentage(this.A7.substring(index, index+=8));
        }
        if(bytes >= index + 8){
//            this.A7_5 = percentage(this.A7.substring(index, index+=8), 2) + " mbar";
            this.A7_5  = percentage(this.A7.substring(index, index+=8));
        }
        if(bytes >= index + 8){
//            this.A7_6 = percentage(this.A7.substring(index, index+=8), 1) + " %";
            this.A7_6 = percentage(this.A7.substring(index, index+=8));
        }
        if(bytes >= index + 8){
//            this.A7_7 = percentage(this.A7.substring(index, index+=8), 1) + "";
            this.A7_7 = percentage(this.A7.substring(index, index+=8));
        }
        if(bytes >= index + 8){
//            this.A7_8 = percentage(this.A7.substring(index, index+=8), 1) + "";
            this.A7_8 = percentage(this.A7.substring(index, index+=8));
        }
        if(bytes >= index + 8){
//            this.A7_9 = percentage(this.A7.substring(index, index+=8), 1) + "";
            this.A7_9 = percentage(this.A7.substring(index, index+=8));
        }
        if(bytes >= index + 8){
//            this.A7_10 = percentage(this.A7.substring(index, index+=8), 1) + "";
            this.A7_10 = percentage(this.A7.substring(index, index+=8));
        }
        if(bytes >= index + 8){
            this.A7_11 = percentage(this.A7.substring(index, index+=8));
        }
        if(bytes >= index + 8){
            this.A7_12 = percentage(this.A7.substring(index, index+=8));
        }
        if(bytes >= index + 8){
            this.A7_13 = percentage(this.A7.substring(index, index+=8));
        }
        if(bytes >= index + 8){
            this.A7_14 = percentage(this.A7.substring(index, index+=8));
        }
        if(bytes >= index + 8){
            this.A7_15 = percentage(this.A7.substring(index, index+=8));
        }
        if(bytes >= index + 8){
            this.A7_16 = percentage(this.A7.substring(index, index+=8));
        }
    }

    public static void main(String[] args) {
        // 00000000 000000f4 0000018f 00000000 000178ac 000000d1 0000000000000000
        // 0  244  399 0 96428 209 0

        // 00 00 00 00        煤尘   没有小数位    mg/m3
        // 00 00 00 ef  239   温度   一个小数位    ℃
        // 00 00 01 8d  394   湿度   一位         %RH
        // 00 00 00 00        一氧化碳  一位       ppm
        // 00 01 79 7e  99638 大气压   两位小数位   mbar
        // 00 00 00 d1  209   氧气浓度  一位       %
        // 00 00 00 00        烟雾报警    一位        无
        // 00 00 00 00

        // 01 03 02 00 00 03 7a d1 01
        String str = "02fffd";
//        System.out.println(Integer.parseInt(str, Character.FORMAT));

        System.out.println(Integer.parseInt(str, Character.FORMAT));

        // 4路 数据  fe dc 01 83 54 40 6e 6b 6b 00 00 21 6a 03 00 10 00 00 00 b2 00 00 02 76 00 01 8c 9a 00 00 00 00 00
        // 8路 数据  fe dc 01 83 62 40 6e 6b 6b 00 05 84 90 03 00 20 00 00 00 00 00 00 00 d4 00 00 01 a1 00 00 00 00 00 01 7a 15 00 00 00 d1 00 00 00 00 00 00 00 00 00
        // 13路 数据  fe dc 01 83 72 40 6e 6b 6c 00 00 00 04 03 00 34 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
        //TransmissionVo a = new TransmissionVo("fe dc 01 83 54 40 6e 6b 6b 00 00 21 6a 03 00 10 00 00 00 b2 00 00 02 76 00 01 8c 9a 00 00 00 00 00");
    }

    /**
     * 根据检测值智能 小数位计算
     * @param str  需要计算的数据
     */
    private String percentage (String str){
        if(StringUtils.isBlank(str)){
            return "0";
        }

        int scale = 0;
        double per = 0;
        int data = Integer.parseInt(str, Character.FORMAT);
        if(data > 1){
            scale = 1;
            per = 10.0;
        }
        if(data > 60000){
            scale = 2;
            per = 100.0;
        }

        if(scale > 0){
            BigDecimal bg = new BigDecimal(data).divide(BigDecimal.valueOf(per));

            double f1 = bg.setScale(scale, BigDecimal.ROUND_HALF_UP).doubleValue();

            return f1 + "";
        }else {
            return data + "";
        }
    }


    /**
     * 小数位计算
     * @param str  需要计算的数据
     * @param type  1 保留一位小数， 2 保留两位小数 0 没有小数位
     * @return 计算新的值
     */
    private String percentage (String str, int type){
        if(StringUtils.isBlank(str)){
            return "0";
        }
        double per = 0;
        int scale = 0;

        if(type == 1){
            per = 10.0;
            scale = 1;
        }else if(type == 2){
            per = 100.0;
            scale = 2;
        }
        int data = Integer.parseInt(str, Character.FORMAT);

        if(data == 0){
            return data + "";
        }

        if(scale > 0){
            BigDecimal bg = new BigDecimal(data).divide(BigDecimal.valueOf(per));

            double f1 = bg.setScale(scale, BigDecimal.ROUND_HALF_UP).doubleValue();

            return f1 + "";
        }else {
            return data + "";
        }
    }

    public String getA1() {
        return A1;
    }

    public void setA1(String a1) {
        A1 = a1;
    }

    public String getA2() {
        return A2;
    }

    public void setA2(String a2) {
        A2 = a2;
    }

    public String getA3() {
        return A3;
    }

    public void setA3(String a3) {
        A3 = a3;
    }

    public String getA4() {
        return A4;
    }

    public void setA4(String a4) {
        A4 = a4;
    }

    public String getA5() {
        return A5;
    }

    public void setA5(String a5) {
        A5 = a5;
    }

    public String getA6() {
        return A6;
    }

    public void setA6(String a6) {
        A6 = a6;
    }

    public String getA7() {
        return A7;
    }

    public String getA7_1() {
        return A7_1;
    }

    public void setA7_1(String a7_1) {
        A7_1 = a7_1;
    }

    public String getA7_2() {
        return A7_2;
    }

    public void setA7_2(String a7_2) {
        A7_2 = a7_2;
    }

    public String getA7_3() {
        return A7_3;
    }

    public void setA7_3(String a7_3) {
        A7_3 = a7_3;
    }

    public String getA7_4() {
        return A7_4;
    }

    public void setA7_4(String a7_4) {
        A7_4 = a7_4;
    }

    public String getA7_5() {
        return A7_5;
    }

    public void setA7_5(String a7_5) {
        A7_5 = a7_5;
    }

    public String getA7_6() {
        return A7_6;
    }

    public void setA7_6(String a7_6) {
        A7_6 = a7_6;
    }

    public String getA7_7() {
        return A7_7;
    }

    public void setA7_7(String a7_7) {
        A7_7 = a7_7;
    }

    public String getA7_8() {
        return A7_8;
    }

    public void setA7_8(String a7_8) {
        A7_8 = a7_8;
    }

    public String getA7_9() {
        return A7_9;
    }

    public void setA7_9(String a7_9) {
        A7_9 = a7_9;
    }

    public String getA7_10() {
        return A7_10;
    }

    public void setA7_10(String a7_10) {
        A7_10 = a7_10;
    }

    public String getA8() {
        return A8;
    }

    public void setA8(String a8) {
        A8 = a8;
    }

    public String getA9() {
        return A9;
    }

    public void setA9(String a9) {
        A9 = a9;
    }

    public String getA7_11() {
        return A7_11;
    }

    public void setA7_11(String a7_11) {
        A7_11 = a7_11;
    }

    public String getA7_12() {
        return A7_12;
    }

    public void setA7_12(String a7_12) {
        A7_12 = a7_12;
    }

    public String getA7_13() {
        return A7_13;
    }

    public void setA7_13(String a7_13) {
        A7_13 = a7_13;
    }

    public String getA7_14() {
        return A7_14;
    }

    public void setA7_14(String a7_14) {
        A7_14 = a7_14;
    }

    public String getA7_15() {
        return A7_15;
    }

    public void setA7_15(String a7_15) {
        A7_15 = a7_15;
    }

    public String getA7_16() {
        return A7_16;
    }

    public void setA7_16(String a7_16) {
        A7_16 = a7_16;
    }

    @Override
    public String toString() {
        return JSONUtil.toJsonStr(this);
    }
}
