diff --git a/config/scripts/duty.sc b/config/scripts/duty.sc index 648380f068..6d6b205261 100644 --- a/config/scripts/duty.sc +++ b/config/scripts/duty.sc @@ -1,7 +1,7 @@ echo "#!type line" | window duty -duty 10 "MSP430 Core.active" CC2420.listen CC2420.transmit "Tmote Sky.0" | window duty echo "#!set 0 label CPU" | window duty echo "#!set 1 label Listen" | window duty echo "#!set 2 label Transmit" | window duty echo "#!set 3 label LEDS" | window duty -echo "#!title Duty Cycle" | window duty \ No newline at end of file +echo "#!title Duty Cycle" | window duty +duty 10 "MSP430 Core.active" CC2420.listen CC2420.transmit "Tmote Sky.0" | window duty diff --git a/java/se/sics/mspsim/cli/Command.java b/java/se/sics/mspsim/cli/Command.java index c40870dc54..24ab2ca020 100644 --- a/java/se/sics/mspsim/cli/Command.java +++ b/java/se/sics/mspsim/cli/Command.java @@ -23,7 +23,7 @@ public abstract class Command { public abstract int executeCommand(CommandContext context); /* default behavior is that it returns *this* instance (which might be bad in some cases) */ - public Object getInstance() { + public Command getInstance() { return this; } } diff --git a/java/se/sics/mspsim/cli/CommandHandler.java b/java/se/sics/mspsim/cli/CommandHandler.java index e45b5b40e4..d58e2e3276 100644 --- a/java/se/sics/mspsim/cli/CommandHandler.java +++ b/java/se/sics/mspsim/cli/CommandHandler.java @@ -1,6 +1,7 @@ package se.sics.mspsim.cli; import java.io.File; import java.io.PrintStream; +import java.net.URISyntaxException; import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; @@ -128,15 +129,45 @@ public int executeCommand(String commandLine, CommandContext context) { return -1; } + private File resolveScript(String script) { + // Only search for script files + if (!script.endsWith(".sc")) { + return null; + } + var scriptFile = new File(scriptDirectory, script); + if (scriptFile.exists()) { + return scriptFile; + } + scriptFile = new File("config/scripts", script); + if (scriptFile.exists()) { + return scriptFile; + } + File parent; + try { + parent = new File(CommandHandler.class.getProtectionDomain().getCodeSource().getLocation().toURI()) + .getParentFile().getParentFile(); + } catch (URISyntaxException e) { + parent = null; + } + if (parent != null) { + var scriptPath = "resources/main/scripts/" + script; + scriptFile = new File(parent, scriptPath); + if (!scriptFile.exists()) { // Running from gradle + scriptFile= new File(parent.getParentFile(), scriptPath); + } + } + return scriptFile.exists() ? scriptFile : null; + } + // This will return an instance that can be configured - // which is basically not OK... TODO - fix this!!! private Command getCommand(String cmd) { Command command = commands.get(cmd); if (command != null) { - return (Command) command.getInstance(); + return command.getInstance(); } - File scriptFile = new File(scriptDirectory, cmd); - if (scriptFile.isFile() && scriptFile.canRead()) { + File scriptFile = resolveScript(cmd); + if (scriptFile != null && scriptFile.isFile() && scriptFile.canRead()) { return new ScriptCommand(scriptFile); } return null; diff --git a/java/se/sics/mspsim/cli/WindowTarget.java b/java/se/sics/mspsim/cli/WindowTarget.java index 9764dde5c4..b67155c2a6 100644 --- a/java/se/sics/mspsim/cli/WindowTarget.java +++ b/java/se/sics/mspsim/cli/WindowTarget.java @@ -41,6 +41,9 @@ private void processLine(CommandContext context, String line) { if (line.startsWith("#!")) { line = line.substring(2); String[] parts = CommandParser.parseLine(line); + if (parts == null || parts.length == 0) { + return; + } String cmd = parts[0]; if ("bounds".equals(cmd)) { try { @@ -56,6 +59,7 @@ private void processLine(CommandContext context, String line) { dataHandler.setProperty("title", new String[] {args}); } } else if ("type".equals(cmd)) { + var hasOldDataHandler = dataHandler != null; if ("line-sample".equals(parts[1])) { dataHandler = new LineSampleChart(); } else if ("line".equals(parts[1])) { @@ -64,9 +68,11 @@ private void processLine(CommandContext context, String line) { context.err.println("Unknown window data handler type: " + parts[1]); } if (dataHandler != null) { - System.out.println("Replacing window data handler! " + parts[1] + " " + dataHandler); + if (hasOldDataHandler) { + System.out.println("Replacing window data handler! " + parts[1] + " " + dataHandler); + window.removeAll(); + } JComponent dataComponent = dataHandler.getComponent(); - window.removeAll(); window.add(dataComponent); String title = window.getTitle(); if (title != null) { diff --git a/java/se/sics/mspsim/extutil/jfreechart/JFreeWindowDataHandler.java b/java/se/sics/mspsim/extutil/jfreechart/JFreeWindowDataHandler.java index ce987065fa..3b62e0578a 100644 --- a/java/se/sics/mspsim/extutil/jfreechart/JFreeWindowDataHandler.java +++ b/java/se/sics/mspsim/extutil/jfreechart/JFreeWindowDataHandler.java @@ -40,6 +40,8 @@ */ package se.sics.mspsim.extutil.jfreechart; +import java.util.ArrayList; +import java.util.HashMap; import org.jfree.data.general.Series; import se.sics.mspsim.cli.AbstractWindowDataHandler; @@ -49,6 +51,16 @@ */ public abstract class JFreeWindowDataHandler extends AbstractWindowDataHandler { + private HashMap labelMap = new HashMap<>(); + + protected String getDataSeriesLabel(int index) { + return labelMap.get(index); + } + + protected String getDataSeriesLabel(int index, String defaultLabel) { + var label = getDataSeriesLabel(index); + return label != null ? label : defaultLabel; + } public abstract int getDataSeriesCount(); public abstract Series getDataSeries(int index); @@ -58,13 +70,8 @@ public abstract class JFreeWindowDataHandler extends AbstractWindowDataHandler { */ @Override public void setProperty(int index, String param, String[] args) { - if (index > getDataSeriesCount()) { - throw new IndexOutOfBoundsException("Illegal index: " + index); - } if ("label".equals(param)) { - getDataSeries(index).setKey(args[0]); - getComponent().revalidate(); - getComponent().repaint(); + labelMap.put(index, args[0]); } } diff --git a/java/se/sics/mspsim/extutil/jfreechart/LineChart.java b/java/se/sics/mspsim/extutil/jfreechart/LineChart.java index 64708b31d9..d5d414efdb 100644 --- a/java/se/sics/mspsim/extutil/jfreechart/LineChart.java +++ b/java/se/sics/mspsim/extutil/jfreechart/LineChart.java @@ -114,10 +114,11 @@ public void setProperty(String param, String[] args) { } private void addSeries() { - XYSeries dataSeries = new XYSeries("series " + (getDataSeriesCount() + 1)); + int index = getDataSeriesCount(); + XYSeries dataSeries = new XYSeries(getDataSeriesLabel(index, "series " + (index + 1))); dataSeries.setMaximumItemCount(200); // renderer.setSeriesPaint(0, Color.black); - renderer.setSeriesShapesVisible(getDataSeriesCount(), false); + renderer.setSeriesShapesVisible(index, false); dataset.addSeries(dataSeries); } diff --git a/java/se/sics/mspsim/ui/JFrameWindowManager.java b/java/se/sics/mspsim/ui/JFrameWindowManager.java index 20668ff4f3..a91a8b6412 100644 --- a/java/se/sics/mspsim/ui/JFrameWindowManager.java +++ b/java/se/sics/mspsim/ui/JFrameWindowManager.java @@ -28,16 +28,17 @@ public void pack() { @Override public void add(Component component) { - window.add(component); + window.getContentPane().add(component); if (!restored) { restored = true; WindowUtils.restoreWindowBounds(name, window); } + window.revalidate(); } @Override public void removeAll() { - window.removeAll(); + window.getContentPane().removeAll(); } @Override