package com.zd.laboratory.socket.service.impl;


import com.zd.laboratory.socket.constant.JXCTPacket;
import com.zd.laboratory.socket.exception.AnalysisException;
import com.zd.laboratory.socket.service.BaseRouter;
import com.zd.laboratory.socket.util.SocketUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @author liubo
 * 配置定时指令操作返回
 */
@Service
public class NTP implements BaseRouter {

    private static Logger log = LoggerFactory.getLogger(NTP.class);

    public NTP() {
        routerMap.put("07", this);
    }

    @Override
    public String routePacket(JXCTPacket tracketPacket) throws AnalysisException {

        String receiveTime = SocketUtils.getHexString(System.currentTimeMillis(), 16);
        String deviceTime = tracketPacket.body.substring(2, 18);
        long dviceTimeCurrent = Long.parseLong(deviceTime, 16);
        log.info(tracketPacket.deviceNumber + "  07 NTP " + deviceTime + "  > " + SocketUtils.simpleDateFormat.format(new Date(dviceTimeCurrent)));
        String sendTime = SocketUtils.getHexString(System.currentTimeMillis(), 16);

        tracketPacket.body = "00" + deviceTime + receiveTime + sendTime;

        return tracketPacket.encodeString();
    }
}
