diff --git a/simulation/src/de/c3ma/ollo/mockup/ESP8266Net.java b/simulation/src/de/c3ma/ollo/mockup/ESP8266Net.java new file mode 100644 index 0000000..6bf43c5 --- /dev/null +++ b/simulation/src/de/c3ma/ollo/mockup/ESP8266Net.java @@ -0,0 +1,83 @@ +package de.c3ma.ollo.mockup; + +import java.io.File; +import java.io.IOException; +import java.io.InterruptedIOException; +import java.net.ServerSocket; +import java.net.Socket; +import java.nio.file.Files; + +import org.luaj.vm2.LuaFunction; +import org.luaj.vm2.LuaTable; +import org.luaj.vm2.LuaValue; +import org.luaj.vm2.lib.OneArgFunction; +import org.luaj.vm2.lib.TwoArgFunction; +import org.luaj.vm2.lib.ZeroArgFunction; + +import de.c3ma.ollo.LuaSimulation; + +/** + * created at 29.12.2017 - 01:29:40
+ * creator: ollo
+ * project: WS2812Emulation
+ * $Id: $
+ * @author ollo
+ */ +public class ESP8266Net extends TwoArgFunction { + + public static final int PORTNUMBER_OFFSET = 4000; + + @Override + public LuaValue call(LuaValue modname, LuaValue env) { + env.checkglobals(); + final LuaTable net = new LuaTable(); + net.set("createServer", new CreateServerFunction()); + + //FIXME net.set("send", new SendFunction()); + net.set("TCP", "TCP"); + env.set("net", net); + env.get("package").get("loaded").set("net", net); + return net; + } + + private class CreateServerFunction extends OneArgFunction { + + @Override + public LuaValue call(LuaValue arg) { + final LuaTable srv = new LuaTable(); + srv.set("listen", new ListenFunction()); + return srv; + } + + } + + private class ListenFunction extends TwoArgFunction { + + @Override + public LuaValue call(LuaValue port, LuaValue function) { + int portnumber = port.checkint(); + LuaFunction onListenFunction = function.checkfunction(); + + System.out.println("[Net] listening " + portnumber + "(simulating at " + (PORTNUMBER_OFFSET+ portnumber) + ")"); + + try + { + ServerSocket serverSocket = new ServerSocket(PORTNUMBER_OFFSET+portnumber); + serverSocket.setSoTimeout( 60000 ); // Timeout after one minute + + Socket client = serverSocket.accept(); + + } + catch ( InterruptedIOException iioe ) + { + System.err.println( "Timeout nach einer Minute!" ); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + System.out.println("[Net] server running"); + return LuaValue.valueOf(true); + } + + } +} diff --git a/simulation/src/de/c3ma/ollo/mockup/ESP8266Time.java b/simulation/src/de/c3ma/ollo/mockup/ESP8266Time.java new file mode 100644 index 0000000..e8f5446 --- /dev/null +++ b/simulation/src/de/c3ma/ollo/mockup/ESP8266Time.java @@ -0,0 +1,78 @@ +package de.c3ma.ollo.mockup; + +import org.luaj.vm2.Globals; +import org.luaj.vm2.LuaFunction; +import org.luaj.vm2.LuaTable; +import org.luaj.vm2.LuaValue; +import org.luaj.vm2.Varargs; +import org.luaj.vm2.lib.OneArgFunction; +import org.luaj.vm2.lib.ThreeArgFunction; +import org.luaj.vm2.lib.TwoArgFunction; +import org.luaj.vm2.lib.VarArgFunction; +import org.luaj.vm2.lib.ZeroArgFunction; + +import de.c3ma.ollo.LuaThreadTmr; + +/** + * created at 29.12.2017 - 00:07:22
+ * creator: ollo
+ * project: Time Emulation
+ * + * Simulating the following modules: + * Sntp + * rtctime + * + * $Id: $
+ * @author ollo
+ */ +public class ESP8266Time extends TwoArgFunction { + + @Override + public LuaValue call(LuaValue modname, LuaValue env) { + env.checkglobals(); + final LuaTable sntp = new LuaTable(); + sntp.set("sync", new SyncFunction()); + env.set("sntp", sntp); + final LuaTable rtctime = new LuaTable(); + rtctime.set("get", new GetFunction()); + env.set("rtctime", rtctime); + env.get("package").get("loaded").set("sntp", sntp); + env.get("package").get("loaded").set("rtctime", rtctime); + + return sntp; + } + + private class SyncFunction extends ThreeArgFunction { + + @Override + public LuaValue call(LuaValue server, LuaValue callbackSuccess, LuaValue callbackFailure) { + String serverName = server.checkjstring(); + LuaFunction cb = callbackSuccess.checkfunction(); + System.out.println("[SNTP] sync " + serverName); + + /*FIXME also make it possible to simulate the time */ + int seconds = (int) (System.currentTimeMillis() / 1000); + int useconds = (int) (System.currentTimeMillis() % 1000); + cb.call(LuaValue.valueOf(seconds), LuaValue.valueOf(useconds), LuaValue.valueOf(serverName)); + + return LuaValue.valueOf(true); + } + + } + + private class GetFunction extends ZeroArgFunction { + + @Override + public LuaValue call() { + LuaValue[] v = new LuaValue[2]; + /*FIXME also make it possible to simulate the time */ + int seconds = (int) (System.currentTimeMillis() / 1000); + int useconds = (int) (System.currentTimeMillis() % 1000); + v[0] = LuaValue.valueOf(seconds); + v[1] = LuaValue.valueOf(useconds); + return LuaValue.varargsOf(v).arg1(); + } + + } + +}