Bladeren bron

progress bar

Tobias Simetsreiter 4 jaren geleden
bovenliggende
commit
d98dfe6304
6 gewijzigde bestanden met toevoegingen van 45 en 23 verwijderingen
  1. 2 0
      src/autostart.cron
  2. 5 5
      src/bootstrap_captive/api/__init__.py
  3. 4 0
      src/bootstrap_captive/install.py
  4. 14 10
      src/http/index.html
  5. 18 4
      src/http/portal.js
  6. 2 4
      src/main.py

+ 2 - 0
src/autostart.cron

@@ -0,0 +1,2 @@
+
+@reboot root /usr/local/bin/bootstrap_captive autostart

+ 5 - 5
src/bootstrap_captive/api/__init__.py

@@ -4,13 +4,10 @@ def process_json(inp):
     if "set_wifi" in inp:
         return set_wifi(inp["set_wifi"])
     if "disable_portal" in inp:
-        disable_portal(inp["disable_portal"])
-        return {
-            "ok":True,
-        }
+        return disable_portal(inp["disable_portal"])
     return {
         "ok":False,
-        "api_err":"Request Handler not fond"
+        "api_err":"Request Handler not found"
     }
 
 def disable_portal(inp):
@@ -19,6 +16,9 @@ def disable_portal(inp):
                      cwd="/",
                      stdout=subprocess.PIPE,
                      stderr=subprocess.STDOUT)
+    return {
+        "ok":True,
+    }
 
 def set_wifi(inp):
     import re

+ 4 - 0
src/bootstrap_captive/install.py

@@ -1,5 +1,9 @@
 
 def com_unpack(args):
+    import os
+    if not "UNSHARXZ_INSTALLER" in os.environ:
+        print("hostapt_captive is already installed")
+        return
     COM="""
 set -e
 set -x

+ 14 - 10
src/http/index.html

@@ -5,16 +5,20 @@
    <script src="portal.js"></script>
  </head>
  <body>
-   <h1>Welcome to Raspi Bootstrap Captive<h1>
-   <div class="form-group">
-     <input id="input_ssid" placeholder="SSID" class="form-control">
-     <input id="input_pw" placeholder="Password" class="form-control">
-     <button onclick="portal.setWifi()" class="btn btn-primary">set</button>
+   <div class="container-fluid">
+     <h1>Welcome to Raspi Bootstrap Captive<h1>
+     <div class="progress">
+       <div id="progress_bar" class="progress-bar" role="progressbar" aria-valuenow="100" aria-valuemin="0" aria-valuemax="100" style="width: 100%"></div>
+     </div>
+     <div class="form-group">
+       <input id="input_ssid" placeholder="SSID" class="form-control">
+       <input id="input_pw" placeholder="Password" class="form-control">
+       <button onclick="portal.setWifi()" class="btn btn-primary">set</button>
+     </div>
+     <div class="form-group">
+       <button onclick="portal.disablePortal()" class="btn btn-warning">Disable Portal</button>
+     </div>
+     <pre id="error_box"></pre>
    </div>
-   <div class="form-group">
-     <button onclick="portal.disablePortal()" class="btn btn-warning">Disable Portal</button>
-   </div>
-   <textarea id="input_resp" name="message" rows="10" cols="30">
-   </textarea>
  </body>
 </html>

+ 18 - 4
src/http/portal.js

@@ -2,23 +2,27 @@ portal = new function(){
     this.setWifi = async () => {
         const ssid = getId("input_ssid").value;
         const pw   = getId("input_pw").value;
-        const resp_div   = getId("input_resp");
         const resp = await postData("", {
             "set_wifi":{
                 "ssid":ssid,
                 "pw":pw,
             }
         })
-        resp_div.value = JSON.stringify(resp, null, 2);
+        error_box.value = JSON.stringify(resp, null, 2);
     }
     this.disablePortal = async () => {
         const resp = await postData("", {
             "disable_portal":true
         })
-        resp_div.value = JSON.stringify(resp, null, 2);
     }
     async function postData(url = '', data = {}) {
         // Default options are marked with *
+        const progress_bar   = getId("progress_bar");
+        const error_box   = getId("error_box");
+        progress_bar.classList.add("progress-bar-striped")
+        progress_bar.classList.add("progress-bar-animated")
+        progress_bar.classList.remove("bg-success")
+        progress_bar.classList.remove("bg-danger")
         const response = await fetch(url, {
             method: 'POST', // *GET, POST, PUT, DELETE, etc.
             mode: 'cors', // no-cors, *cors, same-origin
@@ -32,7 +36,17 @@ portal = new function(){
             referrerPolicy: 'no-referrer', // no-referrer, *no-referrer-when-downgrade, origin, origin-when-cross-origin, same-origin, strict-origin, strict-origin-when-cross-origin, unsafe-url
             body: JSON.stringify(data) // body data type must match "Content-Type" header
         });
-        return response.json(); // parses JSON response into native JavaScript objects
+        progress_bar.classList.remove("progress-bar-striped")
+        progress_bar.classList.remove("progress-bar-animated")
+        resp = await response.json(); // parses JSON response into native JavaScript objects
+        if (resp["ok"]){
+            error_box.innerText = "";
+            progress_bar.classList.add("bg-success")
+        } else {
+            progress_bar.classList.add("bg-danger")
+            error_box.innerText = JSON.stringify(resp, null, 2);
+        }
+        return resp;
     }
     function getId(id){
         return document.getElementById(id);

+ 2 - 4
src/main.py

@@ -46,10 +46,8 @@ def parser():
 
 def com_autostart(args):
     com_install(args)
-    with open("/etc/cron.d/42-bootstrap_captive", "w") as fd:
-        fd.write('''
-@reboot root /usr/local/bin/bootstrap_captive autostart
-''')
+    from subprocess import run
+    run(["cp","autostart.cron","/etc/cron.d/42-bootstrap_captive"], check=True)
     from bootstrap_captive.gpio import switch
     if switch(args):
         com_start(args)