Skip to content

Commit

Permalink
Add trigger distance to sensor model (#81)
Browse files Browse the repository at this point in the history
* Add trigger distance to sensor model

* Add instructions for using CLI in Docker

* Fix command in linting CI for ruff

* Update tests for sensor model

* Add some isinstance tests to sensor model
  • Loading branch information
klaasnicolaas committed Oct 30, 2023
1 parent 7fae0eb commit 3a44811
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 7 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/linting.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 7 additions & 2 deletions app/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions app/blueprints/backend/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
4 changes: 4 additions & 0 deletions app/blueprints/backend/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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,
Expand All @@ -38,13 +40,15 @@ 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
"""
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
Expand Down
4 changes: 2 additions & 2 deletions app/blueprints/backend/templates/admin/sensors/show.html
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ <h5 class="mr-3 font-semibold dark:text-white">{{ sensor.client_id }}</h5>
<thead class="text-xs text-gray-700 uppercase bg-gray-50 dark:bg-gray-700 dark:text-gray-400">
<tr>
<th scope="col" class="px-6 py-3">Type</th>
<th scope="col" class="px-6 py-3">Value</th>
<th scope="col" class="px-6 py-3">Waarde</th>
</tr>
</thead>
<tbody>
Expand All @@ -144,7 +144,7 @@ <h5 class="mr-3 font-semibold dark:text-white">{{ sensor.client_id }}</h5>
</tr>
<tr class="bg-white border-b dark:bg-gray-800 dark:border-gray-700">
<th scope="row" class="px-6 py-4 font-medium text-gray-900 whitespace-nowrap dark:text-white">Trigger distance</th>
<td class="px-6 py-4">mm</td>
<td class="px-6 py-4" id="js--sensor-trigger-distance">{{ sensor.trigger_distance }} mm</td>
</tr>
<tr class="bg-white border-b dark:bg-gray-800 dark:border-gray-700">
<th scope="row" class="px-6 py-4 font-medium text-gray-900 whitespace-nowrap dark:text-white">Threshold</th>
Expand Down
7 changes: 5 additions & 2 deletions tests/unit/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 3a44811

Please sign in to comment.