From cc768cdba9ed2f0caabca313e54de90937a7acda Mon Sep 17 00:00:00 2001 From: Ollo Date: Sun, 23 Jan 2022 14:51:42 +0100 Subject: [PATCH] Mqtt is simulated --- simulation/src/de/c3ma/ollo/LuaThreadTmr.java | 33 ++++++++++++++++++- .../src/de/c3ma/ollo/mockup/ESP8266Mqtt.java | 14 ++++---- .../src/de/c3ma/ollo/mockup/ESP8266Node.java | 2 +- .../src/de/c3ma/ollo/mockup/ESP8266Tmr.java | 2 +- 4 files changed, 42 insertions(+), 9 deletions(-) diff --git a/simulation/src/de/c3ma/ollo/LuaThreadTmr.java b/simulation/src/de/c3ma/ollo/LuaThreadTmr.java index 20d61e8..2967e96 100644 --- a/simulation/src/de/c3ma/ollo/LuaThreadTmr.java +++ b/simulation/src/de/c3ma/ollo/LuaThreadTmr.java @@ -1,5 +1,6 @@ package de.c3ma.ollo; +import org.luaj.vm2.LuaError; import org.luaj.vm2.LuaValue; /** @@ -11,6 +12,7 @@ import org.luaj.vm2.LuaValue; */ public class LuaThreadTmr extends Thread { + private final int NO_TIMER = -2342; private boolean running = true; @@ -22,6 +24,8 @@ public class LuaThreadTmr extends Thread { private final int timerNumber; + private LuaValue[] arguments; + public LuaThreadTmr(int timerNumber, LuaValue code, boolean endlessloop, int delay) { this.code = code; this.running = endlessloop; @@ -29,15 +33,42 @@ public class LuaThreadTmr extends Thread { this.timerNumber = timerNumber; } + public LuaThreadTmr(LuaValue code, LuaValue arg1, LuaValue arg2, LuaValue arg3) { + this.code = code; + this.running = false; + this.delay = 1; + this.timerNumber = NO_TIMER; + arguments = new LuaValue[3]; + arguments[0] = arg1; + arguments[1] = arg2; + arguments[2] = arg3; + } + @Override public void run() { try { do { Thread.sleep(delay); if (code != null) { - code.call(); + if (arguments == null) { + code.call(); + } else { + switch (arguments.length) { + case 1: + code.call(arguments[0]); + break; + case 2: + code.call(arguments[0], arguments[1]); + break; + case 3: + code.call(arguments[0], arguments[1], arguments[2]); + break; + } + } } } while(running); + } catch (LuaError le) { + System.err.println("[TMR] Timer" + timerNumber + " interrupted, due:" + le.getMessage()); } catch(InterruptedException ie) { System.err.println("[TMR] Timer" + timerNumber + " interrupted"); } diff --git a/simulation/src/de/c3ma/ollo/mockup/ESP8266Mqtt.java b/simulation/src/de/c3ma/ollo/mockup/ESP8266Mqtt.java index 194bada..4e6bd58 100644 --- a/simulation/src/de/c3ma/ollo/mockup/ESP8266Mqtt.java +++ b/simulation/src/de/c3ma/ollo/mockup/ESP8266Mqtt.java @@ -17,6 +17,8 @@ import org.luaj.vm2.Varargs; import org.luaj.vm2.lib.TwoArgFunction; import org.luaj.vm2.lib.VarArgFunction; +import de.c3ma.ollo.LuaThreadTmr; + /** * * @author ollo @@ -72,7 +74,7 @@ public class ESP8266Mqtt extends TwoArgFunction implements IMqttMessageListener final LuaTable table = varargs.arg(1).checktable(); final String callback = varargs.arg(2).toString().toString(); final LuaValue code = varargs.arg(3); - System.out.println("[MQTT] On " + this.client + " " + callback); + System.out.println("[MQTT] on_" + callback + " " + this.client); onMqtt.set(ON_PREFIX + callback, code); } else { for(int i=0; i <= varargs.narg(); i++) { @@ -123,7 +125,7 @@ public class ESP8266Mqtt extends TwoArgFunction implements IMqttMessageListener private class SubscribeMqtt extends VarArgFunction { public LuaValue invoke(Varargs varargs) { - final LuaTable onMqtt = new LuaTable(); + final LuaTable subMqtt = new LuaTable(); final int numberArg = varargs.narg(); if (numberArg == 3) { final String topic = varargs.arg(2).toString().toString(); @@ -151,7 +153,7 @@ public class ESP8266Mqtt extends TwoArgFunction implements IMqttMessageListener } return LuaValue.NIL; } - return onMqtt; + return subMqtt; } } @@ -197,9 +199,9 @@ public class ESP8266Mqtt extends TwoArgFunction implements IMqttMessageListener public void messageArrived(String topic, MqttMessage message) throws Exception { LuaValue messageCallback = onMqtt.get(ON_PREFIX + MESSAGE); if (messageCallback != null) { - LuaValue call2 = messageCallback.call(LuaValue.NIL, - LuaValue.valueOf(topic), - LuaValue.valueOf(message.getPayload())); + LuaThreadTmr exec = new LuaThreadTmr(messageCallback, LuaValue.NIL, LuaValue.valueOf(topic), LuaValue.valueOf(message.getPayload())); + exec.start(); + System.out.println("[MQTT] message "+ topic + " : " + message + " received"); //FIXME call the LUA code } else { System.err.println("[MQTT] message "+ topic + " : " + message + " without callback"); diff --git a/simulation/src/de/c3ma/ollo/mockup/ESP8266Node.java b/simulation/src/de/c3ma/ollo/mockup/ESP8266Node.java index efc1d86..c13ffa1 100644 --- a/simulation/src/de/c3ma/ollo/mockup/ESP8266Node.java +++ b/simulation/src/de/c3ma/ollo/mockup/ESP8266Node.java @@ -78,7 +78,7 @@ public class ESP8266Node extends TwoArgFunction { @Override public LuaValue call() { - System.out.println("[Node] Heap"); + //System.out.println("[Node] Heap"); return LuaValue.valueOf(Runtime.getRuntime().freeMemory()); } diff --git a/simulation/src/de/c3ma/ollo/mockup/ESP8266Tmr.java b/simulation/src/de/c3ma/ollo/mockup/ESP8266Tmr.java index 116aedd..eaebda4 100644 --- a/simulation/src/de/c3ma/ollo/mockup/ESP8266Tmr.java +++ b/simulation/src/de/c3ma/ollo/mockup/ESP8266Tmr.java @@ -133,7 +133,7 @@ public class ESP8266Tmr extends TwoArgFunction { private class watchDog extends ZeroArgFunction { public LuaValue call() { - System.out.println("[TMR] Watchdog fed"); + //System.out.println("[TMR] Watchdog fed"); return LuaValue.valueOf(true); }