diff options
Diffstat (limited to 'src/main/java/org')
4 files changed, 42 insertions, 2 deletions
diff --git a/src/main/java/org/traccar/broadcast/BroadcastInterface.java b/src/main/java/org/traccar/broadcast/BroadcastInterface.java index dddba68b6..673ebd8b8 100644 --- a/src/main/java/org/traccar/broadcast/BroadcastInterface.java +++ b/src/main/java/org/traccar/broadcast/BroadcastInterface.java @@ -31,6 +31,9 @@ public interface BroadcastInterface { default void updateEvent(boolean local, long userId, Event event) { } + default void updateCommand(boolean local, long deviceId) { + } + default void invalidateObject(boolean local, Class<? extends BaseModel> clazz, long id) { } diff --git a/src/main/java/org/traccar/broadcast/BroadcastMessage.java b/src/main/java/org/traccar/broadcast/BroadcastMessage.java index 3e22be7e0..985848d04 100644 --- a/src/main/java/org/traccar/broadcast/BroadcastMessage.java +++ b/src/main/java/org/traccar/broadcast/BroadcastMessage.java @@ -63,6 +63,16 @@ public class BroadcastMessage { this.event = event; } + private Long commandDeviceId; + + public Long getCommandDeviceId() { + return commandDeviceId; + } + + public void setCommandDeviceId(Long commandDeviceId) { + this.commandDeviceId = commandDeviceId; + } + private Map<String, Long> changes; public Map<String, Long> getChanges() { diff --git a/src/main/java/org/traccar/broadcast/MulticastBroadcastService.java b/src/main/java/org/traccar/broadcast/MulticastBroadcastService.java index be65b7826..b1b66f1e3 100644 --- a/src/main/java/org/traccar/broadcast/MulticastBroadcastService.java +++ b/src/main/java/org/traccar/broadcast/MulticastBroadcastService.java @@ -103,6 +103,13 @@ public class MulticastBroadcastService implements BroadcastService { } @Override + public void updateCommand(boolean local, long deviceId) { + BroadcastMessage message = new BroadcastMessage(); + message.setCommandDeviceId(deviceId); + sendMessage(message); + } + + @Override public void invalidateObject(boolean local, Class<? extends BaseModel> clazz, long id) { BroadcastMessage message = new BroadcastMessage(); message.setChanges(Map.of(Permission.getKey(clazz), id)); @@ -136,6 +143,8 @@ public class MulticastBroadcastService implements BroadcastService { listeners.forEach(listener -> listener.updatePosition(false, message.getPosition())); } else if (message.getUserId() != null && message.getEvent() != null) { listeners.forEach(listener -> listener.updateEvent(false, message.getUserId(), message.getEvent())); + } else if (message.getCommandDeviceId() != null) { + listeners.forEach(listener -> listener.updateCommand(false, message.getCommandDeviceId())); } else if (message.getChanges() != null) { var iterator = message.getChanges().entrySet().iterator(); if (iterator.hasNext()) { diff --git a/src/main/java/org/traccar/database/CommandsManager.java b/src/main/java/org/traccar/database/CommandsManager.java index d56b4d472..53040ad53 100644 --- a/src/main/java/org/traccar/database/CommandsManager.java +++ b/src/main/java/org/traccar/database/CommandsManager.java @@ -18,6 +18,8 @@ package org.traccar.database; import org.traccar.BaseProtocol; import org.traccar.ServerManager; +import org.traccar.broadcast.BroadcastInterface; +import org.traccar.broadcast.BroadcastService; import org.traccar.model.Command; import org.traccar.model.Device; import org.traccar.model.Position; @@ -40,21 +42,24 @@ import java.util.Collection; import java.util.stream.Collectors; @Singleton -public class CommandsManager { +public class CommandsManager implements BroadcastInterface { private final Storage storage; private final ServerManager serverManager; private final SmsManager smsManager; private final ConnectionManager connectionManager; + private final BroadcastService broadcastService; @Inject public CommandsManager( Storage storage, ServerManager serverManager, @Nullable SmsManager smsManager, - ConnectionManager connectionManager) { + ConnectionManager connectionManager, BroadcastService broadcastService) { this.storage = storage; this.serverManager = serverManager; this.smsManager = smsManager; this.connectionManager = connectionManager; + this.broadcastService = broadcastService; + broadcastService.registerListener(this); } public boolean sendCommand(Command command) throws Exception { @@ -81,6 +86,7 @@ public class CommandsManager { deviceSession.sendCommand(command); } else { storage.addObject(QueuedCommand.fromCommand(command), new Request(new Columns.Exclude("id"))); + broadcastService.updateCommand(true, deviceId); return false; } } @@ -108,4 +114,16 @@ public class CommandsManager { } } + @Override + public void updateCommand(boolean local, long deviceId) { + if (!local) { + DeviceSession deviceSession = connectionManager.getDeviceSession(deviceId); + if (deviceSession != null && deviceSession.supportsLiveCommands()) { + for (Command command : readQueuedCommands(deviceId)) { + deviceSession.sendCommand(command); + } + } + } + } + } |