Bladeren bron

added raspi waveshare plugin

Tobias Simetsreiter 4 jaren geleden
bovenliggende
commit
763ac85e2b

+ 19 - 31
src/bootstrap_captive/api/__init__.py

@@ -1,57 +1,45 @@
 
 def get_form(inp):
     import os
-    import json
     import traceback
-    form_dir = os.path.realpath(os.path.join(__file__,"..","..","form"))
-    print(form_dir)
+    from bootstrap_captive import api_handler
     resp = {
         "ok": True,
         "js": [],
     }
-    for f in os.listdir(form_dir):
+    form_dir = os.path.realpath(os.path.join(__file__,"..","..","form"))
+    print(form_dir)
+    for js in api_handler.get_all_forms():
         try:
-            jsfile = os.path.join(form_dir, f)
-            print(jsfile)
-            if not f[-5:].lower() == ".json":
-                print("Nope")
-                continue
-            with open(jsfile) as fd:
-                js = json.load(fd)
-                js["name"] = f[:-5]
-                try:
-                    mod = get_form_module(f)
-                    js = mod.get_form(js)
-                except Exception as ex:
-                    js["setup_err"] = str(ex)
-                js["ok"] = True;
-                resp["js"].append(js)
+            try:
+                mod = get_form_module(js["name"])
+                js = mod.get_form(js)
+            except Exception as ex:
+                js["setup_err"] = str(ex)
+                traceback.print_exc()
+            js["ok"] = True;
+            resp["js"].append(js)
         except Exception as ex:
             resp["js"].append({
-                "name": f,
                 "ok": False,
                 "err": str(ex),
                 "trace": traceback.format_exc(),
             })
+            traceback.print_exc()
     return resp
 
-def get_form_module(name):
-    import importlib.machinery
-    import os
-    base = os.path.basename(name)
-    b, ext = os.path.splitext(base)
-    modpath = os.path.join(__file__,"..","..","form", b+".py")
-    modpath = os.path.realpath(modpath)
-    return importlib.machinery.SourceFileLoader("bootstrap_portal.form."+b, modpath).load_module()
-
 def submit_form(inp):
     import traceback
     import os
+    from bootstrap_captive import api_handler
 
     try:
-        mod = get_form_module(inp["name"])
-        return mod.submit_form(inp, {})
+        mod = api_handler.get_form_module(inp["name"])
+        form = api_handler.get_form(inp["name"])
+        res = mod.submit_form(inp, form)
+        return res
     except Exception as ex:
+        traceback.print_exc()
         return {
             "ok": False,
             "err": str(ex),

+ 45 - 0
src/bootstrap_captive/api_handler.py

@@ -16,3 +16,48 @@ def process_json(inp):
         "api_err":"Request Handler not found"
     }
 
+def get_form(base):
+    import os
+    import json
+    form_dir = os.path.realpath(os.path.join(__file__,"..","form"))
+    name = os.path.basename(base)
+    b, ext = os.path.splitext(name)
+    jsfile = os.path.join(form_dir, b+".json")
+    print(jsfile)
+    if not os.path.isfile(jsfile):
+        print("Nope")
+        return {}
+    try:
+        with open(jsfile) as fd:
+            js = json.load(fd)
+        return js
+    except Exception as ex:
+        print(ex)
+        return {}
+
+def get_form_module(name):
+    import importlib.machinery
+    import os
+    base = os.path.basename(name)
+    b, ext = os.path.splitext(base)
+    modpath = os.path.join(__file__,"..","form", b+".py")
+    modpath = os.path.realpath(modpath)
+    return importlib.machinery.SourceFileLoader("bootstrap_portal.form."+b, modpath).load_module()
+
+def get_all_forms():
+    import os
+    import json
+    form_dir = os.path.realpath(os.path.join(__file__,"..","form"))
+    for f in os.listdir(form_dir):
+        try:
+            jsfile = os.path.join(form_dir, f)
+            print(jsfile)
+            if not f[-5:].lower() == ".json":
+                print("Nope")
+                continue
+            with open(jsfile) as fd:
+                js = json.load(fd)
+            js["name"] = f[:-5]
+            yield js
+        except Exception as ex:
+            print(ex)

+ 51 - 0
src/bootstrap_captive/form/32-raspi-zero-waveshare.json

@@ -0,0 +1,51 @@
+{
+    "schema": {
+        "update": {
+            "type": "boolean",
+            "default": false
+        },
+        "install": {
+            "type": "boolean",
+            "default": false
+        },
+        "branch": {
+            "type": "string",
+            "default": "master",
+            "pattern": "^[a-z0-9A-Z]*$"
+        },
+        "cron_timer": {
+            "type": "string",
+            "default": "*/10 * * * *"
+        },
+        "subcommand": {
+            "type": "string",
+            "title": "Subcommand",
+            "required": true,
+            "enum": [
+                "eink_quote",
+                "eink_quote_qr"
+            ],
+            "default": "eink_quote_qr"
+        }
+    },
+    "form": [
+        {
+            "key": "update",
+            "inlinetitle": "Update Raspi Waveshare"
+        },
+        {
+            "key": "install",
+            "inlinetitle": "Install Raspi Waveshare"
+        },
+        "branch",
+        "cron_timer",
+        "subcommand"
+    ],
+    "value": {
+        "update": false,
+        "install": false,
+        "branch": "master",
+        "subcommand": "eink_quote_qr",
+        "cron_timer": "*/10 * * * *"
+    }
+}

