diff options
Diffstat (limited to 'src/compileAndRun')
-rw-r--r-- | src/compileAndRun/index.js | 75 | ||||
-rw-r--r-- | src/compileAndRun/process.js | 41 | ||||
-rw-r--r-- | src/compileAndRun/pythonSupportFiles/LuxcenaNeo/NeoBehaviour.py | 29 | ||||
-rw-r--r-- | src/compileAndRun/pythonSupportFiles/LuxcenaNeo/Strip.py | 6 | ||||
-rw-r--r-- | src/compileAndRun/pythonSupportFiles/LuxcenaNeo/__init__.py | 10 | ||||
-rw-r--r-- | src/compileAndRun/pythonSupportFiles/entry.py | 78 |
6 files changed, 239 insertions, 0 deletions
diff --git a/src/compileAndRun/index.js b/src/compileAndRun/index.js new file mode 100644 index 0000000..6f89e44 --- /dev/null +++ b/src/compileAndRun/index.js @@ -0,0 +1,75 @@ +let EventEmitter = require('events'); +let fse = require("fs-extra"); +let Process = require("./process"); +let pythonSupportFiles = __dirname + "/pythonSupportFiles/"; + +class Python extends EventEmitter { + + constructor(profileFolder, usrDataFolder) { + super(); + this.profileFolder = profileFolder; + this.usrDataFolder = usrDataFolder; + + this.stdout = ""; + this.stderr = ""; + } + + status() { + return {}; + } + + compile() { + try { + fse.removeSync(this.profileFolder + "/build"); + fse.copySync(pythonSupportFiles, this.profileFolder + "/build/"); + fse.copySync(this.profileFolder + "/script.py", this.profileFolder + "/build/script.py"); + } catch (err) { + console.log(err); + } + } + + run() { + // Setup paths and commands + let entryPath = this.profileFolder + "/build/entry.py"; + + // Spawn the new process + this.pyProc = new Process("python", [ + "-u", // This makes us able to get real-time output + entryPath, // This is where the entry-point is located + this.usrDataFolder // This is required for the python-script to now where our config-files are + ]); + this.pyProc.start(); + + this.pyProc.proc.stdout.on("data", (_stdout) => { + this.emit("stdout::data", "" + _stdout); + }); + this.pyProc.proc.stdout.on("end", () => { + this.emit("stdout::end"); + }); + this.pyProc.proc.stderr.on("data", (_stderr) => { + this.emit("stderr::data", _stderr); + }); + this.pyProc.proc.stderr.on("end", () => { + this.emit("stderr:end"); + }); + this.pyProc.proc.on("close", (_code) => { + this.emit("close", _code); + }); + + } + + compileAndRun() { + this.compile(); + this.run(); + } + + stop() { + this.pyProc.proc.kill("SIGINT"); + } + +} + +module.exports = { + Python: Python, + Process: Process +}; diff --git a/src/compileAndRun/process.js b/src/compileAndRun/process.js new file mode 100644 index 0000000..67ff546 --- /dev/null +++ b/src/compileAndRun/process.js @@ -0,0 +1,41 @@ +let spawn = require("child_process"); + +class Process { + + constructor(command, args = []) { + this.command = command; + this.args = args; + this.stdout = ""; + this.stderr = ""; + this.fl = false; + } + + start() { + this.proc = spawn.spawn(this.command, this.args); + + this.proc.on('error', (err) => { + console.log(err); + }); + + this.proc.stdout.on('data', (_stdout) => { + this.stdout += _stdout; + }); + + this.proc.stdout.on('end', () => { + }); + + this.proc.stderr.on('data', (_stderr) => { + this.stderr += _stderr; + }); + + this.proc.stderr.on('end', () => { + }); + + this.proc.on('close', (code) => { + }); + + } + +} + +module.exports = Process;
\ No newline at end of file diff --git a/src/compileAndRun/pythonSupportFiles/LuxcenaNeo/NeoBehaviour.py b/src/compileAndRun/pythonSupportFiles/LuxcenaNeo/NeoBehaviour.py new file mode 100644 index 0000000..b0238e7 --- /dev/null +++ b/src/compileAndRun/pythonSupportFiles/LuxcenaNeo/NeoBehaviour.py @@ -0,0 +1,29 @@ +# This is the base-class "main" should inherit from! +# All methods are blocking :) This means that you could potentially loose a "tick" +# For example, if "eachSecond" is taking up the thread, and the clock goes from 11:58 to 12:02, "eachHour", will not be called. +class NeoBehaviour: + + # THIS METHOD SHOULD NOT BE OVERIDDEN! Use onStart if you want a setup-method!!! + # Contains basic setup + def __init__(self): + return + + # This method will be run right after __init__ + def onStart(self): + return + + # This method is called every second (on the clock), given that the thread is open + def eachSecond(self): + return + + # This method is called every mintue (on the clock), given that the thread is open + def eachMinute(self): + return + + # This method is called every whole hour (on the clock), given that the thread is open + def eachHour(self): + return + + # This method is called every day at noon, given that the thread is open + def eachDay(self): + return diff --git a/src/compileAndRun/pythonSupportFiles/LuxcenaNeo/Strip.py b/src/compileAndRun/pythonSupportFiles/LuxcenaNeo/Strip.py new file mode 100644 index 0000000..c4ac8c9 --- /dev/null +++ b/src/compileAndRun/pythonSupportFiles/LuxcenaNeo/Strip.py @@ -0,0 +1,6 @@ +class Strip: + + def __init__(self, segments, segmentConfiguration): + self.segments = segments + self.segmentConfiguration = segmentConfiguration + return diff --git a/src/compileAndRun/pythonSupportFiles/LuxcenaNeo/__init__.py b/src/compileAndRun/pythonSupportFiles/LuxcenaNeo/__init__.py new file mode 100644 index 0000000..40b8e2d --- /dev/null +++ b/src/compileAndRun/pythonSupportFiles/LuxcenaNeo/__init__.py @@ -0,0 +1,10 @@ +from NeoBehaviour import * +from Strip import * + +strip = None # The strip object, should be set in entry.py, and is then accessible in the script-file +forceStop = False # When set to true, execution of the script will halt + +# A function that could be used to halt the script +def stop(): + global forceStop + forceStop = True diff --git a/src/compileAndRun/pythonSupportFiles/entry.py b/src/compileAndRun/pythonSupportFiles/entry.py new file mode 100644 index 0000000..4ba1031 --- /dev/null +++ b/src/compileAndRun/pythonSupportFiles/entry.py @@ -0,0 +1,78 @@ +# This is the entry-point for all Luxcena-Neo python-scripts +# The script should be in the same folder as this, and be named "script.py" +# In the future you could possibly have more files and stuff alongside the "script.py"-file as well +import sys +import json +import importlib +import datetime + +def runSync(moduleSc, sc): + timeNow = datetime.datetime.now() + lastDay = timeNow.day + lastHour = timeNow.hour + lastMinute = timeNow.minute + lastSecond = timeNow.second + + while True: + timeNow = datetime.datetime.now() + + if ("LuxcenaNeo" in dir(moduleSc)): + if moduleSc.LuxcenaNeo.forceStop: break + elif ("neo" in dir(moduleSc)): + if moduleSc.neo.forceStop == True: break + + if (timeNow.second != lastSecond): + lastSecond = timeNow.second + sc.eachSecond() + + if (timeNow.minute != lastMinute): + lastMinute = timeNow.minute + sc.eachMinute() + + if (timeNow.hour != lastHour): + lastHour = timeNow.hour + sc.eachHour() + + if (timeNow.day != lastDay): + lastDay = timeNow.lastDay + sc.eachDay() + +def runAsync(moduleSc, sc): + return + +def main(): + print ("Starting script named \"{0}\"".format("test")) + + root_dir = sys.argv[1] + config_dir = root_dir + "/config/" + + print ("> Loading pixel-configuration...") + with open(config_dir + "strip.json", "r") as rawStripConf: + stripConf = json.load(rawStripConf) + segments = stripConf["segments"] + segmentConfiguration = stripConf["segmentConfiguration"] + + print ("> Initializing script...") + moduleSc = importlib.import_module("script") + + if ("LuxcenaNeo" in dir(moduleSc)): + moduleSc.LuxcenaNeo.strip = moduleSc.LuxcenaNeo.Strip(segments, segmentConfiguration) + elif ("neo" in dir(moduleSc)): + moduleSc.neo.strip = moduleSc.neo.Strip(segments, segmentConfiguration) + else: + raise Exception("Neither LuxcenaNeo nor neo found in script, check docs!") + + sc = moduleSc.Main() + + print ("> Running the script...") + sc.onStart() + + if (("async" in dir(moduleSc)) and (moduleSc.async == True)): + runAsync(moduleSc, sc) + else: + runSync(moduleSc, sc) + + print ("> Script exited...") + +if __name__ == "__main__": + main() |