QrbCommandHelper.java 3.57 KB
package com.example.mina.client.box.qrb3000;

import org.apache.commons.lang3.ArrayUtils;

import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Optional;
import java.util.stream.Stream;

/**
 * @ProjectName: demo
 * @Package: com.example.mina.client.box.qrb3000
 * @ClassName: QrbCommandHelper
 * @Author: HuangLang
 * @Description: QrbCommandHelper
 * @Date: 2021-04-02 下午 6:13
 */
public class QrbCommandHelper {

    public static final byte STX = 0x02;
    public static final byte ETX = 0x03;

    /**
     * 1234 => "1234" => ['4','3','2','1']
     * 1 => [31]
     * 12 => [32,31]
     * 123 => [33,32,31]
     * @param value value to convert
     * @return result
     */
    public static byte[] intToReversedBytes(int value) {
        byte[] result = String.valueOf(value).getBytes(StandardCharsets.US_ASCII);
        ArrayUtils.reverse(result);
        return result;
    }

    public static byte[] addChecksumAndGenerateCommand(ArrayList<Byte> bytes) {
        Optional<Byte> checksum = bytes.stream().reduce((x, y) -> (byte) (x ^ y));
        checksum.ifPresent(bytes::add);
        byte[] result = new byte[bytes.size()];
        for(int i = 0; i < bytes.size(); i ++) result[i] = bytes.get(i);
        return result;
    }

    /**
     * XRI: RF Input Signal Level Check
     * @param row starts from 1
     * @return a command bytes
     */
    public static byte[] genInputSignalLevelCheck(int row){
        ArrayList<Byte> command = new ArrayList<Byte>();
        byte[] vRow = intToReversedBytes(row);

        command.add(STX);
        command.add((byte)0x30);
        command.add((byte)0x30);
        command.add((byte)0x58); // X
        command.add((byte)0x52); // R
        command.add((byte)0x49); // I
        command.add(vRow.length > 2 ? vRow[2] : (byte)0x30);
        command.add(vRow.length > 1 ? vRow[1] : (byte)0x30);
        command.add(vRow.length > 0 ? vRow[0] : (byte)0x30);
        command.add(ETX);

        return addChecksumAndGenerateCommand(command);

    }

    /**
     * Not documented, from logs
     * Request: set attenuation 13, 19
     * 0x02 0x30 0x30 0x58 0x41 0x57 0x41 0x30 0x32 0x39 0x42 0x30 0x31 0x39 0x30 0x30 0x30 0x03 0x7f
     * Request: set attenuation 13, 19
     * 0x02 0x30 0x30 0x58 0x41 0x57 0x41 0x30 0x30 0x31 0x42 0x30 0x30 0x31 0x36 0x33 0x30 0x03 0x79
     * Response:
     * N/A
     * @param row starts from 1
     * @return a command bytes
     */
    public static byte[] genSetAttenuation(int row, int col, int attn){
        ArrayList<Byte> command = new ArrayList<Byte>();
        byte[] vRow = intToReversedBytes(row);
        byte[] vCol = intToReversedBytes(col);
        byte[] vValue = intToReversedBytes(attn);

        command.add(STX);
        command.add((byte)0x30);
        command.add((byte)0x30);
        command.add((byte)0x58); // X
        command.add((byte)0x41); // A
        command.add((byte)0x57); // W
        command.add((byte)0x41); // A
        command.add(vRow.length > 2 ? vRow[2] : (byte)0x30);
        command.add(vRow.length > 1 ? vRow[1] : (byte)0x30);
        command.add(vRow.length > 0 ? vRow[0] : (byte)0x30);
        command.add((byte)0x42); // B
        command.add(vCol.length > 2 ? vCol[2] : (byte)0x30);
        command.add(vCol.length > 1 ? vCol[1] : (byte)0x30);
        command.add(vCol.length > 0 ? vCol[0] : (byte)0x30);
        command.add(vValue.length > 1 ? vValue[1] : (byte)0x30);
        command.add(vValue.length > 0 ? vValue[0] : (byte)0x30);
        command.add((byte)0x30); // no decimal
        command.add(ETX);

        return addChecksumAndGenerateCommand(command);

    }


}