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.dataStream.DataStreamAdapter;
import org.ts.ddcs.dataprocesser.DataProcesser;
import org.ts.ddcs.dataprocesser.DataProcesserInterface;
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;


/**
 * 创建日期: 2017年8月28日<br/>
 * Title:  2014规约报文输出<br/>
 *
 * @author swp
 * @version 1.0
 * Remark：认为有必要的其他信息
 * @mender：（文件的修改者，文件创建者之外的人）
 */

public class DataProcesserSw2014Sink extends DataProcesser implements DataProcesserInterface {
    @Override
    public void packetReadly(byte[] byteBuff, int pos, int length, DataStreamAdapter dataStreamAdapter, DataTemplateInterface template) {
        String logId = LogHelper.createLogId(dataStreamAdapter.getAdapterKey());
        try {
            Date dt = new Date();
            SimpleDateFormat timeFormat = new SimpleDateFormat("yyyyMMddHHmmss");
            String code = BytesUtil.byte2HexStr(template.rtuCode());
            LogHelper.info(logId + " 2014年规约-功能码:" +BytesUtil.byte2HexStr(template.funcCode())+  "-RTU:" + code);
            byte[] dataGram = BytesUtil.subBytes(byteBuff, pos, length);
            String datagramText = BytesUtil.byte2HexStr(dataGram);
            LogHelper.info(logId + " 报文:" +datagramText);
            JSONObject body = new JSONObject();
            body.put(GBConstants.BASE_KEY_DATAGRAM, datagramText);
            body.put(GBConstants.BASE_KEY_RTU, code);
            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);
        } catch (Exception e) {
            LogHelper.error(e);
        }
    }
}
