summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorxengineering <mail2xengineering@protonmail.com>2020-09-13 20:50:54 +0200
committerxengineering <mail2xengineering@protonmail.com>2020-09-13 20:50:54 +0200
commitfbc00d7932a717a227389040a96443e61b597d7c (patch)
treec732c68cd16e5eac3e302a64f62441128ca39749
parentde699b487df334226fe9863ffc7c92d081b38a76 (diff)
downloadpicontrol-fbc00d7932a717a227389040a96443e61b597d7c.tar
picontrol-fbc00d7932a717a227389040a96443e61b597d7c.tar.zst
picontrol-fbc00d7932a717a227389040a96443e61b597d7c.zip
Implement API
-rw-r--r--README.md3
-rwxr-xr-xmain.py37
-rw-r--r--static/js/api_client.js40
-rw-r--r--templates/index.html8
4 files changed, 81 insertions, 7 deletions
diff --git a/README.md b/README.md
index dd6c925..b01c87b 100644
--- a/README.md
+++ b/README.md
@@ -7,5 +7,4 @@ A small Web Application to shutdown or reboot your Raspberry Pi.
## Current State
-**This project is currently in early development state. It is not yet usable.**
-
+The code works but there is still documentation / automation missing.
diff --git a/main.py b/main.py
index 88b4032..aa60a95 100755
--- a/main.py
+++ b/main.py
@@ -21,28 +21,57 @@
"""
+import os
from waitress import serve
-from flask import Flask, render_template, current_app
+from flask import Flask, render_template, current_app, request
app = Flask(__name__)
-@app.route('/')
+@app.route('/', methods=['GET'])
def index():
return render_template('index.html')
-@app.route('/static/css/<css_file>')
+@app.route('/static/css/<css_file>', methods=['GET'])
def css(css_file):
return current_app.send_static_file("css/{}".format(css_file))
-@app.route('/favicon.ico')
+@app.route('/static/js/<js_file>', methods=['GET'])
+def js(js_file):
+ return current_app.send_static_file("js/{}".format(js_file))
+
+
+@app.route('/favicon.ico', methods=['GET'])
def favicon():
return current_app.send_static_file("img/favicon.ico")
+@app.route('/api', methods=['POST'])
+def api():
+ data = request.json
+
+ print("Data: {}".format(data))
+
+ try:
+ command = data["cmd"]
+ except:
+ return "Please provide a valid 'cmd' entry in the json dictionary!"
+
+ if command == "poweroff":
+ retval = {"return_value":"ok","request":data}
+ os.system("sudo poweroff")
+ elif command == "reboot":
+ retval = {"return_value":"ok","request":data}
+ os.system("sudo reboot")
+ else:
+ retval = {"return_value":"unknown command","request":data}
+
+ return retval
+
+
if __name__ == '__main__':
serve(app, listen='*:8080') # production server / bind to port
#serve(app, unix_socket='/run/web-template/unix.sock') # production server / unix domain socket
diff --git a/static/js/api_client.js b/static/js/api_client.js
new file mode 100644
index 0000000..4c80651
--- /dev/null
+++ b/static/js/api_client.js
@@ -0,0 +1,40 @@
+
+
+/*
+ picontrol - A small Web Application to shutdown or reboot your Raspberry Pi.
+
+ Copyright (C) 2020 xengineering
+
+ This program is free software: you can redistribute it and/or modify
+ it under the terms of the GNU Affero General Public License as published
+ by the Free Software Foundation, either version 3 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU Affero General Public License for more details.
+
+ You should have received a copy of the GNU Affero General Public License
+ along with this program. If not, see <https://www.gnu.org/licenses/>.
+*/
+
+
+function poweroff() {
+ var xhr = new XMLHttpRequest();
+ xhr.open("POST", "/api", true);
+ xhr.setRequestHeader("Content-type", "application/json");
+ var data = {"cmd":"poweroff"}
+ var json = JSON.stringify(data);
+ xhr.send(json);
+}
+
+
+function reboot() {
+ var xhr = new XMLHttpRequest();
+ xhr.open("POST", "/api", true);
+ xhr.setRequestHeader("Content-type", "application/json");
+ var data = {"cmd":"reboot"}
+ var json = JSON.stringify(data);
+ xhr.send(json);
+}
diff --git a/templates/index.html b/templates/index.html
index 8ccd1b2..2715585 100644
--- a/templates/index.html
+++ b/templates/index.html
@@ -32,9 +32,15 @@
<div class="content">
<header><h1>picontrol</h1></header>
<p>
- Still in early alpha version ...
+ Reboot with this button:
</p>
+ <button onclick="reboot()">reboot</button>
+ <p>
+ Power off with this button:
+ </p>
+ <button onclick="poweroff()">power off</button>
</div>
</div>
+ <script src="static/js/api_client.js"></script>
</body>
</html>