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;

/**
 * 0xF4
 * 发送升级配置文件
 */
@SuppressWarnings("all")
public class DataTemplateCF4 extends DataTemplate {
    private byte[] serialNo;
    private byte[] sendPacketTime;
    private String dataInfo;

    @Override
    public byte[] SerialNo() {
        return this.serialNo;
    }

    @Override
    public byte[] SendPacketTime() {
        return this.sendPacketTime;
    }

    @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;
                //byte[] packageIndexBuff= BytesHelp.subBytes(this.dataArea, count, 2);
                //this.packageIndex = BytesHelp.bytes2Int(packageIndexBuff,0,2);
                count += 2;

                JSONObject data = new JSONObject();
                data.put("func", "F4");
                List<JSONObject> items = new LinkedList<>();
                JSONObject item = new JSONObject();
                item.put("name", "配置文件发送");
                item.put("value", "已上传完成");
                items.add(item);
                data.put("data", items);
                this.dataInfo = data.toJSONString();

                if (count != this.dataArea.length) {
                    LogHelper.info("******************* F1 DATA ERROR******************* ");
                }
            }
        } catch (Exception e) {
            LogHelper.error(e);
        }
    }

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

    @Override
    public boolean ClearCommand(){
//        try {
//            int count =0;
//
//            count +=2;
//
//            count += 6;
//            byte[] packageIndexBuff= BytesHelp.subBytes(this.dataArea, count, 2);
//            int packageIndex = BytesHelp.bytes2Int(packageIndexBuff,0,2);
//            count += 2;
//
//            LinkedBlockingQueue<CommandEntity> datagramlist = DataProcessHolder.newInstance().dataProcess.relpaySettingCommandToRtu.get(BytesHelp.byte2HexStr(this.rtuCode));
//            if (null != datagramlist){
//                CommandEntity datagram = datagramlist.peek();
//                if (null != datagram) {
//                    if (datagram.getKey().equals(Integer.toString(packageIndex))) {
//                        return  true;
//                    }
//                }
//            }
//        }catch (Exception e){
//            LogHelper.error(e);
//        }

        return true;
    }
}
