Qrb3000ProtocolFactory.java 4.68 KB
package com.example.mina.client.box.qrb3000;

import com.example.mina.client.base.MatrixCommand;
import com.example.mina.client.base.MatrixConstants;
import com.example.mina.client.base.MatrixResponse;
import com.example.mina.processor.VbLogUtils;
import com.example.mina.server.util.CommandHelper;
import lombok.extern.slf4j.Slf4j;
import org.apache.mina.core.buffer.IoBuffer;
import org.apache.mina.core.session.IoSession;
import org.apache.mina.filter.codec.*;

import java.nio.charset.CharacterCodingException;
import java.nio.charset.CharsetDecoder;
import java.nio.charset.CharsetEncoder;
import java.nio.charset.StandardCharsets;

@Slf4j
public class Qrb3000ProtocolFactory implements ProtocolCodecFactory {

    private static final CharsetDecoder DECODER = StandardCharsets.UTF_8.newDecoder();

    private static final CharsetEncoder ENCODER = StandardCharsets.UTF_8.newEncoder();

    @Override
    public ProtocolEncoder getEncoder(IoSession ioSession) throws Exception {
        return new Qrb3000ProtocolEncoder();
    }

    @Override
    public ProtocolDecoder getDecoder(IoSession ioSession) throws Exception {
        return new Qrb3000ProtocolDecoder();
    }

    public static class Qrb3000ProtocolEncoder extends ProtocolEncoderAdapter {

        @Override
        public void encode(IoSession ioSession, Object msg, ProtocolEncoderOutput protocolEncoderOutput)
                throws CharacterCodingException {

            if (!(msg instanceof MatrixCommand)) {
                log.error("error msg, msg is: {}", msg);
                return;
            }

            MatrixCommand mc = (MatrixCommand) msg;
            log.info("---发送数据参数!Attn = {},Col = {},Command = {},Row = {},MatrixId = {},type = {}",
                    mc.getAttn(),
                    mc.getCol(),
                    mc.getCommand(),
                    mc.getRow(),
                    mc.getMatrixId()
            );
            int cmd = mc.getCommand();
            VbLogUtils.infoLog(log, "", mc.getMatrixId(),msg);

            IoBuffer buffer = IoBuffer.allocate(100, false).setAutoExpand(true);

            if (cmd == MatrixConstants.COMMAND_SET_ATTN) {
                buffer.put((byte)0x30);
                buffer.put((byte)0xFE);
                buffer.put((byte)0xFE);
                buffer.put(mc.getRow().byteValue());
                buffer.put(mc.getCol().byteValue());
                buffer.put(mc.getAttn().byteValue());
                buffer.put((byte)0xFE);
                buffer.put((byte)0xFE);
            }
            if (cmd == MatrixConstants.COMMAND_SET_OFFSET) {
                buffer.put((byte)0x68);
                buffer.put((byte)0xFE);
                buffer.put((byte)0xFE);
                buffer.put(mc.getCol().byteValue());
                buffer.put(mc.getOffset().byteValue());
                buffer.put((byte)0xFE);
                buffer.put((byte)0xFE);
                buffer.put((byte)0xFE);

            }
            if (cmd == MatrixConstants.COMMAND_GET_OFFSET) {
                buffer.put((byte)0xFE);
                buffer.put((byte)0xFE);
                buffer.put((byte)0xFE);
                buffer.put(mc.getCol().byteValue());
                buffer.put((byte)0xFE);
                buffer.put((byte)0xFE);
                buffer.put((byte)0xFE);
            }

            buffer.flip();
            protocolEncoderOutput.write(buffer);
        }
    }

    public static class Qrb3000ProtocolDecoder extends ProtocolDecoderAdapter {

        @Override
        public void decode(IoSession ioSession, IoBuffer ioBuffer, ProtocolDecoderOutput protocolDecoderOutput)
                throws Exception {
            log.info("--server返回给----client ------IoBuffer{}", ioBuffer);
            MatrixResponse response = new MatrixResponse();
            int len = ioBuffer.limit();
            byte[] bytes = new byte[len];
            ioBuffer.get(bytes);
            int cmd = bytes[0];
            if(cmd == 0x30 && len==8) {
                int row = CommandHelper.BCD_REV[bytes[3]];
                int col = CommandHelper.BCD_REV[bytes[4]];
                int attn = bytes[5];

                response.setRow(row);
                response.setCol(col);
                response.setIsSetAttn(true);
                response.setAttn(attn);

            } else if (cmd == 0x68 && len ==8) {
                int offset = bytes[6];
                int col = CommandHelper.BCD_REV[bytes[3]];
                response.setIsSetOffset(true);
                response.setCol(col);
                response.setOffset(offset);
            } else {
                int offset = bytes[6];
                response.setOffset(offset);
            }
            protocolDecoderOutput.write(response);
        }

    }

}