From 36c129b9391542099e00af591c9afdb11d25c2a8 Mon Sep 17 00:00:00 2001 From: Jim Shepich III Date: Sun, 13 Jul 2025 14:18:59 -0400 Subject: [PATCH] GPIO working --- .gitignore | 2 + app.py | 109 ++++++++++++++++++++++++++++++++++++++++++ requirements.txt | 3 ++ templates/base.html | 10 ++++ templates/index.html | 10 ++++ templates/status.html | 11 +++++ test.py | 4 ++ 7 files changed, 149 insertions(+) create mode 100644 .gitignore create mode 100644 app.py create mode 100644 requirements.txt create mode 100644 templates/base.html create mode 100644 templates/index.html create mode 100644 templates/status.html create mode 100644 test.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..4a4c19a --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +.venv +config.yaml diff --git a/app.py b/app.py new file mode 100644 index 0000000..a974305 --- /dev/null +++ b/app.py @@ -0,0 +1,109 @@ +import subprocess +from flask import Flask, render_template, request, redirect, url_for, session +from authlib.integrations.flask_client import OAuth +from time import sleep +from dynaconf import Dynaconf +import logging + +logger = logging.getLogger(__name__) + +config = Dynaconf(settings_files = ['config.yaml']) + +class Pinctrl: + def __init__(self): + pass + + def _run(self, verb, pin, option): + if config.dry_run is False: + subprocess.run(['pinctrl', verb, str(pin), option]) + else: + logger.info(f'Simulating command `pinctrl {verb} {pin} {option}`') + + def input(self, pin): + self._run('set', pin, 'ip') + def output(self, pin): + self._run('set', pin, 'op') + def on(self, pin): + self._run('set', pin, 'dh') + def off(self, pin): + self._run('set', pin, 'dl') + +pinctrl = Pinctrl() + +app = Flask(__name__) +oauth = OAuth(app) + +oauth.register( + 'jimlab', + **config.oauth.to_dict() +) + +# Set up lights. +for led, pin in config.pinout.led.items(): + pinctrl.output(pin) + pinctrl.off(pin) + +@app.route('/') +def homepage(): + user = session.get('user') + return render_template('index.html', user=user) + + +@app.route('/login') +def login(): + redirect_uri = url_for('auth', _external=True) + return oauth.jimlab.authorize_redirect(redirect_uri) + + +@app.route('/auth') +def auth(): + token = oauth.jimlab.authorize_access_token() + session['user'] = token['userinfo'] + return redirect('/') + +# @app.route('/') +# def index(): +# logger.info(oauth.jimlab.get('user')) +# return render_template('index.html') + +# @app.route('/checkin', methods=['POST']) +# def check_in(): +# if can_check_in(): +# pinctrl.on(config.pinout.led[config.led_map.guest]) +# print("Visitor checked in") +# else: +# pinctrl.on(config.pinout.led[config.led_map.error]) +# print("Exception occurred: Cannot accept visitors currently") + +# return redirect(url_for('index')) + +# @app.route('/status') +# def get_status_route(): +# status = get_status() +# return render_template('status.html', status=status) + +# def get_status(): +# if is_accepting_visitors(): +# return "accepting" +# else: +# return "not accepting" + +# def is_accepting_visitors(): +# # This would be checked against Authentik server or a configuration file +# # For simplicity, let's assume it's always true in this example +# return True + +# def can_check_in(): +# # Logic to check if the system can accept a visitor +# return is_accepting_visitors() + + +if __name__ == '__main__': + try: + app.run(host='0.0.0.0', port=config.server.port, debug=config.server.debug) + finally: + # Turn off lights + for led, pin in config.pinout.led.items(): + pinctrl.off(pin) + +#with shelve.open('db', 'w', writeback=True) as db \ No newline at end of file diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..dde9d99 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,3 @@ +flask +authlib +dynaconf \ No newline at end of file diff --git a/templates/base.html b/templates/base.html new file mode 100644 index 0000000..d9f2469 --- /dev/null +++ b/templates/base.html @@ -0,0 +1,10 @@ + + + + + {{ title }} + + + {% block content %}{% endblock %} + + \ No newline at end of file diff --git a/templates/index.html b/templates/index.html new file mode 100644 index 0000000..f7534eb --- /dev/null +++ b/templates/index.html @@ -0,0 +1,10 @@ +{% extends "base.html" %} +{% set title = "Visitor Check-in System" %} + +{% block content %} +

Welcome to Visitor Check-in System

+

Visitors are currently {{ status }}

+
+ +
+{% endblock %} \ No newline at end of file diff --git a/templates/status.html b/templates/status.html new file mode 100644 index 0000000..064a8c8 --- /dev/null +++ b/templates/status.html @@ -0,0 +1,11 @@ + + + + + Status + + +

Current Status: {{ status }}

+ Go back to main page + + \ No newline at end of file diff --git a/test.py b/test.py new file mode 100644 index 0000000..41d5fa4 --- /dev/null +++ b/test.py @@ -0,0 +1,4 @@ +import RPi.GPIO as GPIO +GPIO.setmode(GPIO.BCM) +GPIO.setup(17, GPIO.OUT) +GPIO.output(17, GPIO.HIGH)