From 94e554843f36563b79bef292311fd7c7b3b416a9 Mon Sep 17 00:00:00 2001 From: Ollo Date: Wed, 24 Mar 2021 21:01:34 +0100 Subject: [PATCH] Added MQTT simulation --- .../src/de/c3ma/ollo/WS2812Simulation.java | 3 + .../src/de/c3ma/ollo/mockup/ESP8266Mqtt.java | 82 +++++++++++++++++++ 2 files changed, 85 insertions(+) create mode 100644 simulation/src/de/c3ma/ollo/mockup/ESP8266Mqtt.java diff --git a/simulation/src/de/c3ma/ollo/WS2812Simulation.java b/simulation/src/de/c3ma/ollo/WS2812Simulation.java index 4c15f8d..5dfb44a 100644 --- a/simulation/src/de/c3ma/ollo/WS2812Simulation.java +++ b/simulation/src/de/c3ma/ollo/WS2812Simulation.java @@ -14,6 +14,7 @@ import de.c3ma.ollo.mockup.DoFileFunction; import de.c3ma.ollo.mockup.ESP8266Adc; import de.c3ma.ollo.mockup.ESP8266File; import de.c3ma.ollo.mockup.ESP8266Gpio; +import de.c3ma.ollo.mockup.ESP8266Mqtt; import de.c3ma.ollo.mockup.ESP8266Net; import de.c3ma.ollo.mockup.ESP8266Node; import de.c3ma.ollo.mockup.ESP8266Time; @@ -39,6 +40,7 @@ public class WS2812Simulation implements LuaSimulation { private DoFileFunction doFile = new DoFileFunction(globals); private ESP8266Ws2812 ws2812 = new ESP8266Ws2812(); private ESP8266Gpio gpio = new ESP8266Gpio(); + private ESP8266Mqtt mqtt = new ESP8266Mqtt(); private ESP8266Adc adc = new ESP8266Adc(); private String scriptName; @@ -50,6 +52,7 @@ public class WS2812Simulation implements LuaSimulation { globals.load(espNode); globals.load(adc); globals.load(gpio); + globals.load(mqtt); globals.load(new ESP8266Wifi()); globals.load(new ESP8266Net()); globals.load(new ESP8266Time()); diff --git a/simulation/src/de/c3ma/ollo/mockup/ESP8266Mqtt.java b/simulation/src/de/c3ma/ollo/mockup/ESP8266Mqtt.java new file mode 100644 index 0000000..65149cb --- /dev/null +++ b/simulation/src/de/c3ma/ollo/mockup/ESP8266Mqtt.java @@ -0,0 +1,82 @@ +package de.c3ma.ollo.mockup; + +import org.luaj.vm2.LuaTable; +import org.luaj.vm2.LuaValue; +import org.luaj.vm2.Varargs; +import org.luaj.vm2.lib.TwoArgFunction; +import org.luaj.vm2.lib.VarArgFunction; + +/** + * + * @author ollo + * + */ +public class ESP8266Mqtt extends TwoArgFunction { + + @Override + public LuaValue call(LuaValue modname, LuaValue env) { + env.checkglobals(); + final LuaTable mqtt = new LuaTable(); + mqtt.set("Client", new MqttClient()); + env.set("mqtt", mqtt); + env.get("package").get("loaded").set("tmr", mqtt); + + return mqtt; + } + + private class MqttClient extends VarArgFunction { + public LuaValue invoke(Varargs varargs) { + final LuaTable dynMqtt = new LuaTable(); + if (varargs.narg() == 2) { + final String client = varargs.arg(2).toString().toString(); + final int timeout = varargs.arg(3).toint(); + dynMqtt.set("on", new OnMqtt(client, timeout)); + dynMqtt.set("publish", new PublishMqtt()); + dynMqtt.set("subscribe", new SubscribeMqtt()); + System.out.println("[MQTT] New " + client + " client"); + } + return dynMqtt; + } + } + + private class OnMqtt extends VarArgFunction { + + private String client=null; + private int timeout = 0; + + private OnMqtt(String client, int timeout) { + this.client = client; + this.timeout = timeout; + } + + public LuaValue invoke(Varargs varargs) { + final LuaTable onMqtt = new LuaTable(); + if (varargs.narg() == 2) { + System.out.println("[MQTT] On " + this.client); + } + return onMqtt; + } + } + + private class PublishMqtt extends VarArgFunction { + + public LuaValue invoke(Varargs varargs) { + final LuaTable onMqtt = new LuaTable(); + if (varargs.narg() == 2) { + System.out.println("[MQTT] publish "); + } + return onMqtt; + } + } + + private class SubscribeMqtt extends VarArgFunction { + + public LuaValue invoke(Varargs varargs) { + final LuaTable onMqtt = new LuaTable(); + if (varargs.narg() == 2) { + System.out.println("[MQTT] subscribe "); + } + return onMqtt; + } + } +}