sensors added to settings
This commit is contained in:
Binary file not shown.
Binary file not shown.
@@ -7,7 +7,6 @@ from django.core.exceptions import PermissionDenied
|
||||
from django.http import HttpResponseNotAllowed
|
||||
from django.shortcuts import get_object_or_404, redirect
|
||||
from django.urls import path, reverse
|
||||
from django.utils import timezone
|
||||
from django.utils.html import format_html
|
||||
from django.utils.translation import gettext as _, gettext_lazy, ngettext
|
||||
|
||||
@@ -124,29 +123,21 @@ class EventAdmin(ClassificationAdmin):
|
||||
|
||||
@admin.register(Sensor)
|
||||
class SensorAdmin(admin.ModelAdmin):
|
||||
list_display = ("name", "is_active", "heartbeat_light", "last_heartbeat", "last_submission", "token_prefix")
|
||||
list_filter = ("is_active",)
|
||||
search_fields = ("name", "token_prefix")
|
||||
readonly_fields = ("token_prefix", "last_heartbeat", "last_submission", "created_at")
|
||||
list_display = ("name", "owner", "is_active", "heartbeat_light", "last_heartbeat", "last_submission", "token_prefix", "archived_at")
|
||||
list_filter = ("is_active", "owner", "archived_at")
|
||||
search_fields = ("name", "token_prefix", "owner__username", "owner__email")
|
||||
readonly_fields = ("token_prefix", "last_heartbeat", "last_submission", "created_at", "archived_at")
|
||||
actions = ("issue_api_token",)
|
||||
fieldsets = (
|
||||
(gettext_lazy("Sensor"), {"fields": ("name", "is_active", "created_at")}),
|
||||
(gettext_lazy("Sensor"), {"fields": ("name", "owner", "is_active", "created_at", "archived_at")}),
|
||||
(gettext_lazy("API token"), {"fields": ("token_prefix",), "description": gettext_lazy("Save the sensor first, select it in the list, then choose ‘Issue or rotate API token’. The complete token is shown once only; its salted hash is the only value stored.")}),
|
||||
(gettext_lazy("Heartbeat"), {"fields": ("last_heartbeat", "last_submission"), "description": gettext_lazy("Devices should POST to /fireworks/heartbeat/ with Authorization: Bearer <token>. Green means seen within 5 minutes; amber within 15; red is older or never seen.")}),
|
||||
)
|
||||
|
||||
@admin.display(description=gettext_lazy("Heartbeat"))
|
||||
def heartbeat_light(self, obj):
|
||||
if not obj.last_heartbeat:
|
||||
colour, label = "#ef4444", _("Never seen")
|
||||
else:
|
||||
age = timezone.now() - obj.last_heartbeat
|
||||
if age.total_seconds() <= 300:
|
||||
colour, label = "#22c55e", _("Online")
|
||||
elif age.total_seconds() <= 900:
|
||||
colour, label = "#f59e0b", _("Delayed")
|
||||
else:
|
||||
colour, label = "#ef4444", _("Offline")
|
||||
status, label = obj.heartbeat_status()
|
||||
colour = {"online": "#22c55e", "delayed": "#f59e0b"}.get(status, "#ef4444")
|
||||
return format_html('<span style="display:inline-flex;align-items:center;gap:6px"><span style="width:10px;height:10px;border-radius:50%;background:{}"></span>{}</span>', colour, label)
|
||||
|
||||
@admin.action(description=gettext_lazy("Issue or rotate API token (shown once)"))
|
||||
|
||||
@@ -0,0 +1,23 @@
|
||||
from django.conf import settings
|
||||
from django.db import migrations, models
|
||||
import django.db.models.deletion
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
dependencies = [
|
||||
("fireworks_detect", "0007_event_direction_accuracy_degrees_and_more"),
|
||||
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AddField(
|
||||
model_name="sensor",
|
||||
name="owner",
|
||||
field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name="sensors", to=settings.AUTH_USER_MODEL),
|
||||
),
|
||||
migrations.AddField(
|
||||
model_name="sensor",
|
||||
name="archived_at",
|
||||
field=models.DateTimeField(blank=True, editable=False, null=True),
|
||||
),
|
||||
]
|
||||
BIN
Binary file not shown.
@@ -1,6 +1,7 @@
|
||||
import math
|
||||
import secrets
|
||||
|
||||
from django.conf import settings
|
||||
from django.contrib.auth.hashers import check_password, make_password
|
||||
from django.core.exceptions import ValidationError
|
||||
from django.core.files.storage import default_storage
|
||||
@@ -13,7 +14,9 @@ class Sensor(models.Model):
|
||||
"""A physical recorder authorised to submit audio to the device API."""
|
||||
|
||||
name = models.CharField(max_length=100, unique=True, help_text=_("A recognisable device name, e.g. Garden ESP32."))
|
||||
owner = models.ForeignKey(settings.AUTH_USER_MODEL, null=True, blank=True, on_delete=models.SET_NULL, related_name="sensors")
|
||||
is_active = models.BooleanField(default=True)
|
||||
archived_at = models.DateTimeField(null=True, blank=True, editable=False)
|
||||
token_prefix = models.CharField(max_length=15, blank=True, editable=False)
|
||||
token_hash = models.CharField(max_length=256, blank=True, editable=False)
|
||||
last_heartbeat = models.DateTimeField(null=True, blank=True, editable=False)
|
||||
@@ -37,6 +40,23 @@ class Sensor(models.Model):
|
||||
def token_matches(self, token):
|
||||
return bool(self.token_hash) and check_password(token, self.token_hash)
|
||||
|
||||
def heartbeat_status(self):
|
||||
if not self.last_heartbeat:
|
||||
return "never", _("Never seen")
|
||||
age = (timezone.now() - self.last_heartbeat).total_seconds()
|
||||
if age <= 300:
|
||||
return "online", _("Online")
|
||||
if age <= 900:
|
||||
return "delayed", _("Delayed")
|
||||
return "offline", _("Offline")
|
||||
|
||||
def archive(self):
|
||||
self.archived_at = timezone.now()
|
||||
self.is_active = False
|
||||
self.token_hash = ""
|
||||
self.token_prefix = ""
|
||||
self.save(update_fields=["archived_at", "is_active", "token_hash", "token_prefix"])
|
||||
|
||||
def record_heartbeat(self, submission=False):
|
||||
now = timezone.now()
|
||||
self.last_heartbeat = now
|
||||
|
||||
Reference in New Issue
Block a user