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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
|
/*
* Copyright 2012 - 2015 Anton Tananaev (anton.tananaev@gmail.com)
*
* 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 java.net.SocketAddress;
import java.util.Date;
import java.util.regex.Pattern;
import org.jboss.netty.channel.Channel;
import org.traccar.BaseProtocolDecoder;
import org.traccar.helper.DateBuilder;
import org.traccar.helper.Parser;
import org.traccar.helper.PatternBuilder;
import org.traccar.model.Event;
import org.traccar.model.Position;
public class T55ProtocolDecoder extends BaseProtocolDecoder {
public T55ProtocolDecoder(T55Protocol protocol) {
super(protocol);
}
private static final Pattern PATTERN_GPRMC = new PatternBuilder()
.text("$GPRMC,")
.number("(dd)(dd)(dd).?d*,") // time
.expression("([AV]),") // validity
.number("(dd)(dd.d+),") // latitude
.expression("([NS]),")
.number("(d{2,3})(dd.d+),") // longitude
.expression("([EW]),")
.number("(d+.?d*)?,") // speed
.number("(d+.?d*)?,") // course
.number("(dd)(dd)(dd),") // date
.expression("[^*]+")
.text("*")
.expression("[^,]+")
.number(",(d+)") // satellites
.number(",(d+)") // imei
.number(",(d+)").optional(3)
.any()
.compile();
private static final Pattern PATTERN_GPGGA = new PatternBuilder()
.text("$GPGGA,")
.number("(dd)(dd)(dd).?d*,") // time
.number("(d+)(dd.d+),") // latitude
.expression("([NS]),")
.number("(d+)(dd.d+),") // longitude
.expression("([EW]),")
.any()
.compile();
private static final Pattern PATTERN_GPRMA = new PatternBuilder()
.text("$GPRMA,")
.expression("([AV]),") // validity
.number("(dd)(dd.d+),") // latitude
.expression("([NS]),")
.number("(ddd)(dd.d+),") // longitude
.expression("([EW]),,,")
.number("(d+.?d*)?,") // speed
.number("(d+.?d*)?,") // course
.any()
.compile();
private static final Pattern PATTERN_TRCCR = new PatternBuilder()
.text("$TRCCR,")
.number("(dddd)(dd)(dd)") // date
.number("(dd)(dd)(dd).?d*,") // time
.expression("([AV]),") // validity
.number("(-?d+.d+),") // latitude
.number("(-?d+.d+),") // longitude
.number("(d+.d+),") // speed
.number("(d+.d+),") // course
.number("(-?d+.d+),") // altitude
.number("(d+.?d*),") // battery
.any()
.compile();
private Position position = null;
private Position decodeGprmc(String sentence, SocketAddress remoteAddress, Channel channel) {
if (channel != null) {
channel.write("OK1\r\n");
}
Parser parser = new Parser(PATTERN_GPRMC, sentence);
if (!parser.matches()) {
return null;
}
Position position = new Position();
position.setProtocol(getProtocolName());
if (hasDeviceId()) {
position.setDeviceId(getDeviceId());
}
DateBuilder dateBuilder = new DateBuilder()
.setTime(parser.nextInt(), parser.nextInt(), parser.nextInt());
position.setValid(parser.next().equals("A"));
position.setLatitude(parser.nextCoordinate());
position.setLongitude(parser.nextCoordinate());
position.setSpeed(parser.nextDouble());
position.setCourse(parser.nextDouble());
dateBuilder.setDateReverse(parser.nextInt(), parser.nextInt(), parser.nextInt());
position.setTime(dateBuilder.getDate());
if (parser.hasNext(3)) {
position.set(Event.KEY_SATELLITES, parser.next());
if (!identify(parser.next(), channel, remoteAddress)) {
return null;
}
position.setDeviceId(getDeviceId());
}
if (hasDeviceId()) {
return position;
} else {
this.position = position; // save position
return null;
}
}
private Position decodeGpgga(String sentence) {
Parser parser = new Parser(PATTERN_GPGGA, sentence);
if (!parser.matches()) {
return null;
}
Position position = new Position();
position.setProtocol(getProtocolName());
position.setDeviceId(getDeviceId());
DateBuilder dateBuilder = new DateBuilder()
.setCurrentDate()
.setTime(parser.nextInt(), parser.nextInt(), parser.nextInt());
position.setTime(dateBuilder.getDate());
position.setValid(true);
position.setLatitude(parser.nextCoordinate());
position.setLongitude(parser.nextCoordinate());
return position;
}
private Position decodeGprma(String sentence) {
Parser parser = new Parser(PATTERN_GPRMA, sentence);
if (!parser.matches()) {
return null;
}
Position position = new Position();
position.setProtocol(getProtocolName());
position.setDeviceId(getDeviceId());
position.setTime(new Date());
position.setValid(parser.next().equals("A"));
position.setLatitude(parser.nextCoordinate());
position.setLongitude(parser.nextCoordinate());
position.setSpeed(parser.nextDouble());
position.setCourse(parser.nextDouble());
return position;
}
private Position decodeTrccr(String sentence) {
Parser parser = new Parser(PATTERN_TRCCR, sentence);
if (!parser.matches()) {
return null;
}
Position position = new Position();
position.setProtocol(getProtocolName());
position.setDeviceId(getDeviceId());
DateBuilder dateBuilder = new DateBuilder()
.setDate(parser.nextInt(), parser.nextInt(), parser.nextInt())
.setTime(parser.nextInt(), parser.nextInt(), parser.nextInt());
position.setTime(dateBuilder.getDate());
position.setValid(parser.next().equals("A"));
position.setLatitude(parser.nextDouble());
position.setLongitude(parser.nextDouble());
position.setSpeed(parser.nextDouble());
position.setCourse(parser.nextDouble());
position.setAltitude(parser.nextDouble());
position.set(Event.KEY_BATTERY, parser.next());
return position;
}
@Override
protected Object decode(
Channel channel, SocketAddress remoteAddress, Object msg) throws Exception {
String sentence = (String) msg;
if (!sentence.startsWith("$") && sentence.contains("$")) {
int index = sentence.indexOf("$");
String id = sentence.substring(0, index);
if (id.endsWith(",")) {
id = id.substring(0, id.length() - 1);
}
identify(id, channel, remoteAddress);
sentence = sentence.substring(index);
}
if (sentence.startsWith("$PGID")) {
identify(sentence.substring(6, sentence.length() - 3), channel, remoteAddress);
} else if (sentence.startsWith("$PCPTI")) {
identify(sentence.substring(7, sentence.indexOf(",", 7)), channel, remoteAddress);
} else if (sentence.startsWith("IMEI")) {
identify(sentence.substring(5, sentence.length()), channel, remoteAddress);
} else if (sentence.startsWith("$GPFID")) {
if (identify(sentence.substring(7, sentence.length()), channel, remoteAddress) && position != null) {
Position position = this.position;
position.setDeviceId(getDeviceId());
this.position = null;
return position;
}
} else if (Character.isDigit(sentence.charAt(0)) && sentence.length() == 15) {
identify(sentence, channel, remoteAddress);
} else if (sentence.startsWith("$GPRMC")) {
return decodeGprmc(sentence, remoteAddress, channel);
} else if (sentence.startsWith("$GPGGA") && hasDeviceId()) {
return decodeGpgga(sentence);
} else if (sentence.startsWith("$GPRMA") && hasDeviceId()) {
return decodeGprma(sentence);
} else if (sentence.startsWith("$TRCCR") && hasDeviceId()) {
return decodeTrccr(sentence);
}
return null;
}
}
|