aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/org/traccar/BasePipelineFactory.java8
-rw-r--r--src/org/traccar/database/DriversManager.java12
-rw-r--r--src/org/traccar/model/Position.java2
-rw-r--r--src/org/traccar/processing/PopulateDriverHandler.java45
-rw-r--r--src/org/traccar/protocol/AplicomProtocolDecoder.java3
-rw-r--r--src/org/traccar/protocol/MeiligaoProtocolDecoder.java2
-rw-r--r--src/org/traccar/protocol/OsmAndProtocolDecoder.java3
-rw-r--r--src/org/traccar/protocol/TeltonikaProtocolDecoder.java2
-rw-r--r--src/org/traccar/reports/ReportUtils.java16
-rw-r--r--src/org/traccar/reports/model/TripReport.java10
-rw-r--r--test/org/traccar/ProtocolTest.java4
-rw-r--r--test/org/traccar/processing/PopulateDriverTest.java21
-rwxr-xr-xtools/test-generator.py8
13 files changed, 131 insertions, 5 deletions
diff --git a/src/org/traccar/BasePipelineFactory.java b/src/org/traccar/BasePipelineFactory.java
index a6446dbaa..9f0824b6c 100644
--- a/src/org/traccar/BasePipelineFactory.java
+++ b/src/org/traccar/BasePipelineFactory.java
@@ -40,6 +40,7 @@ import org.traccar.events.AlertEventHandler;
import org.traccar.helper.Log;
import org.traccar.processing.ComputedAttributesHandler;
import org.traccar.processing.CopyAttributesHandler;
+import org.traccar.processing.PopulateDriverHandler;
import java.net.InetSocketAddress;
@@ -54,6 +55,7 @@ public abstract class BasePipelineFactory implements ChannelPipelineFactory {
private GeocoderHandler geocoderHandler;
private GeolocationHandler geolocationHandler;
private HemisphereHandler hemisphereHandler;
+ private PopulateDriverHandler populateDriverHandler;
private CopyAttributesHandler copyAttributesHandler;
private ComputedAttributesHandler computedAttributesHandler;
@@ -152,6 +154,8 @@ public abstract class BasePipelineFactory implements ChannelPipelineFactory {
hemisphereHandler = new HemisphereHandler();
}
+ populateDriverHandler = new PopulateDriverHandler();
+
if (Context.getConfig().getBoolean("processing.copyAttributes.enable")) {
copyAttributesHandler = new CopyAttributesHandler();
}
@@ -214,6 +218,10 @@ public abstract class BasePipelineFactory implements ChannelPipelineFactory {
pipeline.addLast("motion", motionHandler);
}
+ if (populateDriverHandler != null) {
+ pipeline.addLast("populateDriver", populateDriverHandler);
+ }
+
if (copyAttributesHandler != null) {
pipeline.addLast("copyAttributes", copyAttributesHandler);
}
diff --git a/src/org/traccar/database/DriversManager.java b/src/org/traccar/database/DriversManager.java
index e89d59311..1744936e3 100644
--- a/src/org/traccar/database/DriversManager.java
+++ b/src/org/traccar/database/DriversManager.java
@@ -210,4 +210,16 @@ public class DriversManager {
}
return drivers;
}
+
+ public final boolean authorizeDriverByUniqueId(String driverUniqueId, long deviceId) {
+ if (driversByUniqueId.containsKey(driverUniqueId)) {
+ long chekingDriverId = getDriverByUniqueId(driverUniqueId).getId();
+ for (long driverId : getAllDeviceDrivers(deviceId)) {
+ if (chekingDriverId == driverId) {
+ return true;
+ }
+ }
+ }
+ return false;
+ }
}
diff --git a/src/org/traccar/model/Position.java b/src/org/traccar/model/Position.java
index 5835310ae..97b3f365f 100644
--- a/src/org/traccar/model/Position.java
+++ b/src/org/traccar/model/Position.java
@@ -81,6 +81,8 @@ public class Position extends Message {
public static final String KEY_RESULT = "result";
+ public static final String KEY_DRIVER_UNIQUE_ID = "driverUniqueId";
+
// Start with 1 not 0
public static final String PREFIX_TEMP = "temp";
public static final String PREFIX_ADC = "adc";
diff --git a/src/org/traccar/processing/PopulateDriverHandler.java b/src/org/traccar/processing/PopulateDriverHandler.java
new file mode 100644
index 000000000..339cc5548
--- /dev/null
+++ b/src/org/traccar/processing/PopulateDriverHandler.java
@@ -0,0 +1,45 @@
+/*
+ * Copyright 2017 Anton Tananaev (anton@traccar.org)
+ * Copyright 2017 Andrey Kunitsyn (andrey@traccar.org)
+ *
+ * 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.
+ */
+package org.traccar.processing;
+
+import org.traccar.BaseDataHandler;
+import org.traccar.Context;
+import org.traccar.model.Position;
+
+public class PopulateDriverHandler extends BaseDataHandler {
+
+ private Position getLastPosition(long deviceId) {
+ if (Context.getIdentityManager() != null) {
+ return Context.getIdentityManager().getLastPosition(deviceId);
+ }
+ return null;
+ }
+
+ @Override
+ protected Position handlePosition(Position position) {
+ if (position.getAttributes().containsKey(Position.KEY_RFID)) {
+ position.set(Position.KEY_DRIVER_UNIQUE_ID, position.getString(Position.KEY_RFID));
+ } else {
+ Position last = getLastPosition(position.getDeviceId());
+ if (last != null && last.getAttributes().containsKey(Position.KEY_DRIVER_UNIQUE_ID)) {
+ position.set(Position.KEY_DRIVER_UNIQUE_ID, last.getString(Position.KEY_DRIVER_UNIQUE_ID));
+ }
+ }
+ return position;
+ }
+
+}
diff --git a/src/org/traccar/protocol/AplicomProtocolDecoder.java b/src/org/traccar/protocol/AplicomProtocolDecoder.java
index eb8d77011..448cd94fc 100644
--- a/src/org/traccar/protocol/AplicomProtocolDecoder.java
+++ b/src/org/traccar/protocol/AplicomProtocolDecoder.java
@@ -263,7 +263,8 @@ public class AplicomProtocolDecoder extends BaseProtocolDecoder {
}
if ((selector & 0x0200) != 0) {
- position.set(Position.KEY_RFID, (((long) buf.readUnsignedShort()) << 32) + buf.readUnsignedInt());
+ position.set(Position.KEY_RFID,
+ String.valueOf(((long) buf.readUnsignedShort()) << 32) + buf.readUnsignedInt());
}
if ((selector & 0x0400) != 0) {
diff --git a/src/org/traccar/protocol/MeiligaoProtocolDecoder.java b/src/org/traccar/protocol/MeiligaoProtocolDecoder.java
index 44e01d5e0..77e117262 100644
--- a/src/org/traccar/protocol/MeiligaoProtocolDecoder.java
+++ b/src/org/traccar/protocol/MeiligaoProtocolDecoder.java
@@ -253,7 +253,7 @@ public class MeiligaoProtocolDecoder extends BaseProtocolDecoder {
}
if (parser.hasNext()) {
- position.set(Position.KEY_RFID, parser.nextHexInt(0));
+ position.set(Position.KEY_RFID, String.valueOf(parser.nextHexInt(0)));
}
return position;
diff --git a/src/org/traccar/protocol/OsmAndProtocolDecoder.java b/src/org/traccar/protocol/OsmAndProtocolDecoder.java
index 15f6f40b8..20cd54b2f 100644
--- a/src/org/traccar/protocol/OsmAndProtocolDecoder.java
+++ b/src/org/traccar/protocol/OsmAndProtocolDecoder.java
@@ -128,6 +128,9 @@ public class OsmAndProtocolDecoder extends BaseProtocolDecoder {
case "batt":
position.set(Position.KEY_BATTERY_LEVEL, Double.parseDouble(value));
break;
+ case "rfid":
+ position.set(Position.KEY_RFID, value);
+ break;
default:
try {
position.set(entry.getKey(), Double.parseDouble(value));
diff --git a/src/org/traccar/protocol/TeltonikaProtocolDecoder.java b/src/org/traccar/protocol/TeltonikaProtocolDecoder.java
index 3f5b68f67..f1fd55d98 100644
--- a/src/org/traccar/protocol/TeltonikaProtocolDecoder.java
+++ b/src/org/traccar/protocol/TeltonikaProtocolDecoder.java
@@ -114,7 +114,7 @@ public class TeltonikaProtocolDecoder extends BaseProtocolDecoder {
position.set(Position.PREFIX_TEMP + 3, readValue(buf, length, true) * 0.1);
break;
case 78:
- position.set(Position.KEY_RFID, readValue(buf, length, false));
+ position.set(Position.KEY_RFID, String.valueOf(readValue(buf, length, false)));
break;
case 182:
position.set(Position.KEY_HDOP, readValue(buf, length, false) * 0.1);
diff --git a/src/org/traccar/reports/ReportUtils.java b/src/org/traccar/reports/ReportUtils.java
index 71c567c29..92918b861 100644
--- a/src/org/traccar/reports/ReportUtils.java
+++ b/src/org/traccar/reports/ReportUtils.java
@@ -26,6 +26,7 @@ import org.jxls.transform.Transformer;
import org.jxls.transform.poi.PoiTransformer;
import org.jxls.util.TransformerFactory;
import org.traccar.Context;
+import org.traccar.model.Driver;
import org.traccar.model.Position;
import org.traccar.reports.model.BaseReport;
import org.traccar.reports.model.StopReport;
@@ -101,6 +102,20 @@ public final class ReportUtils {
return 0;
}
+ public static String findDriverName(Position firstPosition, Position lastPosition) {
+ String driverUniqueId = null;
+ if (firstPosition.getAttributes().containsKey(Position.KEY_DRIVER_UNIQUE_ID)) {
+ driverUniqueId = firstPosition.getString(Position.KEY_DRIVER_UNIQUE_ID);
+ } else if (lastPosition.getAttributes().containsKey(Position.KEY_DRIVER_UNIQUE_ID)) {
+ driverUniqueId = lastPosition.getString(Position.KEY_DRIVER_UNIQUE_ID);
+ }
+ if (driverUniqueId != null && Context.getDriversManager() != null) {
+ Driver driver = Context.getDriversManager().getDriverByUniqueId(driverUniqueId);
+ return driver != null ? driver.getName() : null;
+ }
+ return null;
+ }
+
public static org.jxls.common.Context initializeContext(long userId) {
org.jxls.common.Context jxlsContext = PoiTransformer.createInitialContext();
jxlsContext.putVar("distanceUnit", getDistanceUnit(userId));
@@ -175,6 +190,7 @@ public final class ReportUtils {
trip.setAverageSpeed(speedSum / (endIndex - startIndex));
trip.setMaxSpeed(speedMax);
trip.setSpentFuel(calculateFuel(startTrip, endTrip));
+ trip.setDriverName(findDriverName(startTrip, endTrip));
return trip;
}
diff --git a/src/org/traccar/reports/model/TripReport.java b/src/org/traccar/reports/model/TripReport.java
index efe556f79..0f23581b2 100644
--- a/src/org/traccar/reports/model/TripReport.java
+++ b/src/org/traccar/reports/model/TripReport.java
@@ -145,4 +145,14 @@ public class TripReport extends BaseReport {
public void setDuration(long duration) {
this.duration = duration;
}
+
+ private String driverName;
+
+ public String getDriverName() {
+ return driverName;
+ }
+
+ public void setDriverName(String driverName) {
+ this.driverName = driverName;
+ }
}
diff --git a/test/org/traccar/ProtocolTest.java b/test/org/traccar/ProtocolTest.java
index 3b801c6eb..dc4f70981 100644
--- a/test/org/traccar/ProtocolTest.java
+++ b/test/org/traccar/ProtocolTest.java
@@ -245,6 +245,10 @@ public class ProtocolTest extends BaseTest {
Assert.assertTrue(attributes.get(Position.KEY_ARCHIVE) instanceof Boolean);
}
+ if (attributes.containsKey(Position.KEY_RFID)) {
+ Assert.assertTrue(attributes.get(Position.KEY_RFID) instanceof String);
+ }
+
if (position.getNetwork() != null && position.getNetwork().getCellTowers() != null) {
for (CellTower cellTower : position.getNetwork().getCellTowers()) {
checkInteger(cellTower.getMobileCountryCode(), 0, 999);
diff --git a/test/org/traccar/processing/PopulateDriverTest.java b/test/org/traccar/processing/PopulateDriverTest.java
new file mode 100644
index 000000000..7c6ee9f06
--- /dev/null
+++ b/test/org/traccar/processing/PopulateDriverTest.java
@@ -0,0 +1,21 @@
+package org.traccar.processing;
+
+import org.junit.Assert;
+import org.junit.Test;
+import org.traccar.model.Position;
+
+public class PopulateDriverTest {
+
+ @Test
+ public void testPopulateDriver() {
+ Position position = new Position();
+ PopulateDriverHandler populateDriverHandler = new PopulateDriverHandler();
+ Assert.assertNull(populateDriverHandler.handlePosition(position).getString(Position.KEY_DRIVER_UNIQUE_ID));
+ position.set(Position.KEY_DRIVER_UNIQUE_ID, "123");
+ Assert.assertEquals("123",
+ populateDriverHandler.handlePosition(position).getString(Position.KEY_DRIVER_UNIQUE_ID));
+ position.set(Position.KEY_RFID, "321");
+ Assert.assertEquals("321",
+ populateDriverHandler.handlePosition(position).getString(Position.KEY_DRIVER_UNIQUE_ID));
+ }
+}
diff --git a/tools/test-generator.py b/tools/test-generator.py
index 9e9cf892e..47c1c478f 100755
--- a/tools/test-generator.py
+++ b/tools/test-generator.py
@@ -12,6 +12,7 @@ server = 'localhost:5055'
period = 1
step = 0.001
device_speed = 40
+driver_id = '123456'
waypoints = [
(40.722412, -74.006288),
@@ -34,7 +35,7 @@ for i in range(0, len(waypoints)):
lon = lon1 + (lon2 - lon1) * j / count
points.append((lat, lon))
-def send(conn, lat, lon, course, speed, alarm, ignition, accuracy, rpm, fuel):
+def send(conn, lat, lon, course, speed, alarm, ignition, accuracy, rpm, fuel, rfid):
params = (('id', id), ('timestamp', int(time.time())), ('lat', lat), ('lon', lon), ('bearing', course), ('speed', speed))
if alarm:
params = params + (('alarm', 'sos'),)
@@ -46,6 +47,8 @@ def send(conn, lat, lon, course, speed, alarm, ignition, accuracy, rpm, fuel):
params = params + (('rpm', rpm),)
if fuel:
params = params + (('fuel', fuel),)
+ if rfid:
+ params = params + (('rfid', rfid),)
conn.request('GET', '?' + urllib.urlencode(params))
conn.getresponse().read()
@@ -71,6 +74,7 @@ while True:
accuracy = 100 if (index % 10) == 0 else 0
rpm = random.randint(500, 4000)
fuel = random.randint(0, 80)
- send(conn, lat1, lon1, course(lat1, lon1, lat2, lon2), speed, alarm, ignition, accuracy, rpm, fuel)
+ rfid = driver_id if (index % len(points)) == 0 else False
+ send(conn, lat1, lon1, course(lat1, lon1, lat2, lon2), speed, alarm, ignition, accuracy, rpm, fuel, rfid)
time.sleep(period)
index += 1