From 7746bc58996e3ed82b1bf96069c50e6ab7a553fa Mon Sep 17 00:00:00 2001 From: GroG Date: Sun, 4 Feb 2024 10:39:44 -0800 Subject: [PATCH 1/2] Subscription fixes in config - removal of hardcoded subscriptions (#1401) * Subscription fixes in config - removal of hardcoded subscriptions * small runtime updates * added test * trying to make idempotent test --- .../org/myrobotlab/config/ConfigUtils.java | 71 ++++++++++++++++ .../java/org/myrobotlab/service/InMoov2.java | 83 ------------------- .../org/myrobotlab/service/InMoov2Arm.java | 2 +- .../org/myrobotlab/service/InMoov2Hand.java | 2 +- .../org/myrobotlab/service/InMoov2Head.java | 2 +- .../org/myrobotlab/service/InMoov2Torso.java | 2 +- .../java/org/myrobotlab/service/Runtime.java | 10 ++- .../service/config/InMoov2Config.java | 12 +-- .../org/myrobotlab/service/RuntimeTest.java | 12 +++ 9 files changed, 102 insertions(+), 94 deletions(-) create mode 100644 src/main/java/org/myrobotlab/config/ConfigUtils.java diff --git a/src/main/java/org/myrobotlab/config/ConfigUtils.java b/src/main/java/org/myrobotlab/config/ConfigUtils.java new file mode 100644 index 0000000000..35c8a776a8 --- /dev/null +++ b/src/main/java/org/myrobotlab/config/ConfigUtils.java @@ -0,0 +1,71 @@ +package org.myrobotlab.config; + +import java.io.File; +import java.io.IOException; + +import org.myrobotlab.codec.CodecUtils; +import org.myrobotlab.framework.StartYml; +import org.myrobotlab.io.FileIO; +import org.myrobotlab.service.Runtime; +import org.myrobotlab.service.config.RuntimeConfig; + +public class ConfigUtils { + + /** + * This gets the current resource root without starting a Runtime instance if + * not already started. The resource root depends on config, if Runtime is + * running the logic and current config name is already available. If Runtime + * is not running, we need to go through a series of steps to deterime where + * the resource root is configured. + * + * @return + */ + public static String getResourceRoot() { + + String resource = "resource"; + + // check if runtime is running + if (!Runtime.isAvailable()) { + // check for start.yml + + File checkStartYml = new File("start.yml"); + StartYml startYml = new StartYml(); + if (checkStartYml.exists()) { + String yml; + try { + yml = FileIO.toString("start.yml"); + startYml = CodecUtils.fromYaml(yml, StartYml.class); + + // see if autostart is on with a config + if (startYml.enable) { + // use that config to find runtime.yml + + File runtimeYml = new File(Runtime.ROOT_CONFIG_DIR + File.separator + startYml.config + File.separator + "runtime.yml"); + if (runtimeYml.exists()) { + // parse that file look for resource: entry in file + RuntimeConfig config = (RuntimeConfig) CodecUtils.readServiceConfig(runtimeYml.getAbsolutePath()); + resource = config.resource; + } + + } else { + // start.yml enable = false / so we'll use default config + File runtimeYml = new File(Runtime.ROOT_CONFIG_DIR + File.separator + "default" + File.separator + "runtime.yml"); + if (runtimeYml.exists()) { + // parse that file look for resource: entry in file + RuntimeConfig config = (RuntimeConfig) CodecUtils.readServiceConfig(runtimeYml.getAbsolutePath()); + resource = config.resource; + } + } + + } catch (IOException e) { + // problem getting or parsing + // going to assume default "resource" + } + } // no startYml + return resource; + } else { + // Runtime is available - ask it + return Runtime.getInstance().getConfig().resource; + } + } +} diff --git a/src/main/java/org/myrobotlab/service/InMoov2.java b/src/main/java/org/myrobotlab/service/InMoov2.java index d9b93659cd..53d2019ba7 100644 --- a/src/main/java/org/myrobotlab/service/InMoov2.java +++ b/src/main/java/org/myrobotlab/service/InMoov2.java @@ -1123,7 +1123,6 @@ public void onStartConfig(String configName) { */ @Override public void onStarted(String name) { - InMoov2Config c = (InMoov2Config) config; log.info("onStarted {}", name); try { @@ -1157,81 +1156,21 @@ public void onStarted(String name) { chatBot.attachTextListener(getPeerName("htmlFilter")); startPeer("htmlFilter"); break; - case "controller3": - break; - case "controller4": - break; case "ear": AbstractSpeechRecognizer ear = (AbstractSpeechRecognizer) Runtime.getService(name); ear.attachTextListener(getPeerName("chatBot")); break; - case "eyeTracking": - break; - case "fsm": - break; - case "gpt3": - break; - case "head": - addListener("publishMoveHead", name); - break; - case "headTracking": - break; case "htmlFilter": TextPublisher htmlFilter = (TextPublisher) Runtime.getService(name); htmlFilter.attachTextListener(getPeerName("mouth")); break; - case "imageDisplay": - break; - case "leap": - break; - case "left": - break; - case "leftArm": - addListener("publishMoveLeftArm", name, "onMoveArm"); - break; - case "leftHand": - addListener("publishMoveLeftHand", name, "onMoveHand"); - break; case "mouth": mouth = (AbstractSpeechSynthesis) Runtime.getService(name); mouth.attachSpeechListener(getPeerName("ear")); break; - case "mouthControl": - break; - case "neoPixel": - break; case "opencv": subscribeTo(name, "publishOpenCVData"); break; - case "openni": - break; - case "openWeatherMap": - break; - case "pid": - break; - case "pir": - break; - case "random": - break; - case "right": - break; - case "rightArm": - addListener("publishMoveRightArm", name, "onMoveArm"); - break; - case "rightHand": - addListener("publishMoveRightHand", name, "onMoveHand"); - break; - case "servoMixer": - break; - case "simulator": - break; - case "torso": - addListener("publishMoveTorso", name); - break; - case "ultrasonicRight": - break; - case "ultrasonicLeft": - break; default: log.warn("unknown peer %s not hanled in onStarted", peerKey); break; @@ -1991,28 +1930,6 @@ public void startService() { // chatbot getresponse attached to publishEvent addListener("publishEvent", getPeerName("chatBot"), "getResponse"); - try { - // copy config if it doesn't already exist - String resourceBotDir = FileIO.gluePaths(getResourceDir(), "config"); - List files = FileIO.getFileList(resourceBotDir); - for (File f : files) { - String botDir = "data/config/" + f.getName(); - File bDir = new File(botDir); - if (bDir.exists() || !f.isDirectory()) { - log.info("skipping data/config/{}", botDir); - } else { - log.info("will copy new data/config/{}", botDir); - try { - FileIO.copy(f.getAbsolutePath(), botDir); - } catch (Exception e) { - error(e); - } - } - } - } catch (Exception e) { - error(e); - } - runtime.invoke("publishConfigList"); } diff --git a/src/main/java/org/myrobotlab/service/InMoov2Arm.java b/src/main/java/org/myrobotlab/service/InMoov2Arm.java index d190af46e8..3b74a3bf20 100644 --- a/src/main/java/org/myrobotlab/service/InMoov2Arm.java +++ b/src/main/java/org/myrobotlab/service/InMoov2Arm.java @@ -89,7 +89,7 @@ public static DHRobotArm getDHRobotArm(String name, String side) { return arm; } - @Deprecated /* use onMove */ + @Deprecated /* use onMove(map) */ public void onMoveArm(HashMap map) { onMove(map); } diff --git a/src/main/java/org/myrobotlab/service/InMoov2Hand.java b/src/main/java/org/myrobotlab/service/InMoov2Hand.java index 876bfcf5ca..b30c2bd792 100644 --- a/src/main/java/org/myrobotlab/service/InMoov2Hand.java +++ b/src/main/java/org/myrobotlab/service/InMoov2Hand.java @@ -490,7 +490,7 @@ public LeapData onLeapData(LeapData data) { return data; } - @Deprecated /* use onMove */ + @Deprecated /* use onMove(map) */ public void onMoveHand(HashMap map) { onMove(map); } diff --git a/src/main/java/org/myrobotlab/service/InMoov2Head.java b/src/main/java/org/myrobotlab/service/InMoov2Head.java index f77ef9c823..f3f5edf366 100644 --- a/src/main/java/org/myrobotlab/service/InMoov2Head.java +++ b/src/main/java/org/myrobotlab/service/InMoov2Head.java @@ -221,7 +221,7 @@ public void lookAt(Double x, Double y, Double z) { log.info("object distance is {},rothead servo {},neck servo {} ", distance, rotation, colatitude); } - @Deprecated /* use onMoov */ + @Deprecated /* use onMove(map) */ public void onMoveHead(HashMap map) { onMove(map); } diff --git a/src/main/java/org/myrobotlab/service/InMoov2Torso.java b/src/main/java/org/myrobotlab/service/InMoov2Torso.java index f3953699c5..75fa410ca2 100644 --- a/src/main/java/org/myrobotlab/service/InMoov2Torso.java +++ b/src/main/java/org/myrobotlab/service/InMoov2Torso.java @@ -94,7 +94,7 @@ public void disable() { lowStom.disable(); } - @Deprecated /* use onMove */ + @Deprecated /* use onMove(map) */ public void onMoveTorso(HashMap map) { onMove(map); } diff --git a/src/main/java/org/myrobotlab/service/Runtime.java b/src/main/java/org/myrobotlab/service/Runtime.java index 3a66750f54..e5da31305f 100644 --- a/src/main/java/org/myrobotlab/service/Runtime.java +++ b/src/main/java/org/myrobotlab/service/Runtime.java @@ -229,7 +229,7 @@ public class Runtime extends Service implements MessageListener, /** * default parent path of configPath static ! */ - final static protected String ROOT_CONFIG_DIR = DATA_DIR + fs + "config"; + public final static String ROOT_CONFIG_DIR = DATA_DIR + fs + "config"; /** * number of services created by this runtime @@ -5408,4 +5408,12 @@ public static void removeConfig(String configName) { } } + /** + * Method used to determine is runtime is running without starting it + * @return true if available + */ + static public boolean isAvailable() { + return runtime != null && runtime.isRunning(); + } + } diff --git a/src/main/java/org/myrobotlab/service/config/InMoov2Config.java b/src/main/java/org/myrobotlab/service/config/InMoov2Config.java index 55630c9f1b..c0d624d3e6 100644 --- a/src/main/java/org/myrobotlab/service/config/InMoov2Config.java +++ b/src/main/java/org/myrobotlab/service/config/InMoov2Config.java @@ -522,12 +522,12 @@ public Plan getDefault(Plan plan, String name) { listeners.add(new Listener("publishProcessMessage", getPeerName("py4j"), "onPythonMessage")); // InMoov2 --to--> InMoov2 - listeners.add(new Listener("publishMoveHead", name)); - listeners.add(new Listener("publishMoveRightArm", name)); - listeners.add(new Listener("publishMoveLeftArm", name)); - listeners.add(new Listener("publishMoveRightHand", name)); - listeners.add(new Listener("publishMoveLeftHand", name)); - listeners.add(new Listener("publishMoveTorso", name)); + listeners.add(new Listener("publishMoveHead", getPeerName("head"), "onMove")); + listeners.add(new Listener("publishMoveRightArm", getPeerName("rightArm"), "onMove")); + listeners.add(new Listener("publishMoveLeftArm", getPeerName("leftArm"), "onMove")); + listeners.add(new Listener("publishMoveRightHand", getPeerName("rightHand"), "onMove")); + listeners.add(new Listener("publishMoveLeftHand", getPeerName("leftHand"), "onMove")); + listeners.add(new Listener("publishMoveTorso", getPeerName("torso"), "onMove")); // service --to--> InMoov2 AudioFileConfig mouth_audioFile = (AudioFileConfig) plan.get(getPeerName("mouth.audioFile")); diff --git a/src/test/java/org/myrobotlab/service/RuntimeTest.java b/src/test/java/org/myrobotlab/service/RuntimeTest.java index 50c6c03268..a20a13db38 100644 --- a/src/test/java/org/myrobotlab/service/RuntimeTest.java +++ b/src/test/java/org/myrobotlab/service/RuntimeTest.java @@ -1,6 +1,7 @@ package org.myrobotlab.service; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; import java.util.Date; @@ -103,6 +104,17 @@ public void testRuntimeLocale() { assertEquals("fr-FR", l.toString()); } + + @Test + public void testRuntimeIsAvailable() { + Runtime.getInstance(); + assertTrue(Runtime.isAvailable()); + Runtime.releaseAll(true, true); + assertFalse(Runtime.isAvailable()); + Runtime.getInstance(); + assertTrue(Runtime.isAvailable()); + } + @Test public void testGetDescribeMessage() { From da0308a5c02ba3b2a776634df8df93df7bbe4130 Mon Sep 17 00:00:00 2001 From: grog Date: Sun, 4 Feb 2024 10:58:17 -0800 Subject: [PATCH 2/2] finally fixed this dumb thing --- src/test/java/org/myrobotlab/service/RandomTest.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/test/java/org/myrobotlab/service/RandomTest.java b/src/test/java/org/myrobotlab/service/RandomTest.java index 9c3739f510..7c8add5923 100644 --- a/src/test/java/org/myrobotlab/service/RandomTest.java +++ b/src/test/java/org/myrobotlab/service/RandomTest.java @@ -74,7 +74,7 @@ public void testService() throws Exception { // disable one method - leave other enabled random.disable("clock.startClock"); clock.stopClock(); - clock.setInterval(999999); + clock.setInterval(9999); sleep(200); assertTrue("clock should not be started 3", !clock.isClockRunning()); assertTrue(String.format("random method 2 should be %d => 5000 values", clock.getInterval()), 5000 <= clock.getInterval()); @@ -83,9 +83,9 @@ public void testService() throws Exception { // disable all random.disable(); sleep(200); - clock.setInterval(999999); + clock.setInterval(9999); assertTrue("clock should not be started 4", !clock.isClockRunning()); - assertEquals(999999, (long)clock.getInterval()); + assertEquals(9999, (long)clock.getInterval()); // re-enable all that were previously enabled but not explicitly disabled ones random.enable();