From 0a853f2aa3556554acd0b43a5008c43c345fa300 Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Thu, 29 Sep 2022 17:57:22 -0700 Subject: Synchronize queued commands --- .../org/traccar/broadcast/BroadcastInterface.java | 3 +++ .../org/traccar/broadcast/BroadcastMessage.java | 10 ++++++++++ .../broadcast/MulticastBroadcastService.java | 9 +++++++++ .../java/org/traccar/database/CommandsManager.java | 22 ++++++++++++++++++++-- 4 files changed, 42 insertions(+), 2 deletions(-) (limited to 'src/main/java/org') 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 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 changes; public Map 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 @@ -102,6 +102,13 @@ public class MulticastBroadcastService implements BroadcastService { sendMessage(message); } + @Override + public void updateCommand(boolean local, long deviceId) { + BroadcastMessage message = new BroadcastMessage(); + message.setCommandDeviceId(deviceId); + sendMessage(message); + } + @Override public void invalidateObject(boolean local, Class clazz, long id) { BroadcastMessage message = new BroadcastMessage(); @@ -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); + } + } + } + } + } -- cgit v1.2.3