+ 65 - 0
src/bootstrap_captive/form/32-raspi-zero-waveshare.py

@@ -0,0 +1,65 @@
+
+install_dir = "/opt/eu.tsimnet.git/dasimmet/raspi-zero-waveshare"
+git_remote = "https://git.tsimnet.eu/dasimmet/raspi-zero-waveshare.git"
+
+def submit_form(inp, form):
+    import os
+    import json
+    from bootstrap_captive.helper import runck
+    print(json.dumps(inp, indent=4, sort_keys=True))
+    print(json.dumps(form, indent=4, sort_keys=True))
+    resp = {
+        "ok": True,
+        "out": "",
+        "err": ""
+    }
+    if not inp["subcommand"]:
+        return {
+            "ok": False,
+            "err": "No Subcommand Defined"
+        }
+    subcommand = inp["subcommand"]
+    branch = os.path.basename(inp["branch"])
+    ref = "origin/"+branch
+    cron_timer = inp["cron_timer"]
+    try:
+        proc = None
+        if not os.path.isdir(install_dir):
+            CMD = ["apt-get", "install", "-y", "git"]
+            proc = runck(CMD)
+            os.path.makedirs(install_dir, exist_ok=True)
+            CMD = ["git","init"]
+            proc = runck(CMD, cwd=install_dir)
+            CMD = ["git","remote","add","origin",git_remote]
+            proc = runck(CMD, cwd=install_dir)
+            CMD = ["git","branch", branch]
+            proc = runck(CMD, cwd=install_dir)
+            CMD = ["git","checkout", branch]
+            proc = runck(CMD, cwd=install_dir)
+        if inp["update"] == True:
+            CMD = ["git","fetch"]
+            proc = runck(CMD, cwd=install_dir)
+            CMD = ["git","reset","--hard", ref]
+            proc = runck(CMD, cwd=install_dir)
+            CMD = ["git","submodule","update","--init"]
+            proc = runck(CMD, cwd=install_dir)
+            CMD = ["cp","config.mk.j2","config.mk"]
+            proc = runck(CMD, cwd=install_dir)
+            CMD = ["make","deps"]
+            proc = runck(CMD, cwd=install_dir)
+        if inp["install"] == True:
+            CMD = ["make","install", "CRON_T="+cron_timer, "DEPLOY_DIR="+install_dir, "CRON_SUBCOMMAND="+ subcommand]
+        else:
+            CMD = ["./bin/simpi.py", subcommand]
+        proc = runck(CMD, cwd=install_dir)
+    except Exception as ex:
+        resp["ok"] = False
+        resp["err"] = str(ex)
+
+    resp["out"] = str(CMD) + "     "
+    if proc:
+        resp["ok"] = proc.returncode == 0
+        resp["out"] = resp["out"] + proc.stdout.decode()
+        resp["err"] = proc.stderr.decode()
+
+    return resp

+ 6 - 0
src/bootstrap_captive/helper.py

@@ -0,0 +1,6 @@
+
+
+def runck(*args, **kwargs):
+    from subprocess import run, PIPE
+    return run(*args, stderr=PIPE, stdout=PIPE, check=True, **kwargs)
+

+ 3 - 2
src/bootstrap_captive/server.py

@@ -5,13 +5,14 @@ from threading import Thread
 from socketserver import ThreadingMixIn
 
 def server(args):
-    httpd = ThreadingHTTPServer((args.bind, args.port), MyHttpRequestHandler)
-
     if args.ssl_port:
+        print("Serving on HTTPS Port", args.ssl_port)
         httpd_ssl = ThreadingHTTPServer((args.bind, args.ssl_port), MyHttpRequestHandler)
         httpd_ssl.socket = ssl.wrap_socket (httpd_ssl.socket, certfile=args.certfile, server_side=True)
         Thread(target=httpd_ssl.serve_forever).start()
 
+    print("Serving on HTTP Port", args.port)
+    httpd = ThreadingHTTPServer((args.bind, args.port), MyHttpRequestHandler)
     httpd.serve_forever()
 
 class ThreadingHTTPServer(ThreadingMixIn, http.server.HTTPServer):

+ 1 - 0
src/http/portal.js

@@ -21,6 +21,7 @@ portal = new function(){
         // Default options are marked with *
         const progress_bar   = getId("progress_bar");
         const error_box   = getId("error_box");
+        error_box.innerText = "";
         progress_bar.classList.add("progress-bar-striped")
         progress_bar.classList.add("progress-bar-animated")
         progress_bar.classList.remove("bg-success")