2022-10-06 11:47:21 +02:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# -*- coding: utf-8 -*-
|
2022-10-25 12:50:56 +02:00
|
|
|
import os, time, subprocess, threading, toml
|
2022-10-19 15:14:43 +02:00
|
|
|
from flask import Flask, flash, request, redirect, url_for
|
2022-10-06 11:47:21 +02:00
|
|
|
# HTTP auth
|
|
|
|
from flask_httpauth import HTTPBasicAuth
|
|
|
|
from werkzeug.security import generate_password_hash, check_password_hash
|
2022-10-19 15:14:43 +02:00
|
|
|
# FILE UPLOAD
|
|
|
|
from werkzeug.utils import secure_filename
|
2022-10-19 16:22:05 +02:00
|
|
|
|
|
|
|
app = Flask(__name__)
|
|
|
|
|
|
|
|
app.config.from_file("defaults.toml", load=toml.load)
|
|
|
|
config_locations = ["./", "~/.", "~/.config/"]
|
|
|
|
for location in config_locations:
|
|
|
|
# Optional config files, ~ is expanded to $HOME on *nix, %USERPROFILE% on windows
|
|
|
|
# ~ app.config.from_file("videopi.toml", load=toml.load, silent=True)
|
|
|
|
if app.config.from_file(os.path.expanduser( location + "pilpil-client.toml"), load=toml.load, silent=True):
|
|
|
|
print("Found configuration file in " + os.path.expanduser( location ))
|
|
|
|
|
|
|
|
|
|
|
|
#UPLOAD_FOLDER = os.path.expanduser('~/Videos')
|
|
|
|
#ALLOWED_EXTENSIONS = {'avi', 'mkv', 'mp4'}
|
|
|
|
|
|
|
|
UPLOAD_FOLDER = os.path.expanduser(app.config['DEFAULT']['media_folder_local'])
|
|
|
|
ALLOWED_EXTENSIONS = app.config['DEFAULT']['media_exts']
|
|
|
|
HTTP_SECRET = str(app.config['DEFAULT']['auth'])
|
|
|
|
DEBUG = app.config['DEFAULT']['debug']
|
|
|
|
|
2022-10-19 15:14:43 +02:00
|
|
|
#HTTPS
|
2022-10-19 16:22:05 +02:00
|
|
|
#ASSETS_DIR = os.path.dirname(os.path.abspath(__file__))
|
2022-10-06 11:47:21 +02:00
|
|
|
# HTTP Serve
|
|
|
|
from waitress import serve
|
|
|
|
|
2022-10-19 15:14:43 +02:00
|
|
|
#app.secret_key = b'flafoudi'
|
|
|
|
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
|
|
|
|
# Max upload size 100M ( see nginx default also )
|
|
|
|
app.config['MAX_CONTENT_LENGTH'] = 100 * 1000 * 1000
|
2022-10-06 11:47:21 +02:00
|
|
|
|
|
|
|
auth = HTTPBasicAuth()
|
|
|
|
users = {
|
2022-10-19 16:22:05 +02:00
|
|
|
"": generate_password_hash(HTTP_SECRET),
|
2022-10-06 11:47:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@auth.verify_password
|
|
|
|
def verify_password(username, password):
|
|
|
|
if username in users and check_password_hash(users.get(username), password):
|
|
|
|
return username
|
|
|
|
|
|
|
|
signal = 0
|
|
|
|
|
2022-10-19 15:14:43 +02:00
|
|
|
# Check file extension is allowed
|
|
|
|
def allowed_file(filename):
|
|
|
|
# Check for dot in filename
|
|
|
|
if "." in filename:
|
|
|
|
# Split from right at first dot to find ext and allow files with "." in name
|
|
|
|
if filename.rsplit(".",1)[-1] in ALLOWED_EXTENSIONS:
|
|
|
|
return True
|
|
|
|
|
2022-10-06 11:47:21 +02:00
|
|
|
# Get Wifi signal level
|
2022-10-20 18:02:47 +02:00
|
|
|
def getRSSI():
|
2022-10-21 15:01:55 +02:00
|
|
|
signal = subprocess.run( "./get_rssi.sh", capture_output=True)
|
2022-10-20 18:02:47 +02:00
|
|
|
print(signal)
|
|
|
|
signal = str(signal.stdout, 'UTF-8').strip("-").strip("\n")
|
2022-10-06 11:47:21 +02:00
|
|
|
return signal
|
|
|
|
|
2022-10-24 19:57:39 +02:00
|
|
|
def ledSetup():
|
|
|
|
os.system('echo none | sudo tee /sys/class/leds/led0/trigger')
|
|
|
|
os.system('echo none | sudo tee /sys/class/leds/led1/trigger')
|
2022-10-24 20:04:18 +02:00
|
|
|
led(0, 0)
|
|
|
|
led(1, 0)
|
2022-10-24 19:57:39 +02:00
|
|
|
|
|
|
|
def led( led_id, state ):
|
|
|
|
# 0 : off, 1: on
|
|
|
|
os.system('echo ' + str(state) + ' | sudo tee /sys/class/leds/led' + str(led_id) +'/brightness')
|
|
|
|
|
2022-10-06 11:47:21 +02:00
|
|
|
# Blink the Pi led to allow identification
|
|
|
|
def blinkPy():
|
2022-10-24 19:42:11 +02:00
|
|
|
# disable mmc access led
|
2022-10-24 19:57:39 +02:00
|
|
|
# ~ os.system('echo none | sudo tee /sys/class/leds/led0/trigger')
|
|
|
|
# ~ os.system('echo none | sudo tee /sys/class/leds/led1/trigger')
|
2022-10-24 19:42:11 +02:00
|
|
|
# Blink 10 times
|
2022-10-24 19:57:39 +02:00
|
|
|
for j in range(16):
|
|
|
|
led(0, 1)
|
|
|
|
led(1, 0)
|
2022-10-06 11:47:21 +02:00
|
|
|
time.sleep(.2)
|
2022-10-24 20:01:49 +02:00
|
|
|
led(0, 0)
|
|
|
|
led(1, 1)
|
2022-10-06 11:47:21 +02:00
|
|
|
time.sleep(.2)
|
2022-10-24 19:42:11 +02:00
|
|
|
# restore default behavior
|
2022-10-24 19:57:39 +02:00
|
|
|
# ~ os.system('echo mmc0 | sudo tee /sys/class/leds/led0/trigger')
|
|
|
|
# ~ os.system('echo default-on | sudo tee /sys/class/leds/led1/trigger')
|
2022-10-24 20:03:33 +02:00
|
|
|
led(0, 0)
|
|
|
|
led(1, 0)
|
2022-10-06 11:47:21 +02:00
|
|
|
return "OK"
|
|
|
|
|
2022-10-25 12:50:56 +02:00
|
|
|
def threadBlink():
|
|
|
|
th=threading.Thread(target=blinkPy)
|
|
|
|
th.start()
|
|
|
|
|
2022-10-06 11:47:21 +02:00
|
|
|
@app.route("/")
|
|
|
|
@auth.login_required
|
|
|
|
def main():
|
|
|
|
return "Nothing to see here !"
|
|
|
|
|
|
|
|
@app.route("/rssi")
|
|
|
|
@auth.login_required
|
2022-10-25 12:50:56 +02:00
|
|
|
def rssi():
|
2022-10-06 11:47:21 +02:00
|
|
|
return getRSSI()
|
|
|
|
|
|
|
|
@app.route("/blink")
|
|
|
|
@auth.login_required
|
|
|
|
def blink():
|
2022-10-25 12:50:56 +02:00
|
|
|
threadBlink()
|
|
|
|
return "Blinkin"
|
2022-10-06 11:47:21 +02:00
|
|
|
|
|
|
|
@app.route("/reboot")
|
|
|
|
@auth.login_required
|
|
|
|
def reboot():
|
|
|
|
stdout = subprocess.run(["sudo", "/usr/sbin/reboot"], capture_output=True)
|
|
|
|
print(stdout)
|
|
|
|
return "Rebooting..."
|
|
|
|
|
|
|
|
@app.route("/poweroff")
|
|
|
|
@auth.login_required
|
|
|
|
def shutdown():
|
|
|
|
stdout = subprocess.run(["sudo", "/usr/sbin/poweroff"], capture_output=True)
|
|
|
|
print(stdout)
|
2022-10-25 12:50:56 +02:00
|
|
|
return "Shutting down..."
|
2022-10-06 11:47:21 +02:00
|
|
|
|
2022-10-19 15:14:43 +02:00
|
|
|
# File upload
|
|
|
|
|
|
|
|
@app.route('/upload', methods=['GET', 'POST'])
|
|
|
|
@auth.login_required
|
|
|
|
def upload_file():
|
|
|
|
if request.method == 'POST':
|
|
|
|
# check if the post request has the file part
|
|
|
|
if 'file' not in request.files:
|
|
|
|
return "No file part: " + str(request.files)
|
|
|
|
file = request.files['file']
|
|
|
|
# If the user does not select a file, the browser submits an
|
|
|
|
# empty file without a filename.
|
|
|
|
if file.filename == '':
|
|
|
|
return 'No selected file'
|
|
|
|
if file and allowed_file(file.filename):
|
|
|
|
filename = secure_filename(file.filename)
|
2022-10-19 16:22:05 +02:00
|
|
|
file.save(os.path.join(UPLOAD_FOLDER, filename))
|
2022-10-19 15:14:43 +02:00
|
|
|
return "File saved in " + UPLOAD_FOLDER
|
|
|
|
return "OK"
|
|
|
|
|
2022-10-06 11:47:21 +02:00
|
|
|
if __name__ == '__main__':
|
2022-10-25 12:50:56 +02:00
|
|
|
# Turn ACT and POW leds off on start
|
2022-10-24 19:57:39 +02:00
|
|
|
ledSetup()
|
2022-10-09 18:11:03 +02:00
|
|
|
# app.run()
|
|
|
|
serve(app, host='127.0.0.1', port=5000, url_scheme='https')
|