diff options
Diffstat (limited to 'web/app/controller/Root.js')
-rw-r--r-- | web/app/controller/Root.js | 183 |
1 files changed, 101 insertions, 82 deletions
diff --git a/web/app/controller/Root.js b/web/app/controller/Root.js index e30446c9..0cc2a148 100644 --- a/web/app/controller/Root.js +++ b/web/app/controller/Root.js @@ -75,7 +75,7 @@ Ext.define('Traccar.controller.Root', { }, loadApp: function () { - var attribution; + var attribution, eventId; Ext.getStore('Groups').load(); Ext.getStore('Geofences').load(); Ext.getStore('AttributeAliases').load(); @@ -94,6 +94,11 @@ Ext.define('Traccar.controller.Root', { } else { Ext.create('widget.main'); } + eventId = Ext.Object.fromQueryString(window.location.search).eventId; + if (eventId) { + this.fireEvent('showsingleevent', eventId); + this.removeUrlParameter('eventId'); + } }, beep: function () { @@ -108,103 +113,117 @@ Ext.define('Traccar.controller.Root', { return muteButton && !muteButton.pressed; }, + removeUrlParameter: function (param) { + var params = Ext.Object.fromQueryString(window.location.search); + delete params[param]; + if (Ext.Object.isEmpty(params)) { + window.history.pushState(null, null, window.location.pathname); + } else { + window.history.pushState(null, null, window.location.pathname + '?' + Ext.Object.toQueryString(params)); + } + }, + asyncUpdate: function (first) { - var protocol, pathname, socket, self = this; + var self = this, protocol, pathname, socket; protocol = window.location.protocol === 'https:' ? 'wss:' : 'ws:'; pathname = window.location.pathname.substring(0, window.location.pathname.lastIndexOf('/') + 1); socket = new WebSocket(protocol + '//' + window.location.host + pathname + 'api/socket'); socket.onclose = function (event) { - self.asyncUpdate(false); + Ext.toast(Strings.errorSocket, Strings.errorTitle, 'br'); + + Ext.Ajax.request({ + url: 'api/devices', + success: function(response) { + self.updateDevices(Ext.decode(response.responseText)); + } + }); + + Ext.Ajax.request({ + url: 'api/positions', + headers: { + Accept: 'application/json' + }, + success: function(response) { + self.updatePositions(Ext.decode(response.responseText)); + } + }); + + setTimeout(function() { + self.asyncUpdate(false); + }, Traccar.Style.reconnectTimeout); }; socket.onmessage = function (event) { - var i, j, store, data, array, entity, device, typeKey, alarmKey, text, geofence; - - data = Ext.decode(event.data); + var data = Ext.decode(event.data); if (data.devices) { - array = data.devices; - store = Ext.getStore('Devices'); - for (i = 0; i < array.length; i++) { - entity = store.getById(array[i].id); - if (entity) { - entity.set({ - status: array[i].status, - lastUpdate: array[i].lastUpdate - }, { - dirty: false - }); - } - } + self.updateDevices(data.devices); + } + if (data.positions) { + self.updatePositions(data.positions); } + if (data.events) { + self.updateEvents(data.events); + } + }; + }, - if (data.positions && !data.events) { - array = data.positions; - store = Ext.getStore('LatestPositions'); - for (i = 0; i < array.length; i++) { - entity = store.findRecord('deviceId', array[i].deviceId, 0, false, false, true); - if (entity) { - entity.set(array[i]); - } else { - store.add(Ext.create('Traccar.model.Position', array[i])); - } - } + updateDevices: function (array) { + var i, store, entity; + store = Ext.getStore('Devices'); + for (i = 0; i < array.length; i++) { + entity = store.getById(array[i].id); + if (entity) { + entity.set({ + status: array[i].status, + lastUpdate: array[i].lastUpdate + }, { + dirty: false + }); } + } + }, - if (data.events) { - array = data.events; - store = Ext.getStore('Events'); - for (i = 0; i < array.length; i++) { - store.add(array[i]); - if (array[i].type === 'commandResult' && data.positions) { - 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 if (array[i].type === 'alarm' && data.positions) { - alarmKey = 'alarm'; - text = Strings[alarmKey]; - if (!text) { - text = alarmKey; - } - for (j = 0; j < data.positions.length; j++) { - if (data.positions[j].id === array[i].positionId && data.positions[j].attributes.alarm !== null) { - if (typeof data.positions[j].attributes.alarm === 'string' && data.positions[j].attributes.alarm.length >= 2) { - alarmKey = 'alarm' + data.positions[j].attributes.alarm.charAt(0).toUpperCase() + data.positions[j].attributes.alarm.slice(1); - text = Strings[alarmKey]; - if (!text) { - text = alarmKey; - } - } - break; - } - } - } else { - typeKey = 'event' + array[i].type.charAt(0).toUpperCase() + array[i].type.slice(1); - text = Strings[typeKey]; - if (!text) { - text = typeKey; - } - } - if (array[i].geofenceId !== 0) { - geofence = Ext.getStore('Geofences').getById(array[i].geofenceId); - if (typeof geofence !== 'undefined') { - text += ' \"' + geofence.get('name') + '"'; - } - } - device = Ext.getStore('Devices').getById(array[i].deviceId); - if (typeof device !== 'undefined') { - if (self.mutePressed()) { - self.beep(); - } - Ext.toast(text, device.get('name')); - } + updatePositions: function (array) { + var i, store, data, entity; + store = Ext.getStore('LatestPositions'); + for (i = 0; i < array.length; i++) { + entity = store.findRecord('deviceId', array[i].deviceId, 0, false, false, true); + if (entity) { + entity.set(array[i]); + } else { + store.add(Ext.create('Traccar.model.Position', array[i])); + } + } + }, + + updateEvents: function (array) { + var i, store, device, alarmKey, text, geofence; + store = Ext.getStore('Events'); + for (i = 0; i < array.length; i++) { + store.add(array[i]); + if (array[i].type === 'commandResult') { + text = Strings.eventCommandResult + ': ' + array[i].attributes.result; + } else if (array[i].type === 'alarm') { + alarmKey = 'alarm' + array[i].attributes.alarm.charAt(0).toUpperCase() + array[i].attributes.alarm.slice(1); + text = Strings[alarmKey] || alarmKey; + } else { + text = Traccar.app.getEventString(array[i].type); + } + if (array[i].geofenceId !== 0) { + geofence = Ext.getStore('Geofences').getById(array[i].geofenceId); + if (geofence) { + text += ' \"' + geofence.get('name') + '"'; } } - }; + device = Ext.getStore('Devices').getById(array[i].deviceId); + if (device) { + if (this.mutePressed()) { + this.beep(); + } + Ext.toast(text, device.get('name'), 'br'); + } + } } }); |