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.dict.BaseSettingFlag;
import org.omg.CORBA.ByteHolder;


/**
 * 中心站读取遥测站基本配置表
 * 0x41
 */
@SuppressWarnings("all")
public class DataTemplateF41 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", "41");
                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;

                    if (flagCode.equals(BaseSettingFlag.CENTER_ADD.getId())) {
                        byte[] value = BytesHelp.subBytes(this.dataArea, count, dataTotalLen);
                        count += dataTotalLen;
                    } else if (flagCode.equals(BaseSettingFlag.RTU_CODE.getId())) {
                        byte[] value = BytesHelp.subBytes(this.dataArea, count, dataTotalLen);
                        count += dataTotalLen;
                        JSONObject data = new JSONObject();
                        data.put("rtucode", BytesHelp.byte2HexStr(value));
                        data.put("code", BaseSettingFlag.RTU_CODE.getId());
                        datas.add(data);
                    } else if (flagCode.equals(BaseSettingFlag.PASSWORD.getId())) {
                        byte[] value = BytesHelp.subBytes(this.dataArea, count, dataTotalLen);
                        count += dataTotalLen;

                    } else if (flagCode.equals(BaseSettingFlag.IP1.getId())) {
                        byte[] value = BytesHelp.subBytes(this.dataArea, count, dataTotalLen);
                        count += dataTotalLen;
                        JSONObject data = new JSONObject();
                        if (value[0] != 0x00) {
                            data.put("ip1", BytesHelp.toIp(BytesHelp.subBytes(value, 1, 6)));
                            data.put("ip1port", String.format("%d", Integer.parseInt(BytesHelp.byte2HexStr(BytesHelp.subBytes(value, 7, 3)))));
                            data.put("code", BaseSettingFlag.IP1.getId());
                            datas.add(data);
                        }
                    } else if (flagCode.equals(BaseSettingFlag.IP1_SUB.getId())) {
                        byte[] value = BytesHelp.subBytes(this.dataArea, count, dataTotalLen);
                        count += dataTotalLen;

                    } else if (flagCode.equals(BaseSettingFlag.IP2.getId())) {
                        byte[] value = BytesHelp.subBytes(this.dataArea, count, dataTotalLen);
                        count += dataTotalLen;
                        JSONObject data = new JSONObject();
                        if (value[0] != 0x00) {
                            data.put("ip2", BytesHelp.toIp(BytesHelp.subBytes(value, 1, 6)));
                            data.put("ip2port", String.format("%d", Integer.parseInt(BytesHelp.byte2HexStr(BytesHelp.subBytes(value, 7, 3)))));
                            data.put("code", BaseSettingFlag.IP2.getId());
                            datas.add(data);
                        }
                    } else if (flagCode.equals(BaseSettingFlag.IP2_SUB.getId())) {
                        byte[] value = BytesHelp.subBytes(this.dataArea, count, dataTotalLen);
                        count += dataTotalLen;

                    } else if (flagCode.equals(BaseSettingFlag.IP3.getId())) {
                        byte[] value = BytesHelp.subBytes(this.dataArea, count, dataTotalLen);
                        count += dataTotalLen;
                        JSONObject data = new JSONObject();
                        if (value[0] != 0x00) {
                            data.put("ip3", BytesHelp.toIp(BytesHelp.subBytes(value, 1, 6)));
                            data.put("ip3port", String.format("%d", Integer.parseInt(BytesHelp.byte2HexStr(BytesHelp.subBytes(value, 7, 3)))));
                            data.put("code", BaseSettingFlag.IP3.getId());
                            datas.add(data);
                        }
                    } else if (flagCode.equals(BaseSettingFlag.IP3_SUB.getId())) {
                        byte[] value = BytesHelp.subBytes(this.dataArea, count, dataTotalLen);
                        count += dataTotalLen;

                    } else if (flagCode.equals(BaseSettingFlag.IP4.getId())) {
                        byte[] value = BytesHelp.subBytes(this.dataArea, count, dataTotalLen);
                        count += dataTotalLen;
                        JSONObject data = new JSONObject();
                        if (value[0] != 0x00) {
                            data.put("ip4", BytesHelp.toIp(BytesHelp.subBytes(value, 1, 6)));
                            data.put("ip4port", String.format("%d", Integer.parseInt(BytesHelp.byte2HexStr(BytesHelp.subBytes(value, 7, 3)))));
                            data.put("code", BaseSettingFlag.IP4.getId());
                            datas.add(data);
                        }
                    } else if (flagCode.equals(BaseSettingFlag.IP4_SUB.getId())) {
                        byte[] value = BytesHelp.subBytes(this.dataArea, count, dataTotalLen);
                        count += dataTotalLen;

                    } else if (flagCode.equals("34")) {
                        byte[] value = BytesHelp.subBytes(this.dataArea, count, 39);
                        String ipText = BytesHelp.bytes2String(value, 0, value.length);
                        count += 39;
                        byte[] port = BytesHelp.subBytes(this.dataArea, count, 2);
                        String portText = "" + BytesHelp.bytes2short(port);
                        count += 2;
                        if (null != portText && !portText.equals("0")) {
                            JSONObject data = new JSONObject();
                            data.put("ip1", ipText);
                            data.put("ip1port", portText);
                            data.put("code", "34");
                            datas.add(data);
                        }
                    } else if (flagCode.equals("35")) {
                        byte[] value = BytesHelp.subBytes(this.dataArea, count, 39);
                        String ipText = BytesHelp.bytes2String(value, 0, value.length);
                        count += 39;
                        byte[] port = BytesHelp.subBytes(this.dataArea, count, 2);
                        String portText = "" + BytesHelp.bytes2short(port);
                        count += 2;
                        if (null != portText && !portText.equals("0")) {
                            JSONObject data = new JSONObject();
                            data.put("ip2", ipText);
                            data.put("ip2port", portText);
                            data.put("code", "35");
                            datas.add(data);
                        }
                    } else if (flagCode.equals("36")) {
                        byte[] value = BytesHelp.subBytes(this.dataArea, count, 39);
                        String ipText = BytesHelp.bytes2String(value, 0, value.length);
                        count += 39;
                        byte[] port = BytesHelp.subBytes(this.dataArea, count, 2);
                        String portText = "" + BytesHelp.bytes2short(port);
                        count += 2;
                        if (null != portText && !portText.equals("0")) {
                            JSONObject data = new JSONObject();
                            data.put("ip3", ipText);
                            data.put("ip3port", portText);
                            data.put("code", "36");
                            datas.add(data);
                        }
                    } else if (flagCode.equals("37")) {
                        byte[] value = BytesHelp.subBytes(this.dataArea, count, 39);
                        String ipText = BytesHelp.bytes2String(value, 0, value.length);
                        count += 39;
                        byte[] port = BytesHelp.subBytes(this.dataArea, count, 2);
                        String portText = "" + BytesHelp.bytes2short(port);
                        count += 2;
                        if (null != portText && !portText.equals("0")) {
                            JSONObject data = new JSONObject();
                            data.put("ip4", ipText);
                            data.put("ip4port", portText);
                            data.put("code", "37");
                            datas.add(data);
                        }
                    } else if (flagCode.equals(BaseSettingFlag.WORK_TYPE.getId())) {
                        byte[] value = BytesHelp.subBytes(this.dataArea, count, dataTotalLen);
                        count += dataTotalLen;

                    } else if (flagCode.equals(BaseSettingFlag.COLLECT_SETTING.getId())) {
                        byte[] value = BytesHelp.subBytes(this.dataArea, count, dataTotalLen);
                        count += dataTotalLen;
                        if (value.length == 8) {
                            JSONObject data = new JSONObject();
                            for (int i = 0; i < 8; i++) {
                                String binStr = BytesHelp.BytesToBinStr(BytesHelp.subBytes(value, i, 1));
                                binStr = new StringBuilder(binStr).reverse().toString();
                                for (int j = 0; j < 8; j++) {
                                    String key = "G_" + String.format("%02d", i) + "_V_" + String.format("%02d", j);
                                    data.put(key, binStr.substring(j, j + 1));
                                }
                            }
                            data.put("code", BaseSettingFlag.COLLECT_SETTING.getId());
                            datas.add(data);
                        }
                    } else if (flagCode.equals(BaseSettingFlag.SERVER_ADD.getId())) {
                        byte[] value = BytesHelp.subBytes(this.dataArea, count, dataTotalLen);
                        count += dataTotalLen;

                    } else if (flagCode.equals(BaseSettingFlag.DEVICE_ID.getId())) {
                        byte[] value = BytesHelp.subBytes(this.dataArea, count, dataTotalLen);
                        count += dataTotalLen;

                    } else if (flagCode.equals(BaseSettingFlag.WORK_MODEL.getId())) {
                        byte[] value = BytesHelp.subBytes(this.dataArea, count, dataTotalLen);
                        count += dataTotalLen;
                        JSONObject data = new JSONObject();
                        if (value[0] == 0x00) {
                            data.put("workModel", "0");
                        } else {
                            data.put("workModel", "1");
                        }
                        data.put("code", BaseSettingFlag.WORK_MODEL.getId());
                        datas.add(data);
                    } else if (flagCode.equals(BaseSettingFlag.RTU_KIND.getId())) {
                        byte[] value = BytesHelp.subBytes(this.dataArea, count, dataTotalLen);
                        count += dataTotalLen;
                        JSONObject data = new JSONObject();
                        data.put("code", BaseSettingFlag.RTU_KIND.getId());
                        if (value[0] == 0x50) {
                            data.put("rtuKind", "P");
                        } else if (value[0] == 0x48) {
                            data.put("rtuKind", "H");
                        } else if (value[0] == 0x4B) {
                            data.put("rtuKind", "K");
                        } else if (value[0] == 0x5A) {
                            data.put("rtuKind", "Z");
                        } else if (value[0] == 0x44) {
                            data.put("rtuKind", "D");
                        } else if (value[0] == 0x54) {
                            data.put("rtuKind", "T");
                        } else if (value[0] == 0x4D) {
                            data.put("rtuKind", "M");
                        } else if (value[0] == 0x47) {
                            data.put("rtuKind", "G");
                        } else if (value[0] == 0x51) {
                            data.put("rtuKind", "Q");
                        } else if (value[0] == 0x49) {
                            data.put("rtuKind", "I");
                        } else if (value[0] == 0x4F) {
                            data.put("rtuKind", "O");
                        }
                        datas.add(data);
                    } else if (flagCode.equals(BaseSettingFlag.RAIN_PERCEN.getId())) {
                        byte[] value = BytesHelp.subBytes(this.dataArea, count, dataTotalLen);
                        count += dataTotalLen;
                        JSONObject data = new JSONObject();
                        data.put("code", BaseSettingFlag.RAIN_PERCEN.getId());
                        data.put("rainPercenValue", BytesHelp.byte2HexStr(value).substring(1, 2));
                        datas.add(data);
                    } else if (flagCode.equals(BaseSettingFlag.BLUETOOTH.getId())) {
                        byte[] value = BytesHelp.subBytes(this.dataArea, count, dataTotalLen);
                        count += dataTotalLen;
                        JSONObject data = new JSONObject();
                        data.put("code", BaseSettingFlag.BLUETOOTH.getId());
                        data.put("bluetoothValue", BytesHelp.byte2HexStr(value).substring(1, 2));
                        datas.add(data);
                    } else {
                        // byte[] value = BytesHelp.subBytes(this.dataArea, count, dataTotalLen);
                        count += dataTotalLen;
                    }
                }
                jsonData.put("data", datas);
                this.dataInfo = jsonData.toJSONString();
            }
        } catch (Exception e) {
            LogHelper.error(e);
        }
    }

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