From bf296b4af4714a3fe62cd6f9fcff8ac29fcd0178 Mon Sep 17 00:00:00 2001 From: Abyss777 Date: Thu, 2 Jun 2016 11:54:13 +0500 Subject: Send position with event to websocket --- src/org/traccar/BaseEventHandler.java | 2 +- src/org/traccar/api/AsyncSocket.java | 30 ++++++++++++++++++------- src/org/traccar/database/ConnectionManager.java | 8 +++---- 3 files changed, 27 insertions(+), 13 deletions(-) diff --git a/src/org/traccar/BaseEventHandler.java b/src/org/traccar/BaseEventHandler.java index fde57748c..3e3317f0a 100644 --- a/src/org/traccar/BaseEventHandler.java +++ b/src/org/traccar/BaseEventHandler.java @@ -25,7 +25,7 @@ public abstract class BaseEventHandler extends BaseDataHandler { Event event = analizePosition(position); if (event != null) { - Context.getConnectionManager().updateEvent(event); + Context.getConnectionManager().updateEvent(event, position); } return position; } diff --git a/src/org/traccar/api/AsyncSocket.java b/src/org/traccar/api/AsyncSocket.java index 2259f840f..76ec99406 100644 --- a/src/org/traccar/api/AsyncSocket.java +++ b/src/org/traccar/api/AsyncSocket.java @@ -28,6 +28,8 @@ import javax.json.Json; import javax.json.JsonObjectBuilder; import java.util.Collection; import java.util.Collections; +import java.util.HashMap; +import java.util.Map; public class AsyncSocket extends WebSocketAdapter implements ConnectionManager.UpdateListener { @@ -45,7 +47,9 @@ public class AsyncSocket extends WebSocketAdapter implements ConnectionManager.U public void onWebSocketConnect(Session session) { super.onWebSocketConnect(session); - sendData(KEY_POSITIONS, Context.getConnectionManager().getInitialState(userId)); + Map> data = new HashMap>(); + data.put(KEY_POSITIONS, Context.getConnectionManager().getInitialState(userId)); + sendData(data); Context.getConnectionManager().addListener(userId, this); } @@ -59,25 +63,35 @@ public class AsyncSocket extends WebSocketAdapter implements ConnectionManager.U @Override public void onUpdateDevice(Device device) { - sendData(KEY_DEVICES, Collections.singletonList(device)); + Map> data = new HashMap>(); + data.put(KEY_DEVICES, Collections.singletonList(device)); + sendData(data); } @Override public void onUpdatePosition(Position position) { - sendData(KEY_POSITIONS, Collections.singletonList(position)); + Map> data = new HashMap>(); + data.put(KEY_POSITIONS, Collections.singletonList(position)); + sendData(data); } @Override - public void onUpdateEvent(Event event) { - sendData(KEY_EVENTS, Collections.singletonList(event)); + public void onUpdateEvent(Event event, Position position) { + Map> data = new HashMap>(); + data.put(KEY_EVENTS, Collections.singletonList(event)); + if (position != null) { + data.put(KEY_POSITIONS, Collections.singletonList(position)); + } + sendData(data); } - private void sendData(String key, Collection data) { + private void sendData(Map> data) { if (!data.isEmpty() && isConnected()) { JsonObjectBuilder json = Json.createObjectBuilder(); - json.add(key, JsonConverter.arrayToJson(data)); + for (Map.Entry> entry : data.entrySet()) { + json.add(entry.getKey(), JsonConverter.arrayToJson(entry.getValue())); + } getRemote().sendString(json.build().toString(), null); } } - } diff --git a/src/org/traccar/database/ConnectionManager.java b/src/org/traccar/database/ConnectionManager.java index 9e0da8485..ec5903548 100644 --- a/src/org/traccar/database/ConnectionManager.java +++ b/src/org/traccar/database/ConnectionManager.java @@ -94,7 +94,7 @@ public class ConnectionManager { if (status.equals(Device.STATUS_ONLINE)) { event.setType(Event.TYPE_DEVICE_ONLINE); } - updateEvent(event); + updateEvent(event, null); } device.setStatus(status); @@ -147,7 +147,7 @@ public class ConnectionManager { } } - public synchronized void updateEvent(Event event) { + public synchronized void updateEvent(Event event, Position position) { long deviceId = event.getDeviceId(); try { Context.getDataManager().addEvent(event); @@ -157,7 +157,7 @@ public class ConnectionManager { for (long userId : Context.getPermissionsManager().getDeviceUsers(deviceId)) { if (listeners.containsKey(userId)) { for (UpdateListener listener : listeners.get(userId)) { - listener.onUpdateEvent(event); + listener.onUpdateEvent(event, position); } } } @@ -183,7 +183,7 @@ public class ConnectionManager { public interface UpdateListener { void onUpdateDevice(Device device); void onUpdatePosition(Position position); - void onUpdateEvent(Event event); + void onUpdateEvent(Event event, Position position); } public synchronized void addListener(long userId, UpdateListener listener) { -- cgit v1.2.3 From d9e57775e5bb10e722d0043186308df05eea3296 Mon Sep 17 00:00:00 2001 From: Abyss777 Date: Thu, 2 Jun 2016 12:45:22 +0500 Subject: Show events in toast --- web/app/Application.js | 6 ++++-- web/app/controller/Root.js | 31 ++++++++++++++++++++++++++++++- web/app/model/Event.js | 40 ++++++++++++++++++++++++++++++++++++++++ web/app/store/Events.js | 25 +++++++++++++++++++++++++ web/l10n/en.json | 10 +++++++++- 5 files changed, 108 insertions(+), 4 deletions(-) create mode 100644 web/app/model/Event.js create mode 100644 web/app/store/Events.js diff --git a/web/app/Application.js b/web/app/Application.js index 69ce8f891..e798a73e2 100644 --- a/web/app/Application.js +++ b/web/app/Application.js @@ -30,7 +30,8 @@ Ext.define('Traccar.Application', { 'Device', 'Position', 'Attribute', - 'Command' + 'Command', + 'Event' ], stores: [ @@ -47,7 +48,8 @@ Ext.define('Traccar.Application', { 'SpeedUnits', 'CommandTypes', 'TimeUnits', - 'Languages' + 'Languages', + 'Events' ], controllers: [ diff --git a/web/app/controller/Root.js b/web/app/controller/Root.js index bb65e0048..3e2358c8d 100644 --- a/web/app/controller/Root.js +++ b/web/app/controller/Root.js @@ -114,7 +114,7 @@ Ext.define('Traccar.controller.Root', { } } - if (data.positions) { + if (data.positions && !data.events) { array = data.positions; store = Ext.getStore('LatestPositions'); for (i = 0; i < array.length; i++) { @@ -126,6 +126,35 @@ Ext.define('Traccar.controller.Root', { } } } + + if (data.events) { + array = data.events; + store = Ext.getStore('Events'); + for (i = 0; i < array.length; i++) { + store.add(array[i]); + var text; + if (array[i].type === 'commandResult' && data.positions) { + var j; + for (j = 0; j < data.positions.length; j++) { + if (data.positions[j].id == array[i].positionId) { + text = data.positions[j].attributes.result; + break; + } + } + text = Strings.eventCommandResult + text; + } else { + var typeKey = 'event' + array[i].type.charAt(0).toUpperCase() + array[i].type.slice(1); + var text = Strings[typeKey]; + if (typeof text == "undefined") { + text = typeKey; + } + } + var device = Ext.getStore('Devices').getById(array[i].deviceId); + if (typeof device != "undefined") { + Ext.toast(text, device.getData().name); + } + } + } }; } }); diff --git a/web/app/model/Event.js b/web/app/model/Event.js new file mode 100644 index 000000000..4dd3ea7ff --- /dev/null +++ b/web/app/model/Event.js @@ -0,0 +1,40 @@ +/* + * Copyright 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. + */ + +Ext.define('Traccar.model.Event', { + extend: 'Ext.data.Model', + identifier: 'negative', + + fields: [{ + name: 'id', + type: 'int' + }, { + name: 'type', + type: 'string' + }, { + name: 'serverTime', + type: 'date', + dateFormat: 'c' + }, { + name: 'deviceId', + type: 'int' + }, { + name: 'positionId', + type: 'int' + }, { + name: 'attributes' + }] +}); diff --git a/web/app/store/Events.js b/web/app/store/Events.js new file mode 100644 index 000000000..f98764456 --- /dev/null +++ b/web/app/store/Events.js @@ -0,0 +1,25 @@ +/* + * Copyright 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. + */ + +Ext.define('Traccar.store.Events', { + extend: 'Ext.data.Store', + model: 'Traccar.model.Event', + + proxy: { + type: 'rest', + url: '/api/events' + } +}); \ No newline at end of file diff --git a/web/l10n/en.json b/web/l10n/en.json index 932cbfa78..f4bd253ea 100644 --- a/web/l10n/en.json +++ b/web/l10n/en.json @@ -97,5 +97,13 @@ "commandSosNumber": "Set SOS Number", "commandSilenceTime": "Set Silence Time", "commandSetPhonebook": "Set Phonebook", - "commandVoiceMessage": "Voice Message" + "commandVoiceMessage": "Voice Message", + "eventDeviceOnline": "Device is online", + "eventDeviceOffline": "Device is offline", + "eventDeviceMoving": "Device is moving", + "eventDeviceStopped": "Device is stopped", + "eventDeviceOverspeed": "Device exceeds the speed", + "eventCommandResult": "Command result: ", + "eventGeofenceEnter": "Device has entered geofence", + "eventGeofenceEnter": "Device has exited geofence" } \ No newline at end of file -- cgit v1.2.3 From 0271fa29703f24df24756c117209b9b04e127fc3 Mon Sep 17 00:00:00 2001 From: Abyss777 Date: Thu, 2 Jun 2016 17:11:18 +0500 Subject: Style fixes and optimization. --- src/org/traccar/api/AsyncSocket.java | 8 ++++---- web/app/controller/Root.js | 10 ++++------ web/app/store/Events.js | 2 +- 3 files changed, 9 insertions(+), 11 deletions(-) diff --git a/src/org/traccar/api/AsyncSocket.java b/src/org/traccar/api/AsyncSocket.java index 76ec99406..d1e5594f4 100644 --- a/src/org/traccar/api/AsyncSocket.java +++ b/src/org/traccar/api/AsyncSocket.java @@ -47,7 +47,7 @@ public class AsyncSocket extends WebSocketAdapter implements ConnectionManager.U public void onWebSocketConnect(Session session) { super.onWebSocketConnect(session); - Map> data = new HashMap>(); + Map> data = new HashMap<>(); data.put(KEY_POSITIONS, Context.getConnectionManager().getInitialState(userId)); sendData(data); @@ -63,21 +63,21 @@ public class AsyncSocket extends WebSocketAdapter implements ConnectionManager.U @Override public void onUpdateDevice(Device device) { - Map> data = new HashMap>(); + Map> data = new HashMap<>(); data.put(KEY_DEVICES, Collections.singletonList(device)); sendData(data); } @Override public void onUpdatePosition(Position position) { - Map> data = new HashMap>(); + Map> data = new HashMap<>(); data.put(KEY_POSITIONS, Collections.singletonList(position)); sendData(data); } @Override public void onUpdateEvent(Event event, Position position) { - Map> data = new HashMap>(); + Map> data = new HashMap<>(); data.put(KEY_EVENTS, Collections.singletonList(event)); if (position != null) { data.put(KEY_POSITIONS, Collections.singletonList(position)); diff --git a/web/app/controller/Root.js b/web/app/controller/Root.js index 3e2358c8d..991a2572c 100644 --- a/web/app/controller/Root.js +++ b/web/app/controller/Root.js @@ -94,7 +94,7 @@ Ext.define('Traccar.controller.Root', { }; socket.onmessage = function (event) { - var i, store, data, array, entity; + var i, j, store, data, array, entity, device, typeKey, text; data = Ext.decode(event.data); @@ -132,9 +132,7 @@ Ext.define('Traccar.controller.Root', { store = Ext.getStore('Events'); for (i = 0; i < array.length; i++) { store.add(array[i]); - var text; if (array[i].type === 'commandResult' && data.positions) { - var j; for (j = 0; j < data.positions.length; j++) { if (data.positions[j].id == array[i].positionId) { text = data.positions[j].attributes.result; @@ -143,13 +141,13 @@ Ext.define('Traccar.controller.Root', { } text = Strings.eventCommandResult + text; } else { - var typeKey = 'event' + array[i].type.charAt(0).toUpperCase() + array[i].type.slice(1); - var text = Strings[typeKey]; + typeKey = 'event' + array[i].type.charAt(0).toUpperCase() + array[i].type.slice(1); + text = Strings[typeKey]; if (typeof text == "undefined") { text = typeKey; } } - var device = Ext.getStore('Devices').getById(array[i].deviceId); + device = Ext.getStore('Devices').getById(array[i].deviceId); if (typeof device != "undefined") { Ext.toast(text, device.getData().name); } diff --git a/web/app/store/Events.js b/web/app/store/Events.js index f98764456..54b341d30 100644 --- a/web/app/store/Events.js +++ b/web/app/store/Events.js @@ -22,4 +22,4 @@ Ext.define('Traccar.store.Events', { type: 'rest', url: '/api/events' } -}); \ No newline at end of file +}); -- cgit v1.2.3