package  org.ts.ddcs.dataprocesser.jtDatagram;

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.util.BytesUtil;

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


/**
 * 0x30
 * 测试报
 */

public class DataProcessorJtMultimediaInfo extends DataProcesser implements DataProcesserInterface {

    @Override
    public void packetReadly(byte[] byteBuff, int pos, int length, DataStreamAdapter dataStreamAdapter, DataTemplateInterface template) {
        String logId = LogHelper.createLogId(dataStreamAdapter.getAdapterKey() + " "+this.getClass().getSimpleName());
        try {
            Date dt = new Date();
            SimpleDateFormat timeFormat = new SimpleDateFormat("yyyyMMddHHmmss");
            byte[] packetContext = template.datagramContext();
            byte[] rtuCodeBuff = template.rtuCode();
            String rtuCode = BytesUtil.byte2HexStr(rtuCodeBuff);
            LogHelper.info(logId + " " + " JT 808-功能码:0800 多媒体信息上报" + "-RTU:" + rtuCode);
            dataStreamAdapter.setCode(rtuCode);
            //拼装回复报文，先回复再解析入库
            byte[] funCode = new byte[2];
            funCode[0] = (byte) 0x80;
            funCode[1] = (byte) 0x01;
            byte[] respPacket = funCode;
            int len = 5;
            //消息属性
            byte[] lenBuf = BytesUtil.int2Bytes(len);
            String bin = BytesUtil.bytesToBinStr(lenBuf);
            String parameter = "010000" + bin.substring(bin.length() - 10, bin.length());
            lenBuf = BytesUtil.binStrToBytes(parameter);
            respPacket = BytesUtil.arrayApend(respPacket, lenBuf);
            //版本
            byte[] ver = new byte[1];
            ver[0] = (byte) 0x01;
            respPacket = BytesUtil.arrayApend(respPacket, ver);
            //手机号
            respPacket = BytesUtil.arrayApend(respPacket, template.rtuCode());
            //流水号
            byte[] index = new byte[2];
            index[0] = (byte) 0x00;
            index[1] = (byte) 0x01;
            respPacket = BytesUtil.arrayApend(respPacket, index);
            //消息体流水
            respPacket = BytesUtil.arrayApend(respPacket, BytesUtil.subBytes(packetContext, 16, 2));
            //鉴权消息ID
            byte[] command=new byte[2];
            command[0] = (byte) 0x08;
            command[1] = (byte) 0x00;
            respPacket = BytesUtil.arrayApend(respPacket, command);
            //消息体状态
            byte[] status = new byte[1];
            status[0] = (byte) 0x00;
            respPacket = BytesUtil.arrayApend(respPacket, status);
            int crc = respPacket[0];
            for (int i = 1; i < respPacket.length; i++) {
                crc = crc ^ respPacket[i];
            }
            byte[] crcBuff = new byte[1];
            crcBuff[0] = (byte) crc;
            respPacket = BytesUtil.arrayApend(respPacket, crcBuff);
            byte[] respBuff = BytesUtil.subBytes(packetContext, 0, 1);
            respBuff = BytesUtil.arrayApend(respBuff, respPacket);
            respBuff = BytesUtil.arrayApend(respBuff, BytesUtil.subBytes(packetContext, 0, 1));
            dataStreamAdapter.output(respBuff);
        } catch (Exception e) {
            LogHelper.error(e);
        }
    }
}
