From 87c157fd109ae18c75d1202797e2d32d52dd5cd5 Mon Sep 17 00:00:00 2001 From: Ollo Date: Tue, 16 Mar 2021 22:12:02 +0100 Subject: [PATCH] Simulation starts implementation of dynamic nodemcu tasks --- simulation/src/de/c3ma/ollo/LuaThreadTmr.java | 4 +- .../src/de/c3ma/ollo/mockup/ESP8266Tmr.java | 69 +++++++++++++++++++ 2 files changed, 71 insertions(+), 2 deletions(-) diff --git a/simulation/src/de/c3ma/ollo/LuaThreadTmr.java b/simulation/src/de/c3ma/ollo/LuaThreadTmr.java index 0a958fd..20d61e8 100644 --- a/simulation/src/de/c3ma/ollo/LuaThreadTmr.java +++ b/simulation/src/de/c3ma/ollo/LuaThreadTmr.java @@ -18,7 +18,7 @@ public class LuaThreadTmr extends Thread { private LuaValue code; - private final int delay; + private int delay; private final int timerNumber; @@ -28,7 +28,7 @@ public class LuaThreadTmr extends Thread { this.delay = delay; this.timerNumber = timerNumber; } - + @Override public void run() { try { diff --git a/simulation/src/de/c3ma/ollo/mockup/ESP8266Tmr.java b/simulation/src/de/c3ma/ollo/mockup/ESP8266Tmr.java index 4c1782a..a57e750 100644 --- a/simulation/src/de/c3ma/ollo/mockup/ESP8266Tmr.java +++ b/simulation/src/de/c3ma/ollo/mockup/ESP8266Tmr.java @@ -6,6 +6,7 @@ import org.luaj.vm2.Varargs; import org.luaj.vm2.lib.OneArgFunction; import org.luaj.vm2.lib.TwoArgFunction; import org.luaj.vm2.lib.VarArgFunction; +import org.luaj.vm2.lib.ZeroArgFunction; import de.c3ma.ollo.LuaThreadTmr; @@ -21,6 +22,8 @@ public class ESP8266Tmr extends TwoArgFunction { private static final int MAXTHREADS = 7; private static LuaThreadTmr[] allThreads = new LuaThreadTmr[MAXTHREADS]; + private static LuaThreadTmr[] dynamicThreads = new LuaThreadTmr[MAXTHREADS]; + private static int dynamicThreadCounter=0; public static int gTimingFactor = 1; @@ -30,6 +33,7 @@ public class ESP8266Tmr extends TwoArgFunction { final LuaTable tmr = new LuaTable(); tmr.set("stop", new stop()); tmr.set("alarm", new alarm()); + tmr.set("create", new create()); env.set("tmr", tmr); env.get("package").get("loaded").set("tmr", tmr); @@ -37,6 +41,9 @@ public class ESP8266Tmr extends TwoArgFunction { for (Thread t : allThreads) { t = null; } + for (Thread t : dynamicThreads) { + t = null; + } return tmr; } @@ -87,6 +94,68 @@ public class ESP8266Tmr extends TwoArgFunction { } } + private class dynRegister extends VarArgFunction { + private final int dynIndex; + public dynRegister(int index) { + this.dynIndex = index; + } + public Varargs invoke(Varargs varargs) { + if (varargs.narg() == 4) { + final String endlessloop = varargs.arg(3).toString().toString(); + final int delay = varargs.arg(2).toint(); + final LuaValue code = varargs.arg(4); + dynamicThreads[dynIndex] = new LuaThreadTmr(dynIndex, code, (endlessloop.contains("AUTO")), Math.max(delay / gTimingFactor, 1)); + System.out.println("[TMR] DynTimer" + dynamicThreadCounter + " registered"); + } + return LuaValue.valueOf(true); + } + } + + private class dynStart extends VarArgFunction { + private final int dynIndex; + public dynStart(int index) { + this.dynIndex = index; + } + public Varargs invoke(Varargs varargs) { + if (varargs.narg()== 0) { + if (dynamicThreads[dynIndex] != null) { + dynamicThreads[dynIndex].start(); + System.out.println("[TMR] DynTimer" + dynIndex + " started"); + } + } + return LuaValue.valueOf(true); + } + } + + private class dynStop extends ZeroArgFunction { + private final int dynIndex; + public dynStop(int index) { + this.dynIndex = index; + } + public LuaValue call() { + if (dynamicThreads[dynIndex] != null) { + dynamicThreads[dynIndex].stopThread(); + dynamicThreads[dynIndex] = null; + System.out.println("[TMR] DynTimer" + dynIndex + " stopped"); + } + return LuaValue.valueOf(true); + } + } + + private class create extends ZeroArgFunction { + public LuaValue call() { + if (dynamicThreadCounter >= MAXTHREADS) { + return LuaValue.error("[TMR] DynTimer" + dynamicThreadCounter + " exeeded maximum"); + } + final LuaTable dynTimer = new LuaTable(); + dynTimer.set("register", new dynRegister(dynamicThreadCounter)); + dynTimer.set("start", new dynStart(dynamicThreadCounter)); + dynTimer.set("unregister", new dynStop(dynamicThreadCounter)); + dynamicThreadCounter++; + return dynTimer; + } + } + public void stopAllTimer() { for (int i = 0; i < allThreads.length; i++) { stopTmr(i);