GPIO working
This commit is contained in:
commit
36c129b939
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
@ -0,0 +1,2 @@
|
||||
.venv
|
||||
config.yaml
|
||||
109
app.py
Normal file
109
app.py
Normal file
@ -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
|
||||
3
requirements.txt
Normal file
3
requirements.txt
Normal file
@ -0,0 +1,3 @@
|
||||
flask
|
||||
authlib
|
||||
dynaconf
|
||||
10
templates/base.html
Normal file
10
templates/base.html
Normal file
@ -0,0 +1,10 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>{{ title }}</title>
|
||||
</head>
|
||||
<body style="text-align: center;">
|
||||
{% block content %}{% endblock %}
|
||||
</body>
|
||||
</html>
|
||||
10
templates/index.html
Normal file
10
templates/index.html
Normal file
@ -0,0 +1,10 @@
|
||||
{% extends "base.html" %}
|
||||
{% set title = "Visitor Check-in System" %}
|
||||
|
||||
{% block content %}
|
||||
<h1>Welcome to Visitor Check-in System</h1>
|
||||
<p>Visitors are currently {{ status }}</p>
|
||||
<form action="/checkin" method="post">
|
||||
<input type="submit" value="Check In Visitor">
|
||||
</form>
|
||||
{% endblock %}
|
||||
11
templates/status.html
Normal file
11
templates/status.html
Normal file
@ -0,0 +1,11 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>Status</title>
|
||||
</head>
|
||||
<body>
|
||||
<h1>Current Status: {{ status }}</h1>
|
||||
<a href="/">Go back to main page</a>
|
||||
</body>
|
||||
</html>
|
||||
Loading…
x
Reference in New Issue
Block a user