From fbc00d7932a717a227389040a96443e61b597d7c Mon Sep 17 00:00:00 2001 From: xengineering Date: Sun, 13 Sep 2020 20:50:54 +0200 Subject: Implement API --- README.md | 3 +-- main.py | 37 +++++++++++++++++++++++++++++++++---- static/js/api_client.js | 40 ++++++++++++++++++++++++++++++++++++++++ templates/index.html | 8 +++++++- 4 files changed, 81 insertions(+), 7 deletions(-) create mode 100644 static/js/api_client.js 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/') +@app.route('/static/css/', methods=['GET']) def css(css_file): return current_app.send_static_file("css/{}".format(css_file)) -@app.route('/favicon.ico') +@app.route('/static/js/', 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 . +*/ + + +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 @@

picontrol

- Still in early alpha version ... + Reboot with this button:

+ +

+ Power off with this button: +

+
+ -- cgit v1.2.3-70-g09d2