Skip to content

Commit

Permalink
Merge pull request traccar#2349 from dcbastos/master
Browse files Browse the repository at this point in the history
Adds unknown devices automatically.
  • Loading branch information
tananaev authored Sep 27, 2016
2 parents 1849957 + ad070aa commit 73d63c1
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
3 changes: 3 additions & 0 deletions debug.xml
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@
<entry key='database.password'></entry>

<entry key='database.ignoreUnknown'>true</entry>

<!-- Automatically registers unknown devices. -->
<entry key='database.registerUnknown'>false</entry>

<entry key='database.xml'>false</entry>
<entry key='database.saveOriginal'>true</entry>
Expand Down
20 changes: 20 additions & 0 deletions src/org/traccar/BaseProtocolDecoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,27 @@
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.sql.SQLException;

public abstract class BaseProtocolDecoder extends ExtendedObjectDecoder {

private final Protocol protocol;

public long addUnknownDevice(String uniqueId) {
Device device = new Device();
device.setName(uniqueId);
device.setUniqueId(uniqueId);

try {
Context.getDeviceManager().addDevice(device);
Log.info("Automatically registered device " + uniqueId);
return device.getId();
} catch (SQLException e) {
Log.warning(e);
return 0;
}
}

public String getProtocolName() {
return protocol.getName();
}
Expand All @@ -55,6 +71,10 @@ private long findDeviceId(SocketAddress remoteAddress, String... uniqueIds) {
Log.warning(e);
}
if (deviceId == 0) {
if (Context.getConfig().getBoolean("database.registerUnknown")) {
return addUnknownDevice(uniqueIds[0]);
}

StringBuilder message = new StringBuilder("Unknown device -");
for (String uniqueId : uniqueIds) {
message.append(" ").append(uniqueId);
Expand Down

0 comments on commit 73d63c1

Please sign in to comment.