2017-12-29 21:49:17 +01:00
|
|
|
package de.c3ma.ollo;
|
|
|
|
|
|
|
|
import java.io.File;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.nio.file.Files;
|
|
|
|
|
2018-01-12 20:38:35 +01:00
|
|
|
import javax.swing.SwingUtilities;
|
2018-01-02 14:29:49 +01:00
|
|
|
|
2017-12-29 21:49:17 +01:00
|
|
|
import org.luaj.vm2.Globals;
|
|
|
|
import org.luaj.vm2.LuaValue;
|
|
|
|
import org.luaj.vm2.lib.jse.JsePlatform;
|
|
|
|
|
|
|
|
import de.c3ma.ollo.mockup.DoFileFunction;
|
|
|
|
import de.c3ma.ollo.mockup.ESP8266File;
|
2018-01-02 12:47:31 +01:00
|
|
|
import de.c3ma.ollo.mockup.ESP8266Net;
|
2017-12-29 21:49:17 +01:00
|
|
|
import de.c3ma.ollo.mockup.ESP8266Node;
|
2018-01-02 12:47:31 +01:00
|
|
|
import de.c3ma.ollo.mockup.ESP8266Time;
|
2017-12-29 21:49:17 +01:00
|
|
|
import de.c3ma.ollo.mockup.ESP8266Tmr;
|
|
|
|
import de.c3ma.ollo.mockup.ESP8266Uart;
|
|
|
|
import de.c3ma.ollo.mockup.ESP8266Wifi;
|
|
|
|
import de.c3ma.ollo.mockup.ESP8266Ws2812;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* created at 28.12.2017 - 13:19:32<br />
|
|
|
|
* creator: ollo<br />
|
|
|
|
* project: WS2812Emulation<br />
|
|
|
|
* $Id: $<br />
|
|
|
|
*
|
|
|
|
* @author ollo<br />
|
|
|
|
*/
|
|
|
|
public class WS2812Simulation implements LuaSimulation {
|
|
|
|
|
2018-01-12 20:38:35 +01:00
|
|
|
private Globals globals = JsePlatform.standardGlobals();
|
|
|
|
private ESP8266Tmr espTmr = new ESP8266Tmr();
|
|
|
|
private ESP8266File espFile = new ESP8266File();
|
|
|
|
private ESP8266Node espNode = new ESP8266Node(this);
|
|
|
|
private DoFileFunction doFile = new DoFileFunction(globals);
|
|
|
|
private ESP8266Ws2812 ws2812 = new ESP8266Ws2812();
|
|
|
|
private String scriptName;
|
|
|
|
|
|
|
|
public WS2812Simulation(File sourceFolder) {
|
|
|
|
globals.load(new ESP8266Uart());
|
|
|
|
globals.load(ws2812);
|
|
|
|
globals.load(espTmr);
|
|
|
|
globals.load(espFile);
|
|
|
|
globals.load(espNode);
|
|
|
|
globals.load(new ESP8266Wifi());
|
|
|
|
globals.load(new ESP8266Net());
|
|
|
|
globals.load(new ESP8266Time());
|
|
|
|
globals.set("dofile", doFile);
|
|
|
|
|
|
|
|
try {
|
|
|
|
File tempFile = File.createTempFile("NodemcuSimuFile", "");
|
|
|
|
File tempDir = new File(tempFile.getParent() + File.separator + "Nodemcu" + System.currentTimeMillis());
|
|
|
|
tempDir.mkdir();
|
|
|
|
|
|
|
|
System.out.println("[Nodemcu] Directory is " + tempDir.getAbsolutePath());
|
|
|
|
|
|
|
|
// Copy all files into the temporary folder
|
|
|
|
for (File f : sourceFolder.listFiles()) {
|
|
|
|
Files.copy(f.toPath(), new File(tempDir.getAbsolutePath() + File.separator + f.getName()).toPath());
|
|
|
|
}
|
|
|
|
|
|
|
|
espFile.setWorkingDirectory(tempDir);
|
|
|
|
espNode.setWorkingDirectory(tempDir);
|
|
|
|
doFile.setWorkingDirectory(tempDir);
|
|
|
|
} catch (IOException e) {
|
|
|
|
System.err.println("[Nodemcu] " + e.getMessage());
|
|
|
|
espFile = null;
|
|
|
|
espNode = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-17 21:11:42 +01:00
|
|
|
public static void main(final String[] args) {
|
2018-01-12 20:38:35 +01:00
|
|
|
SwingUtilities.invokeLater(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
if (args.length == 0) {
|
|
|
|
printUsage();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (args.length >= 1) {
|
|
|
|
File f = new File(args[0]);
|
|
|
|
if (f.exists()) {
|
|
|
|
WS2812Simulation simu = new WS2812Simulation(f.getParentFile());
|
|
|
|
System.out.println("File : " + f.getAbsolutePath());
|
|
|
|
|
|
|
|
if (args.length >= 2) {
|
|
|
|
simu.setWS2812Layout(new File(args[1]));
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
if (args.length >= 3) {
|
|
|
|
File additionalFile = new File(args[2]);
|
|
|
|
if (additionalFile.exists() && (simu.doFile != null)) {
|
|
|
|
|
|
|
|
Files.copy(additionalFile.toPath(), new File(simu.doFile.getWorkingDirectory()
|
|
|
|
+ File.separator + additionalFile.getName()).toPath());
|
|
|
|
System.out.println("Integrate " + additionalFile.getName() + " into simulation");
|
|
|
|
} else {
|
|
|
|
System.err.println("Script " + args[2] + " cannot be found");
|
2018-01-17 21:11:42 +01:00
|
|
|
printUsage();
|
2018-01-12 20:38:35 +01:00
|
|
|
System.exit(1);
|
|
|
|
}
|
|
|
|
}
|
2018-01-17 21:11:42 +01:00
|
|
|
if (args.length >= 4) {
|
|
|
|
try {
|
|
|
|
ESP8266Tmr.gTimingFactor = Integer.parseInt(args[3]);
|
|
|
|
} catch (NumberFormatException nfe) {
|
|
|
|
System.err.println("Timing factor not parsable: " + nfe.getMessage());
|
|
|
|
printUsage();
|
|
|
|
}
|
|
|
|
}
|
2018-01-12 20:38:35 +01:00
|
|
|
|
|
|
|
simu.callScript(f.getName());
|
|
|
|
} catch (IOException e) {
|
|
|
|
System.err.println("[Nodemcu] " + e.getMessage());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
printUsage();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
private void setWS2812Layout(File file) {
|
|
|
|
if (file.exists()) {
|
2018-01-30 20:59:21 +01:00
|
|
|
ws2812.setLayout(file, this);
|
2018-01-12 20:38:35 +01:00
|
|
|
} else {
|
|
|
|
throw new RuntimeException("WS2812 Layout: " + file.getAbsolutePath() + " does not exists");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private static void printUsage() {
|
|
|
|
System.out.println("Usage:");
|
|
|
|
System.out.println("one argument required: file to execute.");
|
2018-01-17 21:11:42 +01:00
|
|
|
System.out.println(".e.g: init.lua (ws2812 layout configuration) (additional LUA script) (timing speedup factor)");
|
2018-01-12 20:38:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void rebootTriggered() {
|
|
|
|
System.out.println("=================== Reboot in Simulation -> call it again =================");
|
|
|
|
this.espTmr.stopAllTimer();
|
|
|
|
try {
|
|
|
|
Thread.sleep(200);
|
|
|
|
if (this.scriptName != null) {
|
|
|
|
System.out.println("Reexecuting...");
|
|
|
|
callScript(this.scriptName);
|
|
|
|
}
|
|
|
|
} catch (InterruptedException e) {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
private void callScript(String filename) {
|
|
|
|
this.scriptName = filename;
|
|
|
|
|
|
|
|
if ((espFile != null) && (espFile.getFileInWorkingDir(filename) != null)) {
|
|
|
|
LuaValue chunk = globals.loadfile(espFile.getFileInWorkingDir(filename).getAbsolutePath());
|
|
|
|
chunk.call();
|
|
|
|
} else {
|
|
|
|
throw new RuntimeException("Copy into temporary folder failed; script not available");
|
|
|
|
}
|
|
|
|
}
|
2017-12-29 21:49:17 +01:00
|
|
|
}
|