package org.ts.ddcs.DataTemplate;

import com.alibaba.fastjson.JSONObject;
import org.ts.ddcs.Log.LogHelper;
import org.ts.ddcs.Tool.BytesHelp;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

/**
 * 查询时钟
 */
@SuppressWarnings("all")
public class DataTemplateF51 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;

//                byte[] flag = BytesHelp.subBytes(this.dataArea, count, 2);
//                count += 2;
//
//                byte[] timeBuff = BytesHelp.subBytes(this.dataArea, count, 2);
//                count += 6;
                SimpleDateFormat timeFormat = new SimpleDateFormat("yyyyMMddHHmmss");
                SimpleDateFormat timeFormat2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

                JSONObject data = new JSONObject();
                data.put("func", "51");
                List<JSONObject> items = new LinkedList<>();
                JSONObject item = new JSONObject();
                item.put("name", "查询时钟");
                item.put("value",timeFormat2.format(timeFormat.parse(timeFormat.format(new Date()).substring(0,2)+ BytesHelp.byte2HexStr(this.sendPacketTime))));
                items.add(item);
                data.put("data", items);
                this.dataInfo = data.toJSONString();
            }
        } catch (Exception e) {
            LogHelper.error(e);
        }
    }

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