18045010223
2025-07-07 0d3a683a0c97154b1f2e6657398664537e4e3e82
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package org.yzh.protocol.commons;
 
import io.github.yezhihao.protostar.annotation.Message;
import io.github.yezhihao.protostar.util.ClassUtils;
import org.yzh.protocol.basics.JTMessage;
 
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
/**
 * @author yezhihao
 * https://gitee.com/yezhihao/jt808-server
 */
public class MessageId {
 
    private static final Map<Integer, String> messageId = new HashMap<>(256);
    private static final Map<Integer, Class> messageClass = new HashMap<>(256);
 
    static {
        for (Class clazz : new Class[]{JT808.class, JT1078.class, JSATL12.class}) {
            Field[] fields = clazz.getFields();
            for (Field field : fields) {
                try {
                    if (!Integer.TYPE.isAssignableFrom(field.getType()))
                        continue;
                    int id = field.getInt(null);
                    String hexId = leftPad(Integer.toHexString(id), 4, '0');
                    String name = field.getName();
                    messageId.put(id, "[" + hexId + "]" + name);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
 
        List<Class> classList = ClassUtils.getClassList("org.yzh.protocol");
        for (Class<?> clazz : classList) {
            Message annotation = clazz.getAnnotation(Message.class);
            if (annotation != null) {
                int[] value = annotation.value();
                for (int i : value) {
                    messageClass.put(i, clazz);
                }
            }
        }
    }
 
    public static Class<JTMessage> getClass(Integer id) {
        return messageClass.get(id);
    }
 
    public static String getName(int id) {
        String name = messageId.get(id);
        if (name != null) {
            return name;
        }
        return leftPad(Integer.toHexString(id), 4, '0');
    }
 
    public static String leftPad(String str, int size, char ch) {
        int length = str.length();
        int pads = size - length;
        if (pads > 0) {
            char[] result = new char[size];
            str.getChars(0, length, result, pads);
            while (pads > 0)
                result[--pads] = ch;
            return new String(result);
        }
        return str;
    }
}