diff options
-rw-r--r-- | src/org/traccar/Server.java | 38 | ||||
-rw-r--r-- | src/org/traccar/protocol/xexun2/Xexun2ProtocolDecoder.java | 3 | ||||
-rw-r--r-- | test/org/traccar/protocol/xexun2/Xexun2ProtocolDecoderTest.java | 2 |
3 files changed, 19 insertions, 24 deletions
diff --git a/src/org/traccar/Server.java b/src/org/traccar/Server.java index 305edecde..c7d52337e 100644 --- a/src/org/traccar/Server.java +++ b/src/org/traccar/Server.java @@ -28,6 +28,7 @@ import java.sql.ResultSet; import java.sql.SQLException; import java.text.DateFormat; import java.text.SimpleDateFormat; +import java.util.Calendar; import java.util.Date; import java.util.logging.Logger; import java.util.logging.FileHandler; @@ -102,10 +103,10 @@ public class Server implements DataManager { * Database connection */ private Connection connection; - - private NamedParameterStatement selectDevice; - - private NamedParameterStatement insertPosition; + + private String selectDeviceQuery; + + private String insertPositionQuery; /** * Init database @@ -130,27 +131,23 @@ public class Server implements DataManager { connection = DriverManager.getConnection(url); } - // Init statements - String selectDeviceQuery = properties.getProperty("database.selectDevice"); - if (selectDeviceQuery != null) { - selectDevice = new NamedParameterStatement(connection, selectDeviceQuery); - } - - String insertPositionQuery = properties.getProperty("database.insertPosition"); - if (insertPositionQuery != null) { - insertPosition = new NamedParameterStatement(connection, insertPositionQuery); - } + selectDeviceQuery = properties.getProperty("database.selectDevice"); + insertPositionQuery = properties.getProperty("database.insertPosition"); } /** * Devices */ private Map devices; + private Calendar devicesLastUpdate; + private Long devicesListRefreshDelay = new Long(5 * 60 * 1000); public synchronized List getDevices() throws SQLException { List deviceList = new LinkedList(); + NamedParameterStatement selectDevice = + new NamedParameterStatement(connection, selectDeviceQuery); ResultSet result = selectDevice.executeQuery(); while (result.next()) { Device device = new Device(); @@ -161,18 +158,16 @@ public class Server implements DataManager { return deviceList; } - + public Device getDeviceByImei(String imei) throws SQLException { - - // Init device list - if (devices == null) { + + if ((devices == null) || (Calendar.getInstance().getTimeInMillis() - devicesLastUpdate.getTimeInMillis() > devicesListRefreshDelay)) { devices = new HashMap(); - List deviceList = getDevices(); - for (Object device: deviceList) { devices.put(((Device) device).getImei(), device); } + devicesLastUpdate = Calendar.getInstance(); } return (Device) devices.get(imei); @@ -180,6 +175,9 @@ public class Server implements DataManager { public synchronized void setPosition(Position position) throws SQLException { + NamedParameterStatement insertPosition = + new NamedParameterStatement(connection, insertPositionQuery); + insertPosition.setLong("device_id", position.getDeviceId()); insertPosition.setTimestamp("time", position.getTime()); insertPosition.setBoolean("valid", position.getValid()); diff --git a/src/org/traccar/protocol/xexun2/Xexun2ProtocolDecoder.java b/src/org/traccar/protocol/xexun2/Xexun2ProtocolDecoder.java index f7ff9f528..8f1b53328 100644 --- a/src/org/traccar/protocol/xexun2/Xexun2ProtocolDecoder.java +++ b/src/org/traccar/protocol/xexun2/Xexun2ProtocolDecoder.java @@ -78,9 +78,6 @@ public class Xexun2ProtocolDecoder extends OneToOneDecoder { "F:(\\d+.\\d+)V," + // Power ".*"); - //"111111120009,+436763737552,GPRMC,120009.590,A,4639.6774,N,01418.5737,E,0.00,0.00,111111" - //",,,A*68,F,, imei:012207000694569,04,481.2,F:4.15V,0,139,2689,232,03,2725,0576" - /** * Decode message */ diff --git a/test/org/traccar/protocol/xexun2/Xexun2ProtocolDecoderTest.java b/test/org/traccar/protocol/xexun2/Xexun2ProtocolDecoderTest.java index 6fa2a7430..75b132e98 100644 --- a/test/org/traccar/protocol/xexun2/Xexun2ProtocolDecoderTest.java +++ b/test/org/traccar/protocol/xexun2/Xexun2ProtocolDecoderTest.java @@ -33,7 +33,7 @@ public class Xexun2ProtocolDecoderTest { Xexun2ProtocolDecoder decoder = new Xexun2ProtocolDecoder(new TestDataManager(), 0); assertNotNull(decoder.decode(null, null, - "111111120009,+436763737552,GPRMC,120009.590,A,4639.6774,N,01418.5737,E,0.00,0.00,111111,,,A*68,F,, imei:012207000694569,04,481.2,F:4.15V,0,139,2689,232,03,2725,0576")); + "111111120009,+436763737552,GPRMC,120009.590,A,4639.6774,N,01418.5737,E,0.00,0.00,111111,,,A*68,F,, imei:359853000144328,04,481.2,F:4.15V,0,139,2689,232,03,2725,0576")); } |