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;

/**
 * @author liubo
 * 问询继电器时间返回
 */
@Service
public class AskTimeResult implements BaseRouter {

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

    public AskTimeResult() {
        routerMap.put("03", this);
    }

    @Override
    public String routePacket(JXCTPacket tracketPacket) throws AnalysisException {
        //  指令操作反馈，1字节，（0-操作成功，1--操作失败）
        String command = tracketPacket.body.substring(0, 2);
        if ("00".equals(command)) {//配置继电器时间返回
            log.info("00 配置继电器时间返回");
            String result = tracketPacket.body.substring(2, 4);
            if ("00".equals(result)) {
                log.info(tracketPacket.deviceNumber + " 配置时间成功");
            } else {
                log.info(tracketPacket.deviceNumber + " 配置时间失败");
            }
        } else if ("02".equals(command)) {
            log.info("02问询继电器时间返回");
            //日期，年，1字节，年份的前两位固定为20，直接取值，如0x14,十进制为20
            String year = tracketPacket.body.substring(2, 4);
            //日期，月，1字节
            String month = tracketPacket.body.substring(4, 6);
            //日期，日，1字节
            String day = tracketPacket.body.substring(6, 8);
            //日期，时，1字节
            String hour = tracketPacket.body.substring(8, 10);
            //日期，分，1字节
            String minute = tracketPacket.body.substring(10, 12);
            //日期，秒，1字节
            String seconds = tracketPacket.body.substring(12, 14);

            log.info(tracketPacket.deviceNumber + " 年：" + SocketUtils.getHex2Int(year) + " 月：" + SocketUtils.getHex2Int(month)
                    + " 日：" + SocketUtils.getHex2Int(day) + " 时：" + SocketUtils.getHex2Int(hour)
                    + " 分:" + SocketUtils.getHex2Int(minute) + " 秒：" + SocketUtils.getHex2Int(seconds));
        } else {
            log.info("其他指令：" + command);
        }

        return null;
    }
}
