Skip to content

Commit

Permalink
Fxes
Browse files Browse the repository at this point in the history
  • Loading branch information
lucashicks1 committed Oct 25, 2023
1 parent 54c08e4 commit bc2c999
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 24 deletions.
4 changes: 2 additions & 2 deletions db-handler/app/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
39 changes: 20 additions & 19 deletions db-handler/app/routers/figures_router.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -20,17 +18,20 @@
@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
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}
Expand All @@ -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
7 changes: 5 additions & 2 deletions db-handler/app/utils.py
Original file line number Diff line number Diff line change
@@ -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:
Expand All @@ -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:
Expand Down
5 changes: 4 additions & 1 deletion figurines/microcontroller_comms.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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')
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit bc2c999

Please sign in to comment.