package org.ts.ddcs.DataTemplate;

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

import java.util.LinkedList;
import java.util.List;


/**
 * 0x45 查询软件版本
 */
@SuppressWarnings("all")
public class DataTemplateF45 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;
                addrFlag = BytesHelp.subBytes(dataArea, count, 2); //地址标识符
                count += 2;
                dataAreaRtuCode = BytesHelp.subBytes(dataArea, count, 5); //遥测站地址
                count += 5;
                int len = this.dataArea[count];
                count += 1;
                byte[] versions = BytesHelp.subBytes(this.dataArea, count, len);
                count += len;
                String ver = new String(versions, "utf-8");
                LogHelper.info("当前RTU版本：" + ver);

                JSONObject data = new JSONObject();
                data.put("func", "45");
                List<JSONObject> items = new LinkedList<>();
                JSONObject item = new JSONObject();
                item.put("name", "当前RTU版本");
                item.put("value", ver);
                items.add(item);
                data.put("data", items);
                this.dataInfo = data.toJSONString();
            }
        } catch (Exception e) {
            LogHelper.error(e);
        }
    }

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