Skip to content

Commit

Permalink
Random service with worky global enable/disable
Browse files Browse the repository at this point in the history
  • Loading branch information
supertick committed Oct 24, 2023
1 parent 1dc0085 commit cd9806d
Showing 1 changed file with 25 additions and 8 deletions.
33 changes: 25 additions & 8 deletions src/main/java/org/myrobotlab/service/Random.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
}
}
}

Expand Down Expand Up @@ -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
Expand All @@ -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);
}
}
}
}
Expand All @@ -335,6 +348,7 @@ public void disable() {
// events
purgeTasks();
enabled = false;
broadcastState();
}

public void enable() {
Expand All @@ -345,7 +359,9 @@ public void enable() {
addTask(fullKey, 0, msg.interval, "process", fullKey);
}
}

enabled = true;
broadcastState();
}

public void purge() {
Expand Down Expand Up @@ -392,9 +408,10 @@ public static void main(String[] args) {
List<String> ret = random.getServiceList();
Set<String> mi = random.getMethodsFromName("c1");
List<MethodEntry> 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");

Expand Down

0 comments on commit cd9806d

Please sign in to comment.