package  org.ts.ddcs.dataprocesser.swDatagram;

import com.alibaba.fastjson.JSONObject;
import org.ts.ddcs.main.DataProcessHolder;
import org.ts.ddcs.common.constant.GBConstants;
import org.ts.ddcs.dataprocesser.DataProcesser;
import org.ts.ddcs.dataprocesser.DataProcesserInterface;
import org.ts.ddcs.dataStream.DataStreamAdapter;
import org.ts.ddcs.datatemplate.DataTemplateInterface;
import org.ts.ddcs.log.LogHelper;
import org.ts.ddcs.net.DataChannelHolder;
import org.ts.ddcs.util.BytesUtil;


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

/**
 * 0xF3
 * RTU重启
 */

public class DataProcesser2014PF3 extends DataProcesser implements DataProcesserInterface {


    @Override
    @SuppressWarnings("all")
    public void packetReadly(byte[] byteBuff, int pos, int length, DataStreamAdapter dataStreamAdapter, DataTemplateInterface template) {
        String logId = LogHelper.createLogId(dataStreamAdapter.getAdapterKey() + " DataProcesser2014PF3");
        try {
            Date dt = new Date();
            SimpleDateFormat timeFormat = new SimpleDateFormat("yyyyMMddHHmmss");
            byte[] rtuCodeBuff = template.rtuCode();
            String rtuCode = BytesUtil.byte2HexStr(rtuCodeBuff);
            LogHelper.info(logId + " " + " 2014年规约-功能码:F3" + "-RTU:" + rtuCode);
            dataStreamAdapter.setCode(rtuCode);
            byte[] dataGram = BytesUtil.subBytes(byteBuff, pos, length);
            String gram = BytesUtil.byte2HexStr(dataGram);

            JSONObject body = new JSONObject();
            body.put(GBConstants.BASE_KEY_DATAGRAM, gram);
            body.put(GBConstants.BASE_KEY_RTU, rtuCode);
            body.put(GBConstants.BASE_KEY_AGREEMENT, template.agreement());
            body.put(GBConstants.BASE_KEY_DATAGRAM_PICK_TM, timeFormat.format(dt));
            body.put("afc",BytesUtil.byte2HexStr(template.funcCode()));
            DataChannelHolder.newInstance().dataChannel.online(dataStreamAdapter);
           /// DataProcessHolder.newInstance().dataProcess.submitRelayDatagramTask(body);
            LogHelper.info(logId + " " + "success pick datagram" + body.toJSONString());
        } catch (Exception e) {
            LogHelper.error(e);
        }
    }
}
