Added MQTT simulation

This commit is contained in:
Ollo 2021-03-24 21:01:34 +01:00
parent 92a1c2c8d2
commit 94e554843f
2 changed files with 85 additions and 0 deletions

View File

@ -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());

View File

@ -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;
}
}
}