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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
|
/*
* Copyright 2024 Anton Tananaev (anton@traccar.org)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.traccar.protocol;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.Channel;
import jakarta.json.Json;
import jakarta.json.JsonObject;
import org.traccar.BaseProtocolDecoder;
import org.traccar.NetworkMessage;
import org.traccar.Protocol;
import org.traccar.helper.BitUtil;
import org.traccar.helper.Checksum;
import org.traccar.model.Position;
import org.traccar.session.DeviceSession;
import java.io.StringReader;
import java.net.SocketAddress;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.TimeZone;
public class SnapperProtocolDecoder extends BaseProtocolDecoder {
public SnapperProtocolDecoder(Protocol protocol) {
super(protocol);
}
public static final int MSG_HELLO = 0x01;
public static final int MSG_SENDING_START = 0x02;
public static final int MSG_SENDING_FINISH = 0x03;
public static final int MSG_SEND_EVENTS = 0x21;
public static final int MSG_SEND_TECH_INFO = 0x23;
public static final int MSG_UPDATE_CMS_NUM = 0x24;
public static final int MSG_SEND_SYSTEM_INFO = 0x26;
public static final int MSG_SEND_USER_PHONE_NUMBERS = 0x31;
public static final int MSG_SEND_GPS_DATA = 0x32;
public static final int MSG_SEND_LBS_DATA = 0x33;
public static final int MSG_SEND_SYSTEM_STATUS = 0x34;
public static final int MSG_SEND_TRANSIT_SETTINGS = 0x35;
public static final int MSG_GET_SETTINGS = 0x36;
public static final int MSG_SEND_CONCATENATED_PACKET = 0x37;
public static final int MSG_SEND_DEBUG_INFO = 0x38;
private void sendResponse(
Channel channel, SocketAddress remoteAddress, int index, int type, String answer) {
if (channel != null) {
ByteBuf response = Unpooled.buffer();
response.writeByte('K');
response.writeByte(3); // protocol version
response.writeLongLE(0); // reserved
response.writeShortLE(0); // encryption
response.writeIntLE(answer.length());
response.writeIntLE(0); // reserved
response.writeShortLE(index);
response.writeByte(Checksum.sum(ByteBuffer.wrap(answer.getBytes(StandardCharsets.US_ASCII))));
response.writeShortLE(type);
response.writeCharSequence(answer, StandardCharsets.US_ASCII);
channel.writeAndFlush(new NetworkMessage(response, remoteAddress));
}
}
private void decodeEvents(Position position, ByteBuf buf) {
position.set(Position.KEY_EVENT, buf.readUnsignedByte());
buf.readUnsignedByte(); // info 1
buf.readUnsignedByte(); // info 2
buf.readUnsignedIntLE(); // timestamp
buf.readUnsignedByte(); // timezone
}
private void decodeTechInfo(Position position, ByteBuf buf) {
for (int i = 0; i < 5; i++) {
buf.readUnsignedByte(); // index
int type = buf.readUnsignedByte();
switch (type) {
case 0x00:
position.set(Position.KEY_POWER, buf.readUnsignedByte() * 0.1);
position.set(Position.KEY_DEVICE_TEMP, buf.readByte());
position.set(Position.KEY_RSSI, buf.readUnsignedByte());
break;
case 0x01:
position.set("interiorTemp", buf.readByte());
position.set("engineTemp", buf.readByte());
buf.readUnsignedByte(); // reserved
break;
default:
buf.skipBytes(3);
break;
}
}
}
private void decodeGpsData(Position position, ByteBuf buf) throws ParseException {
String content = buf.readCharSequence(buf.readableBytes(), StandardCharsets.US_ASCII).toString();
JsonObject json = Json.createReader(new StringReader(content)).readObject();
int flags = Integer.parseInt(json.getString("f"), 16);
if (!BitUtil.check(flags, 3)) {
return;
}
position.setValid(BitUtil.check(flags, 1));
if (!BitUtil.check(flags, 6)) {
position.setLatitude(-position.getLatitude());
}
if (!BitUtil.check(flags, 7)) {
position.setLongitude(-position.getLongitude());
}
DateFormat dateFormat = new SimpleDateFormat("ddMMyyHHmmss");
dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
position.setTime(dateFormat.parse(json.getString("d") + json.getString("t").split("\\.")[0]));
String lat = json.getString("la");
position.setLatitude(Integer.parseInt(lat.substring(0, 2)) + Double.parseDouble(lat.substring(2)) / 60);
String lon = json.getString("lo");
position.setLongitude(Integer.parseInt(lon.substring(0, 3)) + Double.parseDouble(lon.substring(3)) / 60);
position.setAltitude(Double.parseDouble(json.getString("a")));
position.setSpeed(Double.parseDouble(json.getString("s")));
position.setCourse(Double.parseDouble(json.getString("c")));
position.set(Position.KEY_SATELLITES, Integer.parseInt(json.getString("sv")));
}
@Override
protected Object decode(
Channel channel, SocketAddress remoteAddress, Object msg) throws Exception {
ByteBuf buf = (ByteBuf) msg;
buf.readUnsignedByte(); // header
buf.readUnsignedByte(); // protocol version
buf.readUnsignedIntLE(); // system bonus identifier
String serialNumber = String.valueOf(buf.readUnsignedIntLE());
DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, serialNumber);
if (deviceSession == null) {
return null;
}
buf.readUnsignedShortLE(); // encryption
int length = buf.readIntLE();
buf.readUnsignedByte(); // flags
buf.readUnsignedMediumLE(); // reserved
int index = buf.readUnsignedShortLE();
buf.readUnsignedByte(); // checksum
int type = buf.readUnsignedShortLE();
if (type == MSG_HELLO) {
sendResponse(channel, remoteAddress, index, type, "hello");
} else {
sendResponse(channel, remoteAddress, index, type, "OK");
}
Position position = new Position(getProtocolName());
position.setDeviceId(deviceSession.getDeviceId());
switch (type) {
case MSG_SEND_EVENTS:
decodeEvents(position, buf);
getLastLocation(position, null); // TODO read timestamp
return position;
case MSG_SEND_TECH_INFO:
decodeTechInfo(position, buf);
getLastLocation(position, null);
return position;
case MSG_SEND_GPS_DATA:
decodeGpsData(position, buf.readSlice(length));
return position;
case MSG_SEND_CONCATENATED_PACKET:
int count = buf.readUnsignedShortLE();
for (int i = 0; i < count; i++) {
int partType = buf.readUnsignedShortLE();
int partLength = buf.readUnsignedShortLE();
switch (partType) {
case MSG_SEND_EVENTS:
decodeEvents(position, buf);
break;
case MSG_SEND_TECH_INFO:
decodeTechInfo(position, buf);
break;
case MSG_SEND_GPS_DATA:
decodeGpsData(position, buf.readSlice(partLength));
break;
default:
buf.skipBytes(partLength);
break;
}
}
if (position.getFixTime() == null) {
getLastLocation(position, null);
}
return position;
default:
return null;
}
}
}
|