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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
package org.yzh.client;
 
import org.yzh.client.netty.ClientConfig;
import org.yzh.client.netty.HandlerMapping;
import org.yzh.client.netty.TCPClient;
import org.yzh.commons.util.DateUtils;
import org.yzh.commons.util.StrUtils;
import org.yzh.protocol.codec.JTMessageAdapter;
import org.yzh.protocol.commons.JT808;
import org.yzh.protocol.commons.transform.attribute.AlarmADAS;
import org.yzh.protocol.commons.transform.attribute.AlarmBSD;
import org.yzh.protocol.commons.transform.attribute.AlarmDSM;
import org.yzh.protocol.commons.transform.attribute.AlarmTPMS;
import org.yzh.protocol.t808.T0100;
import org.yzh.protocol.t808.T0200;
 
import java.time.LocalDateTime;
import java.util.Map;
import java.util.TreeMap;
import java.util.concurrent.ThreadLocalRandom;
 
public class ClientTest {
 
    public static final JTMessageAdapter messageAdapter = new JTMessageAdapter("org.yzh.protocol");
 
    public static final ClientConfig jtConfig = new ClientConfig.Builder()
            .setIp("127.0.0.1")
            .setPort(7000)
            .setMaxFrameLength(2 + 21 + 1023 * 2 + 1 + 2)
            .setDelimiters(new byte[]{0x7e})
            .setDecoder(messageAdapter)
            .setEncoder(messageAdapter)
            .setHandlerMapping(new HandlerMapping("org.yzh.client"))
            .build();
 
    public static void main(String[] args) {
        TCPClient tcpClient = new TCPClient("0", jtConfig).start();
        tcpClient.writeObject(T0100("1"));
        for (int i = 0; i < 1; i++) {
            try {
                Thread.sleep(3000L);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            boolean alarm = i % 10 == 0;
            tcpClient.writeObject(T0200("1", true));
        }
    }
 
    public static T0100 T0100(String id) {
        String deviceId = "T" + StrUtils.leftPad(id, 6, '0');
        String clientId = "1" + StrUtils.leftPad(id, 10, '0');
        String plateNo = "测A" + StrUtils.leftPad(id, 5, '0');
 
        T0100 message = new T0100();
        message.setMessageId(JT808.终端注册);
        message.setProtocolVersion(1);
        message.setVersion(true);
        message.setClientId(clientId);
 
        message.setProvinceId(31);
        message.setCityId(115);
        message.setMakerId("yzh");
        message.setDeviceModel("www.jtt808.cn");
        message.setDeviceId(deviceId);
        message.setPlateColor(1);
        message.setPlateNo(plateNo);
        return message;
    }
 
    private static ThreadLocalRandom random = ThreadLocalRandom.current();
 
    public static T0200 T0200(String id) {
        return T0200(id, true);
    }
 
    public static T0200 T0200(String id, boolean alarm) {
        String clientId = "1" + StrUtils.leftPad(id, 10, '0');
 
        T0200 message = new T0200();
        message.setMessageId(JT808.位置信息汇报);
        message.setProtocolVersion(1);
        message.setVersion(true);
        message.setClientId(clientId);
 
        message.setWarnBit(1024);
        message.setStatusBit(2048);
        message.setLatitude(39915931);
        message.setLongitude(116403829);
        message.setAltitude(312);
        message.setSpeed(111);
        message.setDirection(99);
        LocalDateTime dateTime = LocalDateTime.now();
        message.setDeviceTime(dateTime);
 
        if (alarm) {
            AlarmADAS alarmADAS = new AlarmADAS();
            alarmADAS.setId(64);
            alarmADAS.setState(1);
            //alarmADAS.setType(random.nextInt(1, 11));
            alarmADAS.setType(6);
            alarmADAS.setLevel(1);
            alarmADAS.setFrontSpeed(10);
            alarmADAS.setFrontDistance(10);
            alarmADAS.setDeviateType(1);
            alarmADAS.setRoadSign(1);
            alarmADAS.setRoadSignValue(10);
            alarmADAS.setSpeed(10);
            alarmADAS.setAltitude(100);
            alarmADAS.setLatitude(32111111);
            alarmADAS.setLongitude(123111111);
            alarmADAS.setAlarmTime(dateTime);
            alarmADAS.setStatusBit(1);
            alarmADAS.setDeviceId(clientId);
            alarmADAS.setDateTime(dateTime);
            alarmADAS.setSequenceNo(1);
            alarmADAS.setFileTotal(1);
            alarmADAS.setReserved(1);
 
            AlarmDSM alarmDSM = new AlarmDSM();
            alarmDSM.setId(65);
            alarmDSM.setState(2);
          //  alarmADAS.setType(random.nextInt(1, 11));
            alarmADAS.setType(5);
            alarmDSM.setLevel(2);
            alarmDSM.setFatigueDegree(20);
            alarmDSM.setReserved(20);
            alarmDSM.setSpeed(20);
            alarmDSM.setAltitude(200);
            alarmDSM.setLatitude(32222222);
            alarmDSM.setLongitude(123222222);
            alarmDSM.setAlarmTime(dateTime);
            alarmDSM.setStatusBit(2);
            alarmDSM.setDeviceId(clientId);
            alarmDSM.setDateTime(dateTime);
            alarmDSM.setSequenceNo(2);
            alarmDSM.setFileTotal(2);
            alarmDSM.setReserved(2);
 
            AlarmTPMS alarmTPMS = new AlarmTPMS();
            alarmTPMS.setId(66);
            alarmTPMS.setState(3);
            alarmTPMS.setSpeed(30);
            alarmTPMS.setAltitude(300);
            alarmTPMS.setLatitude(32333333);
            alarmTPMS.setLongitude(123333333);
            alarmTPMS.setAlarmTime(dateTime);
            alarmTPMS.setStatusBit(3);
            alarmTPMS.setDeviceId(clientId);
            alarmTPMS.setDateTime(dateTime);
            alarmTPMS.setSequenceNo(3);
            alarmTPMS.setFileTotal(3);
            alarmTPMS.setReserved(3);
 
            AlarmBSD alarmBSD = new AlarmBSD();
            alarmBSD.setId(67);
            alarmBSD.setState(4);
            alarmBSD.setType(random.nextInt(1, 3));
            alarmBSD.setSpeed(40);
            alarmBSD.setAltitude(400);
            alarmBSD.setLatitude(32444444);
            alarmBSD.setLongitude(123444444);
            alarmBSD.setAlarmTime(dateTime);
            alarmBSD.setStatusBit(4);
            alarmBSD.setDeviceId(clientId);
            alarmBSD.setDateTime(dateTime);
            alarmBSD.setSequenceNo(4);
            alarmBSD.setFileTotal(4);
            alarmBSD.setReserved(4);
 
 
            Map<Integer, Object> attributes = new TreeMap<>();
//            attributes.put(AlarmADAS.key, alarmADAS);
            attributes.put(AlarmDSM.key, alarmDSM);
//            attributes.put(AlarmTPMS.key, alarmTPMS);
//            attributes.put(AlarmBSD.key, alarmBSD);
 
            message.setAttributes(attributes);
        }
        return message;
    }
}