import logging logger = logging.getLogger(__name__) 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 # I think this could also be gpio controlled #See: https://github.com/technyon/nuki_hub#gpio-lock-control-optional def openDoor(): global doorIsOpen doorIsOpen = True logger.info("Still needs gpio out") pass def closeDoor(): global doorIsOpen doorIsOpen = False logger.info("Still needs gpio out") 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: logger.info(f"checking auth: {auth.name}") for timetable in auth.timetables: logger.info(f" checking timetable {timetable.id}") logger.info(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) logger.info(f" comparing time: Start:{starttime} Current:{current_time} End:{endtime}") if starttime < current_time < endtime: logger.info("Access Valid!") return True logger.info("No more auths found") return False except exc.NoResultFound: raise Exception("No Access with that key found, this might be a db error")