Files
gatekeeper/app/services/door.py
2026-05-23 17:32:28 +02:00

57 lines
2.1 KiB
Python

import paho.mqtt.client as mqttClient
import paho.mqtt.publish as publish
from sqlmodel import select
from fastapi import Depends, HTTPException, status
from sqlalchemy.orm import selectinload
import sqlalchemy.exc as exc
import datetime
from app.services.database import Session, get_session
from app.model.models import *
doorIsOpen = True
client = mqttClient.Client(client_id="", userdata=None, protocol=mqttClient.MQTTv5)
client.tls_set(tls_version=mqttClient.ssl.PROTOCOL_TLS)
client.username_pw_set("username", "passwort")
#client.connect("host", port=8883)
# I think this could also be gpio controlled
#See: https://github.com/technyon/nuki_hub#gpio-lock-control-optional
def openDoor():
doorIsOpen = True
publish.single(topic="/lock/action", payload="unlock")
pass
def closeDoor():
doorIsOpen = False
publish.single(topic="/lock/action", payload="lock")
pass
def isDoorOpen():
return doorIsOpen
def checkAccess(uuid: str, db: Session):
try:
current_weekday = datetime.datetime.weekday(datetime.date.today())
current_time = datetime.datetime.now()
card = db.exec(select(Card).where(Card.uuid == uuid)).one()
for auth in card.group.accessauths:
print(f"checking auth: {auth.name}")
for timetable in auth.timetables:
print(f" checking timetable {timetable.id}")
print(f" comparing weekday: CUR:{current_weekday} TT:{timetable.weekday}")
if current_weekday == timetable.weekday:
starttime = datetime.datetime.combine(datetime.date.today(), timetable.starttime)
endtime = starttime + datetime.timedelta(minutes=timetable.duration)
print(f" comparing time: Start:{starttime} Current:{current_time} End:{endtime}")
if starttime < current_time < endtime:
print("Access Valid!")
return True
print("No more auths found")
return False
except exc.NoResultFound:
raise Exception("No Access with that key found, this might be a db error")