Rbm3000ProtocolFactory.java 4.98 KB
package com.example.mina.client.box.rbm3000;

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 Rbm3000ProtocolFactory 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);

            // Len==8 cmd【0】 0x30 set row/col attn=1
            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((byte)0xFE);
                buffer.put((byte)0xFE);
                buffer.put((byte)0xFE);
            }else if (cmd == MatrixConstants.COMMAND_GET_ATTN) {
                buffer.put((byte)0x68);
                buffer.put((byte)0xFE);
                buffer.put((byte)0xFE);
                if(mc.getRow() != null){
                    // row
                    buffer.put((byte)0x0A);
                    buffer.put(mc.getRow().byteValue());
                } else {
                    // col
                    buffer.put((byte)0x0B);
                    buffer.put(mc.getCol().byteValue());
                }
                buffer.put((byte)0xFE);
                buffer.put((byte)0xFE);
                buffer.put((byte)0xFE);

            } else {
                buffer.put((byte)0x62);
                buffer.put((byte)0xFE);
                buffer.put((byte)0xFE);
                buffer.put((byte)0xFE);
                buffer.put((byte)0xFE);
                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 rowCol = bytes[6];
                int colRow = bytes[5];

                response.setRow(row);
                response.setCol(col);
                response.setRowCol(rowCol);
                response.setColRow(colRow);
                response.setIsSetAttn(true);

            }
            if (cmd == 0x68 && len == 8) {
                boolean isA = (bytes[3] == (byte) 0x0A);
                int tmp = bytes[4];
                if (isA) {
                    // col
                    response.setCol(tmp);
                } else {
                    // row
                    response.setRow(tmp);
                }
            }
            protocolDecoderOutput.write(response);
        }

    }

}