dialup/main.py

86 lines
2.4 KiB
Python

import asyncio
from markupsafe import escape
from unificontrol import UnifiClient
from dotenv import load_dotenv
from os import getenv
from ssl import get_server_certificate
from flask import Flask, request, render_template, Response, send_from_directory
import os
from pyVoIP.VoIP import VoIPPhone
app = Flask(__name__)
app.config['TEMPLATES_AUTO_RELOAD'] = True
global unifi
VISITOR_COUNT_FILE = "visitor_count"
SIP = None
def main():
if not os.path.exists(VISITOR_COUNT_FILE):
os.mknod(VISITOR_COUNT_FILE)
with open(VISITOR_COUNT_FILE, "w") as f:
f.write("0")
load_dotenv()
HOSTNAME = str(getenv("HOSTNAME"))
USERNAME = str(getenv("USERNAME"))
PASSWORD = str(getenv("PASSWORD"))
global SIP
SIP=VoIPPhone(
str(getenv("SIP_SERVER")), int(getenv("SIP_PORT")), str(getenv("SIP_USERNAME")), str(getenv("SIP_PASSWORD")))
SIP.start()
import time
for _ in range(1000):
print(SIP.get_status())
time.sleep(1)
cert = get_server_certificate((HOSTNAME, 443))
global unifi
unifi = UnifiClient(host=HOSTNAME,
username=USERNAME, password=PASSWORD, cert=cert)
app.run(host="0.0.0.0", port=80)
def visitor_count():
visitor_count = 1337
# Yes there is a race condition here, I don't care
try:
with open(VISITOR_COUNT_FILE, "r+") as f:
visitors = int(f.read().strip())
visitors += 1
f.seek(0)
f.write(str(visitors))
f.truncate()
visitor_count = visitors
except Exception as e:
print(f"error {e}")
return visitor_count
@app.route('/guest/s/default/', methods=["GET"])
def root():
id = escape(request.args.get('id'))
page = render_template("index.html", dialing_up=False, id=id, visitor_count=visitor_count())
response = Response(page, mimetype="text/html")
return response
@app.route("/dialup", methods=["POST"])
def dialup():
# Spawn audio on phone
# authorize guest after a certain amount of time
id = escape(request.args.get('id'))
page = render_template("index.html", dialing_up=True, id=id, visitor_count=visitor_count())
#unifi.authorize_guest(id, 1)
response = Response(page, mimetype="text/html")
return response
@app.route('/afbeeldingen/<path:path>')
def static_folder(path):
return send_from_directory('static', path)
if __name__ == "__main__":
main()