From f6333c73fe8bdd0027bb16a715193e1587b934a2 Mon Sep 17 00:00:00 2001 From: Abyss777 Date: Mon, 25 Jul 2016 08:59:36 +0500 Subject: - Move lastPosition update to the end of pipeline - Optimize MotionEventHandler to do not use Device.motion - Remove Device.motion from model and database - some optimizations --- src/org/traccar/DefaultDataHandler.java | 1 - src/org/traccar/MainEventHandler.java | 7 +++++++ src/org/traccar/database/DeviceManager.java | 10 ++++++---- src/org/traccar/events/GeofenceEventHandler.java | 2 +- src/org/traccar/events/MotionEventHandler.java | 18 +++++++----------- src/org/traccar/events/OverspeedEventHandler.java | 5 ++--- src/org/traccar/model/Device.java | 13 ------------- 7 files changed, 23 insertions(+), 33 deletions(-) (limited to 'src') diff --git a/src/org/traccar/DefaultDataHandler.java b/src/org/traccar/DefaultDataHandler.java index 8923c3a0e..7194c6a77 100644 --- a/src/org/traccar/DefaultDataHandler.java +++ b/src/org/traccar/DefaultDataHandler.java @@ -25,7 +25,6 @@ public class DefaultDataHandler extends BaseDataHandler { try { Context.getDataManager().addPosition(position); - Context.getDeviceManager().updateLatestPosition(position); } catch (Exception error) { Log.warning(error); } diff --git a/src/org/traccar/MainEventHandler.java b/src/org/traccar/MainEventHandler.java index 771009aca..c01760283 100644 --- a/src/org/traccar/MainEventHandler.java +++ b/src/org/traccar/MainEventHandler.java @@ -26,6 +26,7 @@ import org.jboss.netty.handler.timeout.IdleStateEvent; import org.traccar.helper.Log; import org.traccar.model.Position; +import java.sql.SQLException; import java.text.SimpleDateFormat; public class MainEventHandler extends IdleStateAwareChannelHandler { @@ -36,6 +37,11 @@ public class MainEventHandler extends IdleStateAwareChannelHandler { if (e.getMessage() != null && e.getMessage() instanceof Position) { Position position = (Position) e.getMessage(); + try { + Context.getDeviceManager().updateLatestPosition(position); + } catch (SQLException error) { + Log.warning(error); + } String uniqueId = Context.getIdentityManager().getDeviceById(position.getDeviceId()).getUniqueId(); @@ -54,6 +60,7 @@ public class MainEventHandler extends IdleStateAwareChannelHandler { s.append(", result: ").append(cmdResult); } Log.info(s.toString()); + } } diff --git a/src/org/traccar/database/DeviceManager.java b/src/org/traccar/database/DeviceManager.java index 4dd7b41cb..5a0c87985 100644 --- a/src/org/traccar/database/DeviceManager.java +++ b/src/org/traccar/database/DeviceManager.java @@ -105,7 +105,6 @@ public class DeviceManager implements IdentityManager { } } device.setStatus(Device.STATUS_OFFLINE); - device.setMotion(Device.STATUS_STOPPED); } } for (Long cachedDeviceId : devicesById.keySet()) { @@ -176,7 +175,6 @@ public class DeviceManager implements IdentityManager { if (devicesById.containsKey(device.getId())) { Device cachedDevice = devicesById.get(device.getId()); cachedDevice.setStatus(device.getStatus()); - cachedDevice.setMotion(device.getMotion()); } } @@ -191,10 +189,14 @@ public class DeviceManager implements IdentityManager { positions.remove(deviceId); } + public boolean isLatestPosition(Position position) { + Position lastPosition = getLastPosition(position.getDeviceId()); + return lastPosition == null || position.getFixTime().compareTo(lastPosition.getFixTime()) > 0; + } + public void updateLatestPosition(Position position) throws SQLException { - Position lastPosition = getLastPosition(position.getDeviceId()); - if (lastPosition == null || position.getFixTime().compareTo(lastPosition.getFixTime()) > 0) { + if (isLatestPosition(position)) { dataManager.updateLatestPosition(position); diff --git a/src/org/traccar/events/GeofenceEventHandler.java b/src/org/traccar/events/GeofenceEventHandler.java index 1ea7aee41..a9bed6f82 100644 --- a/src/org/traccar/events/GeofenceEventHandler.java +++ b/src/org/traccar/events/GeofenceEventHandler.java @@ -47,7 +47,7 @@ public class GeofenceEventHandler extends BaseEventHandler { if (device == null) { return null; } - if (position.getId() != device.getPositionId() || !position.getValid()) { + if (!Context.getDeviceManager().isLatestPosition(position) || !position.getValid()) { return null; } diff --git a/src/org/traccar/events/MotionEventHandler.java b/src/org/traccar/events/MotionEventHandler.java index 4a3d2f0f0..7957f3570 100644 --- a/src/org/traccar/events/MotionEventHandler.java +++ b/src/org/traccar/events/MotionEventHandler.java @@ -42,26 +42,22 @@ public class MotionEventHandler extends BaseEventHandler { if (device == null) { return null; } - if (position.getId() != device.getPositionId() || !position.getValid()) { + if (!Context.getDeviceManager().isLatestPosition(position) || !position.getValid()) { return null; } Collection result = null; double speed = position.getSpeed(); - boolean valid = position.getValid(); - String motion = device.getMotion(); - if (motion == null) { - motion = Device.STATUS_STOPPED; + double oldSpeed = 0; + Position lastPosition = Context.getDeviceManager().getLastPosition(position.getDeviceId()); + if (lastPosition != null) { + oldSpeed = lastPosition.getSpeed(); } try { - if (valid && speed > SPEED_THRESHOLD && !motion.equals(Device.STATUS_MOVING)) { - device.setMotion(Device.STATUS_MOVING); - Context.getDeviceManager().updateDeviceStatus(device); + if (speed > SPEED_THRESHOLD && oldSpeed <= SPEED_THRESHOLD) { result = new ArrayList<>(); result.add(new Event(Event.TYPE_DEVICE_MOVING, position.getDeviceId(), position.getId())); - } else if (valid && speed < SPEED_THRESHOLD && motion.equals(Device.STATUS_MOVING)) { - device.setMotion(Device.STATUS_STOPPED); - Context.getDeviceManager().updateDeviceStatus(device); + } else if (speed <= SPEED_THRESHOLD && oldSpeed > SPEED_THRESHOLD) { result = new ArrayList<>(); result.add(new Event(Event.TYPE_DEVICE_STOPPED, position.getDeviceId(), position.getId())); } diff --git a/src/org/traccar/events/OverspeedEventHandler.java b/src/org/traccar/events/OverspeedEventHandler.java index fd005e170..6c9b523f4 100644 --- a/src/org/traccar/events/OverspeedEventHandler.java +++ b/src/org/traccar/events/OverspeedEventHandler.java @@ -44,15 +44,14 @@ public class OverspeedEventHandler extends BaseEventHandler { if (device == null) { return null; } - if (position.getId() != device.getPositionId() || !position.getValid()) { + if (!Context.getDeviceManager().isLatestPosition(position) || !position.getValid()) { return null; } Collection events = new ArrayList<>(); double speed = position.getSpeed(); - boolean valid = position.getValid(); - if (valid && globalSpeedLimit != 0 && speed > globalSpeedLimit) { + if (globalSpeedLimit != 0 && speed > globalSpeedLimit) { try { if (Context.getDataManager().getLastEvents( position.getDeviceId(), Event.TYPE_DEVICE_OVERSPEED, suppressRepeated).isEmpty()) { diff --git a/src/org/traccar/model/Device.java b/src/org/traccar/model/Device.java index 1669aee31..e90742836 100644 --- a/src/org/traccar/model/Device.java +++ b/src/org/traccar/model/Device.java @@ -92,19 +92,6 @@ public class Device extends Extensible { this.groupId = groupId; } - public static final String STATUS_MOVING = "moving"; - public static final String STATUS_STOPPED = "stopped"; - - private String motion; - - public String getMotion() { - return motion; - } - - public void setMotion(String motion) { - this.motion = motion; - } - private List geofenceIds; public List getGeofenceIds() { -- cgit v1.2.3 From 451c358879e1f6f2fda26e4b85ef97982fbeaba5 Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Mon, 25 Jul 2016 16:49:08 +1200 Subject: Improve history clearing logic --- debug.xml | 4 ++-- src/org/traccar/Main.java | 9 ++++----- src/org/traccar/database/DataManager.java | 27 +++++---------------------- 3 files changed, 11 insertions(+), 29 deletions(-) (limited to 'src') diff --git a/debug.xml b/debug.xml index 346eda292..304ae7f3d 100644 --- a/debug.xml +++ b/debug.xml @@ -323,8 +323,8 @@ DELETE FROM notifications WHERE id = :id; - - DELETE FROM positions WHERE id != :positionId and deviceid = :deviceId and servertime < :serverTime; + + DELETE FROM positions WHERE serverTime < :serverTime AND id NOT IN (SELECT positionId FROM devices); diff --git a/src/org/traccar/Main.java b/src/org/traccar/Main.java index e7e8d8ab9..e992691ad 100644 --- a/src/org/traccar/Main.java +++ b/src/org/traccar/Main.java @@ -23,8 +23,8 @@ import java.util.TimerTask; import java.util.Locale; public final class Main { - static final long CLEAN_DELAY = 0; - static final long CLEAN_PERIOD = 24 * 60 * 60 * 1000; + + private static final long CLEAN_PERIOD = 24 * 60 * 60 * 1000; private Main() { } @@ -40,8 +40,7 @@ public final class Main { Context.getWebServer().start(); } - Timer timer = new Timer(); - timer.scheduleAtFixedRate(new TimerTask() { + new Timer().scheduleAtFixedRate(new TimerTask() { @Override public void run() { try { @@ -50,7 +49,7 @@ public final class Main { Log.warning(error); } } - }, CLEAN_DELAY, CLEAN_PERIOD); + }, 0, CLEAN_PERIOD); Runtime.getRuntime().addShutdownHook(new Thread() { @Override diff --git a/src/org/traccar/database/DataManager.java b/src/org/traccar/database/DataManager.java index 04d0d44ea..78f1b4109 100644 --- a/src/org/traccar/database/DataManager.java +++ b/src/org/traccar/database/DataManager.java @@ -305,28 +305,11 @@ public class DataManager { } public void clearPositionsHistory() throws SQLException { - int histDays = config.getInteger("database.positionsHistoryDays"); - if (histDays == 0) { - return; - } - - String sql = getQuery("database.clearPositionsHistory"); - if (sql == null) { - return; - } - - for (Device device : getAllDevices()) { - Date lastUpdate = device.getLastUpdate(); - if (lastUpdate != null) { - - Date dateBefore = new Date(lastUpdate.getTime() - histDays * 24 * 3600 * 1000); - - QueryBuilder.create(dataSource, sql) - .setLong("positionId", device.getPositionId()) - .setLong("deviceId", device.getId()) - .setDate("serverTime", dateBefore) - .executeUpdate(); - } + int historyDays = config.getInteger("database.positionsHistoryDays"); + if (historyDays != 0) { + QueryBuilder.create(dataSource, getQuery("database.deletePositions")) + .setDate("serverTime", new Date(System.currentTimeMillis() - historyDays * 24 * 3600 * 1000)) + .executeUpdate(); } } -- cgit v1.2.3 From 8c4eb2eff9da9bf0538e413444c1e6dae981b99e Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Mon, 25 Jul 2016 23:12:41 +1200 Subject: Fix H02 ignition decoding --- src/org/traccar/protocol/H02ProtocolDecoder.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src') diff --git a/src/org/traccar/protocol/H02ProtocolDecoder.java b/src/org/traccar/protocol/H02ProtocolDecoder.java index 83ed4b099..bb18a68e2 100644 --- a/src/org/traccar/protocol/H02ProtocolDecoder.java +++ b/src/org/traccar/protocol/H02ProtocolDecoder.java @@ -80,7 +80,7 @@ public class H02ProtocolDecoder extends BaseProtocolDecoder { } } - position.set(Position.KEY_IGNITION, !BitUtil.check(status, 10)); + position.set(Position.KEY_IGNITION, BitUtil.check(status, 10)); position.set(Position.KEY_STATUS, status); } -- cgit v1.2.3 From 80847cc39a0570a91618c2d9da283767a99fd975 Mon Sep 17 00:00:00 2001 From: Abyss777 Date: Mon, 25 Jul 2016 16:28:16 +0500 Subject: Fix space --- src/org/traccar/events/MotionEventHandler.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src') diff --git a/src/org/traccar/events/MotionEventHandler.java b/src/org/traccar/events/MotionEventHandler.java index 7957f3570..4b652a727 100644 --- a/src/org/traccar/events/MotionEventHandler.java +++ b/src/org/traccar/events/MotionEventHandler.java @@ -28,7 +28,7 @@ import org.traccar.model.Position; public class MotionEventHandler extends BaseEventHandler { - private static final double SPEED_THRESHOLD = 0.01; + private static final double SPEED_THRESHOLD = 0.01; private int suppressRepeated; public MotionEventHandler() { -- cgit v1.2.3 From fb0a1689fda9a06ecf5e6a9a97ba8b2b704cacb9 Mon Sep 17 00:00:00 2001 From: Abyss777 Date: Mon, 25 Jul 2016 17:47:50 +0500 Subject: Added group and server attributes --- debug.xml | 7 +++--- schema/changelog-3.7.xml | 8 +++++++ setup/unix/traccar.xml | 7 +++--- setup/windows/traccar.xml | 7 +++--- src/org/traccar/model/Group.java | 12 +--------- src/org/traccar/model/Server.java | 12 +--------- swagger.json | 6 +++-- web/app/model/Group.js | 2 ++ web/app/model/Server.js | 2 ++ web/app/view/BaseEditDialog.js | 5 +++++ web/app/view/BaseEditDialogController.js | 14 ++++++++++++ web/app/view/DeviceDialog.js | 19 +++------------- web/app/view/DeviceDialogController.js | 38 -------------------------------- web/app/view/ServerDialog.js | 2 +- web/app/view/UserDialog.js | 13 ----------- web/app/view/UserDialogController.js | 20 +---------------- 16 files changed, 54 insertions(+), 120 deletions(-) delete mode 100644 web/app/view/DeviceDialogController.js (limited to 'src') diff --git a/debug.xml b/debug.xml index 095f7d4b2..f078a7e9d 100644 --- a/debug.xml +++ b/debug.xml @@ -102,7 +102,8 @@ latitude = :latitude, longitude = :longitude, zoom = :zoom, - twelveHourFormat = :twelveHourFormat + twelveHourFormat = :twelveHourFormat, + attributes = :attributes WHERE id = :id; @@ -190,11 +191,11 @@ - INSERT INTO groups (name, groupId) VALUES (:name, :groupId); + INSERT INTO groups (name, groupId, attributes) VALUES (:name, :groupId, :attributes); - UPDATE groups SET name = :name, groupId = :groupId WHERE id = :id; + UPDATE groups SET name = :name, groupId = :groupId, attributes = :attributes WHERE id = :id; diff --git a/schema/changelog-3.7.xml b/schema/changelog-3.7.xml index c988b8bdb..ae85f69f3 100644 --- a/schema/changelog-3.7.xml +++ b/schema/changelog-3.7.xml @@ -26,6 +26,14 @@ + + + + + + + + diff --git a/setup/unix/traccar.xml b/setup/unix/traccar.xml index 90cb5f74e..186e981cb 100644 --- a/setup/unix/traccar.xml +++ b/setup/unix/traccar.xml @@ -51,7 +51,8 @@ latitude = :latitude, longitude = :longitude, zoom = :zoom, - twelveHourFormat = :twelveHourFormat + twelveHourFormat = :twelveHourFormat, + attributes = :attributes WHERE id = :id; @@ -139,11 +140,11 @@ - INSERT INTO groups (name, groupId) VALUES (:name, :groupId); + INSERT INTO groups (name, groupId, attributes) VALUES (:name, :groupId, :attributes); - UPDATE groups SET name = :name, groupId = :groupId WHERE id = :id; + UPDATE groups SET name = :name, groupId = :groupId, attributes = :attributes WHERE id = :id; diff --git a/setup/windows/traccar.xml b/setup/windows/traccar.xml index 25128a3e0..aa3809763 100644 --- a/setup/windows/traccar.xml +++ b/setup/windows/traccar.xml @@ -51,7 +51,8 @@ latitude = :latitude, longitude = :longitude, zoom = :zoom, - twelveHourFormat = :twelveHourFormat + twelveHourFormat = :twelveHourFormat, + attributes = :attributes WHERE id = :id; @@ -139,11 +140,11 @@ - INSERT INTO groups (name, groupId) VALUES (:name, :groupId); + INSERT INTO groups (name, groupId, attributes) VALUES (:name, :groupId, :attributes); - UPDATE groups SET name = :name, groupId = :groupId WHERE id = :id; + UPDATE groups SET name = :name, groupId = :groupId, attributes = :attributes WHERE id = :id; diff --git a/src/org/traccar/model/Group.java b/src/org/traccar/model/Group.java index 00f2b2cfc..e70b3f3d5 100644 --- a/src/org/traccar/model/Group.java +++ b/src/org/traccar/model/Group.java @@ -15,17 +15,7 @@ */ package org.traccar.model; -public class Group { - - private long id; - - public long getId() { - return id; - } - - public void setId(long id) { - this.id = id; - } +public class Group extends Extensible { private String name; diff --git a/src/org/traccar/model/Server.java b/src/org/traccar/model/Server.java index 270fd61fa..b1557bf8f 100644 --- a/src/org/traccar/model/Server.java +++ b/src/org/traccar/model/Server.java @@ -15,17 +15,7 @@ */ package org.traccar.model; -public class Server { - - private long id; - - public long getId() { - return id; - } - - public void setId(long id) { - this.id = id; - } +public class Server extends Extensible { private boolean registration; diff --git a/swagger.json b/swagger.json index a16794a41..8b4157917 100644 --- a/swagger.json +++ b/swagger.json @@ -1235,7 +1235,8 @@ }, "twelveHourFormat": { "type": "boolean" - } + }, + "attributes": {} } }, "Command": { @@ -1286,7 +1287,8 @@ }, "groupId": { "type": "integer" - } + }, + "attributes": {} } }, "DevicePermission": { diff --git a/web/app/model/Group.js b/web/app/model/Group.js index a28897feb..bb18b5b3f 100644 --- a/web/app/model/Group.js +++ b/web/app/model/Group.js @@ -27,5 +27,7 @@ Ext.define('Traccar.model.Group', { }, { name: 'groupId', type: 'int' + }, { + name: 'attributes' }] }); diff --git a/web/app/model/Server.js b/web/app/model/Server.js index 3f6e466d9..02968c2d6 100644 --- a/web/app/model/Server.js +++ b/web/app/model/Server.js @@ -54,6 +54,8 @@ Ext.define('Traccar.model.Server', { }, { name: 'twelveHourFormat', type: 'boolean' + }, { + name: 'attributes' }], proxy: { diff --git a/web/app/view/BaseEditDialog.js b/web/app/view/BaseEditDialog.js index 051dfbe93..1af095c98 100644 --- a/web/app/view/BaseEditDialog.js +++ b/web/app/view/BaseEditDialog.js @@ -18,6 +18,11 @@ Ext.define('Traccar.view.BaseEditDialog', { extend: 'Traccar.view.BaseDialog', buttons: [{ + text: Strings.sharedAttributes, + handler: 'showAttributesView' + }, { + xtype: 'tbfill' + }, { text: Strings.sharedSave, handler: 'onSaveClick' }, { diff --git a/web/app/view/BaseEditDialogController.js b/web/app/view/BaseEditDialogController.js index 33bd01bd8..511309650 100644 --- a/web/app/view/BaseEditDialogController.js +++ b/web/app/view/BaseEditDialogController.js @@ -38,5 +38,19 @@ Ext.define('Traccar.view.BaseEditDialogController', { record.save(); } button.up('window').close(); + }, + + showAttributesView: function (button) { + var dialog, record; + dialog = button.up('window').down('form'); + record = dialog.getRecord(); + Ext.create('Traccar.view.BaseWindow', { + title: Strings.sharedAttributes, + modal: false, + items: { + xtype: 'attributesView', + record: record + } + }).show(); } }); diff --git a/web/app/view/DeviceDialog.js b/web/app/view/DeviceDialog.js index 2938d5dc3..e88618fc5 100644 --- a/web/app/view/DeviceDialog.js +++ b/web/app/view/DeviceDialog.js @@ -18,10 +18,10 @@ Ext.define('Traccar.view.DeviceDialog', { extend: 'Traccar.view.BaseEditDialog', requires: [ - 'Traccar.view.DeviceDialogController' + 'Traccar.view.BaseEditDialog' ], - controller: 'deviceDialog', + controller: 'baseEditDialog', title: Strings.deviceDialog, items: { @@ -45,18 +45,5 @@ Ext.define('Traccar.view.DeviceDialog', { displayField: 'name', valueField: 'id' }] - }, - - buttons: [{ - text : Strings.sharedAttributes, - handler: 'showAttributesView' - }, { - xtype: 'tbfill' - }, { - text: Strings.sharedSave, - handler: 'onSaveClick' - }, { - text: Strings.sharedCancel, - handler: 'closeView' - }] + } }); diff --git a/web/app/view/DeviceDialogController.js b/web/app/view/DeviceDialogController.js deleted file mode 100644 index 0a0f86883..000000000 --- a/web/app/view/DeviceDialogController.js +++ /dev/null @@ -1,38 +0,0 @@ -/* - * Copyright 2016 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. - */ - -Ext.define('Traccar.view.DeviceDialogController', { - extend: 'Traccar.view.BaseEditDialogController', - alias: 'controller.deviceDialog', - - requires: [ - 'Traccar.view.Attributes' - ], - - showAttributesView: function (button) { - var dialog, record; - dialog = button.up('window').down('form'); - record = dialog.getRecord(); - Ext.create('Traccar.view.BaseWindow', { - title: Strings.sharedAttributes, - modal: false, - items: { - xtype: 'attributesView', - record: record - } - }).show(); - } -}); diff --git a/web/app/view/ServerDialog.js b/web/app/view/ServerDialog.js index 67f3a7ab2..1af000042 100644 --- a/web/app/view/ServerDialog.js +++ b/web/app/view/ServerDialog.js @@ -31,7 +31,7 @@ Ext.define('Traccar.view.ServerDialog', { name: 'registration', fieldLabel: Strings.serverRegistration, allowBlank: false - },{ + }, { xtype: 'checkboxfield', name: 'readonly', fieldLabel: Strings.serverReadonly, diff --git a/web/app/view/UserDialog.js b/web/app/view/UserDialog.js index 378cc0681..df8a26e7e 100644 --- a/web/app/view/UserDialog.js +++ b/web/app/view/UserDialog.js @@ -89,18 +89,5 @@ Ext.define('Traccar.view.UserDialog', { fieldLabel: Strings.settingsTwelveHourFormat, allowBlank: false }] - }], - - buttons: [{ - text : Strings.sharedAttributes, - handler: 'showAttributesView' - }, { - xtype: 'tbfill' - }, { - text: Strings.sharedSave, - handler: 'onSaveClick' - }, { - text: Strings.sharedCancel, - handler: 'closeView' }] }); diff --git a/web/app/view/UserDialogController.js b/web/app/view/UserDialogController.js index 49dfd9d70..c3a4ca62d 100644 --- a/web/app/view/UserDialogController.js +++ b/web/app/view/UserDialogController.js @@ -15,13 +15,9 @@ */ Ext.define('Traccar.view.UserDialogController', { - extend: 'Ext.app.ViewController', + extend: 'Traccar.view.BaseEditDialogController', alias: 'controller.userDialog', - requires: [ - 'Traccar.view.Attributes' - ], - init: function () { if (Traccar.app.getUser().get('admin')) { this.lookupReference('adminField').setDisabled(false); @@ -48,19 +44,5 @@ Ext.define('Traccar.view.UserDialogController', { }); } button.up('window').close(); - }, - - showAttributesView: function (button) { - var dialog, record; - dialog = button.up('window').down('form'); - record = dialog.getRecord(); - Ext.create('Traccar.view.BaseWindow', { - title: Strings.sharedAttributes, - modal: false, - items: { - xtype: 'attributesView', - record: record - } - }).show(); } }); -- cgit v1.2.3 From 2c48057bd628c523fbfad0559b3a1627daa46e47 Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Tue, 26 Jul 2016 01:39:50 +1200 Subject: Fix HuaSheng decoder response --- src/org/traccar/protocol/HuaShengProtocolDecoder.java | 10 ++++------ test/org/traccar/protocol/HuaShengProtocolDecoderTest.java | 4 ++-- 2 files changed, 6 insertions(+), 8 deletions(-) (limited to 'src') diff --git a/src/org/traccar/protocol/HuaShengProtocolDecoder.java b/src/org/traccar/protocol/HuaShengProtocolDecoder.java index 6901f37fb..e7c83d3b3 100644 --- a/src/org/traccar/protocol/HuaShengProtocolDecoder.java +++ b/src/org/traccar/protocol/HuaShengProtocolDecoder.java @@ -38,7 +38,7 @@ public class HuaShengProtocolDecoder extends BaseProtocolDecoder { public static final int MSG_LOGIN = 0xAA02; public static final int MSG_LOGIN_RSP = 0xFF03; - private static void sendResponse(Channel channel, int type, ChannelBuffer content) { + private static void sendResponse(Channel channel, int type, int index, ChannelBuffer content) { if (channel != null) { ChannelBuffer response = ChannelBuffers.dynamicBuffer(); response.writeByte(0xC0); @@ -46,7 +46,7 @@ public class HuaShengProtocolDecoder extends BaseProtocolDecoder { response.writeShort(12 + content.readableBytes()); response.writeShort(type); response.writeShort(0); - response.writeInt(1); + response.writeInt(index); response.writeBytes(content); response.writeByte(0xC0); channel.write(response); @@ -80,7 +80,7 @@ public class HuaShengProtocolDecoder extends BaseProtocolDecoder { if (deviceSession != null && channel != null) { ChannelBuffer content = ChannelBuffers.dynamicBuffer(); content.writeByte(0); // success - sendResponse(channel, MSG_LOGIN_RSP, content); + sendResponse(channel, MSG_LOGIN_RSP, index, content); } } else { buf.skipBytes(length); @@ -127,9 +127,7 @@ public class HuaShengProtocolDecoder extends BaseProtocolDecoder { buf.skipBytes(length); } - ChannelBuffer content = ChannelBuffers.dynamicBuffer(); - content.writeInt(index); - sendResponse(channel, MSG_POSITION_RSP, content); + sendResponse(channel, MSG_POSITION_RSP, index, ChannelBuffers.dynamicBuffer()); return position; diff --git a/test/org/traccar/protocol/HuaShengProtocolDecoderTest.java b/test/org/traccar/protocol/HuaShengProtocolDecoderTest.java index 083397c77..7cb6b65a0 100644 --- a/test/org/traccar/protocol/HuaShengProtocolDecoderTest.java +++ b/test/org/traccar/protocol/HuaShengProtocolDecoderTest.java @@ -13,8 +13,8 @@ public class HuaShengProtocolDecoderTest extends ProtocolTest { verifyNothing(decoder, binary( "c000000077aa0200000000000e000100143347315f48312e315f56312e30372e54000300133335353835353035303434303635380004000b3531323030303000050005010006000400070004000800050000090018383936313032353431343533333239313833360d000a000f796573696e7465726e6574c0")); - //verifyPosition(decoder, binary( - // "c00000003faa0000000000003ba5a5005a3f00dbdc00000031363037303530373132353700e6d186ffcc7a25002201160010000000010015000000000000000000c0")); + verifyPosition(decoder, binary( + "c00000004baa00000000000005c400000131363037303630323537303800e6c82effcc7cb0003900a30089000000010015000000000000000000f20559ff577ce3980005000a060500000087c0")); verifyNothing(decoder, binary( "c0010c003e0002000000000010020012a0014f42445f3347315f56312e302e330013a0043335353835353035303434303635380006a08701000006a0a1035fc0")); -- cgit v1.2.3 From 60c442ac19776f0e07fafd1db8a0b75f971fd40e Mon Sep 17 00:00:00 2001 From: Abyss777 Date: Tue, 26 Jul 2016 14:50:32 +0500 Subject: Overspeed handler enhanced to use attributes --- debug.xml | 2 +- src/org/traccar/events/OverspeedEventHandler.java | 41 +++++++++++++++++++++-- 2 files changed, 39 insertions(+), 4 deletions(-) (limited to 'src') diff --git a/debug.xml b/debug.xml index f078a7e9d..bd55b7ee1 100644 --- a/debug.xml +++ b/debug.xml @@ -44,7 +44,7 @@ true 60 true - 90 + true true true true diff --git a/src/org/traccar/events/OverspeedEventHandler.java b/src/org/traccar/events/OverspeedEventHandler.java index 6c9b523f4..b2b1aeb5f 100644 --- a/src/org/traccar/events/OverspeedEventHandler.java +++ b/src/org/traccar/events/OverspeedEventHandler.java @@ -24,16 +24,19 @@ import org.traccar.Context; import org.traccar.model.Device; import org.traccar.model.Event; import org.traccar.model.Position; +import org.traccar.model.Server; import org.traccar.helper.Log; import org.traccar.helper.UnitsConverter; public class OverspeedEventHandler extends BaseEventHandler { - private double globalSpeedLimit; + public static final String ATTRIBUTE_SPEED_LIMIT = "speedLimit"; + + private boolean checkGroups; private int suppressRepeated; public OverspeedEventHandler() { - globalSpeedLimit = UnitsConverter.knotsFromKph(Context.getConfig().getInteger("event.globalSpeedLimit", 0)); + checkGroups = Context.getConfig().getBoolean("event.overspeed.groupsEnabled"); suppressRepeated = Context.getConfig().getInteger("event.suppressRepeated", 60); } @@ -50,8 +53,40 @@ public class OverspeedEventHandler extends BaseEventHandler { Collection events = new ArrayList<>(); double speed = position.getSpeed(); + Double speedLimit = new Double(0); - if (globalSpeedLimit != 0 && speed > globalSpeedLimit) { + if (device.getAttributes().containsKey(ATTRIBUTE_SPEED_LIMIT)) { + speedLimit = Double.parseDouble((String) device.getAttributes().get(ATTRIBUTE_SPEED_LIMIT)); + } + if (speedLimit == 0 && checkGroups) { + long groupId = device.getGroupId(); + while (groupId != 0) { + if (Context.getDeviceManager().getGroupById(groupId).getAttributes() + .containsKey(ATTRIBUTE_SPEED_LIMIT)) { + speedLimit = Double.parseDouble((String) Context.getDeviceManager().getGroupById(groupId) + .getAttributes().get(ATTRIBUTE_SPEED_LIMIT)); + if (speedLimit != 0) { + break; + } + } + if (Context.getDeviceManager().getGroupById(groupId) != null) { + groupId = Context.getDeviceManager().getGroupById(groupId).getGroupId(); + } else { + groupId = 0; + } + } + } + if (speedLimit == 0) { + try { + Server server = Context.getDataManager().getServer(); + if (server.getAttributes().containsKey(ATTRIBUTE_SPEED_LIMIT)) { + speedLimit = Double.parseDouble((String) server.getAttributes().get(ATTRIBUTE_SPEED_LIMIT)); + } + } catch (SQLException error) { + Log.warning(error); + } + } + if (speedLimit != 0 && speed > UnitsConverter.knotsFromKph(speedLimit)) { try { if (Context.getDataManager().getLastEvents( position.getDeviceId(), Event.TYPE_DEVICE_OVERSPEED, suppressRepeated).isEmpty()) { -- cgit v1.2.3 From 387b774ae1b87db6b722e50576eb0b814501b101 Mon Sep 17 00:00:00 2001 From: Abyss777 Date: Tue, 26 Jul 2016 15:26:27 +0500 Subject: Use primitive --- src/org/traccar/events/OverspeedEventHandler.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src') diff --git a/src/org/traccar/events/OverspeedEventHandler.java b/src/org/traccar/events/OverspeedEventHandler.java index b2b1aeb5f..e488b9df2 100644 --- a/src/org/traccar/events/OverspeedEventHandler.java +++ b/src/org/traccar/events/OverspeedEventHandler.java @@ -53,7 +53,7 @@ public class OverspeedEventHandler extends BaseEventHandler { Collection events = new ArrayList<>(); double speed = position.getSpeed(); - Double speedLimit = new Double(0); + double speedLimit = 0; if (device.getAttributes().containsKey(ATTRIBUTE_SPEED_LIMIT)) { speedLimit = Double.parseDouble((String) device.getAttributes().get(ATTRIBUTE_SPEED_LIMIT)); -- cgit v1.2.3 From 457bda5a0e8501dfae1d3dd4e0d39f778ee4d9da Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Wed, 27 Jul 2016 13:58:37 +1200 Subject: Update custom T55 protocol format --- src/org/traccar/protocol/T55ProtocolDecoder.java | 11 +++++++++-- test/org/traccar/protocol/T55ProtocolDecoderTest.java | 2 +- 2 files changed, 10 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/org/traccar/protocol/T55ProtocolDecoder.java b/src/org/traccar/protocol/T55ProtocolDecoder.java index 34efc376f..1909d9bea 100644 --- a/src/org/traccar/protocol/T55ProtocolDecoder.java +++ b/src/org/traccar/protocol/T55ProtocolDecoder.java @@ -50,7 +50,9 @@ public class T55ProtocolDecoder extends BaseProtocolDecoder { .expression("[^,]+") .number(",(d+)") // satellites .number(",(d+)") // imei - .number(",(d+)").optional(3) + .number(",([01])") // ignition + .number(",(d+)") // fuel + .number(",(d+)").optional(5) // battery .any() .compile(); @@ -123,13 +125,18 @@ public class T55ProtocolDecoder extends BaseProtocolDecoder { dateBuilder.setDateReverse(parser.nextInt(), parser.nextInt(), parser.nextInt()); position.setTime(dateBuilder.getDate()); - if (parser.hasNext(3)) { + if (parser.hasNext(5)) { position.set(Position.KEY_SATELLITES, parser.next()); + deviceSession = getDeviceSession(channel, remoteAddress, parser.next()); if (deviceSession == null) { return null; } position.setDeviceId(deviceSession.getDeviceId()); + + position.set(Position.KEY_IGNITION, parser.hasNext() && parser.next().equals("1")); + position.set(Position.KEY_FUEL, parser.nextInt()); + position.set(Position.KEY_BATTERY, parser.nextInt()); } if (deviceSession != null) { diff --git a/test/org/traccar/protocol/T55ProtocolDecoderTest.java b/test/org/traccar/protocol/T55ProtocolDecoderTest.java index 753b4f689..4cc5f549a 100644 --- a/test/org/traccar/protocol/T55ProtocolDecoderTest.java +++ b/test/org/traccar/protocol/T55ProtocolDecoderTest.java @@ -23,7 +23,7 @@ public class T55ProtocolDecoderTest extends ProtocolTest { "4711/022789000688081/$GPRMC,133343,A,5308.56325,N,1029.12850,E,0.000000,0.000000,290316,,*2A")); verifyPosition(decoder, text( - "$GPRMC,073501.000,A,1255.5125,N,07738.2948,E,0.00,0.53,080316,,,D*73,12,865733027593268,10011")); + "$GPRMC,073446.000,A,1255.5125,N,07738.2948,E,0.00,0.53,080316,D*71,11,865733027593268,1,090,086")); verifyNothing(decoder, text( "$GPFID,ID123456ABC")); -- cgit v1.2.3 From b00b35b33095110cef46c8a255686cad87ac7150 Mon Sep 17 00:00:00 2001 From: Abyss777 Date: Wed, 27 Jul 2016 11:31:39 +0500 Subject: Added option to rise event only once during overspeed period. --- debug.xml | 1 + src/org/traccar/events/OverspeedEventHandler.java | 13 +++++++++++-- 2 files changed, 12 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/debug.xml b/debug.xml index c08d46531..b33abc455 100644 --- a/debug.xml +++ b/debug.xml @@ -45,6 +45,7 @@ 60 true true + true true true true diff --git a/src/org/traccar/events/OverspeedEventHandler.java b/src/org/traccar/events/OverspeedEventHandler.java index e488b9df2..b61277548 100644 --- a/src/org/traccar/events/OverspeedEventHandler.java +++ b/src/org/traccar/events/OverspeedEventHandler.java @@ -33,10 +33,12 @@ public class OverspeedEventHandler extends BaseEventHandler { public static final String ATTRIBUTE_SPEED_LIMIT = "speedLimit"; private boolean checkGroups; + private boolean riseOnce; private int suppressRepeated; public OverspeedEventHandler() { checkGroups = Context.getConfig().getBoolean("event.overspeed.groupsEnabled"); + riseOnce = Context.getConfig().getBoolean("event.overspeed.riseOnce"); suppressRepeated = Context.getConfig().getInteger("event.suppressRepeated", 60); } @@ -86,7 +88,15 @@ public class OverspeedEventHandler extends BaseEventHandler { Log.warning(error); } } - if (speedLimit != 0 && speed > UnitsConverter.knotsFromKph(speedLimit)) { + double oldSpeed = 0; + if (riseOnce) { + Position lastPosition = Context.getDeviceManager().getLastPosition(position.getDeviceId()); + if (lastPosition != null) { + oldSpeed = lastPosition.getSpeed(); + } + } + speedLimit = UnitsConverter.knotsFromKph(speedLimit); + if (speedLimit != 0 && speed > speedLimit && oldSpeed <= speedLimit) { try { if (Context.getDataManager().getLastEvents( position.getDeviceId(), Event.TYPE_DEVICE_OVERSPEED, suppressRepeated).isEmpty()) { @@ -95,7 +105,6 @@ public class OverspeedEventHandler extends BaseEventHandler { } catch (SQLException error) { Log.warning(error); } - } return events; } -- cgit v1.2.3 From b1e7bd492b9f6739936e533b4ab16d4825f1ce3c Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Thu, 28 Jul 2016 14:35:02 +1200 Subject: Fix saving original raw binary data --- debug.xml | 2 +- src/org/traccar/ExtendedObjectDecoder.java | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/debug.xml b/debug.xml index f078a7e9d..fab27920e 100644 --- a/debug.xml +++ b/debug.xml @@ -80,7 +80,7 @@ true false - false + true ./schema/changelog-master.xml diff --git a/src/org/traccar/ExtendedObjectDecoder.java b/src/org/traccar/ExtendedObjectDecoder.java index d7ca30de4..1cfc93541 100644 --- a/src/org/traccar/ExtendedObjectDecoder.java +++ b/src/org/traccar/ExtendedObjectDecoder.java @@ -36,7 +36,8 @@ public abstract class ExtendedObjectDecoder implements ChannelUpstreamHandler { if (Context.getConfig().getBoolean("database.saveOriginal") && decodedMessage instanceof Position) { Position position = (Position) decodedMessage; if (originalMessage instanceof ChannelBuffer) { - position.set(Position.KEY_ORIGINAL, ChannelBuffers.hexDump((ChannelBuffer) originalMessage)); + ChannelBuffer buf = (ChannelBuffer) originalMessage; + position.set(Position.KEY_ORIGINAL, ChannelBuffers.hexDump(buf, 0, buf.writerIndex())); } else if (originalMessage instanceof String) { position.set(Position.KEY_ORIGINAL, DatatypeConverter.printHexBinary( ((String) originalMessage).getBytes(StandardCharsets.US_ASCII))); -- cgit v1.2.3 From f7e82c4d9a66b8ab1ba90d4cae690f9c798d03a7 Mon Sep 17 00:00:00 2001 From: Abyss777 Date: Thu, 28 Jul 2016 11:42:10 +0500 Subject: Removed suppressing repeated events during analyzing. --- debug.xml | 1 - src/org/traccar/events/GeofenceEventHandler.java | 37 +++++------------------ src/org/traccar/events/MotionEventHandler.java | 32 ++++---------------- src/org/traccar/events/OverspeedEventHandler.java | 11 +------ 4 files changed, 15 insertions(+), 66 deletions(-) (limited to 'src') diff --git a/debug.xml b/debug.xml index b33abc455..62e34d5d7 100644 --- a/debug.xml +++ b/debug.xml @@ -42,7 +42,6 @@ target/tracker-server.log true - 60 true true true diff --git a/src/org/traccar/events/GeofenceEventHandler.java b/src/org/traccar/events/GeofenceEventHandler.java index a9bed6f82..a0291dcfa 100644 --- a/src/org/traccar/events/GeofenceEventHandler.java +++ b/src/org/traccar/events/GeofenceEventHandler.java @@ -15,30 +15,23 @@ */ package org.traccar.events; -import java.sql.SQLException; import java.util.ArrayList; import java.util.Collection; import java.util.List; import org.traccar.BaseEventHandler; import org.traccar.Context; -import org.traccar.database.DataManager; import org.traccar.database.GeofenceManager; -import org.traccar.helper.Log; import org.traccar.model.Device; import org.traccar.model.Event; import org.traccar.model.Position; public class GeofenceEventHandler extends BaseEventHandler { - private int suppressRepeated; private GeofenceManager geofenceManager; - private DataManager dataManager; public GeofenceEventHandler() { - suppressRepeated = Context.getConfig().getInteger("event.suppressRepeated", 60); geofenceManager = Context.getGeofenceManager(); - dataManager = Context.getDataManager(); } @Override @@ -63,29 +56,15 @@ public class GeofenceEventHandler extends BaseEventHandler { device.setGeofenceIds(currentGeofences); Collection events = new ArrayList<>(); - try { - if (dataManager.getLastEvents(position.getDeviceId(), - Event.TYPE_GEOFENCE_ENTER, suppressRepeated).isEmpty()) { - for (long geofenceId : newGeofences) { - Event event = new Event(Event.TYPE_GEOFENCE_ENTER, position.getDeviceId(), position.getId()); - event.setGeofenceId(geofenceId); - events.add(event); - } - } - } catch (SQLException error) { - Log.warning(error); + for (long geofenceId : newGeofences) { + Event event = new Event(Event.TYPE_GEOFENCE_ENTER, position.getDeviceId(), position.getId()); + event.setGeofenceId(geofenceId); + events.add(event); } - try { - if (dataManager.getLastEvents(position.getDeviceId(), - Event.TYPE_GEOFENCE_EXIT, suppressRepeated).isEmpty()) { - for (long geofenceId : oldGeofences) { - Event event = new Event(Event.TYPE_GEOFENCE_EXIT, position.getDeviceId(), position.getId()); - event.setGeofenceId(geofenceId); - events.add(event); - } - } - } catch (SQLException error) { - Log.warning(error); + for (long geofenceId : oldGeofences) { + Event event = new Event(Event.TYPE_GEOFENCE_EXIT, position.getDeviceId(), position.getId()); + event.setGeofenceId(geofenceId); + events.add(event); } return events; } diff --git a/src/org/traccar/events/MotionEventHandler.java b/src/org/traccar/events/MotionEventHandler.java index 4b652a727..ddb99185f 100644 --- a/src/org/traccar/events/MotionEventHandler.java +++ b/src/org/traccar/events/MotionEventHandler.java @@ -15,13 +15,11 @@ */ package org.traccar.events; -import java.sql.SQLException; import java.util.ArrayList; import java.util.Collection; import org.traccar.BaseEventHandler; import org.traccar.Context; -import org.traccar.helper.Log; import org.traccar.model.Device; import org.traccar.model.Event; import org.traccar.model.Position; @@ -29,11 +27,6 @@ import org.traccar.model.Position; public class MotionEventHandler extends BaseEventHandler { private static final double SPEED_THRESHOLD = 0.01; - private int suppressRepeated; - - public MotionEventHandler() { - suppressRepeated = Context.getConfig().getInteger("event.suppressRepeated", 60); - } @Override protected Collection analyzePosition(Position position) { @@ -53,25 +46,12 @@ public class MotionEventHandler extends BaseEventHandler { if (lastPosition != null) { oldSpeed = lastPosition.getSpeed(); } - try { - if (speed > SPEED_THRESHOLD && oldSpeed <= SPEED_THRESHOLD) { - result = new ArrayList<>(); - result.add(new Event(Event.TYPE_DEVICE_MOVING, position.getDeviceId(), position.getId())); - } else if (speed <= SPEED_THRESHOLD && oldSpeed > SPEED_THRESHOLD) { - result = new ArrayList<>(); - result.add(new Event(Event.TYPE_DEVICE_STOPPED, position.getDeviceId(), position.getId())); - } - - if (result != null && !result.isEmpty()) { - for (Event event : result) { - if (!Context.getDataManager().getLastEvents(position.getDeviceId(), - event.getType(), suppressRepeated).isEmpty()) { - event = null; - } - } - } - } catch (SQLException error) { - Log.warning(error); + if (speed > SPEED_THRESHOLD && oldSpeed <= SPEED_THRESHOLD) { + result = new ArrayList<>(); + result.add(new Event(Event.TYPE_DEVICE_MOVING, position.getDeviceId(), position.getId())); + } else if (speed <= SPEED_THRESHOLD && oldSpeed > SPEED_THRESHOLD) { + result = new ArrayList<>(); + result.add(new Event(Event.TYPE_DEVICE_STOPPED, position.getDeviceId(), position.getId())); } return result; } diff --git a/src/org/traccar/events/OverspeedEventHandler.java b/src/org/traccar/events/OverspeedEventHandler.java index b61277548..0328b9109 100644 --- a/src/org/traccar/events/OverspeedEventHandler.java +++ b/src/org/traccar/events/OverspeedEventHandler.java @@ -34,12 +34,10 @@ public class OverspeedEventHandler extends BaseEventHandler { private boolean checkGroups; private boolean riseOnce; - private int suppressRepeated; public OverspeedEventHandler() { checkGroups = Context.getConfig().getBoolean("event.overspeed.groupsEnabled"); riseOnce = Context.getConfig().getBoolean("event.overspeed.riseOnce"); - suppressRepeated = Context.getConfig().getInteger("event.suppressRepeated", 60); } @Override @@ -97,14 +95,7 @@ public class OverspeedEventHandler extends BaseEventHandler { } speedLimit = UnitsConverter.knotsFromKph(speedLimit); if (speedLimit != 0 && speed > speedLimit && oldSpeed <= speedLimit) { - try { - if (Context.getDataManager().getLastEvents( - position.getDeviceId(), Event.TYPE_DEVICE_OVERSPEED, suppressRepeated).isEmpty()) { - events.add(new Event(Event.TYPE_DEVICE_OVERSPEED, position.getDeviceId(), position.getId())); - } - } catch (SQLException error) { - Log.warning(error); - } + events.add(new Event(Event.TYPE_DEVICE_OVERSPEED, position.getDeviceId(), position.getId())); } return events; } -- cgit v1.2.3 From 70f684945754b6bdadd2bf038ab955d93a95977d Mon Sep 17 00:00:00 2001 From: Abyss777 Date: Thu, 28 Jul 2016 13:44:05 +0500 Subject: Organized server cache Moved lookupAttribute function to DeviceManager --- debug.xml | 3 +- src/org/traccar/api/resource/ServerResource.java | 5 ++- src/org/traccar/database/DeviceManager.java | 32 ++++++++++++++++++ src/org/traccar/database/PermissionsManager.java | 11 ++++++- src/org/traccar/events/OverspeedEventHandler.java | 40 +++-------------------- 5 files changed, 51 insertions(+), 40 deletions(-) (limited to 'src') diff --git a/debug.xml b/debug.xml index 62e34d5d7..4c3b63d42 100644 --- a/debug.xml +++ b/debug.xml @@ -41,9 +41,10 @@ all target/tracker-server.log + true + true true - true true true true diff --git a/src/org/traccar/api/resource/ServerResource.java b/src/org/traccar/api/resource/ServerResource.java index 9e42687ab..0ca0d62aa 100644 --- a/src/org/traccar/api/resource/ServerResource.java +++ b/src/org/traccar/api/resource/ServerResource.java @@ -37,14 +37,13 @@ public class ServerResource extends BaseResource { @PermitAll @GET public Server get() throws SQLException { - return Context.getDataManager().getServer(); + return Context.getPermissionsManager().getServer(); } @PUT public Response update(Server entity) throws SQLException { Context.getPermissionsManager().checkAdmin(getUserId()); - Context.getDataManager().updateServer(entity); - Context.getPermissionsManager().refresh(); + Context.getPermissionsManager().updateServer(entity); return Response.ok(entity).build(); } diff --git a/src/org/traccar/database/DeviceManager.java b/src/org/traccar/database/DeviceManager.java index 5a0c87985..3476139f2 100644 --- a/src/org/traccar/database/DeviceManager.java +++ b/src/org/traccar/database/DeviceManager.java @@ -32,6 +32,7 @@ import org.traccar.helper.Log; import org.traccar.model.Device; import org.traccar.model.Group; import org.traccar.model.Position; +import org.traccar.model.Server; public class DeviceManager implements IdentityManager { @@ -40,6 +41,7 @@ public class DeviceManager implements IdentityManager { private final Config config; private final DataManager dataManager; private final long dataRefreshDelay; + private boolean lookupGroupsAttribute; private Map devicesById; private Map devicesByUniqueId; @@ -54,6 +56,7 @@ public class DeviceManager implements IdentityManager { this.dataManager = dataManager; this.config = Context.getConfig(); dataRefreshDelay = config.getLong("database.refreshDelay", DEFAULT_REFRESH_DELAY) * 1000; + lookupGroupsAttribute = config.getBoolean("deviceManager.lookupGroupsAttribute"); if (dataManager != null) { try { updateGroupCache(true); @@ -312,4 +315,33 @@ public class DeviceManager implements IdentityManager { dataManager.removeGroup(groupId); groupsById.remove(groupId); } + + public String lookupAttribute(long deviceId, String attributeName) { + String result = null; + Device device = getDeviceById(deviceId); + if (device != null) { + if (device.getAttributes().containsKey(attributeName)) { + result = (String) device.getAttributes().get(attributeName); + } + if (result == null && lookupGroupsAttribute) { + long groupId = device.getGroupId(); + while (groupId != 0) { + if (getGroupById(groupId) != null) { + result = (String) getGroupById(groupId).getAttributes().get(attributeName); + if (result != null) { + break; + } + groupId = getGroupById(groupId).getGroupId(); + } else { + groupId = 0; + } + } + } + if (result == null) { + Server server = Context.getPermissionsManager().getServer(); + result = (String) server.getAttributes().get(attributeName); + } + } + return result; + } } diff --git a/src/org/traccar/database/PermissionsManager.java b/src/org/traccar/database/PermissionsManager.java index d786dcc4e..92fcc3ebd 100644 --- a/src/org/traccar/database/PermissionsManager.java +++ b/src/org/traccar/database/PermissionsManager.java @@ -34,7 +34,7 @@ public class PermissionsManager { private final DataManager dataManager; - private Server server; + private volatile Server server; private final Map users = new HashMap<>(); @@ -153,4 +153,13 @@ public class PermissionsManager { } } + public Server getServer() { + return server; + } + + public void updateServer(Server server) throws SQLException { + dataManager.updateServer(server); + this.server = server; + } + } diff --git a/src/org/traccar/events/OverspeedEventHandler.java b/src/org/traccar/events/OverspeedEventHandler.java index 0328b9109..a1efbde48 100644 --- a/src/org/traccar/events/OverspeedEventHandler.java +++ b/src/org/traccar/events/OverspeedEventHandler.java @@ -15,7 +15,6 @@ */ package org.traccar.events; -import java.sql.SQLException; import java.util.ArrayList; import java.util.Collection; @@ -24,19 +23,15 @@ import org.traccar.Context; import org.traccar.model.Device; import org.traccar.model.Event; import org.traccar.model.Position; -import org.traccar.model.Server; -import org.traccar.helper.Log; import org.traccar.helper.UnitsConverter; public class OverspeedEventHandler extends BaseEventHandler { public static final String ATTRIBUTE_SPEED_LIMIT = "speedLimit"; - private boolean checkGroups; private boolean riseOnce; public OverspeedEventHandler() { - checkGroups = Context.getConfig().getBoolean("event.overspeed.groupsEnabled"); riseOnce = Context.getConfig().getBoolean("event.overspeed.riseOnce"); } @@ -54,37 +49,12 @@ public class OverspeedEventHandler extends BaseEventHandler { Collection events = new ArrayList<>(); double speed = position.getSpeed(); double speedLimit = 0; - - if (device.getAttributes().containsKey(ATTRIBUTE_SPEED_LIMIT)) { - speedLimit = Double.parseDouble((String) device.getAttributes().get(ATTRIBUTE_SPEED_LIMIT)); - } - if (speedLimit == 0 && checkGroups) { - long groupId = device.getGroupId(); - while (groupId != 0) { - if (Context.getDeviceManager().getGroupById(groupId).getAttributes() - .containsKey(ATTRIBUTE_SPEED_LIMIT)) { - speedLimit = Double.parseDouble((String) Context.getDeviceManager().getGroupById(groupId) - .getAttributes().get(ATTRIBUTE_SPEED_LIMIT)); - if (speedLimit != 0) { - break; - } - } - if (Context.getDeviceManager().getGroupById(groupId) != null) { - groupId = Context.getDeviceManager().getGroupById(groupId).getGroupId(); - } else { - groupId = 0; - } - } + String speedLimitAttribute = Context.getDeviceManager().lookupAttribute(device.getId(), ATTRIBUTE_SPEED_LIMIT); + if (speedLimitAttribute != null) { + speedLimit = Double.parseDouble(speedLimitAttribute); } if (speedLimit == 0) { - try { - Server server = Context.getDataManager().getServer(); - if (server.getAttributes().containsKey(ATTRIBUTE_SPEED_LIMIT)) { - speedLimit = Double.parseDouble((String) server.getAttributes().get(ATTRIBUTE_SPEED_LIMIT)); - } - } catch (SQLException error) { - Log.warning(error); - } + return null; } double oldSpeed = 0; if (riseOnce) { @@ -94,7 +64,7 @@ public class OverspeedEventHandler extends BaseEventHandler { } } speedLimit = UnitsConverter.knotsFromKph(speedLimit); - if (speedLimit != 0 && speed > speedLimit && oldSpeed <= speedLimit) { + if (speed > speedLimit && oldSpeed <= speedLimit) { events.add(new Event(Event.TYPE_DEVICE_OVERSPEED, position.getDeviceId(), position.getId())); } return events; -- cgit v1.2.3 From 6f89644fdcb3dbeb966cadbd810f9795922b8c4e Mon Sep 17 00:00:00 2001 From: Abyss777 Date: Thu, 28 Jul 2016 17:57:24 +0500 Subject: Rename parameter --- debug.xml | 2 +- src/org/traccar/events/OverspeedEventHandler.java | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) (limited to 'src') diff --git a/debug.xml b/debug.xml index 4c3b63d42..223378eac 100644 --- a/debug.xml +++ b/debug.xml @@ -45,7 +45,7 @@ true true - true + true true true true diff --git a/src/org/traccar/events/OverspeedEventHandler.java b/src/org/traccar/events/OverspeedEventHandler.java index a1efbde48..dbdb01ffb 100644 --- a/src/org/traccar/events/OverspeedEventHandler.java +++ b/src/org/traccar/events/OverspeedEventHandler.java @@ -29,10 +29,10 @@ public class OverspeedEventHandler extends BaseEventHandler { public static final String ATTRIBUTE_SPEED_LIMIT = "speedLimit"; - private boolean riseOnce; + private boolean notRepeat; public OverspeedEventHandler() { - riseOnce = Context.getConfig().getBoolean("event.overspeed.riseOnce"); + notRepeat = Context.getConfig().getBoolean("event.overspeed.notRepeat"); } @Override @@ -57,7 +57,7 @@ public class OverspeedEventHandler extends BaseEventHandler { return null; } double oldSpeed = 0; - if (riseOnce) { + if (notRepeat) { Position lastPosition = Context.getDeviceManager().getLastPosition(position.getDeviceId()); if (lastPosition != null) { oldSpeed = lastPosition.getSpeed(); -- cgit v1.2.3 From 4315f0dc46e0c09e945658a6c47ce45ba576641d Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Fri, 29 Jul 2016 10:58:22 +1200 Subject: Include content length in OsmAnd response --- src/org/traccar/protocol/OsmAndProtocolDecoder.java | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/org/traccar/protocol/OsmAndProtocolDecoder.java b/src/org/traccar/protocol/OsmAndProtocolDecoder.java index f46511b27..5d59c3b41 100644 --- a/src/org/traccar/protocol/OsmAndProtocolDecoder.java +++ b/src/org/traccar/protocol/OsmAndProtocolDecoder.java @@ -15,9 +15,11 @@ */ package org.traccar.protocol; +import org.eclipse.jetty.http.HttpHeader; import org.jboss.netty.channel.Channel; import org.jboss.netty.handler.codec.http.DefaultHttpResponse; import org.jboss.netty.handler.codec.http.HttpRequest; +import org.jboss.netty.handler.codec.http.HttpResponse; import org.jboss.netty.handler.codec.http.HttpResponseStatus; import org.jboss.netty.handler.codec.http.HttpVersion; import org.jboss.netty.handler.codec.http.QueryStringDecoder; @@ -40,6 +42,14 @@ public class OsmAndProtocolDecoder extends BaseProtocolDecoder { super(protocol); } + private void sendResponse(Channel channel, HttpResponseStatus status) { + if (channel != null) { + HttpResponse response = new DefaultHttpResponse(HttpVersion.HTTP_1_1, status); + response.headers().add(HttpHeader.CONTENT_LENGTH.asString(), 0); + channel.write(response); + } + } + @Override protected Object decode( Channel channel, SocketAddress remoteAddress, Object msg) throws Exception { @@ -64,8 +74,7 @@ public class OsmAndProtocolDecoder extends BaseProtocolDecoder { DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, value); if (deviceSession == null) { if (channel != null) { - channel.write( - new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.BAD_REQUEST)); + sendResponse(channel, HttpResponseStatus.BAD_REQUEST); } return null; } @@ -124,7 +133,7 @@ public class OsmAndProtocolDecoder extends BaseProtocolDecoder { } if (channel != null) { - channel.write(new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK)); + sendResponse(channel, HttpResponseStatus.OK); } return position; -- cgit v1.2.3 From 88c95715221d78d834aef23a20be437eceee8006 Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Fri, 29 Jul 2016 12:16:09 +1200 Subject: Replace Jetty import with Netty alternative --- src/org/traccar/protocol/OsmAndProtocolDecoder.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/org/traccar/protocol/OsmAndProtocolDecoder.java b/src/org/traccar/protocol/OsmAndProtocolDecoder.java index 5d59c3b41..f9f04d7e3 100644 --- a/src/org/traccar/protocol/OsmAndProtocolDecoder.java +++ b/src/org/traccar/protocol/OsmAndProtocolDecoder.java @@ -15,9 +15,9 @@ */ package org.traccar.protocol; -import org.eclipse.jetty.http.HttpHeader; import org.jboss.netty.channel.Channel; import org.jboss.netty.handler.codec.http.DefaultHttpResponse; +import org.jboss.netty.handler.codec.http.HttpHeaders; import org.jboss.netty.handler.codec.http.HttpRequest; import org.jboss.netty.handler.codec.http.HttpResponse; import org.jboss.netty.handler.codec.http.HttpResponseStatus; @@ -45,7 +45,7 @@ public class OsmAndProtocolDecoder extends BaseProtocolDecoder { private void sendResponse(Channel channel, HttpResponseStatus status) { if (channel != null) { HttpResponse response = new DefaultHttpResponse(HttpVersion.HTTP_1_1, status); - response.headers().add(HttpHeader.CONTENT_LENGTH.asString(), 0); + response.headers().add(HttpHeaders.Names.CONTENT_LENGTH, 0); channel.write(response); } } -- cgit v1.2.3 From e177e7d083a4dd3f64172802661bf658d26d1163 Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Mon, 1 Aug 2016 15:49:16 +1000 Subject: Check OsmAnd messages for device id --- src/org/traccar/protocol/OsmAndProtocolDecoder.java | 13 +++++++------ test/org/traccar/protocol/OsmAndProtocolDecoderTest.java | 3 +++ 2 files changed, 10 insertions(+), 6 deletions(-) (limited to 'src') diff --git a/src/org/traccar/protocol/OsmAndProtocolDecoder.java b/src/org/traccar/protocol/OsmAndProtocolDecoder.java index f9f04d7e3..c5884a4d0 100644 --- a/src/org/traccar/protocol/OsmAndProtocolDecoder.java +++ b/src/org/traccar/protocol/OsmAndProtocolDecoder.java @@ -73,9 +73,7 @@ public class OsmAndProtocolDecoder extends BaseProtocolDecoder { case "deviceid": DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, value); if (deviceSession == null) { - if (channel != null) { - sendResponse(channel, HttpResponseStatus.BAD_REQUEST); - } + sendResponse(channel, HttpResponseStatus.BAD_REQUEST); return null; } position.setDeviceId(deviceSession.getDeviceId()); @@ -132,11 +130,14 @@ public class OsmAndProtocolDecoder extends BaseProtocolDecoder { position.setTime(new Date()); } - if (channel != null) { + DeviceSession deviceSession = getDeviceSession(channel, remoteAddress); + if (deviceSession != null) { sendResponse(channel, HttpResponseStatus.OK); + return position; + } else { + sendResponse(channel, HttpResponseStatus.BAD_REQUEST); + return null; } - - return position; } } diff --git a/test/org/traccar/protocol/OsmAndProtocolDecoderTest.java b/test/org/traccar/protocol/OsmAndProtocolDecoderTest.java index d035ba7ef..6ec885058 100644 --- a/test/org/traccar/protocol/OsmAndProtocolDecoderTest.java +++ b/test/org/traccar/protocol/OsmAndProtocolDecoderTest.java @@ -10,6 +10,9 @@ public class OsmAndProtocolDecoderTest extends ProtocolTest { OsmAndProtocolDecoder decoder = new OsmAndProtocolDecoder(new OsmAndProtocol()); + verifyNothing(decoder, request( + "/?timestamp=1377177267&lat=60.0&lon=30.0")); + verifyPosition(decoder, request( "/?id=902064&lat=42.06288&lon=-88.23412×tamp=2016-01-27T18%3A55%3A47Z&hdop=6.0&altitude=224.0&speed=0.0")); -- cgit v1.2.3