Skip to content

Commit

Permalink
Subscription fixes in config - removal of hardcoded subscriptions (#1401
Browse files Browse the repository at this point in the history
)

* Subscription fixes in config - removal of hardcoded subscriptions

* small runtime updates

* added test

* trying to make idempotent test
  • Loading branch information
supertick authored Feb 4, 2024
1 parent 350fafa commit 7746bc5
Show file tree
Hide file tree
Showing 9 changed files with 102 additions and 94 deletions.
71 changes: 71 additions & 0 deletions src/main/java/org/myrobotlab/config/ConfigUtils.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
}
83 changes: 0 additions & 83 deletions src/main/java/org/myrobotlab/service/InMoov2.java
Original file line number Diff line number Diff line change
Expand Up @@ -1123,7 +1123,6 @@ public void onStartConfig(String configName) {
*/
@Override
public void onStarted(String name) {
InMoov2Config c = (InMoov2Config) config;

log.info("onStarted {}", name);
try {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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<File> 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");
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/myrobotlab/service/InMoov2Arm.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, Double> map) {
onMove(map);
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/myrobotlab/service/InMoov2Hand.java
Original file line number Diff line number Diff line change
Expand Up @@ -490,7 +490,7 @@ public LeapData onLeapData(LeapData data) {
return data;
}

@Deprecated /* use onMove */
@Deprecated /* use onMove(map) */
public void onMoveHand(HashMap<String, Double> map) {
onMove(map);
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/myrobotlab/service/InMoov2Head.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, Double> map) {
onMove(map);
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/myrobotlab/service/InMoov2Torso.java
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public void disable() {
lowStom.disable();
}

@Deprecated /* use onMove */
@Deprecated /* use onMove(map) */
public void onMoveTorso(HashMap<String, Double> map) {
onMove(map);
}
Expand Down
10 changes: 9 additions & 1 deletion src/main/java/org/myrobotlab/service/Runtime.java
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ public class Runtime extends Service<RuntimeConfig> 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
Expand Down Expand Up @@ -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();
}

}
12 changes: 6 additions & 6 deletions src/main/java/org/myrobotlab/service/config/InMoov2Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
Expand Down
12 changes: 12 additions & 0 deletions src/test/java/org/myrobotlab/service/RuntimeTest.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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() {
Expand Down

0 comments on commit 7746bc5

Please sign in to comment.