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;

/**
 * 中心站设置遥测站IP和端口
 * 0xE7
 */

@SuppressWarnings("all")
public class DataTemplateSzyCE7 extends DataTemplateSzy2012 {

    private String dataInfo;

    @Override
    public  void  Stream(){
        try {
            if(dataLen>0){
                int count =0;

                JSONObject jsonData = new JSONObject();
                jsonData.put("func", "E7");
                JSONArray datas = new JSONArray();
                while (true) {
                    if (count >= this.dataArea.length) {
                        break;
                    }
                    byte[] f = BytesHelp.subBytes(this.dataArea, count, 1);
                    count += 1;
                    String flagCode = BytesHelp.byte2HexStr(f);

                    if (flagCode.equals(BaseSettingFlag.CENTER_ADD.getId())) {

                    } else if (flagCode.equals(BaseSettingFlag.RTU_CODE.getId())) {

                    } else if (flagCode.equals(BaseSettingFlag.PASSWORD.getId())) {

                    } else if (flagCode.equals(BaseSettingFlag.IP1.getId())) {

                        byte[] kind= BytesHelp.subBytes(this.dataArea, count, 1);
                        count +=1;
                        byte[] len = BytesHelp.subBytes(this.dataArea, count, 1);
                        int dataTotalLen = (int)len[0];
                        count += 1;
                        byte[] value = BytesHelp.subBytes(this.dataArea, count, dataTotalLen);
                        count += dataTotalLen;

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

                        JSONObject data = new JSONObject();
                        if (kind[0] != 0x00) {
                            data.put("ip2", BytesHelp.toIp(BytesHelp.subBytes(value, 0, 6)));
                            data.put("ip2port", String.format("%d", Integer.parseInt(BytesHelp.byte2HexStr(BytesHelp.subBytes(value, 6, 3)))));
                            data.put("code", BaseSettingFlag.IP2.getId());
                            datas.add(data);
                        }
                    } else if (flagCode.equals(BaseSettingFlag.IP2_SUB.getId())) {
                        byte[] kind= BytesHelp.subBytes(this.dataArea, count, 1);
                        count +=1;
                        byte[] len = BytesHelp.subBytes(this.dataArea, count, 1);
                        int dataTotalLen = (int)len[0];
                        count += 1;
                        byte[] value = BytesHelp.subBytes(this.dataArea, count, dataTotalLen);
                        count += dataTotalLen;
                    } else if (flagCode.equals(BaseSettingFlag.IP3.getId())) {
                        byte[] kind= BytesHelp.subBytes(this.dataArea, count, 1);
                        count +=1;
                        byte[] len = BytesHelp.subBytes(this.dataArea, count, 1);
                        int dataTotalLen = (int)len[0];
                        count += 1;
                        byte[] value = BytesHelp.subBytes(this.dataArea, count, dataTotalLen);
                        count += dataTotalLen;
                        JSONObject data = new JSONObject();
                        if (kind[0] != 0x00) {
                            data.put("ip3", BytesHelp.toIp(BytesHelp.subBytes(value, 0, 6)));
                            data.put("ip3port", String.format("%d", Integer.parseInt(BytesHelp.byte2HexStr(BytesHelp.subBytes(value, 6, 3)))));
                            data.put("code", BaseSettingFlag.IP3.getId());
                            datas.add(data);
                        }
                    } else if (flagCode.equals(BaseSettingFlag.IP3_SUB.getId())) {
                        byte[] kind= BytesHelp.subBytes(this.dataArea, count, 1);
                        count +=1;
                        byte[] len = BytesHelp.subBytes(this.dataArea, count, 1);
                        int dataTotalLen = (int)len[0];
                        count += 1;
                        byte[] value = BytesHelp.subBytes(this.dataArea, count, dataTotalLen);
                        count += dataTotalLen;
                    } else if (flagCode.equals(BaseSettingFlag.IP4.getId())) {
                        byte[] kind= BytesHelp.subBytes(this.dataArea, count, 1);
                        count +=1;
                        byte[] len = BytesHelp.subBytes(this.dataArea, count, 1);
                        int dataTotalLen = (int)len[0];
                        count += 1;
                        byte[] value = BytesHelp.subBytes(this.dataArea, count, dataTotalLen);
                        count += dataTotalLen;
                        JSONObject data = new JSONObject();
                        if (kind[0] != 0x00) {
                            data.put("ip4", BytesHelp.toIp(BytesHelp.subBytes(value, 0, 6)));
                            data.put("ip4port", String.format("%d", Integer.parseInt(BytesHelp.byte2HexStr(BytesHelp.subBytes(value, 6, 3)))));
                            data.put("code", BaseSettingFlag.IP4.getId());
                            datas.add(data);
                        }
                    } else if (flagCode.equals(BaseSettingFlag.IP4_SUB.getId())) {
                        byte[] kind= BytesHelp.subBytes(this.dataArea, count, 1);
                        count +=1;
                        byte[] len = BytesHelp.subBytes(this.dataArea, count, 1);
                        int dataTotalLen = (int)len[0];
                        count += 1;
                        byte[] value = BytesHelp.subBytes(this.dataArea, count, dataTotalLen);
                        count += dataTotalLen;
                    } else if (flagCode.equals(BaseSettingFlag.WORK_TYPE.getId())) {

                    } else if (flagCode.equals(BaseSettingFlag.COLLECT_SETTING.getId())) {

                    } else if (flagCode.equals(BaseSettingFlag.SERVER_ADD.getId())) {

                    } else if (flagCode.equals(BaseSettingFlag.DEVICE_ID.getId())) {

                    }
                }
                jsonData.put("data", datas);
                this.dataInfo = jsonData.toJSONString();
                if (count != this.dataArea.length){
                    LogHelper.info("******************* E7 DATA ERROR******************* ");
                }
            }
        }catch (Exception e){
            LogHelper.error(e);
        }
    }

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