package org.ts.ddcs.DataTemplate;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.ts.ddcs.Entity.CommandEntity;
import org.ts.ddcs.log.LogHelper;
import org.ts.ddcs.Tool.BytesHelp;
import org.ts.ddcs.Tool.StringUtil;
import org.ts.ddcs.Worker.DataProcessHolder;
import org.ts.ddcs.dict.BaseSettingFlag;
import org.ts.ddcs.dict.RunSettingFlag;
import org.springframework.web.client.RestTemplate;

import java.math.BigDecimal;
import java.net.URI;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.LinkedBlockingQueue;

/**
 * 水资源 查询自报种类及时间间隔
 */
@SuppressWarnings("all")
public class DataTemplateSzyC53 extends DataTemplateSzy2012 {

    private String dataInfo;

    private static final String AFN = "53";

    @Override
    public void Stream() {
        try {
            if (null != this.dataArea && this.dataArea.length > 0) {
                int count = 0;
                JSONObject jsonData = new JSONObject();
                jsonData.put("func", "53");
                JSONArray datas = new JSONArray();
                String binStr = BytesHelp.BytesToBinStr2(BytesHelp.subBytes(this.dataArea, 0, 2));
                count += 2;
                binStr = new StringBuilder(binStr).reverse().toString();
                for (int i = 0; i < 14; i++) {
                    JSONObject data = new JSONObject();
                    String key = "szy_intervalTime_setting_" + String.format("%02d", i);
                    data.put("key", key);
                    if (binStr.substring(i, i + 1).equals("1")) {
                        byte[] valueBuff = BytesHelp.subBytes(this.dataArea,count,2);
                        byte tmpByte=valueBuff[0];
                        valueBuff[0] = valueBuff[1];
                        valueBuff[1] = tmpByte;
                        String value = BytesHelp.byte2HexStr(valueBuff);
                        data.put(key, "" + Integer.parseInt(value));

                    } else {
                        data.put(key, "0");
                    }
                    datas.add(data);
                    count += 2;
                }
                jsonData.put("data", datas);
                this.dataInfo = jsonData.toJSONString();
                if (count != this.dataArea.length) {
                    LogHelper.info("TEMPLATE DATA ERROR : " + rtuCode + " : 53");
                }
            }
        } catch (Exception e) {
            LogHelper.error(e);
        }
    }

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

    public static String createDatagram(String rtuCode) {
        try {
            if (rtuCode != null && rtuCode.length() == 10) {
                String content = "30" + rtuCode + DataTemplateSzyC53.AFN;
                byte[] buff = BytesHelp.hexStr2Bytes(content);
                byte[] crc = new byte[1];
                crc[0] = BytesHelp.GetCRCByte(buff);
                byte[] lenBuff = new byte[1];
                lenBuff[0] = (byte) (content.length() / 2);
                String datagram = "68" + BytesHelp.byte2HexStr(lenBuff) + "68" + content + BytesHelp.byte2HexStr(crc) + "16";
                return datagram;
            }
        } catch (Exception e) {
            LogHelper.error(e);
        }
        return null;
    }
}
