Added MQTT client in simulation

This commit is contained in:
Ollo 2021-04-03 12:35:09 +02:00
parent 97bf9357f7
commit e497bbe48e
4 changed files with 41 additions and 8 deletions

View File

@ -1,8 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER">
<attributes>
<attribute name="module" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="lib" path="libs/luaj-jme-3.0.1.jar" sourcepath="libs/luaj-3.0.1/src"/>
<classpathentry kind="lib" path="libs/luaj-jse-3.0.1.jar"/>
<classpathentry kind="lib" path="libs/org.eclipse.paho.client.mqttv3-1.2.5.jar"/>
<classpathentry kind="output" path="bin"/>
</classpath>

View File

@ -1,7 +1,13 @@
# Dependencies
## Lua
The following file is expected here:
`luaj-3.0.1.zip`
It can be downloaded here:
https://sourceforge.net/projects/luaj/files/latest/download
## MQTT
It can be downloaded here:
https://search.maven.org/classic/#search%7Cgav%7C1%7Cg%3A%22org.eclipse.paho%22%20AND%20a%3A%22org.eclipse.paho.client.mqttv3%22

View File

@ -1,13 +1,17 @@
package de.c3ma.ollo.mockup;
import java.util.UUID;
import org.eclipse.paho.client.mqttv3.IMqttClient;
import org.eclipse.paho.client.mqttv3.MqttClient;
import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
import org.eclipse.paho.client.mqttv3.MqttException;
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;
import de.c3ma.ollo.LuaThreadTmr;
/**
*
* @author ollo
@ -15,18 +19,20 @@ import de.c3ma.ollo.LuaThreadTmr;
*/
public class ESP8266Mqtt extends TwoArgFunction {
private IMqttClient mMqttClient = null;
@Override
public LuaValue call(LuaValue modname, LuaValue env) {
env.checkglobals();
final LuaTable mqtt = new LuaTable();
mqtt.set("Client", new MqttClient());
mqtt.set("Client", new LuaMqttClient());
env.set("mqtt", mqtt);
env.get("package").get("loaded").set("tmr", mqtt);
System.out.println("[MQTT] Modlue loaded");
return mqtt;
}
private class MqttClient extends VarArgFunction {
private class LuaMqttClient extends VarArgFunction {
public LuaValue invoke(Varargs varargs) {
final LuaTable dynMqtt = new LuaTable();
if (varargs.narg() == 2) {
@ -107,14 +113,30 @@ private class ConnectMqtt extends VarArgFunction {
public LuaValue invoke(Varargs varargs) {
final LuaTable onMqtt = new LuaTable();
if (varargs.narg() == 6) {
if ((varargs.narg() == 6) && (mMqttClient == null)) {
final LuaTable table = varargs.arg(1).checktable();
final String targetIP = varargs.arg(2).toString().toString();
final int portnumber = varargs.arg(3).toint();
final boolean unkownParameter = varargs.arg(4).toboolean();
final boolean secureTLS = varargs.arg(4).toboolean();
final LuaValue codeOnConnected = varargs.arg(5);
final LuaValue codeOnFailed = varargs.arg(6);
System.out.println("[MQTT] connect to " + targetIP + ":" + portnumber);
String publisherId = "LuaSim" + UUID.randomUUID().toString();
try {
mMqttClient = new MqttClient("tcp://" + targetIP + ":" + portnumber,publisherId);
MqttConnectOptions options = new MqttConnectOptions();
options.setAutomaticReconnect(false);
options.setCleanSession(true);
options.setConnectionTimeout(10);
mMqttClient.connect(options);
System.out.println("[MQTT] connected to " + targetIP + ":" + portnumber);
codeOnConnected.call();
} catch (MqttException e) {
System.err.println("[MQTT] connect failed : " + e.getMessage());
codeOnFailed.call();
}
} else if (mMqttClient != null) {
System.err.println("[MQTT] client already exists : " + mMqttClient);
return LuaValue.NIL;
} else {
for(int i=0; i <= varargs.narg(); i++) {
System.err.println("[MQTT] connect ["+(i) + "] (" + varargs.arg(i).typename() + ") " + varargs.arg(i).toString() );