Skip to content

Commit

Permalink
Merge tananaev/master
Browse files Browse the repository at this point in the history
Conflicts:
	src/org/traccar/web/WebServer.java
  • Loading branch information
guterresrafael committed Nov 27, 2015
2 parents c57bd2d + 7db2eb1 commit ca06e8d
Show file tree
Hide file tree
Showing 19 changed files with 203 additions and 63 deletions.
1 change: 1 addition & 0 deletions debug.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
<entry key='web.old'>true</entry>-->
<entry key='web.path'>web</entry>
<entry key='web.debug'>true</entry>
<entry key='web.console'>true</entry>

<entry key='geocoder.enable'>true</entry>
<entry key='geocoder.type'>nominatim</entry>
Expand Down
4 changes: 2 additions & 2 deletions src/org/traccar/Main.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012 - 2013 Anton Tananaev ([email protected])
* Copyright 2012 - 2015 Anton Tananaev ([email protected])
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -34,11 +34,11 @@ public static void main(String[] args) throws Exception {
Context.getWebServer().start();
}

// Shutdown server properly
Runtime.getRuntime().addShutdownHook(new Thread() {
@Override
public void run() {
Log.info("Shutting down server...");

if (Context.getWebServer() != null) {
Context.getWebServer().stop();
}
Expand Down
4 changes: 2 additions & 2 deletions src/org/traccar/WebDataHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,8 @@ protected Position handlePosition(Position position) {
.replace("{protocol}", String.valueOf(position.getProtocol()))
.replace("{deviceTime}", String.valueOf(position.getDeviceTime().getTime()))
.replace("{fixTime}", String.valueOf(position.getFixTime().getTime()))
.replace("{valid}", String.valueOf(position.getLatitude()))
.replace("{latitude}", String.valueOf(position.getValid()))
.replace("{valid}", String.valueOf(position.getValid()))
.replace("{latitude}", String.valueOf(position.getLatitude()))
.replace("{longitude}", String.valueOf(position.getLongitude()))
.replace("{altitude}", String.valueOf(position.getAltitude()))
.replace("{speed}", String.valueOf(position.getSpeed()))
Expand Down
1 change: 1 addition & 0 deletions src/org/traccar/database/DataManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ private void initDatabase() throws Exception {
ds.setPassword(config.getString("database.password"));
ds.setIdleConnectionTestPeriod(600);
ds.setTestConnectionOnCheckin(true);
ds.setMaxStatementsPerConnection(config.getInteger("database.maxStatements"));
int maxPoolSize = config.getInteger("database.maxPoolSize");
if (maxPoolSize != 0) {
ds.setMaxPoolSize(maxPoolSize);
Expand Down
28 changes: 24 additions & 4 deletions src/org/traccar/protocol/H02ProtocolDecoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,17 @@ private Position decodeBinary(ChannelBuffer buf, Channel channel) {
.any()
.number("(dd)(dd)(dd),") // time
.expression("([AV])?,") // validity
.number("-?(d+)-?(dd.d+),") // latitude
.groupBegin()
.number("(d+)(dd.d+),") // latitude
.or()
.number("-(d+)-(d+.d+),") // latitude
.groupEnd()
.expression("([NS]),")
.number("-?(d+)-?(dd.d+),") // longitude
.groupBegin()
.number("(d+)(dd.d+),") // longitude
.or()
.number("-(d+)-(d+.d+),") // longitude
.groupEnd()
.expression("([EW]),")
.number("(d+.?d*),") // speed
.number("(d+.?d*)?,") // course
Expand Down Expand Up @@ -155,8 +163,20 @@ private Position decodeText(String sentence, Channel channel) {
position.setValid(parser.next().equals("A"));
}

position.setLatitude(parser.nextCoordinate());
position.setLongitude(parser.nextCoordinate());
if (parser.hasNext(2)) {
position.setLatitude(parser.nextCoordinate());
}
if (parser.hasNext(2)) {
position.setLatitude(parser.nextCoordinate());
}

if (parser.hasNext(2)) {
position.setLongitude(parser.nextCoordinate());
}
if (parser.hasNext(2)) {
position.setLongitude(parser.nextCoordinate());
}

position.setSpeed(parser.nextDouble());
position.setCourse(parser.nextDouble());

Expand Down
69 changes: 37 additions & 32 deletions src/org/traccar/protocol/TeltonikaProtocolDecoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,49 +81,54 @@ private List<Position> parseLocation(Channel channel, ChannelBuffer buf) {

long time = buf.readUnsignedInt() & 0x3fffffff;
time += 1167609600; // 2007-01-01 00:00:00
position.setTime(new Date(time * 1000));

globalMask = buf.readUnsignedByte();
if (!BitUtil.check(globalMask, 0)) {
return null;
}
if (BitUtil.check(globalMask, 0)) {

int locationMask = buf.readUnsignedByte();
position.setTime(new Date(time * 1000));

if (BitUtil.check(locationMask, 0)) {
position.setLatitude(buf.readFloat());
position.setLongitude(buf.readFloat());
}
int locationMask = buf.readUnsignedByte();

if (BitUtil.check(locationMask, 1)) {
position.setAltitude(buf.readUnsignedShort());
}
if (BitUtil.check(locationMask, 0)) {
position.setLatitude(buf.readFloat());
position.setLongitude(buf.readFloat());
}

if (BitUtil.check(locationMask, 2)) {
position.setCourse(buf.readUnsignedByte() * 360.0 / 256);
}
if (BitUtil.check(locationMask, 1)) {
position.setAltitude(buf.readUnsignedShort());
}

if (BitUtil.check(locationMask, 3)) {
position.setSpeed(UnitsConverter.knotsFromKph(buf.readUnsignedByte()));
}
if (BitUtil.check(locationMask, 2)) {
position.setCourse(buf.readUnsignedByte() * 360.0 / 256);
}

if (BitUtil.check(locationMask, 4)) {
int satellites = buf.readUnsignedByte();
position.set(Event.KEY_SATELLITES, satellites);
position.setValid(satellites >= 3);
}
if (BitUtil.check(locationMask, 3)) {
position.setSpeed(UnitsConverter.knotsFromKph(buf.readUnsignedByte()));
}

if (BitUtil.check(locationMask, 5)) {
position.set(Event.KEY_LAC, buf.readUnsignedShort());
position.set(Event.KEY_CID, buf.readUnsignedShort());
}
if (BitUtil.check(locationMask, 4)) {
int satellites = buf.readUnsignedByte();
position.set(Event.KEY_SATELLITES, satellites);
position.setValid(satellites >= 3);
}

if (BitUtil.check(locationMask, 6)) {
position.set(Event.KEY_GSM, buf.readUnsignedByte());
}
if (BitUtil.check(locationMask, 5)) {
position.set(Event.KEY_LAC, buf.readUnsignedShort());
position.set(Event.KEY_CID, buf.readUnsignedShort());
}

if (BitUtil.check(locationMask, 6)) {
position.set(Event.KEY_GSM, buf.readUnsignedByte());
}

if (BitUtil.check(locationMask, 7)) {
position.set("operator", buf.readUnsignedInt());
}

} else {

getLastLocation(position, new Date(time * 1000));

if (BitUtil.check(locationMask, 7)) {
position.set("operator", buf.readUnsignedInt());
}

} else {
Expand Down
5 changes: 5 additions & 0 deletions src/org/traccar/protocol/Tk103ProtocolDecoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,11 @@ protected Object decode(
}
position.setDeviceId(getDeviceId());

int alarm = sentence.indexOf("BO01");
if (alarm != -1) {
position.set(Event.KEY_ALARM, Integer.parseInt(sentence.substring(alarm + 4, alarm + 5)));
}

DateBuilder dateBuilder = new DateBuilder();
if (parser.next() == null) {
dateBuilder.setDate(parser.nextInt(), parser.nextInt(), parser.nextInt());
Expand Down
14 changes: 2 additions & 12 deletions src/org/traccar/protocol/TytanProtocolDecoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,8 @@
import java.net.SocketAddress;
import java.nio.charset.Charset;
import java.util.Date;
import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;

import org.jboss.netty.buffer.ChannelBuffer;
import org.jboss.netty.buffer.ChannelBuffers;
Expand Down Expand Up @@ -81,16 +79,8 @@ private void decodeExtraData(Position position, ChannelBuffer buf, int end) {
position.set("authorized", ChannelBuffers.hexDump(buf.readBytes(8)));
break;
case 24:
Set<Integer> temps = new LinkedHashSet<>();
int temp = buf.readUnsignedByte();
for (int i = 3; i >= 0; i--) {
n = (temp >> (2 * i)) & 0x03;
if (!temps.contains(n)) {
temps.add(n);
}
}
for (int i : temps) {
position.set(Event.PREFIX_TEMP + i, buf.readUnsignedByte());
for (int i = 0; i < length / 2; i++) {
position.set(Event.PREFIX_TEMP + buf.readUnsignedByte(), buf.readByte());
}
break;
case 28:
Expand Down
4 changes: 3 additions & 1 deletion src/org/traccar/protocol/UlbotechProtocolDecoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,9 @@ protected Object decode(

case DATA_EVENT:
position.set(Event.KEY_EVENT, buf.readUnsignedByte());
position.set("event-mask", buf.readUnsignedInt());
if (length > 1) {
position.set("event-mask", buf.readUnsignedInt());
}
break;

default:
Expand Down
15 changes: 15 additions & 0 deletions src/org/traccar/web/CommandServlet.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
/*
* Copyright 2015 Anton Tananaev ([email protected])
*
* 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.web;

import javax.json.Json;
Expand Down
52 changes: 52 additions & 0 deletions src/org/traccar/web/ConsoleServlet.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* Copyright 2015 Anton Tananaev ([email protected])
*
* 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.web;

import org.h2.server.web.ConnectionInfo;
import org.h2.server.web.WebServlet;
import org.traccar.Context;
import org.traccar.helper.Log;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class ConsoleServlet extends WebServlet {

@Override
public void init() {
super.init();

try {
Field field = WebServlet.class.getDeclaredField("server");
field.setAccessible(true);
org.h2.server.web.WebServer server = (org.h2.server.web.WebServer) field.get(this);

ConnectionInfo connectionInfo = new ConnectionInfo("Traccar|"
+ Context.getConfig().getString("database.driver") + "|"
+ Context.getConfig().getString("database.url") + "|"
+ Context.getConfig().getString("database.user"));

Method method = org.h2.server.web.WebServer.class.getDeclaredMethod("updateSetting", ConnectionInfo.class);
method.setAccessible(true);
method.invoke(server, connectionInfo);

} catch (NoSuchFieldException | IllegalAccessException | NoSuchMethodException | InvocationTargetException e) {
Log.warning(e);
}
}

}
13 changes: 7 additions & 6 deletions src/org/traccar/web/WebServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ public WebServer(Config config, DataSource dataSource) {
break;
case "new":
initApi();
if (config.getBoolean("web.console")) {
initConsole();
}
initWebApp();
break;
case "old":
Expand Down Expand Up @@ -118,12 +121,10 @@ private void initOldApi() {
handlers.addHandler(servletHandler);
}

private void initRestApi() {
ResourceConfig resourceConfig = new ResourceConfig();
resourceConfig.packages("org.traccar.api");
ServletContextHandler servletHandler = new ServletContextHandler(ServletContextHandler.NO_SESSIONS);
ServletHolder servletHolder = new ServletHolder(new ServletContainer(resourceConfig));
servletHandler.addServlet(servletHolder, "/rest/*");
private void initConsole() {
ServletContextHandler servletHandler = new ServletContextHandler(ServletContextHandler.SESSIONS);
servletHandler.setContextPath("/console");
servletHandler.addServlet(new ServletHolder(new ConsoleServlet()), "/*");
handlers.addHandler(servletHandler);
}

Expand Down
12 changes: 8 additions & 4 deletions test/org/traccar/ProtocolDecoderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -119,21 +119,25 @@ protected void verifyPosition(BaseProtocolDecoder decoder, Object object, Positi
}

protected void verifyPositions(BaseProtocolDecoder decoder, Object object) throws Exception {
verifyDecodedList(decoder.decode(null, null, object), null);
verifyDecodedList(decoder.decode(null, null, object), true, null);
}

protected void verifyPositions(BaseProtocolDecoder decoder, boolean checkLocation, Object object) throws Exception {
verifyDecodedList(decoder.decode(null, null, object), checkLocation, null);
}

protected void verifyPositions(BaseProtocolDecoder decoder, Object object, Position position) throws Exception {
verifyDecodedList(decoder.decode(null, null, object), position);
verifyDecodedList(decoder.decode(null, null, object), true, position);
}

private void verifyDecodedList(Object decodedObject, Position expected) {
private void verifyDecodedList(Object decodedObject, boolean checkLocation, Position expected) {

Assert.assertNotNull("list is null", decodedObject);
Assert.assertTrue("not a list", decodedObject instanceof List);
Assert.assertFalse("list if empty", ((List) decodedObject).isEmpty());

for (Object item : (List) decodedObject) {
verifyDecodedPosition(item, true, false, expected);
verifyDecodedPosition(item, checkLocation, false, expected);
}

}
Expand Down
6 changes: 6 additions & 0 deletions test/org/traccar/protocol/H02ProtocolDecoderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ public class H02ProtocolDecoderTest extends ProtocolDecoderTest {
public void testDecode() throws Exception {

H02ProtocolDecoder decoder = new H02ProtocolDecoder(new H02Protocol());

verifyPosition(decoder, buffer(
"*HQ,1451316485,V1,121557,A,-23-3.3408,S,-48-2.8926,W,0.1,158,241115,FFFFFFFF#"));

verifyPosition(decoder, buffer(
"*HQ,1451316485,V1,121557,A,-23-35.3408,S,-48-2.8926,W,0.1,158,241115,FFFFFFFF#"));

verifyPosition(decoder, buffer(
"*HQ,355488020119695,V1,050418,,2827.61232,N,07703.84822,E,0.00,0,031015,FFFEFBFF#"),
Expand Down
3 changes: 3 additions & 0 deletions test/org/traccar/protocol/TeltonikaProtocolDecoderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ public void testDecode() throws Exception {
verifyNothing(decoder, binary(
"000F313233343536373839303132333435"));

verifyPositions(decoder, false, binary(
"0000000000000055070450aa14320201f00150aa17f3031f42332a4c4193d68c008d00020901f00150aa1b6a031f423383f54193624f009d00000a01f00150aa1c230fc01a0000552b040164f400dd00f0010143100c0105000000050400006846"));

verifyPositions(decoder, binary(
"000000000000003508010000014f8e016420002141bbaf0f4e96a7fffa0000120000000602010047030242669c92000002c7000000009100000000000100002df3"));

Expand Down
3 changes: 3 additions & 0 deletions test/org/traccar/protocol/Tk103ProtocolDecoderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ public void testDecode() throws Exception {

Tk103ProtocolDecoder decoder = new Tk103ProtocolDecoder(new Tk103Protocol());

verifyPosition(decoder, text(
"(013612345678BO012061830A2934.0133N10627.2544E040.0080331309.6200000000L000770AD"));

verifyAttributes(decoder, text(
"(088047194605BZ00,510,010,36e6,932c,43,36e6,766b,36,36e6,7668,32"));

Expand Down
Loading

0 comments on commit ca06e8d

Please sign in to comment.