diff --git a/db-handler/app/constants.py b/db-handler/app/constants.py index 425334b..f682ba8 100644 --- a/db-handler/app/constants.py +++ b/db-handler/app/constants.py @@ -25,5 +25,5 @@ LOGGER_TIME_FORMAT = '%I:%M:%S %p' # Values for microcontroller and servos/actuators -FREE = 0 # 0 is sent to the microcontroller if user is not busy -BUSY = 1 # 1 is sent to the microcontroller if user is busy +FREE = 1 # 0 is sent to the microcontroller if user is not busy +BUSY = 0 # 1 is sent to the microcontroller if user is busy diff --git a/db-handler/app/routers/figures_router.py b/db-handler/app/routers/figures_router.py index 072739c..17dfb4a 100644 --- a/db-handler/app/routers/figures_router.py +++ b/db-handler/app/routers/figures_router.py @@ -6,12 +6,10 @@ from app.dependencies.database import cal_col, user_col from app import utils from app.examples.figurines_payloads import FIGURINES_EXAMPLE -from app.constants import LOGGER_FORMAT, LOGGER_TIME_FORMAT, BUSY, FREE +from app.constants import LOGGER_FORMAT, LOGGER_TIME_FORMAT, BUSY, FREE, FAMILY_NAME router = APIRouter(prefix='/figurines', tags=['Figurines']) - - logging.basicConfig(level=logging.INFO, format=LOGGER_FORMAT, datefmt=LOGGER_TIME_FORMAT) _LOGGER = logging.getLogger(__name__) logging.getLogger(__name__).setLevel(logging.DEBUG) @@ -20,7 +18,7 @@ @router.get( '', summary='Gets map of all users and their availability for that timeslot. 1 represents busy, ' - '0 represents free', + '0 represents free', ) def get_available() -> Annotated[dict, Body(examples=[FIGURINES_EXAMPLE])]: """Endpoint that determines whether a user is free or not @@ -28,9 +26,12 @@ def get_available() -> Annotated[dict, Body(examples=[FIGURINES_EXAMPLE])]: Returns: dict: map, mapping a user to their status """ - users: dict = {} - timeslot_time: str = utils.current_to_timeslot() - _LOGGER.info("Getting availability for %s", timeslot_time) + # users: dict = {} + time: tuple = utils.current_to_timeslot() + timeslot_time: str = time[0] + timeslot_time = "15:30" + day: str = time[1] + _LOGGER.info("Getting availability for %s, %s", day, timeslot_time) # Finds the timeslot in the database at the current time booked_timeslot = cal_col.find_one( {'day': time_lib.strftime('%A').lower(), 'time': timeslot_time} @@ -39,15 +40,15 @@ def get_available() -> Annotated[dict, Body(examples=[FIGURINES_EXAMPLE])]: # Populates dictionary. 1 if user is booked, 0 is free if booked_timeslot is None: _LOGGER.info("Current time is not shown on calendar") - return {key:BUSY for key in user_col.distinct("user_id")} - - _LOGGER.debug("Received timeslot back: %s", str(booked_timeslot)) - booked_users = booked_timeslot.get("booked_users") - - # Populates dictionary. 1 if user is booked, 0 is free - return {user:BUSY if user in booked_users else FREE for user in user_col.distinct("user_id")} - - # for user in user_col.distinct('user_id'): - # status = 1 if user in booked_users else 0 - # users[user] = status - # return users + return {key: BUSY for key in user_col.distinct("user_id")} + + users: dict = { + f"0{FAMILY_NAME}": FREE if FAMILY_NAME in booked_timeslot.get("booked_users") else BUSY, + f"1Timmy": BUSY if "Timmy" in booked_timeslot.get("booked_users") else FREE, + f"2Kimmy": BUSY if "Kimmy" in booked_timeslot.get("booked_users") else FREE, + f"3Jimmy": BUSY if "Jimmy" in booked_timeslot.get("booked_users") else FREE, + f"4Timmy_Jr": BUSY if "Timmy_Jr" in booked_timeslot.get("booked_users") else FREE + } + + _LOGGER.debug("Sending dict: %s \n", users) + return users diff --git a/db-handler/app/utils.py b/db-handler/app/utils.py index 45eec9a..69be6aa 100644 --- a/db-handler/app/utils.py +++ b/db-handler/app/utils.py @@ -1,11 +1,14 @@ """Utils file for basic utility functions used throughout the database handler""" from datetime import datetime, timedelta import random +from typing import Tuple + from app import constants from app.dependencies import database as db +from app.constants import DAYS -def current_to_timeslot() -> str: +def current_to_timeslot() -> tuple[str, str]: """Calculates the current timeslot to a string that is used in the database Returns: @@ -23,7 +26,7 @@ def current_to_timeslot() -> str: now += timedelta(seconds=time_change) print(now) # Gets current time with minutes rounded down to the closest 15 minute timeslot - return f'{now.hour:02}:{now.minute // constants.TIMESLOT_LEN * constants.TIMESLOT_LEN:02}' + return f'{now.hour:02}:{now.minute // constants.TIMESLOT_LEN * constants.TIMESLOT_LEN:02}', str(DAYS[now.weekday()]) def timeslot_num_to_time(slot_num: int) -> str: diff --git a/figurines/microcontroller_comms.py b/figurines/microcontroller_comms.py index 42f2aa6..ef62c2f 100644 --- a/figurines/microcontroller_comms.py +++ b/figurines/microcontroller_comms.py @@ -15,7 +15,7 @@ API_ENDPOINT = "http://127.0.0.1:8000/figurines" # API Endpoint used to get calendar data API_REQUEST_TIMEOUT = 5 # Tiemout value in seconds before request times out -POLL_DELAY = 15 # Delay in seconds inbetween polling for figurines +POLL_DELAY = 5 # Delay in seconds inbetween polling for figurines read_state = True # Global used to determine read/write state of serial port serial_count = 0 # Count of serial reads before switching over to write @@ -170,6 +170,7 @@ def convert_response(response: dict) -> bytes: bytes: bytes to send to the serial port """ _LOGGER.info("Converting response data to bytes") + _LOGGER.debug(sorted(response)) com_string = ''.join(str(response[key]) for key in sorted(response)) data = 'FIG' + com_string return bytes(data, encoding='utf-8') @@ -212,6 +213,8 @@ def main(): _LOGGER.error("Could not find valid serial connection and timed out, try again") continue + _LOGGER.info("Serial port has been found \n") + while serial_port is not None: try: serial_port.read()