Merge pull request #1 from PAMGuard/main

Fixes to NoGUI, Network send and spectrogram graphics.  (#23)
This commit is contained in:
Douglas Gillespie 2022-02-27 14:20:31 +00:00 committed by GitHub
commit e3f83071ae
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
20 changed files with 468 additions and 103 deletions

View File

@ -54,6 +54,7 @@ import generalDatabase.DBControlUnit;
import javafx.stage.Stage;
import Array.ArrayManager;
import PamController.command.NetworkController;
import PamController.command.TerminalController;
import PamController.command.WatchdogComms;
import PamController.masterReference.MasterReferencePoint;
import PamController.settings.output.xml.PamguardXMLWriter;
@ -269,6 +270,11 @@ public class PamController implements PamControllerInterface, PamSettings {
setupPamguard();
setupGarbageCollector();
// if (PamGUIManager.getGUIType() == PamGUIManager.NOGUI) {
// }
// diagnosticTimer = new Timer(1000, new DiagnosticTimer());
// diagnosticTimer.start();
}
@ -293,7 +299,10 @@ public class PamController implements PamControllerInterface, PamSettings {
*/
public static void create(int runMode) {
if (uniqueController == null) {
new PamController(runMode, null);
PamController pamcontroller = new PamController(runMode, null);
// I don't see any reason not have have this running with the GUI.
TerminalController tc = new TerminalController(pamcontroller);
tc.getTerminalCommands();
}
}

View File

@ -1,6 +1,7 @@
package PamController;
import PamView.GuiFrameManager;
import PamView.NullGuiController;
import pamViewFX.PamGuiManagerFX;
/**
@ -39,7 +40,7 @@ public class PamGUIManager {
public static PAMControllerGUI createGUI(PamController pamController, Object object) {
switch (guiType) {
case NOGUI:
return null;
return new NullGuiController(pamController);
case SWING:
return new GuiFrameManager(pamController);
case FX:

View File

@ -0,0 +1,118 @@
package PamController.command;
import java.util.ArrayList;
import PamController.PamControlledUnit;
import PamController.PamController;
/**
* Interpret commands which may have come either from a terminal or from a Network
* (e.g. UDP) interface.
* @author dg50
*
*/
public abstract class CommandManager extends PamControlledUnit {
private PamController pamController;
private static String unitType = "Command Manager";
private ArrayList<ExtCommand> commandsList = new ArrayList<ExtCommand>();
public CommandManager(PamController pamController, String unitName) {
super(unitType, unitName);
this.pamController = pamController;
commandsList.add(new StartCommand());
commandsList.add(new StopCommand());
commandsList.add(new PingCommand());
commandsList.add(new StatusCommand());
commandsList.add(new SummaryCommand());
commandsList.add(new ExitCommand());
commandsList.add(new KillCommand());
commandsList.add(new HelpCommand(this));
}
protected ArrayList<ExtCommand> getCommandsList() {
return commandsList;
}
public ExtCommand findCommand(String command) {
for (ExtCommand aCommand:commandsList) {
if (aCommand.getName().equals(command)) {
return aCommand;
}
}
return null;
}
/**
* Interpret and act on a udp command string.
* @param command command string
* @return false if the command was to exit
* the program (in which case this thread will
* exit and close the port). True otherwise.
*/
public boolean interpretCommand(String command) {
//System.out.println(String.format("New UDP Command %s", command));
command = command.toLowerCase();
// strip of the first two letters if they begin pg ...
if (command.substring(0,2).equals("pg")) {
command = command.substring(2);
}
ExtCommand extCommand = findCommand(command);
if (extCommand == null) {
sendData("Cmd \"" + command + "\" Not Recognised.");
return false;
}
if (extCommand.canExecute() == false) {
sendData("Cmd \"" + command + "\" Cannot Execute.");
sendData(" Cmd return string = " + extCommand.getReturnString());
return false;
}
extCommand.execute();
sendData(extCommand.getReturnString());
//
// if (command.equals("pgstart")) {
// sendData("PgAck " + "pgstart");
// pamController.pamStart();
// }
// else if (command.equals("pgstop")) {
// sendData("PgAck " + "pgstop");
// pamController.pamStop();
// }
// else if (command.equals("pgping")) {
// sendData("PgAck " + "pgping");
// }
// else if (command.equals("pgstatus")) {
// sendData("PgAck Status " + pamController.getPamStatus());
// }
// else if (command.equals("pgsetrec")) {
// sendData("PgAck pgsetrec");
//
// //triggerRecording(String name, int seconds);
// }
// else if (command.equals("pgexit")) {
// sendData("Exiting PAMGUARD");
// System.exit(0);
// return false;
// }
// else{
// sendData("PgAck " + "Cmd Not Recognised.");
// }
return true;
}
/**
* Reply to data called from InterpredData
* @param dataString
* @return true if replay successful
*/
abstract public boolean sendData(String dataString);
}

View File

@ -21,7 +21,11 @@ public class ExitCommand extends ExtCommand {
System.exit(0);
return true;
}
@Override
public String getHint() {
return "Exit PAMGuard, stopping detectors prior to exiting";
}
}

View File

@ -107,5 +107,11 @@ public abstract class ExtCommand {
this.immediate = immediate;
}
/**
* Get a hint text for the help command.
* @return hint text.
*/
public String getHint() {
return null;
};
}

