From f82cd4aff2edb2ffe843916f0e12193dd90c3035 Mon Sep 17 00:00:00 2001 From: Thomas Klaehn Date: Mon, 18 Jan 2021 09:56:52 +0100 Subject: [PATCH] Initial commit Signed-off-by: Thomas Klaehn --- .gitignore | 3 ++ MANIFEST.in | 1 + README.md | 24 ++++++++++++++++ setup.py | 24 ++++++++++++++++ start-weblight | 3 ++ weblight.service | 10 +++++++ weblight/__init__.py | 2 ++ weblight/app.py | 32 +++++++++++++++++++++ weblight/templates/index.html | 52 +++++++++++++++++++++++++++++++++++ 9 files changed, 151 insertions(+) create mode 100644 .gitignore create mode 100644 MANIFEST.in create mode 100644 README.md create mode 100644 setup.py create mode 100644 start-weblight create mode 100644 weblight.service create mode 100644 weblight/__init__.py create mode 100644 weblight/app.py create mode 100644 weblight/templates/index.html diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..5c80531 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +__pycache__ + +dist/ diff --git a/MANIFEST.in b/MANIFEST.in new file mode 100644 index 0000000..84dae5c --- /dev/null +++ b/MANIFEST.in @@ -0,0 +1 @@ +recursive-include weblight/templates * diff --git a/README.md b/README.md new file mode 100644 index 0000000..d6fb6bb --- /dev/null +++ b/README.md @@ -0,0 +1,24 @@ +# Weblight + +Tool to switch on/off the yard light with web page. + +## Installation + +```shell +python setup.py install +``` + +## Usage + +### Manual + +```shell +start-weblight +``` + +### systemd + +```shell +$ systemctl enable weblight +$ systemctl start weblight +``` diff --git a/setup.py b/setup.py new file mode 100644 index 0000000..49e122e --- /dev/null +++ b/setup.py @@ -0,0 +1,24 @@ +import sys +import os +import shutil +import stat +from setuptools import setup + +NAME = 'Web Light' +VERSION = '1' +AUTHOR = 'Thomas Klaehn' +EMAIL = 'tkl@blackfinn.de' +PACKAGES = ['weblight'] +SCRIPTS = ['start-weblight'] +SERVICEDIR = "/lib/systemd/system" +REQUIRES = ['Flask', 'RPi.GPIO'] +DAEMON_START_SCRIPT = os.path.join(SERVICEDIR, 'weblight.service') + +if sys.argv[1] == 'install': + os.makedirs(SERVICEDIR, exist_ok=True) + shutil.copyfile('weblight.service', DAEMON_START_SCRIPT) + os.chmod(DAEMON_START_SCRIPT, stat.S_IRUSR | stat.S_IWUSR | stat.S_IRGRP | stat.S_IROTH) + +setup(name=NAME, version=VERSION, long_description=__doc__, author=AUTHOR, author_email=EMAIL, + packages=PACKAGES, scripts=SCRIPTS, include_package_data=True, zip_safe=False, + install_requires=REQUIRES) diff --git a/start-weblight b/start-weblight new file mode 100644 index 0000000..d4874f3 --- /dev/null +++ b/start-weblight @@ -0,0 +1,3 @@ +#!/bin/bash + +python -m weblight $@ diff --git a/weblight.service b/weblight.service new file mode 100644 index 0000000..746c947 --- /dev/null +++ b/weblight.service @@ -0,0 +1,10 @@ +[Unit] +Description=Weblight +After=multi-user.target + +[Service] +Type=idle +ExecStart=/usr/local/bin/start-weblight + +[Install] +WantedBy=multi-user.target diff --git a/weblight/__init__.py b/weblight/__init__.py new file mode 100644 index 0000000..0ccc333 --- /dev/null +++ b/weblight/__init__.py @@ -0,0 +1,2 @@ +from .app import app + diff --git a/weblight/app.py b/weblight/app.py new file mode 100644 index 0000000..9ff3fa4 --- /dev/null +++ b/weblight/app.py @@ -0,0 +1,32 @@ +from flask import Flask +from flask import render_template +from flask import redirect +from flask import url_for +from flask import make_response + +import RPi.GPIO as GPIO + +RELAY_1 = 26 + +GPIO.setwarnings(False) +GPIO.setmode(GPIO.BCM) +GPIO.setup(RELAY_1,GPIO.OUT) + +app = Flask(__name__) + +@app.route('/') +def index(): + return render_template('index.html') + +@app.route('/', methods=['POST']) +def reroute(state): + if state == 'on': + GPIO.output(RELAY_1, 0) + else: + GPIO.output(RELAY_1, 1) + + response = make_response(redirect(url_for('index'))) + return response + +if __name__ == '__main__': + app.run(debug=True, host='0.0.0.0', port=8000) diff --git a/weblight/templates/index.html b/weblight/templates/index.html new file mode 100644 index 0000000..c2a2be7 --- /dev/null +++ b/weblight/templates/index.html @@ -0,0 +1,52 @@ + + + + Yard Light + + + + + +

Yard Light

+

+

+

+
+
+

+
+

+ +