Mqtt is simulated
This commit is contained in:
parent
875f188a47
commit
cc768cdba9
@ -1,5 +1,6 @@
|
|||||||
package de.c3ma.ollo;
|
package de.c3ma.ollo;
|
||||||
|
|
||||||
|
import org.luaj.vm2.LuaError;
|
||||||
import org.luaj.vm2.LuaValue;
|
import org.luaj.vm2.LuaValue;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -11,6 +12,7 @@ import org.luaj.vm2.LuaValue;
|
|||||||
*/
|
*/
|
||||||
public class LuaThreadTmr extends Thread {
|
public class LuaThreadTmr extends Thread {
|
||||||
|
|
||||||
|
private final int NO_TIMER = -2342;
|
||||||
|
|
||||||
private boolean running = true;
|
private boolean running = true;
|
||||||
|
|
||||||
@ -22,6 +24,8 @@ public class LuaThreadTmr extends Thread {
|
|||||||
|
|
||||||
private final int timerNumber;
|
private final int timerNumber;
|
||||||
|
|
||||||
|
private LuaValue[] arguments;
|
||||||
|
|
||||||
public LuaThreadTmr(int timerNumber, LuaValue code, boolean endlessloop, int delay) {
|
public LuaThreadTmr(int timerNumber, LuaValue code, boolean endlessloop, int delay) {
|
||||||
this.code = code;
|
this.code = code;
|
||||||
this.running = endlessloop;
|
this.running = endlessloop;
|
||||||
@ -29,15 +33,42 @@ public class LuaThreadTmr extends Thread {
|
|||||||
this.timerNumber = timerNumber;
|
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
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
try {
|
try {
|
||||||
do {
|
do {
|
||||||
Thread.sleep(delay);
|
Thread.sleep(delay);
|
||||||
if (code != null) {
|
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);
|
} while(running);
|
||||||
|
} catch (LuaError le) {
|
||||||
|
System.err.println("[TMR] Timer" + timerNumber + " interrupted, due:" + le.getMessage());
|
||||||
} catch(InterruptedException ie) {
|
} catch(InterruptedException ie) {
|
||||||
System.err.println("[TMR] Timer" + timerNumber + " interrupted");
|
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.TwoArgFunction;
|
||||||
import org.luaj.vm2.lib.VarArgFunction;
|
import org.luaj.vm2.lib.VarArgFunction;
|
||||||
|
|
||||||
|
import de.c3ma.ollo.LuaThreadTmr;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* @author ollo
|
* @author ollo
|
||||||
@ -72,7 +74,7 @@ public class ESP8266Mqtt extends TwoArgFunction implements IMqttMessageListener
|
|||||||
final LuaTable table = varargs.arg(1).checktable();
|
final LuaTable table = varargs.arg(1).checktable();
|
||||||
final String callback = varargs.arg(2).toString().toString();
|
final String callback = varargs.arg(2).toString().toString();
|
||||||
final LuaValue code = varargs.arg(3);
|
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);
|
onMqtt.set(ON_PREFIX + callback, code);
|
||||||
} else {
|
} else {
|
||||||
for(int i=0; i <= varargs.narg(); i++) {
|
for(int i=0; i <= varargs.narg(); i++) {
|
||||||
@ -123,7 +125,7 @@ public class ESP8266Mqtt extends TwoArgFunction implements IMqttMessageListener
|
|||||||
private class SubscribeMqtt extends VarArgFunction {
|
private class SubscribeMqtt extends VarArgFunction {
|
||||||
|
|
||||||
public LuaValue invoke(Varargs varargs) {
|
public LuaValue invoke(Varargs varargs) {
|
||||||
final LuaTable onMqtt = new LuaTable();
|
final LuaTable subMqtt = new LuaTable();
|
||||||
final int numberArg = varargs.narg();
|
final int numberArg = varargs.narg();
|
||||||
if (numberArg == 3) {
|
if (numberArg == 3) {
|
||||||
final String topic = varargs.arg(2).toString().toString();
|
final String topic = varargs.arg(2).toString().toString();
|
||||||
@ -151,7 +153,7 @@ public class ESP8266Mqtt extends TwoArgFunction implements IMqttMessageListener
|
|||||||
}
|
}
|
||||||
return LuaValue.NIL;
|
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 {
|
public void messageArrived(String topic, MqttMessage message) throws Exception {
|
||||||
LuaValue messageCallback = onMqtt.get(ON_PREFIX + MESSAGE);
|
LuaValue messageCallback = onMqtt.get(ON_PREFIX + MESSAGE);
|
||||||
if (messageCallback != null) {
|
if (messageCallback != null) {
|
||||||
LuaValue call2 = messageCallback.call(LuaValue.NIL,
|
LuaThreadTmr exec = new LuaThreadTmr(messageCallback, LuaValue.NIL, LuaValue.valueOf(topic), LuaValue.valueOf(message.getPayload()));
|
||||||
LuaValue.valueOf(topic),
|
exec.start();
|
||||||
LuaValue.valueOf(message.getPayload()));
|
System.out.println("[MQTT] message "+ topic + " : " + message + " received");
|
||||||
//FIXME call the LUA code
|
//FIXME call the LUA code
|
||||||
} else {
|
} else {
|
||||||
System.err.println("[MQTT] message "+ topic + " : " + message + " without callback");
|
System.err.println("[MQTT] message "+ topic + " : " + message + " without callback");
|
||||||
|
@ -78,7 +78,7 @@ public class ESP8266Node extends TwoArgFunction {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public LuaValue call() {
|
public LuaValue call() {
|
||||||
System.out.println("[Node] Heap");
|
//System.out.println("[Node] Heap");
|
||||||
return LuaValue.valueOf(Runtime.getRuntime().freeMemory());
|
return LuaValue.valueOf(Runtime.getRuntime().freeMemory());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -133,7 +133,7 @@ public class ESP8266Tmr extends TwoArgFunction {
|
|||||||
private class watchDog extends ZeroArgFunction {
|
private class watchDog extends ZeroArgFunction {
|
||||||
|
|
||||||
public LuaValue call() {
|
public LuaValue call() {
|
||||||
System.out.println("[TMR] Watchdog fed");
|
//System.out.println("[TMR] Watchdog fed");
|
||||||
return LuaValue.valueOf(true);
|
return LuaValue.valueOf(true);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user