package org.ts.ddcs.DataTemplate;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.ts.ddcs.Log.LogHelper;
import org.ts.ddcs.Tool.BytesHelp;
import org.ts.ddcs.Tool.StringUtil;
import org.ts.ddcs.dict.BaseSettingFlag;
import org.ts.ddcs.dict.RunSettingFlag;

import java.math.BigDecimal;

/**
 * 中心站读取遥测站运行参数配置表
 */
@SuppressWarnings("all")
public class DataTemplateF43 extends DataTemplate {
    private String dataInfo;
    private byte[] serialNo;
    private byte[] sendPacketTime;
    private byte[] addrFlag;
    private byte[] dataAreaRtuCode;

    @Override
    public void Stream() {
        try {
            if (dataLen > 0) {
                int count = 0;
                this.serialNo = BytesHelp.subBytes(this.dataArea, count, 2); //流水号
                count += 2;
                this.sendPacketTime = BytesHelp.subBytes(this.dataArea, count, 6); // 发报时间
                count += 6;
                this.addrFlag = BytesHelp.subBytes(this.dataArea, count, 2); //地址标识符
                count += 2;
                this.dataAreaRtuCode = BytesHelp.subBytes(this.dataArea, count, 5); //遥测站地址
                count += 5;
                JSONObject jsonData = new JSONObject();
                jsonData.put("func", "43");
                JSONArray datas = new JSONArray();
                while (true) {
                    if (count >= dataLen) {
                        break;
                    }
                    byte[] f = BytesHelp.subBytes(this.dataArea, count, 1);
                    count += 1;
                    String flagCode = BytesHelp.byte2HexStr(f);
                    byte[] len = BytesHelp.subBytes(this.dataArea, count, 1);
                    int dataTotalLen = len[0] >> 3;
                    int decimalLen = len[0] & 7;
                    count += 1;
                    byte[] value = BytesHelp.subBytes(this.dataArea, count, dataTotalLen);
                    count += dataTotalLen;
                    if (flagCode.equals(RunSettingFlag.intervalTime.getId())) {//定时报时间间隔
                        JSONObject data = new JSONObject();
                        data.put("value", BytesHelp.byte2HexStr(value));
                        data.put("key","intervalTime");
                        data.put("code", RunSettingFlag.intervalTime.getId());
                        datas.add(data);
                    } else if (flagCode.equals(RunSettingFlag.addIntervalTime.getId())) {//加报时间间隔
                        JSONObject data = new JSONObject();
                        data.put("value", StringUtil.getPrettyNumber(BytesHelp.byte2HexStr(value)));
                        data.put("key","addIntervalTime");
                        data.put("code", RunSettingFlag.addIntervalTime.getId());
                        datas.add(data);
                    } else if (flagCode.equals(RunSettingFlag.rainTime.getId())) {//降水量日起始时间
                        JSONObject data = new JSONObject();
                        data.put("value", StringUtil.getPrettyNumber(BytesHelp.byte2HexStr(value)));
                        data.put("key","rainTime");
                        data.put("code", RunSettingFlag.rainTime.getId());
                        datas.add(data);
                    } else if (flagCode.equals(RunSettingFlag.collectInterval.getId())) {//采样间隔
                        JSONObject data = new JSONObject();
                        data.put("value", StringUtil.getPrettyNumber(BytesHelp.byte2HexStr(value)));
                        data.put("key","collectInterval");
                        data.put("code", RunSettingFlag.collectInterval.getId());
                        datas.add(data);
                    } else if (flagCode.equals(RunSettingFlag.dataSaveInterval.getId())) {//水位数据存储间隔
                        JSONObject data = new JSONObject();
                        data.put("value", StringUtil.getPrettyNumber(BytesHelp.byte2HexStr(value)));
                        data.put("key","dataSaveInterval");
                        data.put("code", RunSettingFlag.dataSaveInterval.getId());
                        datas.add(data);
                    } else if (flagCode.equals(RunSettingFlag.rainAddValue.getId())) {//雨量加报阈值
                        JSONObject data = new JSONObject();
                        data.put("value", StringUtil.getPrettyNumber(BytesHelp.byte2HexStr(value)));
                        data.put("key","rainAddValue");
                        data.put("code", RunSettingFlag.rainAddValue.getId());
                        datas.add(data);
                    } else if (flagCode.equals(RunSettingFlag.waterLevelBase.getId())) {//水位基值1
                        JSONObject data = new JSONObject();
                        BigDecimal dec = BytesHelp.toDecimal(BytesHelp.byte2HexStr(value), decimalLen);
                        //BigDecimal newValue = dec.setScale(decimalLen, BigDecimal.ROUND_DOWN);
                        data.put("key","waterLevelBase");
                        data.put("value", dec.toPlainString());
                        data.put("code", RunSettingFlag.waterLevelBase.getId());
                        datas.add(data);
                    } else if (flagCode.equals(RunSettingFlag.waterLevelCorrectValue.getId())) {//水位修正基值1
                        JSONObject data = new JSONObject();
                        BigDecimal dec = BytesHelp.toDecimal(BytesHelp.byte2HexStr(value), decimalLen);
                        data.put("key","waterLevelCorrectValue");
                        data.put("value", dec.toPlainString());
                        data.put("code", RunSettingFlag.waterLevelCorrectValue.getId());
                        datas.add(data);
                    } else if (flagCode.equals(RunSettingFlag.addWaterLevel.getId())) {//加报水位1
                        JSONObject data = new JSONObject();
                        BigDecimal dec = BytesHelp.toDecimal(BytesHelp.byte2HexStr(value), decimalLen);
                        data.put("key","addWaterLevel");
                        data.put("value", dec.toPlainString());
                        data.put("code", RunSettingFlag.addWaterLevel.getId());
                        datas.add(data);
                    } else if (flagCode.equals(RunSettingFlag.addWaterUpValue.getId())) {//加报水位以上加报阈值
                        JSONObject data = new JSONObject();
                        BigDecimal dec = BytesHelp.toDecimal(BytesHelp.byte2HexStr(value), decimalLen);
                        data.put("key","addWaterUpValue");
                        data.put("value", dec.toPlainString());
                        data.put("code", RunSettingFlag.addWaterUpValue.getId());
                        datas.add(data);
                    } else if (flagCode.equals(RunSettingFlag.addWaterDownValue.getId())) {//加报水位以下加报阈值
                        JSONObject data = new JSONObject();
                        BigDecimal dec = BytesHelp.toDecimal(BytesHelp.byte2HexStr(value), decimalLen);
                        data.put("key","addWaterDownValue");
                        data.put("value", dec.toPlainString());
                        data.put("code", RunSettingFlag.addWaterDownValue.getId());
                        datas.add(data);
                    } else if (flagCode.equals(RunSettingFlag.flowValue.getId())) {//流量加报阈值
                        JSONObject data = new JSONObject();
                        BigDecimal dec = BytesHelp.toDecimal(BytesHelp.byte2HexStr(value), decimalLen);
                        data.put("key","flowValue");
                        data.put("value", dec.toPlainString());
                        data.put("code", RunSettingFlag.flowValue.getId());
                        datas.add(data);
                    } else if (flagCode.equals(RunSettingFlag.flowSpeetValue.getId())) {//流速加报阈值
                        JSONObject data = new JSONObject();
                        BigDecimal dec = BytesHelp.toDecimal(BytesHelp.byte2HexStr(value), decimalLen);
                        data.put("key","flowSpeetValue");
                        data.put("value", dec.toPlainString());
                        data.put("code", RunSettingFlag.flowSpeetValue.getId());
                        datas.add(data);
                    } else if (flagCode.equals(RunSettingFlag.waterGateValue.getId())) {//闸位加报阀值
                        JSONObject data = new JSONObject();
                        BigDecimal dec = BytesHelp.toDecimal(BytesHelp.byte2HexStr(value), decimalLen);
                        data.put("key","waterGateValue");
                        data.put("value", dec.toPlainString());
                        data.put("code", RunSettingFlag.waterGateValue.getId());
                        datas.add(data);
                    }
                }
                jsonData.put("data", datas);
                this.dataInfo = jsonData.toJSONString();
            }
        } catch (Exception e) {
            LogHelper.error(e);
        }
    }

    @Override
    public String getDataInfo() {
        return dataInfo;
    }
}
