Newer
Older
casic-pan-tilt-br / casic-server / src / main / java / com / casic / missiles / client / MsgUtil.java
casic_zt on 23 May 2024 3 KB first commit
package com.casic.missiles.client;

import com.casic.missiles.modular.system.util.ByteUtil;
import org.springframework.stereotype.Component;

import static com.casic.missiles.modular.system.util.ByteUtil.intToBcds;

@Component
public class MsgUtil {


    public static String code_ = "68-ED-A4-38-37-FF";

    public static String getMsg(String data, String type, String code) {
        byte[] code_bytes = ByteUtil.hexStringToBytes(ByteUtil.convertToHex(code_));
        byte[] gas_bytes = {0x00, 0x01};
        byte[] strength_bytes = {0x00, 0x02};
        switch (type) {
            case "1":
                byte[] prefix_bytes = {0x01, 0x03, 0x04};
                byte[] data_bytes = ByteUtil.hexStringToBytes(Integer.toHexString((int) Double.parseDouble(data)), 4);
                byte[] bytes = ByteUtil.byteMergerAll(prefix_bytes, data_bytes);
                byte[] crc_bytes_1 = ByteUtil.hexStringToBytes(ByteUtil.buildCRC16(bytes));
                byte[] bytes_1 = ByteUtil.byteMergerAll(bytes, crc_bytes_1);
                byte[] bytes_1_1 = ByteUtil.byteMergerAll(bytes_1, gas_bytes);
                byte[] bytes2_1 = ByteUtil.byteMergerAll(bytes_1_1, code_bytes);
                String crc = ByteUtil.binToHexString(bytes2_1);
                return crc;
            case "0":
                byte[] prefix0_bytes = {0x01, 0x03, 0x02};
                byte[] data0_bytes = ByteUtil.hexStringToBytes(Integer.toHexString((int) Double.parseDouble(data)), 2);
                byte[] bytes0 = ByteUtil.byteMergerAll(prefix0_bytes, data0_bytes);
                byte[] crc_bytes = ByteUtil.hexStringToBytes(ByteUtil.buildCRC16(bytes0));
                byte[] bytes1 = ByteUtil.byteMergerAll(bytes0, crc_bytes);
                byte[] bytes2 = ByteUtil.byteMergerAll(bytes1, code_bytes);
                String crc0 = ByteUtil.bcdToString(bytes2);
                return crc0;
            default:
                return "";
        }

    }

    public static void main(String[] args) {
        String crc = getMsg("257", "0", "");
        System.out.println(crc);

        System.out.println(ByteUtil.hexStringToBytes("12654", 8));
        System.out.println(0xff);
        System.out.println((byte) 126);
    }


    /**
     * 设备编号加密
     *
     * @param devCode
     * @return
     */
    public static String encode(String devCode) {

        String commandStr = "be0c30" + ByteUtil.bcdToString(ByteUtil.intToBcds(devCode.length() + 2, 2)) + devCode + "0000" + ByteUtil.buildCRC16(ByteUtil.bcdStringToBytes(devCode)) + "e0fe";
        return commandStr;
    }

    /**
     * 设备编号解密
     *
     * @param devCode
     * @return
     */
    public static String decode(String devCode) {
        String commandStr = "be0c32" + ByteUtil.bcdToString(ByteUtil.intToBcds(devCode.length() + 2, 2)) + devCode + "0000" + ByteUtil.buildCRC16(ByteUtil.bcdStringToBytes(devCode)) + "e0fe";
        return commandStr;
    }


    /**
     * 设备编号加密返回
     *
     * @param msg
     * @return
     */
    public static String encodeResult(String msg) {

        return msg.substring(10);
    }

    /**
     * 设备编号解密返回
     *
     * @param msg
     * @return
     */
    public static String decodeResult(String msg) {

        return msg.substring(6);
    }

}