From cd9806d19be166cfd46a7b8a5b45d943e3ea90d5 Mon Sep 17 00:00:00 2001 From: grog Date: Tue, 24 Oct 2023 09:20:54 -0700 Subject: [PATCH] Random service with worky global enable/disable --- .../java/org/myrobotlab/service/Random.java | 33 ++++++++++++++----- 1 file changed, 25 insertions(+), 8 deletions(-) diff --git a/src/main/java/org/myrobotlab/service/Random.java b/src/main/java/org/myrobotlab/service/Random.java index 93b8178fd2..dca027aa3a 100644 --- a/src/main/java/org/myrobotlab/service/Random.java +++ b/src/main/java/org/myrobotlab/service/Random.java @@ -183,14 +183,17 @@ public void addRandom(long minIntervalMs, long maxIntervalMs, String name, Strin msg.interval = getRandom(minIntervalMs, maxIntervalMs); log.info("add random message {} in {} ms", key, msg.interval); - addTask(key, 0, msg.interval, "process", key); + if (enabled) { + // only if global enabled is enabled do we start the task + addTask(key, 0, msg.interval, "process", key); + } broadcastState(); } public void process(String key) { - if (!enabled) { - return; - } + // if (!enabled) { + // return; + // } RandomMessage msg = randomData.get(key); if (msg == null || !msg.enabled) { @@ -230,7 +233,11 @@ public void process(String key) { purgeTask(key); if (!msg.oneShot) { msg.interval = getRandom(msg.minIntervalMs, msg.maxIntervalMs); - addTask(key, 0, msg.interval, "process", key); + // must re-schedule unless one shot + if (enabled) { + // only if global enabled is enabled do we start the task + addTask(key, 0, msg.interval, "process", key); + } } } @@ -316,7 +323,10 @@ public void enable(String key) { return; } randomData.get(key).enabled = true; - addTask(key, 0, msg.interval, "process", key); + if (enabled) { + // only if global enabled is enabled do we start the task + addTask(key, 0, msg.interval, "process", key); + } return; } // must be name - disable "all" for this service @@ -325,7 +335,10 @@ public void enable(String key) { if (msg.name.equals(name)) { msg.enabled = true; String fullKey = String.format("%s.%s", msg.name, msg.method); - addTask(fullKey, 0, msg.interval, "process", fullKey); + if (enabled) { + // only if global enabled is enabled do we start the task + addTask(fullKey, 0, msg.interval, "process", fullKey); + } } } } @@ -335,6 +348,7 @@ public void disable() { // events purgeTasks(); enabled = false; + broadcastState(); } public void enable() { @@ -345,7 +359,9 @@ public void enable() { addTask(fullKey, 0, msg.interval, "process", fullKey); } } + enabled = true; + broadcastState(); } public void purge() { @@ -392,9 +408,10 @@ public static void main(String[] args) { List ret = random.getServiceList(); Set mi = random.getMethodsFromName("c1"); List mes = MethodCache.getInstance().query("Clock", "setInterval"); - + random.disable(); random.addRandom(200, 1000, "i01", "setHeadSpeed", 8, 20, 8, 20, 8, 20); random.addRandom(200, 1000, "i01", "moveHead", 65, 115, 65, 115, 65, 115); + random.enable(); // Python python = (Python) Runtime.start("python", "Python");