Skip to content

Commit

Permalink
Merge pull request #1460 from nfi/mspsim-cli
Browse files Browse the repository at this point in the history
mspsim: fix deprecated warnings and search for CLI scripts
  • Loading branch information
nfi authored Nov 5, 2024
2 parents d448183 + a6c4d6a commit 154ff06
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 18 deletions.
4 changes: 2 additions & 2 deletions config/scripts/duty.sc
Original file line number Diff line number Diff line change
@@ -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
echo "#!title Duty Cycle" | window duty
duty 10 "MSP430 Core.active" CC2420.listen CC2420.transmit "Tmote Sky.0" | window duty
2 changes: 1 addition & 1 deletion java/se/sics/mspsim/cli/Command.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
37 changes: 34 additions & 3 deletions java/se/sics/mspsim/cli/CommandHandler.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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;
Expand Down
10 changes: 8 additions & 2 deletions java/se/sics/mspsim/cli/WindowTarget.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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])) {
Expand All @@ -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) {
Expand Down
19 changes: 13 additions & 6 deletions java/se/sics/mspsim/extutil/jfreechart/JFreeWindowDataHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -49,6 +51,16 @@
*/
public abstract class JFreeWindowDataHandler extends AbstractWindowDataHandler {

private HashMap<Integer,String> 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);
Expand All @@ -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]);
}
}

Expand Down
5 changes: 3 additions & 2 deletions java/se/sics/mspsim/extutil/jfreechart/LineChart.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
5 changes: 3 additions & 2 deletions java/se/sics/mspsim/ui/JFrameWindowManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 154ff06

Please sign in to comment.