View File

@ -0,0 +1,42 @@
package PamController.command;
import java.util.ArrayList;
public class HelpCommand extends ExtCommand {
private CommandManager commandManager;
public HelpCommand(CommandManager commandManager) {
super("help", true);
this.commandManager = commandManager;
}
@Override
public boolean execute() {
ArrayList<ExtCommand> commands = commandManager.getCommandsList();
String out = "Available commands are:\n";
for (ExtCommand command : commands) {
out += command.getName();
String hint = command.getHint();
if (hint != null) {
out += String.format(": %s\n", hint);
}
else {
out += "\n";
}
}
commandManager.sendData(out);
return true;
}
@Override
public String getReturnString() {
return super.getReturnString();
}
@Override
public String getHint() {
return "Get a list of available commands";
}
}

View File

@ -17,5 +17,10 @@ public class KillCommand extends ExtCommand {
System.exit(0);
return true;
}
@Override
public String getHint() {
return "kill PAMguard, don't necessarily stop detectors or clean anything up";
}
}

View File

@ -25,7 +25,7 @@ import SoundRecorder.trigger.RecorderTriggerData;
* @author Doug Gillespie
*
*/
public class NetworkController extends PamControlledUnit {
public class NetworkController extends CommandManager {
private PamController pamController;
@ -44,22 +44,12 @@ public class NetworkController extends PamControlledUnit {
private NetworkRecorderTrigger[] recorderTriggers;
private static String unitType = "Network Controller";
private ArrayList<ExtCommand> networkCommands = new ArrayList<ExtCommand>();
private static String unitName = "Network Controller";
public NetworkController(PamController pamController) {
super(unitType, unitType);
super(pamController, unitName);
this.pamController = pamController;
networkCommands.add(new StartCommand());
networkCommands.add(new StopCommand());
networkCommands.add(new PingCommand());
networkCommands.add(new StatusCommand());
networkCommands.add(new SummaryCommand());
networkCommands.add(new ExitCommand());
networkCommands.add(new KillCommand());
listenerThread = new ListenerThread();
Thread aThread = new Thread(listenerThread);
aThread.start();
@ -130,77 +120,8 @@ public class NetworkController extends PamControlledUnit {
}
}
/**
* Interpret and act on a udp command string.
* @param command command string
* @return false if the command was to exit
* the program (in which case this thread will
* exit and close the port). True otherwise.
*/
private boolean interpretCommand(String command) {
//System.out.println(String.format("New UDP Command %s", command));
command = command.toLowerCase();
// strip of the first two letters if they begin pg ...
if (command.substring(0,2).equals("pg")) {
command = command.substring(2);
}
ExtCommand extCommand = findCommand(command);
if (extCommand == null) {
sendData("Cmd \"" + command + "\" Not Recognised.");
return false;
}
if (extCommand.canExecute() == false) {
sendData("Cmd \"" + command + "\" Cannot Execute.");
sendData(" Cmd return string = " + extCommand.getReturnString());
return false;
}
extCommand.execute();
sendData(extCommand.getReturnString());
//
// if (command.equals("pgstart")) {
// sendData("PgAck " + "pgstart");
// pamController.pamStart();
// }
// else if (command.equals("pgstop")) {
// sendData("PgAck " + "pgstop");
// pamController.pamStop();
// }
// else if (command.equals("pgping")) {
// sendData("PgAck " + "pgping");
// }
// else if (command.equals("pgstatus")) {
// sendData("PgAck Status " + pamController.getPamStatus());
// }
// else if (command.equals("pgsetrec")) {
// sendData("PgAck pgsetrec");
//
// //triggerRecording(String name, int seconds);
// }
// else if (command.equals("pgexit")) {
// sendData("Exiting PAMGUARD");
// System.exit(0);
// return false;
// }
// else{
// sendData("PgAck " + "Cmd Not Recognised.");
// }
return true;
}
private ExtCommand findCommand(String command) {
for (ExtCommand aCommand:networkCommands) {
if (aCommand.getName().equals(command)) {
return aCommand;
}
}
return null;
}
private boolean sendData(String dataString) {
public boolean sendData(String dataString) {
DatagramPacket packet = new DatagramPacket(dataString.getBytes(), dataString.length());
packet.setAddress(udpPacket.getAddress());
packet.setPort(udpPacket.getPort());
@ -229,6 +150,7 @@ public class NetworkController extends PamControlledUnit {
return new String(udpPacket.getData(), 0, udpPacket.getLength());
}
@Override
public void notifyModelChanged(int changeType) {
switch (changeType){
case PamControllerInterface.INITIALIZATION_COMPLETE:

View File

@ -13,4 +13,8 @@ public class PingCommand extends ExtCommand {
return true;
}
@Override
public String getHint() {
return "ping PAMGuard to see if it's alive";
}
}

View File

@ -35,4 +35,9 @@ public class StartCommand extends ExtCommand {
return returnString;
}
@Override
public String getHint() {
return "Start PAMGuard processing";
}
}

View File

@ -61,4 +61,9 @@ public class StatusCommand extends ExtCommand {
return watchdogComms.getModifiedWatchdogState(status);
}
@Override
public String getHint() {
return "Get the current PAMGuard status. 0=idle, 1=running";
}
}

View File

@ -15,4 +15,8 @@ public class StopCommand extends ExtCommand {
}
@Override
public String getHint() {
return "Stop PAMGuard processing";
}
}

View File

@ -35,7 +35,7 @@ public class SummaryCommand extends ExtCommand {
lastCallTime = PamCalendar.getSessionStartTime();
}
long nowTime = PamCalendar.getTimeInMillis();
totalString = PamCalendar.formatDateTime(lastCallTime) + "-" + PamCalendar.formatDateTime(nowTime);
totalString = PamCalendar.formatDBDateTime(lastCallTime) + "-" + PamCalendar.formatDBDateTime(nowTime);
int usedModules = 0;
for (int i = 0; i < nMod; i++) {
aModule = pamController.getControlledUnit(i);
@ -44,16 +44,14 @@ public class SummaryCommand extends ExtCommand {
continue;
}
usedModules ++;
/*
strcat(moduleSummaryText, "\n");
strcat(moduleSummaryText, module->getModuleName());
strcat(moduleSummaryText, ":");
strcat(moduleSummaryText, moduleText);
*/
totalString += String.format("\n<%s>%s:%s<\\%s>", aModule.getShortUnitType(),
aModule.getUnitName(), aString, aModule.getShortUnitType());
}
return totalString;
}
@Override
public String getHint() {
return "Get summary information about each running process";
}
}

View File

@ -0,0 +1,69 @@
package PamController.command;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import PamController.PamController;
public class TerminalController extends CommandManager {
private BufferedReader reader;
private static String unitName = "Terminal Controller";
public TerminalController(PamController pamController) {
super(pamController, unitName);
}
@Override
public boolean sendData(String dataString) {
System.out.println(dataString);
return true;
}
@Override
public void pamClose() {
super.pamClose();
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public void getTerminalCommands() {
Thread t = new Thread(new TerminalTread());
t.start();
}
private class TerminalTread implements Runnable {
@Override
public void run() {
readCommands();
}
}
private void readCommands() {
reader = new BufferedReader(
new InputStreamReader(System.in));
try {
while (true) {
String command = reader.readLine();
if (command != null && command.length() > 0) {
interpretCommand(command);
}
// System.out.println("you typed: " + inLine);
// if (inLine.contains("exit")) {
// break;
// }
}
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("Exiting PAMGuard, leave control thread");
}
}

View File

@ -0,0 +1,140 @@
package PamView;
import javax.swing.JFrame;
import PamController.PAMControllerGUI;
import PamController.PamControlledUnit;
import PamController.PamController;
import PamController.PamSettings;
import PamModel.PamModel;
import PamModel.PamModuleInfo;
import pamViewFX.pamTask.PamTaskUpdate;
/**
* Null GUI controller which will get used with the -nogui options.
* @author dg50
*
*/
public class NullGuiController implements PAMControllerGUI {
private PamController pamController;
public NullGuiController(PamController pamController) {
this.pamController = pamController;
}
@Override
public void pamStarted() {
// TODO Auto-generated method stub
}
@Override
public void pamEnded() {
// TODO Auto-generated method stub
}
@Override
public void modelChanged(int changeType) {
// TODO Auto-generated method stub
}
@Override
public void addControlledUnit(PamControlledUnit controlledUnit) {
// TODO Auto-generated method stub
}
@Override
public void showControlledUnit(PamControlledUnit unit) {
// TODO Auto-generated method stub
}
@Override
public void removeControlledUnit(PamControlledUnit controlledUnit) {
// TODO Auto-generated method stub
}
@Override
public void setTitle(String title) {
// TODO Auto-generated method stub
}
@Override
public int getFrameNumber() {
// TODO Auto-generated method stub
return 0;
}
@Override
public JFrame getGuiFrame() {
// TODO Auto-generated method stub
return null;
}
@Override
public void enableGUIControl(boolean enable) {
// TODO Auto-generated method stub
}
@Override
public void addView(PamViewInterface newView) {
// TODO Auto-generated method stub
}
@Override
public void destroyModel() {
// TODO Auto-generated method stub
}
@Override
public void notifyModelChanged(int changeType) {
// TODO Auto-generated method stub
}
@Override
public void notifyLoadProgress(PamTaskUpdate progress) {
// TODO Auto-generated method stub
}
@Override
public PamSettings getInitialSettings() {
// TODO Auto-generated method stub
return null;
}
@Override
public void init() {
// TODO Auto-generated method stub
}
@Override
public PamViewInterface initPrimaryView(PamController pamController, PamModel pamModelInterface) {
// TODO Auto-generated method stub
return null;
}
@Override
public boolean hasCallBack() {
// TODO Auto-generated method stub
return false;
}
@Override
public String getModuleName(Object parentFrame, PamModuleInfo moduleInfo) {
// TODO Auto-generated method stub
return null;
}
}

View File

@ -700,8 +700,11 @@ public class PamDetectionOverlayGraphics extends PanelOverlayDraw {
pamDetection.getSampleDuration() * 1000./parentDataBlock.getSampleRate(),
frequency[0], 0);
boolean isWrapped = false;
if (botRight.x < topLeft.x){
botRight.x = g.getClipBounds().width;
// this means it's wrapped.
isWrapped = true;
botRight.x += g.getClipBounds().width;
}
if (generalProjector.isViewer()) {
Coordinate3d middle = new Coordinate3d();
@ -723,8 +726,13 @@ public class PamDetectionOverlayGraphics extends PanelOverlayDraw {
// Not actually drawing on a spectrogramProjector, so don't have any info on the background color
}
if (isWrapped) {
g.drawRect((int) topLeft.x-g.getClipBounds().width, (int) topLeft.y,
(int) botRight.x - (int) topLeft.x, (int) botRight.y - (int) topLeft.y);
}
g.drawRect((int) topLeft.x, (int) topLeft.y,
(int) botRight.x - (int) topLeft.x, (int) botRight.y - (int) topLeft.y);
return new Rectangle((int) topLeft.x, (int) topLeft.y,
(int) botRight.x - (int) topLeft.x, (int) botRight.y - (int) topLeft.y);
}

View File

@ -65,12 +65,17 @@ public class RWEOverlayGraphics extends PamDetectionOverlayGraphics {
int[] polyY = new int[pf.length];
int[] edgeX = new int[pf.length*2];
int[] edgeY = new int[pf.length*2];
boolean isWrapped = false;
for (int i = 0, j = pf.length*2-1; i < pf.length; i++, j--) {
double t = t0 + (double) tbin[i] * ffthop / fs * 1000.;
double f = pf[i] * fs/fftLen;
Point2D pos = generalProjector.getCoord3d(t, f, 0).getPoint2D();
polyX[i] = (int) pos.getX();
polyY[i] = (int) pos.getY();
if (polyX[i] < polyX[0]) {
isWrapped = true;
polyX[i] += g.getClipBounds().width;
}
edgeX[i] = edgeX[j] = polyX[i];
Point2D pe = generalProjector.getCoord3d(t, highF[i]*fs/fftLen, 0).getPoint2D();
edgeY[i] = (int) pe.getY();
@ -87,6 +92,13 @@ public class RWEOverlayGraphics extends PamDetectionOverlayGraphics {
}
// g.drawPolyline(polyX, polyY, polyX.length);
g.drawPolygon(edgeX, edgeY, edgeX.length);
if (isWrapped) {
int dx = g.getClipBounds().width;
for (int i = 0; i < edgeX.length; i++) {
edgeX[i] -= dx;
}
g.drawPolygon(edgeX, edgeY, edgeX.length);
}
return r;
}

View File

@ -646,15 +646,17 @@ public class NetworkReceiver extends PamControlledUnit implements PamSettings, N
try {
objectId = ds.readInt();
moduleVersion = ds.readInt();
baseData.readBaseData(ds, receivedData.getDataVersion());
// millis = ds.readLong();
// if (dataVersion2 >= 2) {
// nanos = ds.readLong();
// channelMap = ds.readInt();
// }
baseData.readBaseData(ds, receivedData.getDataVersion());
dataLength = ds.readInt();
data = new byte[dataLength];
ds.read(data);
int bytesRead = ds.read(data);
// System.out.printf("NetRX read %d of expected %d bytes\n", bytesRead, dataLength);
// if (1>0) return null;
ds.close();
} catch (IOException e) {
e.printStackTrace();

View File

@ -6,12 +6,13 @@ import java.io.IOException;
import networkTransfer.NetworkObject;
import networkTransfer.receive.NetworkReceiver;
import PamguardMVC.DataUnitBaseData;
import PamguardMVC.PamDataBlock;
import PamguardMVC.PamDataUnit;
import binaryFileStorage.BinaryDataSource;
import binaryFileStorage.BinaryObjectData;
import binaryFileStorage.BinaryStore;
import jsonStorage.JSONObjectDataSource;
//import com.sun.xml.internal.messaging.saaj.util.ByteOutputStream;
@ -73,16 +74,23 @@ public class NetworkObjectPacker {
short dataType1 = NetworkReceiver.NET_PAM_DATA;
BinaryDataSource binarySource = dataBlock.getBinaryDataSource();
int dataType2 = dataBlock.getQuickId();
BinaryObjectData packedObject = binarySource.getPackedData(dataUnit);
byte[] data = packedObject.getData();
int duDataLength = data.length + 20;
int duDataLength = data.length + 12;
DataUnitBaseData baseData = dataUnit.getBasicData();
int baseDataLength = baseData.getBaseDataBinaryLength();
duDataLength += baseDataLength;
// ByteOutputStream bos = new ByteOutputStream(duDataLength);
ByteArrayOutputStream bos = new ByteArrayOutputStream(duDataLength);
DataOutputStream dos = new DataOutputStream(bos);
try {
try { // these are the extra 20 bytes refered to above.
dos.writeInt(packedObject.getObjectType());
dos.writeInt(binarySource.getModuleVersion());
dos.writeLong(dataUnit.getTimeMilliseconds());
// dos.writeLong(dataUnit.getTimeMilliseconds());
baseData.writeBaseData(dos, BinaryStore.getCurrentFileFormat());
dos.writeInt(data.length);
dos.write(data);
} catch (IOException e) {
@ -131,7 +139,7 @@ public class NetworkObjectPacker {
try {
dos.writeInt(DATASTARTFLAG);
dos.writeInt(totalSize);
dos.writeShort(1); // header version
dos.writeShort(BinaryStore.getCurrentFileFormat()); // header version
dos.writeShort(buoyId1);
dos.writeShort(buoyId2);
dos.writeShort(dataType1);

View File

@ -28,6 +28,7 @@ import PamController.PamGUIManager;
import PamController.PamSettingManager;
import PamController.PamguardVersionInfo;
import PamController.pamBuoyGlobals;
import PamController.command.TerminalController;
import PamModel.SMRUEnable;
import PamUtils.FileFunctions;
import PamUtils.PamExceptionHandler;
@ -298,7 +299,7 @@ public class Pamguard {
if(runMode == PamController.RUN_REMOTE) {
spashTime = 0;
}
if (spashTime > 0) {
if (spashTime > 0 && (PamGUIManager.getGUIType() != PamGUIManager.NOGUI)) {
new Splash(spashTime, chosenRunMode);
}
//
@ -323,9 +324,11 @@ public class Pamguard {
else {
PamController.create(chosenRunMode);
}
}
static private String writePropertyString(String key) {
String property = System.getProperty(key);
if (property == null) {