package  org.ts.ddcs.dataprocesser.szyDatagram;

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;


/**
 * 2012规约报文解析器
 * @author dylan
 */
public class DataProcesser2012 extends DataProcesser implements DataProcesserInterface {

    @Override
    public void packetReadly(byte[] byteBuff, int pos, int length, DataStreamAdapter dataStreamAdapter, DataTemplateInterface template) {
        String logId = LogHelper.createLogId(dataStreamAdapter.getAdapterKey() + " DataProcesser2014P2F");
        try {
            Date dt = new Date();
            SimpleDateFormat timeFormat = new SimpleDateFormat("yyyyMMddHHmmss");
            byte[] afnBuff = template.funcCode();
            String afn = BytesUtil.byte2HexStr(afnBuff);
            String rtuCode = BytesUtil.byte2HexStr(template.rtuCode());
            LogHelper.info(logId + " 2012年水资源规约 AFN:" + afn + " RTU:" + rtuCode);
            dataStreamAdapter.setCode(rtuCode);
            DataChannelHolder.newInstance().dataChannel.online(dataStreamAdapter);
            byte[] ca = new byte[1];
            ca[0] = byteBuff[3];
            ca[0] = (byte) (ca[0] & 0x7F);
            if ((byte) 0xC0 == afnBuff[0]) {
                LogHelper.info(logId + " " + rtuCode + " 自报报文");
                String content = BytesUtil.byte2HexStr(ca) + rtuCode + "C0";
                content += "00";
                byte[] contenbuff = BytesUtil.hexStr2Bytes(content);
                byte[] crc = new byte[1]; //系统计算的校验码
                crc[0] = BytesUtil.GetCRCByte(contenbuff);
                int datagramLen = 1 + 1 + 1 + contenbuff.length + 1 + 1;
                byte[] datagram = new byte[datagramLen];
                int count = 0;
                datagram[count] = 0x68;
                count += 1;
                datagram[count] = (byte) contenbuff.length;
                count += 1;
                datagram[count] = 0x68;
                count += 1;
                System.arraycopy(contenbuff, 0, datagram, count, contenbuff.length);
                count += contenbuff.length;
                datagram[count] = crc[0];
                count += 1;
                datagram[count] = 0x16;
                String datagramStr = BytesUtil.byte2HexStr(datagram);
                LogHelper.info(logId + " " + rtuCode + " 回复 " + datagramStr);
                dataStreamAdapter.output(datagram);
            } else if ((byte) 0x02 == afnBuff[0]) {
                LogHelper.info(logId + " " + rtuCode + " 链路检测");
            } else if ((byte) 0x10 == afnBuff[0]) {
                LogHelper.info(logId + " " + rtuCode + " 设置终端编号10H ,设置遥测终端,中继站地址");
            } else if ((byte) 0x11 == afnBuff[0]) {
                LogHelper.info(logId + " " + rtuCode + " 接收设置遥测终端,中继站时钟 开始***");
            } else if ((byte) 0x12 == afnBuff[0]) {
                LogHelper.info(logId + " " + rtuCode + " 设置终端工作模式12H");
            } else if ((byte) 0xA1 == afnBuff[0]) {
                LogHelper.info(logId + " " + rtuCode + " 接收设置终端自报种类及时间间隔 开始***");
            } else if ((byte) 0x50 == afnBuff[0]) {
                LogHelper.info(logId + " " + rtuCode + " 查询终端编号50H");
            } else if ((byte) 0x51 == afnBuff[0]) {
                LogHelper.info(rtuCode + " 查询终端时钟 51H");
            } else if ((byte) 0x52 == afnBuff[0]) {
                LogHelper.info(logId + " " + rtuCode + " 查询终端工作模式52H");
            } else if ((byte) 0x53 == afnBuff[0]) {
                LogHelper.info(logId + " " + rtuCode + " 查询终端自报种类及时间间隔53H");
            } else if ((byte) 0x81 == afnBuff[0]) {
                LogHelper.info(logId + " " + rtuCode + " 随机自报报警数据");
                String content = BytesUtil.byte2HexStr(ca) + rtuCode + "81";
                content += "00";
                byte[] contenbuff = BytesUtil.hexStr2Bytes(content);
                byte[] crc = new byte[1]; //系统计算的校验码
                crc[0] = BytesUtil.GetCRCByte(contenbuff);
                int datagramLen = 1 + 1 + 1 + contenbuff.length + 1 + 1;
                byte[] datagram = new byte[datagramLen];
                int count = 0;
                datagram[count] = 0x68;
                count += 1;
                datagram[count] = (byte) contenbuff.length;
                count += 1;
                datagram[count] = 0x68;
                count += 1;
                System.arraycopy(contenbuff, 0, datagram, count, contenbuff.length);
                count += contenbuff.length;
                datagram[count] = crc[0];
                count += 1;
                datagram[count] = 0x16;
                String datagramStr = BytesUtil.byte2HexStr(datagram);
                LogHelper.info(logId + " " + rtuCode + " 回复 " + datagramStr);
                dataStreamAdapter.output(datagram);
            } else if ((byte) 0x82 == afnBuff[0]) {
                LogHelper.info(logId + " " + rtuCode + " 人工置数82H");
            } else if ((byte) 0x90 == afnBuff[0]) {
                LogHelper.info(logId + " " + rtuCode + " 复位终端参数及状态90H");
            } else if ((byte) 0x91 == afnBuff[0]) {
                LogHelper.info(logId + " " + rtuCode + " 清空终端历史数据单元91H");
            } else if ((byte) 0x87 == afnBuff[0]) {
                LogHelper.info(logId + " " + rtuCode + " 接收设置终端发报IP 开始***");
            } else if ((byte) 0xB0 == afnBuff[0]) {
                LogHelper.info(logId + " " + rtuCode + " 升级 开始***");
            } else if ((byte) 0xF5 == afnBuff[0]) {

            }

            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()));
            //DataProcessHolder.newInstance().dataProcess.submitRelayDatagramTask(body);
            //LogHelper.info(logId + " " + "success pick datagram" + body.toJSONString());
        } catch (Exception e) {
            e.printStackTrace();
            LogHelper.error(e);
        }
    }
}
