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 org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

/**
 * @author liubo
 * 继电器联动指令（6条）（功能码：0x05）返回
 * 立即操作
 */
@Service
public class LinkageResult implements BaseRouter {
    private static Logger log = LoggerFactory.getLogger(LinkageResult.class);

    public LinkageResult() {
        routerMap.put("05", this);
    }

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

        //指令1字节
        String command = tracketPacket.body.substring(0, 2);
        if ("00".equals(command) || "01".equals(command)) {// 联动配置指令返回
//            联动条序号，1字节，（0-5，6代表保存的所有联动关系）
            String position = tracketPacket.body.substring(4, 6);
            //配置/清空联动反馈的标志，1字节，（0-操作成功，1-操作失败）
            String result = tracketPacket.body.substring(6, 8);
            log.info(tracketPacket.deviceNumber + " 联动配置指令结果：" + result);
        } else if ("02".equals(command)) {//发送的指令字为0x02（问询），非全部问询时，有下述指令返回
            //联动条序号，1字节，（0-5，6代表保存的所有联动关系）
            String positoin = tracketPacket.body.substring(2, 4);
            if ("06".equals(positoin)) {//全部问询
                for (int i = 0; i < 6; i++) {
                    //继电器位号，2字节
                    String beforeTime = tracketPacket.body.substring(4 + i * 2, 8 + i * 2);
                    log.info(i + "继电器位号:" + beforeTime);
                }
            } else {
                //继电器位号，2字节
                String beforeTime = tracketPacket.body.substring(4, 8);
                log.info("继电器位号:" + beforeTime);
            }
        }

        return null;
    }
}
