From a2da810690e548c2e416f9a3f4b308588f698b99 Mon Sep 17 00:00:00 2001 From: Douglas Gillespie <50671166+douggillespie@users.noreply.github.com> Date: Thu, 6 Apr 2023 16:44:47 +0100 Subject: [PATCH] Support batch processing Multicast controller and a couple of additional commands to work with the batch processing plugin. --- src/Acquisition/AcquisitionControl.java | 15 +++++ src/Acquisition/FolderInputSystem.java | 14 ++++ src/PamController/PamController.java | 6 +- src/PamController/command/BatchCommand.java | 64 +++++++++++++++++++ .../command/BatchStatusCommand.java | 31 +++++++++ src/PamController/command/CommandManager.java | 11 ++-- src/PamController/command/ExitCommand.java | 4 +- src/PamController/command/HelpCommand.java | 2 +- ...ntroller.java => MulticastController.java} | 40 +++++++++--- .../command/NetworkController.java | 6 +- .../command/TerminalController.java | 2 +- src/PamController/pamBuoyGlobals.java | 18 +++--- src/pamguard/Pamguard.java | 7 +- 13 files changed, 183 insertions(+), 37 deletions(-) create mode 100644 src/PamController/command/BatchCommand.java create mode 100644 src/PamController/command/BatchStatusCommand.java rename src/PamController/command/{MultiportController.java => MulticastController.java} (65%) diff --git a/src/Acquisition/AcquisitionControl.java b/src/Acquisition/AcquisitionControl.java index f0bd367d..406796c8 100644 --- a/src/Acquisition/AcquisitionControl.java +++ b/src/Acquisition/AcquisitionControl.java @@ -873,4 +873,19 @@ public class AcquisitionControl extends RawInputControlledUnit implements PamSet return getDaqProcess().setAnalysisStartTime(startTime); } + /** + * Very specific command handler for batch status which will only work + * with the folderinputSystem. + * @return + */ + public String getBatchStatus() { + DaqSystem runningSystem = getAcquisitionProcess().getRunningSystem(); + if (runningSystem instanceof FolderInputSystem) { + return ((FolderInputSystem) runningSystem).getBatchStatus(); + } + else { + return null; + } + } + } diff --git a/src/Acquisition/FolderInputSystem.java b/src/Acquisition/FolderInputSystem.java index cd112e2c..5c51f56c 100644 --- a/src/Acquisition/FolderInputSystem.java +++ b/src/Acquisition/FolderInputSystem.java @@ -893,5 +893,19 @@ public class FolderInputSystem extends FileInputSystem implements PamSettings, D return false; } + /** + * Get a status update for batch processing. + */ + public String getBatchStatus() { + int nFiles = 0; + if (allFiles != null) { + nFiles = allFiles.size(); + } + int generalStatus = PamController.getInstance().getPamStatus(); + File currFile = getCurrentFile(); + String bs = String.format("%d,%d,%d,%s", nFiles,currentFile,generalStatus,currFile); + return bs; + } + } diff --git a/src/PamController/PamController.java b/src/PamController/PamController.java index 6398066d..444bb19a 100644 --- a/src/PamController/PamController.java +++ b/src/PamController/PamController.java @@ -55,7 +55,7 @@ import generalDatabase.DBControlUnit; import javafx.application.Platform; import javafx.stage.Stage; import Array.ArrayManager; -import PamController.command.MultiportController; +import PamController.command.MulticastController; import PamController.command.NetworkController; import PamController.command.TerminalController; import PamController.command.WatchdogComms; @@ -258,8 +258,8 @@ public class PamController implements PamControllerInterface, PamSettings { if (pamBuoyGlobals.getNetworkControlPort() != null) { networkController = new NetworkController(this); } - if (pamBuoyGlobals.getMultiportAddress() != null) { - new MultiportController(this); + if (pamBuoyGlobals.getMulticastAddress() != null) { + new MulticastController(this); } guiFrameManager = PamGUIManager.createGUI(this, object); diff --git a/src/PamController/command/BatchCommand.java b/src/PamController/command/BatchCommand.java new file mode 100644 index 00000000..a3dadfa8 --- /dev/null +++ b/src/PamController/command/BatchCommand.java @@ -0,0 +1,64 @@ +package PamController.command; + +import networkTransfer.send.NetworkSender; +import pamguard.GlobalArguments; + +public class BatchCommand extends ExtCommand { + + public static final String commandId = "batchcommand"; + private CommandManager commandManager; + + public BatchCommand(CommandManager commandManager) { + super(commandId, true); + this.commandManager = commandManager; + } + + @Override + public String execute(String command) { + /** + * this should have to identifiers. If they match the identifiers of + * this pamguard instance, then the command part is passed back to the manager + * otherwise it is ignored. + */ + if (command == null) { + return null; + } + String[] bits = command.split(" "); + if (bits.length < 4) { + return null; + } + int id1 = 0; + int id2 = 0; + int expId1 = 0, expId2 = 0; + try { + id1 = Integer.valueOf(bits[1]); + id2 = Integer.valueOf(bits[2]); + } + catch (NumberFormatException e) { + System.out.println("Invalid BatchCommand: " + command); + return null; + } + String nid1 = GlobalArguments.getParam(NetworkSender.ID1); + String nid2 = GlobalArguments.getParam(NetworkSender.ID2); + if (bits[1].trim().equals(nid1) == false) { + return null; + } + + // now trim the string to the end of the third comma and send on the rest + int comPos = -1; + for (int i = 0; i < 3; i++) { + comPos = command.indexOf(" ", comPos+1); + if (comPos < 0) { + return null; + } + } + String trueCommand = command.substring(comPos); + trueCommand = trueCommand.trim(); +// System.out.printf(">>>>>>>>>>>>>>>>>>> Batchcommand execute \"%s\" in command manager %s\n", trueCommand, commandManager.getClass().getName()); + if (commandManager.interpretCommand(trueCommand)) { +// return commandManager.get + } + return null; + } + +} diff --git a/src/PamController/command/BatchStatusCommand.java b/src/PamController/command/BatchStatusCommand.java new file mode 100644 index 00000000..a58012b2 --- /dev/null +++ b/src/PamController/command/BatchStatusCommand.java @@ -0,0 +1,31 @@ +package PamController.command; + +import Acquisition.AcquisitionControl; +import PamController.PamController; +import pamViewFX.PamControlledGUIFX; + +/** + * Command to get PAMGuard to send back the batch processing status. + * + * @author dg50 + * + */ +public class BatchStatusCommand extends ExtCommand { + + public static final String commandId = "batchstatus"; + + public BatchStatusCommand() { + super(commandId, true); + // TODO Auto-generated constructor stub + } + + @Override + public String execute(String command) { + AcquisitionControl daqControl = (AcquisitionControl) PamController.getInstance().findControlledUnit(AcquisitionControl.class, null); + if (daqControl == null) { + return null; + } + return daqControl.getBatchStatus(); + } + +} diff --git a/src/PamController/command/CommandManager.java b/src/PamController/command/CommandManager.java index 01533e28..87f4b888 100644 --- a/src/PamController/command/CommandManager.java +++ b/src/PamController/command/CommandManager.java @@ -40,6 +40,8 @@ public abstract class CommandManager extends PamControlledUnit { commandsList.add(new HelpCommand(this)); commandsList.add(new GetXMLSettings()); commandsList.add(new SetXMLSettings()); + commandsList.add(new BatchStatusCommand()); + commandsList.add(new BatchCommand(this)); } @@ -81,16 +83,16 @@ public abstract class CommandManager extends PamControlledUnit { } ExtCommand extCommand = findCommand(command); if (extCommand == null) { - sendData("Cmd \"" + commandString + "\" Not Recognised."); + sendData(extCommand, "Cmd \"" + commandString + "\" Not Recognised."); return false; } if (extCommand.canExecute() == false) { - sendData("Cmd \"" + command + "\" Cannot Execute."); + sendData(extCommand, "Cmd \"" + command + "\" Cannot Execute."); // sendData(" Cmd return string = " + extCommand.getReturnString()); return false; } String output = extCommand.executeCommand(commandString); - sendData(output); + sendData(extCommand,output); return true; @@ -154,9 +156,10 @@ public abstract class CommandManager extends PamControlledUnit { /** * Reply to data called from InterpredData + * @param extCommand * @param dataString * @return true if replay successful */ - abstract public boolean sendData(String dataString); + abstract public boolean sendData(ExtCommand extCommand, String dataString); } diff --git a/src/PamController/command/ExitCommand.java b/src/PamController/command/ExitCommand.java index a44c91ce..0871c7f2 100644 --- a/src/PamController/command/ExitCommand.java +++ b/src/PamController/command/ExitCommand.java @@ -10,8 +10,10 @@ import PamController.PamSettingManager; */ public class ExitCommand extends ExtCommand { + public static final String commandId = "Exit"; + public ExitCommand() { - super("Exit", false); + super(commandId, false); } @Override diff --git a/src/PamController/command/HelpCommand.java b/src/PamController/command/HelpCommand.java index 400516ad..3350c828 100644 --- a/src/PamController/command/HelpCommand.java +++ b/src/PamController/command/HelpCommand.java @@ -25,7 +25,7 @@ public class HelpCommand extends ExtCommand { out += "\n"; } } - commandManager.sendData(out); + commandManager.sendData(this, out); return getName(); } diff --git a/src/PamController/command/MultiportController.java b/src/PamController/command/MulticastController.java similarity index 65% rename from src/PamController/command/MultiportController.java rename to src/PamController/command/MulticastController.java index ead9e397..de92b6ae 100644 --- a/src/PamController/command/MultiportController.java +++ b/src/PamController/command/MulticastController.java @@ -12,12 +12,14 @@ import java.nio.charset.StandardCharsets; import PamController.PamController; import PamController.pamBuoyGlobals; +import networkTransfer.send.NetworkSender; +import pamguard.GlobalArguments; -public class MultiportController extends CommandManager { +public class MulticastController extends CommandManager { //The multicast addresses are in the range 224.0.0.0 through 239.255.255.255 - private static String unitName = "Multiport Controller"; + private static String unitName = "Multicast Controller"; private PamController pamController; private String mAddress; private int mPort; @@ -29,11 +31,11 @@ public class MultiportController extends CommandManager { private byte[] byteBuffer = new byte[MAX_COMMAND_LENGTH]; private DatagramPacket lastDatagram; - public MultiportController(PamController pamController) { + public MulticastController(PamController pamController) { super(pamController, unitName); this.pamController = pamController; - this.mAddress = pamBuoyGlobals.getMultiportAddress(); - this.mPort = pamBuoyGlobals.getMuliportPort(); + this.mAddress = pamBuoyGlobals.getMulticastAddress(); + this.mPort = pamBuoyGlobals.getMulticastPort(); Thread t = new Thread(new ListenerThread()); t.start(); @@ -83,17 +85,35 @@ public class MultiportController extends CommandManager { private void processDatagram(DatagramPacket datagram) { lastDatagram = datagram; String str = new String(datagram.getData(), 0, datagram.getLength()); -// str = str.substring(0, datagram.getLength()); - System.out.println("Datagram received \"" + str + "\""); + str = str.substring(0, datagram.getLength()); +// System.out.println("Datagram received \"" + str + "\""); interpretCommand(str); } @Override - public boolean sendData(String dataString) { + public boolean sendData(ExtCommand extCommand, String dataString) { + if (dataString == null || dataString.length() == 0) { + return false; + } DatagramPacket senderInfo = lastDatagram; - System.out.printf("Send back data \"%s\" to %s port %d\n", dataString, senderInfo.getAddress(), senderInfo.getPort()); + String commandName; + if (extCommand == null) { + commandName = "Unknown"; + } + else { + commandName = extCommand.getName(); + } +// System.out.printf("Send back data \"%s\" to %s port %d\n", dataString, senderInfo.getAddress(), senderInfo.getPort()); + /* + * for multicast, we need to send a slightly different string back which has the station id as part of + * the returned string. These will be 0 if they weren't passed in at the command line. + */ + String id1 = GlobalArguments.getParam(NetworkSender.ID1); + String id2 = GlobalArguments.getParam(NetworkSender.ID2); + String bigString = String.format("%s,%s,%s,%s", commandName, id1, id2, dataString); + // dataString += "\n"; - DatagramPacket packet = new DatagramPacket(dataString.getBytes(), dataString.length()); + DatagramPacket packet = new DatagramPacket(bigString.getBytes(), bigString.length()); packet.setAddress(senderInfo.getAddress()); packet.setPort(senderInfo.getPort()); try { diff --git a/src/PamController/command/NetworkController.java b/src/PamController/command/NetworkController.java index 2090851b..82268bd1 100644 --- a/src/PamController/command/NetworkController.java +++ b/src/PamController/command/NetworkController.java @@ -124,16 +124,14 @@ public class NetworkController extends CommandManager { } } - - public boolean sendData(String dataString) { + @Override + public boolean sendData(ExtCommand extCommand, String dataString) { DatagramPacket packet = new DatagramPacket(dataString.getBytes(), dataString.length()); packet.setAddress(udpPacket.getAddress()); packet.setPort(udpPacket.getPort()); try { receiveSocket.send(packet); -// receiveSocket. } catch (IOException e) { - // TODO Auto-generated catch block e.printStackTrace(); return false; } diff --git a/src/PamController/command/TerminalController.java b/src/PamController/command/TerminalController.java index e8589cd2..533b23ee 100644 --- a/src/PamController/command/TerminalController.java +++ b/src/PamController/command/TerminalController.java @@ -17,7 +17,7 @@ public class TerminalController extends CommandManager { } @Override - public boolean sendData(String dataString) { + public boolean sendData(ExtCommand extcommand, String dataString) { System.out.println(dataString); return true; } diff --git a/src/PamController/pamBuoyGlobals.java b/src/PamController/pamBuoyGlobals.java index dee3a1db..9b0a436f 100644 --- a/src/PamController/pamBuoyGlobals.java +++ b/src/PamController/pamBuoyGlobals.java @@ -14,9 +14,9 @@ public class pamBuoyGlobals { // public static Integer useGstreamer = 0; // public static boolean useNetworkCont = false; private static Integer networkControlPort = null; -private static String multiportAddress; +private static String multicastAddress; // private static boolean useDSP = false; -private static int muliportPort; +private static int mulicastPort; /** * @return the networkControlPort @@ -33,28 +33,28 @@ private static int muliportPort; } /** - * Set parameters for mulitport configutation. + * Set parameters for mulitport configuration. * @param mAddr * @param mPort */ public static void setMultiportConfig(String mAddr, int mPort) { - multiportAddress = mAddr; - muliportPort = mPort; + multicastAddress = mAddr; + mulicastPort = mPort; } /** * @return the multiportAddress */ - public static String getMultiportAddress() { - return multiportAddress; + public static String getMulticastAddress() { + return multicastAddress; } /** * @return the muliportPort */ - public static int getMuliportPort() { - return muliportPort; + public static int getMulticastPort() { + return mulicastPort; } diff --git a/src/pamguard/Pamguard.java b/src/pamguard/Pamguard.java index 2da04ce4..a80c16cc 100644 --- a/src/pamguard/Pamguard.java +++ b/src/pamguard/Pamguard.java @@ -222,8 +222,7 @@ public class Pamguard { PamGUIManager.setType(PamGUIManager.NOGUI); System.out.println("no gui operation."); } - /////////////// - else if (anArg.equalsIgnoreCase("-psf")) { + else if (anArg.equalsIgnoreCase("-psf") || anArg.equalsIgnoreCase("-psfx")) { String autoPsf = args[iArg++]; PamSettingManager.remote_psf = autoPsf; System.out.println("Running using settings from " + autoPsf); @@ -234,12 +233,12 @@ public class Pamguard { pamBuoyGlobals.setNetworkControlPort(Integer.parseInt(port)); System.out.println("Setting UDP control port " + port); } - else if (anArg.equalsIgnoreCase("-mport")) { + else if (anArg.equalsIgnoreCase("-multicast") || anArg.equalsIgnoreCase("-mport")) { // multicast control (for multiple PAMGuards) String mAddr = args[iArg++]; int mPort = Integer.parseInt(args[iArg++]); pamBuoyGlobals.setMultiportConfig(mAddr, mPort); - System.out.printf("Setting multiport control addr %s port %d\n", mAddr, mPort); + System.out.printf("Setting multicast control addr %s port %d\n", mAddr, mPort); } else if (anArg.equalsIgnoreCase("-nolog")) { System.out.println("Disabling log file from command line switch...");