Mqtt is simulated
This commit is contained in:
parent
875f188a47
commit
cc768cdba9
@ -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");
|
||||
}
|
||||
|
@ -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");
|
||||
|
@ -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());
|
||||
}
|
||||
|
||||
|
@ -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);
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user