diff --git a/.github/workflows/linting.yaml b/.github/workflows/linting.yaml index 3c3da59..8a80c5d 100644 --- a/.github/workflows/linting.yaml +++ b/.github/workflows/linting.yaml @@ -34,7 +34,7 @@ jobs: # - name: 🚀 Run ruff linter # run: poetry run ruff check --output-format=github . # - name: 🚀 Run ruff formatter - # run: poetry run ruff format --check + # run: poetry run ruff format --check . pre-commit-hooks: name: pre-commit-hooks diff --git a/README.md b/README.md index fb84d60..98c8a28 100644 --- a/README.md +++ b/README.md @@ -90,6 +90,20 @@ To start the application in development mode, you can use the default Flask CLI flask run ``` +### Run Flask CLI commands + +If you want to run Flask CLI commands within a docker container, you should use the following format: + +```bash +docker exec stair_challenge_flask flask init_db +``` + +CLI commands: + +- `init_db` - Initialize the database +- `create_admin` - Create an admin user (only works when attached to shell) +- `seed_workouts` - Seed the workouts table with some default workouts + ## FAQ ### Temporary failure in name resolution diff --git a/app/__init__.py b/app/__init__.py index cb2be80..3c77a87 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -376,10 +376,15 @@ def on_topic_status( if sensor: # If the sensor already exists sensor.ip_address = data["ip_address"] - sensor.max_distance = data["max_distance"] + sensor.max_distance = data.get("max_distance") sensor.threshold = data["threshold"] - sensor.status = data["status"] + sensor.status = data.get("status") sensor.last_update = datetime.now() + + # Only update trigger_distance if status is "trigger" + if data["status"] == "trigger": + sensor.trigger_distance = data.get("distance", None) + db.session.commit() else: # If the sensor doesn't exist diff --git a/app/blueprints/backend/__init__.py b/app/blueprints/backend/__init__.py index 9afc06d..7e15f17 100644 --- a/app/blueprints/backend/__init__.py +++ b/app/blueprints/backend/__init__.py @@ -69,6 +69,7 @@ def add_sensor() -> None: client_id=f"sensor-{request.form.get('client_id')}", ip_address=request.form.get("ip_address"), max_distance=0, + trigger_distance=0, threshold=0, status="added", last_update=datetime.now(), diff --git a/app/blueprints/backend/models.py b/app/blueprints/backend/models.py index 9baa769..28ceb80 100644 --- a/app/blueprints/backend/models.py +++ b/app/blueprints/backend/models.py @@ -18,6 +18,7 @@ class Sensor(db.Model): client_id = db.Column(db.String(50), unique=True) ip_address = db.Column(db.String(50), unique=True) max_distance = db.Column(db.Integer) + trigger_distance = db.Column(db.Integer, nullable=True) threshold = db.Column(db.Integer) status = db.Column(db.String(50)) last_update = db.Column(db.DateTime) @@ -27,6 +28,7 @@ def __init__( client_id: str, ip_address: str, max_distance: int, + trigger_distance: int | None, threshold: int, status: str, last_update: datetime, @@ -38,6 +40,7 @@ def __init__( client_id (str): Sensor client ID ip_address (str): Sensor IP address max_distance (int): Sensor max distance + trigger_distance (int): Sensor trigger distance threshold (int): Sensor threshold status (str): Sensor status last_update (datetime): Sensor last update @@ -45,6 +48,7 @@ def __init__( self.client_id = client_id self.ip_address = ip_address self.max_distance = max_distance + self.trigger_distance = trigger_distance self.threshold = threshold self.status = status self.last_update = last_update diff --git a/app/blueprints/backend/templates/admin/sensors/show.html b/app/blueprints/backend/templates/admin/sensors/show.html index 76020c2..4c79d08 100644 --- a/app/blueprints/backend/templates/admin/sensors/show.html +++ b/app/blueprints/backend/templates/admin/sensors/show.html @@ -134,7 +134,7 @@
{{ sensor.client_id }}
Type - Value + Waarde @@ -144,7 +144,7 @@
{{ sensor.client_id }}
Trigger distance - mm + {{ sensor.trigger_distance }} mm Threshold diff --git a/tests/unit/test_models.py b/tests/unit/test_models.py index 5117a25..b12705a 100644 --- a/tests/unit/test_models.py +++ b/tests/unit/test_models.py @@ -44,10 +44,13 @@ def test_user_id(user: User) -> None: def test_sensor_model() -> None: """Test sensor model.""" - sensor = Sensor(1, "127.0.0.1", 100, 50, "active", datetime.utcnow()) - assert sensor.client_id == 1 + sensor = Sensor("sensor-1", "127.0.0.1", 100, 20, 50, "active", datetime.utcnow()) + assert sensor.client_id == "sensor-1" + assert not isinstance(sensor.client_id, int) assert sensor.ip_address == "127.0.0.1" + assert isinstance(sensor.ip_address, str) assert sensor.max_distance == 100 + assert sensor.trigger_distance == 20 assert sensor.threshold == 50 assert sensor.status == "active" assert isinstance(sensor.last_update, datetime)