Skip to content

Commit

Permalink
Logs from unknown device
Browse files Browse the repository at this point in the history
  • Loading branch information
tananaev committed Dec 30, 2023
1 parent e7fa59e commit 4959d90
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 6 deletions.
10 changes: 10 additions & 0 deletions src/main/java/org/traccar/model/LogRecord.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,16 @@ public void setUniqueId(String uniqueId) {
this.uniqueId = uniqueId;
}

private long deviceId;

public long getDeviceId() {
return deviceId;
}

public void setDeviceId(long deviceId) {
this.deviceId = deviceId;
}

private String data;

public String getData() {
Expand Down
28 changes: 22 additions & 6 deletions src/main/java/org/traccar/session/ConnectionManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ public class ConnectionManager implements BroadcastInterface {

private final Map<Long, DeviceSession> sessionsByDeviceId = new ConcurrentHashMap<>();
private final Map<SocketAddress, Map<String, DeviceSession>> sessionsByEndpoint = new ConcurrentHashMap<>();
private final Map<SocketAddress, String> unknownByEndpoint = new ConcurrentHashMap<>();

private final Config config;
private final CacheManager cacheManager;
Expand Down Expand Up @@ -122,13 +123,15 @@ public DeviceSession getDeviceSession(

Device device = deviceLookupService.lookup(uniqueIds);

String firstUniqueId = uniqueIds[0];
if (device == null && config.getBoolean(Keys.DATABASE_REGISTER_UNKNOWN)) {
if (uniqueIds[0].matches(config.getString(Keys.DATABASE_REGISTER_UNKNOWN_REGEX))) {
device = addUnknownDevice(uniqueIds[0]);
if (firstUniqueId.matches(config.getString(Keys.DATABASE_REGISTER_UNKNOWN_REGEX))) {
device = addUnknownDevice(firstUniqueId);
}
}

if (device != null) {
unknownByEndpoint.remove(remoteAddress);
device.checkDisabled();

DeviceSession oldSession = sessionsByDeviceId.remove(device.getId());
Expand All @@ -153,6 +156,7 @@ public DeviceSession getDeviceSession(

return deviceSession;
} else {
unknownByEndpoint.put(remoteAddress, firstUniqueId);
LOGGER.warn("Unknown device - " + String.join(" ", uniqueIds)
+ " (" + ((InetSocketAddress) remoteAddress).getHostString() + ")");
return null;
Expand Down Expand Up @@ -181,7 +185,8 @@ private Device addUnknownDevice(String uniqueId) {
}

public void deviceDisconnected(Channel channel, boolean supportsOffline) {
Map<String, DeviceSession> endpointSessions = sessionsByEndpoint.remove(channel.remoteAddress());
SocketAddress remoteAddress = channel.remoteAddress();
Map<String, DeviceSession> endpointSessions = sessionsByEndpoint.remove(remoteAddress);
if (endpointSessions != null) {
for (DeviceSession deviceSession : endpointSessions.values()) {
if (supportsOffline) {
Expand All @@ -191,6 +196,7 @@ public void deviceDisconnected(Channel channel, boolean supportsOffline) {
cacheManager.removeDevice(deviceSession.getDeviceId());
}
}
unknownByEndpoint.remove(remoteAddress);
}

public void deviceUnknown(long deviceId) {
Expand Down Expand Up @@ -336,9 +342,19 @@ public synchronized <T1 extends BaseModel, T2 extends BaseModel> void invalidate

public synchronized void updateLog(LogRecord record) {
var sessions = sessionsByEndpoint.getOrDefault(record.getAddress(), Map.of());
for (var session : sessions.entrySet()) {
record.setUniqueId(session.getKey());
for (long userId : deviceUsers.getOrDefault(session.getValue().getDeviceId(), Set.of())) {
if (sessions.isEmpty()) {
String unknownUniqueId = unknownByEndpoint.get(record.getAddress());
if (unknownUniqueId != null) {
record.setUniqueId(unknownUniqueId);
listeners.values().stream()
.flatMap(Set::stream)
.forEach((listener) -> listener.onUpdateLog(record));
}
} else {
var firstEntry = sessions.entrySet().iterator().next();
record.setUniqueId(firstEntry.getKey());
record.setDeviceId(firstEntry.getValue().getDeviceId());
for (long userId : deviceUsers.getOrDefault(record.getDeviceId(), Set.of())) {
for (UpdateListener listener : listeners.getOrDefault(userId, Set.of())) {
listener.onUpdateLog(record);
}
Expand Down

0 comments on commit 4959d90

Please sign in to comment.