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 org.ts.ddcs.util.Commands;


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


/**
 * 2014规约报文解析器均匀时段水文信息报
 *
 * @author dylan
 */
public class DataProcesser2014P31 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() + " DataProcesser2014P31");
        try {
            Date dt = new Date();
            SimpleDateFormat timeFormat = new SimpleDateFormat("yyyyMMddHHmmss");
            byte[] rtuCodeBuff = template.rtuCode();
            String rtuCode = BytesUtil.byte2HexStr(rtuCodeBuff);
            LogHelper.info(logId + " " + " 2014年规约-功能码:31" + "-RTU:" + rtuCode);
            dataStreamAdapter.setCode(rtuCode);
            byte[] packetContext = template.datagramContext();
            //拼装回复报文，先回复再解析入库
            byte[] respPacket = BytesUtil.subBytes(packetContext, 0, 2);  //帧起始符
            respPacket = BytesUtil.arrayApend(respPacket, rtuCodeBuff);//遥测站地址
            respPacket = BytesUtil.arrayApend(respPacket, BytesUtil.subBytes(packetContext, 2, 1));//中心站地址
            respPacket = BytesUtil.arrayApend(respPacket, BytesUtil.subBytes(packetContext, 8, 2));//密码
            respPacket = BytesUtil.arrayApend(respPacket, BytesUtil.subBytes(packetContext, 10, 1));//功能码
            respPacket = BytesUtil.arrayApend(respPacket, BytesUtil.hexStr2Bytes("8008")); //报文下行标识及长度
            respPacket = BytesUtil.arrayApend(respPacket, BytesUtil.subBytes(packetContext, 13, 1));//报文起始符
            respPacket = BytesUtil.arrayApend(respPacket, BytesUtil.subBytes(packetContext, 14, 2));//流水号
            respPacket = BytesUtil.arrayApend(respPacket, BytesUtil.getFBTm());//发报时间
            respPacket = BytesUtil.arrayApend(respPacket, Commands.PACKET_CONTROL_FLAG_ESC);//RTU2014年报文正文结束符
            byte[] crc16 = BytesUtil.crcFun2014(respPacket);
            respPacket = BytesUtil.arrayApend(respPacket, crc16);//CRC16
            dataStreamAdapter.output(respPacket);
            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);
        }
    }
}
