Skip to content

Commit

Permalink
Add validation if sensor exists in database (#78)
Browse files Browse the repository at this point in the history
  • Loading branch information
klaasnicolaas committed Oct 27, 2023
1 parent 1799ee8 commit 7fae0eb
Showing 1 changed file with 19 additions and 2 deletions.
21 changes: 19 additions & 2 deletions app/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,21 @@ def register_mqtt_events(app: Flask) -> None:
app: The Flask application.
"""

def is_client_id_valid(client_id: str) -> bool:
"""Run check on valid sensor in database.
Args:
----
client_id (str): The client ID to check.
Returns:
-------
bool: True if the client ID is valid, False if not.
"""
with app.app_context():
sensor = Sensor.query.filter_by(client_id=f"sensor-{client_id}").first()
return sensor is not None

def on_topic_trigger(
client: MQTTClient, # pylint: disable=unused-argument
userdata: dict, # pylint: disable=unused-argument
Expand All @@ -318,15 +333,17 @@ def on_topic_trigger(
message: An instance of MQTTMessage.
"""
data: dict = json.loads(message.payload)
if workout_mode:
client_id = data["client_id"]

if workout_mode and is_client_id_valid(client_id):
match workout_id:
case 1:
# Kameleon
colors = Colors()
led_controller.set_color(colors.get_random_unique_color())
case 2:
# Trap op, trap af
workout_counting(data["client_id"])
workout_counting(client_id)
case 3:
# Meeloper
print("Workout 3")
Expand Down

0 comments on commit 7fae0eb

Please sign in to comment.