From f538963be5fcee2280b22050c8a51475c3a984f8 Mon Sep 17 00:00:00 2001 From: Abyss777 Date: Tue, 6 Jun 2017 11:43:09 +0500 Subject: - Reworked fields and attributes formatting - Convert position and reports fields in the model - Add hidden columns to Route report - Add Number attributes to possible chart types --- web/app/AttributeFormatter.js | 70 +++++++++++++++++++++++++++++++++++++ web/app/controller/Root.js | 24 +++++++++++++ web/app/model/Position.js | 32 +++-------------- web/app/model/ReportSummary.js | 9 +++-- web/app/model/ReportTrip.js | 9 +++-- web/app/store/DistanceUnits.js | 9 +++++ web/app/store/PositionAttributes.js | 2 +- web/app/store/ReportChartTypes.js | 12 ++----- web/app/store/SpeedUnits.js | 9 +++++ web/app/view/ReportController.js | 18 ++++++++++ web/app/view/StateController.js | 9 +++-- web/app/view/dialog/ReportConfig.js | 2 +- web/app/view/edit/Attributes.js | 4 +-- web/l10n/en.json | 2 ++ 14 files changed, 162 insertions(+), 49 deletions(-) diff --git a/web/app/AttributeFormatter.js b/web/app/AttributeFormatter.js index 302a894..9d66794 100644 --- a/web/app/AttributeFormatter.js +++ b/web/app/AttributeFormatter.js @@ -26,6 +26,10 @@ Ext.define('Traccar.AttributeFormatter', { return Ext.getStore('SpeedUnits').formatValue(value, Traccar.app.getPreference('speedUnit')); }, + speedConverter: function (value) { + return Ext.getStore('SpeedUnits').convertValue(value, Traccar.app.getPreference('speedUnit')); + }, + courseFormatter: function (value) { var courseValues = ['N', 'NE', 'E', 'SE', 'S', 'SW', 'W', 'NW']; return courseValues[Math.floor(value / 45)]; @@ -35,26 +39,51 @@ Ext.define('Traccar.AttributeFormatter', { return Ext.getStore('DistanceUnits').formatValue(value, Traccar.app.getPreference('distanceUnit')); }, + distanceConverter: function (value) { + return Ext.getStore('DistanceUnits').convertValue(value, Traccar.app.getPreference('distanceUnit')); + }, + voltageFormatter: function (value) { return Number(value.toFixed(Traccar.Style.numberPrecision)) + ' ' + Strings.sharedVoltAbbreviation; }, + voltageConverter: function (value) { + return Number(value); + }, + percentageFormatter: function (value) { return Number(value.toFixed(Traccar.Style.numberPrecision)) + ' %'; }, + percentageConverter: function (value) { + return Number(value); + }, + temperatureFormatter: function (value) { return Number(value.toFixed(Traccar.Style.numberPrecision)) + ' °C'; }, + temperatureConverter: function (value) { + return Number(value); + }, + + volumeFormatter: function (value) { return Number(value.toFixed(Traccar.Style.numberPrecision)) + ' ' + Strings.sharedLiterAbbreviation; }, + volumeConverter: function (value) { + return Number(value); + }, + consumptionFormatter: function (value) { return Number(value.toFixed(Traccar.Style.numberPrecision)) + ' ' + Strings.sharedLiterPerHourAbbreviation; }, + consumptionConverter: function (value) { + return Number(value); + }, + hoursFormatter: function (value) { var hours = Math.round(value / 3600000); return (hours + ' ' + Strings.sharedHourAbbreviation); @@ -131,6 +160,18 @@ Ext.define('Traccar.AttributeFormatter', { } }, + getConverter: function (key) { + if (key === 'speed') { + return this.speedConverter; + } else if (key === 'distance' || key === 'accuracy') { + return this.distanceConverter; + } else { + return function (value) { + return value; + }; + } + }, + getAttributeFormatter: function (key) { var dataType = Ext.getStore('PositionAttributes').getAttributeDataType(key); if (!dataType) { @@ -154,5 +195,34 @@ Ext.define('Traccar.AttributeFormatter', { return this.defaultFormatter; } } + }, + + getAttributeConverter: function (key) { + var dataType = Ext.getStore('PositionAttributes').getAttributeDataType(key); + if (!dataType) { + return function (value) { + return value; + }; + } else { + if (dataType === 'distance') { + return this.distanceConverter; + } else if (dataType === 'speed') { + return this.speedConverter; + } else if (dataType === 'voltage') { + return this.voltageConverter; + } else if (dataType === 'percentage') { + return this.percentageConverter; + } else if (dataType === 'temperature') { + return this.temperatureConverter; + } else if (dataType === 'volume') { + return this.volumeConverter; + } else if (dataType === 'consumption') { + return this.consumptionConverter; + } else { + return function (value) { + return value; + }; + } + } } }); diff --git a/web/app/controller/Root.js b/web/app/controller/Root.js index 8d604b6..5d22a74 100644 --- a/web/app/controller/Root.js +++ b/web/app/controller/Root.js @@ -26,7 +26,31 @@ Ext.define('Traccar.controller.Root', { ], init: function () { + var i, data, attribute, chartTypesStore = Ext.getStore('ReportChartTypes'); Ext.state.Manager.setProvider(new Ext.state.CookieProvider()); + data = Ext.getStore('PositionAttributes').getData().items; + for (i = 0; i < data.length; i++) { + attribute = data[i]; + Traccar.model.Position.addFields([{ + name: 'attribute.' + attribute.get('key'), + attributeKey: attribute.get('key'), + calculate: this.calculateAttribute, + persist: false + }]); + if (attribute.get('valueType') === 'number') { + chartTypesStore.add({ + key: 'attribute.' + attribute.get('key'), + name: attribute.get('name') + }); + } + } + }, + + calculateAttribute: function (data) { + var value = data.attributes[this.attributeKey]; + if (value !== undefined) { + return Traccar.AttributeFormatter.getAttributeConverter(this.attributeKey)(value); + } }, onLaunch: function () { diff --git a/web/app/model/Position.js b/web/app/model/Position.js index 4b1a429..6db5465 100644 --- a/web/app/model/Position.js +++ b/web/app/model/Position.js @@ -45,7 +45,8 @@ Ext.define('Traccar.model.Position', { type: 'boolean' }, { name: 'accuracy', - type: 'float' + type: 'float', + convert: Traccar.AttributeFormatter.getConverter('accuracy') }, { name: 'latitude', type: 'float' @@ -57,7 +58,8 @@ Ext.define('Traccar.model.Position', { type: 'float' }, { name: 'speed', - type: 'float' + type: 'float', + convert: Traccar.AttributeFormatter.getConverter('speed') }, { name: 'course', type: 'float' @@ -66,31 +68,5 @@ Ext.define('Traccar.model.Position', { type: 'string' }, { name: 'attributes' - }, { - name: 'speedConverted', - type: 'float', - calculate: function (data) { - return Ext.getStore('SpeedUnits').convertValue( - data.speed, Traccar.app.getPreference('speedUnit')); - } - }, { - name: 'distanceConverted', - type: 'float', - calculate: function (data) { - return Ext.getStore('DistanceUnits').convertValue( - data.attributes.distance, Traccar.app.getPreference('distanceUnit')); - } - }, { - name: 'rpm', - type: 'float', - calculate: function (data) { - return data.attributes.rpm; - } - }, { - name: 'fuel', - type: 'float', - calculate: function (data) { - return data.attributes.fuel; - } }] }); diff --git a/web/app/model/ReportSummary.js b/web/app/model/ReportSummary.js index d236ecd..5cfbdcd 100644 --- a/web/app/model/ReportSummary.js +++ b/web/app/model/ReportSummary.js @@ -28,13 +28,16 @@ Ext.define('Traccar.model.ReportSummary', { type: 'string' }, { name: 'maxSpeed', - type: 'float' + type: 'float', + convert: Traccar.AttributeFormatter.getConverter('speed') }, { name: 'averageSpeed', - type: 'float' + type: 'float', + convert: Traccar.AttributeFormatter.getConverter('speed') }, { name: 'distance', - type: 'float' + type: 'float', + convert: Traccar.AttributeFormatter.getConverter('distance') }, { name: 'engineHours', type: 'int' diff --git a/web/app/model/ReportTrip.js b/web/app/model/ReportTrip.js index 51f9807..d243a8a 100644 --- a/web/app/model/ReportTrip.js +++ b/web/app/model/ReportTrip.js @@ -28,13 +28,16 @@ Ext.define('Traccar.model.ReportTrip', { type: 'string' }, { name: 'maxSpeed', - type: 'float' + type: 'float', + convert: Traccar.AttributeFormatter.getConverter('speed') }, { name: 'averageSpeed', - type: 'float' + type: 'float', + convert: Traccar.AttributeFormatter.getConverter('speed') }, { name: 'distance', - type: 'float' + type: 'float', + convert: Traccar.AttributeFormatter.getConverter('distance') }, { name: 'duration', type: 'int' diff --git a/web/app/store/DistanceUnits.js b/web/app/store/DistanceUnits.js index 514a051..189f79b 100644 --- a/web/app/store/DistanceUnits.js +++ b/web/app/store/DistanceUnits.js @@ -43,6 +43,15 @@ Ext.define('Traccar.store.DistanceUnits', { }, formatValue: function (value, unit) { + var model; + if (!unit) { + unit = 'km'; + } + model = this.findRecord('key', unit); + return value.toFixed(2) + ' ' + model.get('name'); + }, + + convertAndFormat: function (value, unit) { var model; if (!unit) { unit = 'km'; diff --git a/web/app/store/PositionAttributes.js b/web/app/store/PositionAttributes.js index 985c558..7215429 100644 --- a/web/app/store/PositionAttributes.js +++ b/web/app/store/PositionAttributes.js @@ -62,7 +62,7 @@ Ext.define('Traccar.store.PositionAttributes', { valueType: 'string' }, { key: 'alarm', - name: Strings.alarm, + name: Strings.positionAlarm, valueType: 'string' }, { key: 'status', diff --git a/web/app/store/ReportChartTypes.js b/web/app/store/ReportChartTypes.js index 889db50..8ca074f 100644 --- a/web/app/store/ReportChartTypes.js +++ b/web/app/store/ReportChartTypes.js @@ -21,16 +21,10 @@ Ext.define('Traccar.store.ReportChartTypes', { fields: ['key', 'name'], data: [{ - key: 'speedConverted', + key: 'speed', name: Strings.positionSpeed }, { - key: 'distanceConverted', - name: Strings.positionDistance - }, { - key: 'rpm', - name: Strings.positionRpm - }, { - key: 'fuel', - name: Strings.positionFuel + key: 'accuracy', + name: Strings.positionAccuracy }] }); diff --git a/web/app/store/SpeedUnits.js b/web/app/store/SpeedUnits.js index ce97695..fc2e573 100644 --- a/web/app/store/SpeedUnits.js +++ b/web/app/store/SpeedUnits.js @@ -43,6 +43,15 @@ Ext.define('Traccar.store.SpeedUnits', { }, formatValue: function (value, unit) { + var model; + if (!unit) { + unit = 'kn'; + } + model = this.findRecord('key', unit); + return value.toFixed(1) + ' ' + model.get('name'); + }, + + convertAndFormat: function (value, unit) { var model; if (!unit) { unit = 'kn'; diff --git a/web/app/view/ReportController.js b/web/app/view/ReportController.js index d83d998..8631b7c 100644 --- a/web/app/view/ReportController.js +++ b/web/app/view/ReportController.js @@ -64,6 +64,10 @@ Ext.define('Traccar.view.ReportController', { return this.getView().getComponent('chart'); }, + init: function () { + this.addAttributeColumns(); + }, + onConfigureClick: function () { var dialog = Ext.create('Traccar.view.dialog.ReportConfig'); dialog.lookupReference('eventTypeField').setHidden(this.lookupReference('reportTypeField').getValue() !== 'events'); @@ -383,6 +387,20 @@ Ext.define('Traccar.view.ReportController', { this.updateButtons(); }, + addAttributeColumns: function () { + var i, data, attribute; + data = Ext.getStore('PositionAttributes').getData().items; + for (i = 0; i < data.length; i++) { + attribute = data[i]; + this.routeColumns.push({ + text: attribute.get('name'), + dataIndex: 'attribute.' + attribute.get('key'), + renderer: Traccar.AttributeFormatter.getAttributeFormatter(attribute.get('key')), + hidden: true + }); + } + }, + routeColumns: [{ text: Strings.reportDeviceName, dataIndex: 'deviceId', diff --git a/web/app/view/StateController.js b/web/app/view/StateController.js index 0723dc1..4c99a8b 100644 --- a/web/app/view/StateController.js +++ b/web/app/view/StateController.js @@ -101,7 +101,7 @@ Ext.define('Traccar.view.StateController', { }, updatePosition: function () { - var attributes, store, key, aliasIndex, name; + var attributes, store, key, aliasIndex, name, value; store = Ext.getStore('Attributes'); store.removeAll(); @@ -126,11 +126,16 @@ Ext.define('Traccar.view.StateController', { } else { name = Ext.getStore('PositionAttributes').getAttributeName(key, true); } + if (this.position.get('attribute.' + key) !== undefined) { + value = Traccar.AttributeFormatter.getAttributeFormatter(key)(this.position.get('attribute.' + key)); + } else { + value = Traccar.AttributeFormatter.defaultFormatter(attributes[key]); + } store.add(Ext.create('Traccar.model.Attribute', { priority: 1024, name: name, attribute: key, - value: Traccar.AttributeFormatter.getAttributeFormatter(key)(attributes[key]) + value: value })); } } diff --git a/web/app/view/dialog/ReportConfig.js b/web/app/view/dialog/ReportConfig.js index b8493cb..836b605 100644 --- a/web/app/view/dialog/ReportConfig.js +++ b/web/app/view/dialog/ReportConfig.js @@ -61,7 +61,7 @@ Ext.define('Traccar.view.dialog.ReportConfig', { reference: 'chartTypeField', store: 'ReportChartTypes', hidden: true, - value: 'speedConverted', + value: 'speed', valueField: 'key', displayField: 'name', queryMode: 'local' diff --git a/web/app/view/edit/Attributes.js b/web/app/view/edit/Attributes.js index b080e56..0d16e1e 100644 --- a/web/app/view/edit/Attributes.js +++ b/web/app/view/edit/Attributes.js @@ -58,9 +58,9 @@ Ext.define('Traccar.view.edit.Attributes', { attribute = Ext.getStore(this.attributesStore).getById(record.get('name')); } if (attribute && attribute.get('dataType') === 'speed') { - return Ext.getStore('SpeedUnits').formatValue(value, Traccar.app.getPreference('speedUnit', 'kn')); + return Ext.getStore('SpeedUnits').convertAndFormat(value, Traccar.app.getPreference('speedUnit', 'kn')); } else if (attribute && attribute.get('dataType') === 'distance') { - return Ext.getStore('DistanceUnits').formatValue(value, Traccar.app.getPreference('distanceUnit', 'km')); + return Ext.getStore('DistanceUnits').convertAndFormat(value, Traccar.app.getPreference('distanceUnit', 'km')); } else { return value; } diff --git a/web/l10n/en.json b/web/l10n/en.json index 22a9cfd..30f66ba 100644 --- a/web/l10n/en.json +++ b/web/l10n/en.json @@ -164,6 +164,7 @@ "positionRssi": "RSSI", "positionGps": "GPS", "positionEvent": "Event", + "positionAlarm": "Alarm", "positionStatus": "Status", "positionOdometer": "Odometer", "positionServiceOdometer": "Service Odometer", @@ -186,6 +187,7 @@ "positionThrottle": "Throttle", "positionMotion": "Motion", "positionArmed": "Armed", + "positionAcceleration": "Acceleration", "positionDeviceTemp": "Device Temperature", "positionOperator": "Operator", "positionCommand": "Acceleration", -- cgit v1.2.3