Skip to content

Commit

Permalink
Initial Update to the Firmware Dashboard
Browse files Browse the repository at this point in the history
Signed-off-by: Akarshan Kapoor <[email protected]>
  • Loading branch information
Kappuccino111 committed Sep 21, 2024
1 parent 6c93b7a commit 79f8104
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 50 deletions.
82 changes: 46 additions & 36 deletions server/django/sensordata/templates/firmware_dashboard.html
Original file line number Diff line number Diff line change
@@ -1,62 +1,47 @@
{% extends 'base.html' %}

{% block content %}
<div class="container mt-4">
<h2>Firmware Update Dashboard</h2>

<!-- Filter devices by firmware version -->
<form method="get" class="form-inline mb-3">
<label for="firmware_version" class="mr-2">Filter by Firmware Version:</label>
<select name="firmware_version" class="form-control mr-2">
<option value="">-- All Versions --</option>
{% for version in firmware_versions %}
<option value="{{ version }}" {% if request.GET.firmware_version == version %}selected{% endif %}>
{{ version }}
</option>
{% endfor %}
</select>
<button type="submit" class="btn btn-primary">Filter</button>
</form>

<!-- Table of devices with checkboxes to select them -->
<form method="post" id="firmware-update-form">
{% csrf_token %}

<div class="form-group"></div>
<label for="firmware_id">Select Firmware to Update:</label>
<select name="firmware_id" class="form-control" required>
{% for version in firmware_versions %}
<option value="{{ version }}">{{ version }}</option>
{% endfor %}
</select>
</div>
<button type="submit" class="btn btn-success" id="update-firmware-btn">Update Firmware for Selected
Devices</button>

<br><br>

<table class="table table-bordered">
<thead>
<tr>
<th>Select</th>
<th>Device ID</th>
<th>Device Name</th>
<th>Device Name/ID</th>
<th>Current Firmware Version</th>
</tr>
</thead>
<tbody>
{% for device in devices %}
<tr>
<td><input type="checkbox" name="device_ids" value="{{ device.id }}"></td>
<td>{{ device.id }}</td>
<td>{{ device.endpoint }}</td>
<td>{{ device.resource_set.first.str_value }}</td>
<!-- Assuming first resource holds firmware version -->
<td>{{ device.current_firmware }}</td>
</tr>
{% endfor %}
</tbody>
</table>

<!-- Firmware selection for update -->
<div class="form-group">
<label for="firmware_id">Select Firmware to Update:</label>
<select name="firmware_id" class="form-control" required>
{% for firmware in firmware_versions %}
<option value="{{ firmware.id }}">{{ firmware.version }}</option>
{% endfor %}
</select>
</div>

<button type="submit" class="btn btn-success">Update Firmware for Selected Devices</button>
</form>

<!-- Ongoing Firmware Updates -->
<h3>Ongoing Firmware Updates</h3>
<h3 class="mt-4">Ongoing Firmware Updates</h3>
<table class="table table-striped">
<thead>
<tr>
Expand All @@ -79,15 +64,40 @@ <h3>Ongoing Firmware Updates</h3>
</table>
</div>

<!-- Pop-up Modal -->
<div class="modal fade" id="updateModal" tabindex="-1" role="dialog" aria-labelledby="updateModalLabel"
aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="updateModalLabel">Firmware Update</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">&times;</span>
</button>
</div>
<div class="modal-body">
Firmware updates are being initiated for the selected devices.
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>

<script>
document.getElementById('firmware-update-form').addEventListener('submit', function (event) {
// Ensure at least one device is selected
event.preventDefault(); // Prevent the form from submitting

var checkboxes = document.querySelectorAll('input[name="device_ids"]:checked');
if (checkboxes.length === 0) {
alert("Please select at least one device.");
event.preventDefault();
return;
}

// Show the pop-up
$('#updateModal').modal('show');

});
</script>

{% endblock %}
26 changes: 12 additions & 14 deletions server/django/sensordata/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -417,27 +417,25 @@ def license_dashboard_view(request):

@login_required
def firmware_dashboard_view(request):
"""View to display all firmware updates and manage updates for multiple devices."""

firmware_version = request.GET.get('firmware_version', None)
# Get all devices (endpoints)
devices = Endpoint.objects.all()

# Fetch the current firmware version for each device
for device in devices:
firmware_resource = Resource.objects.filter(
endpoint=device,
resource_type__object_id=3, # Assuming object_id 3 is for firmware
resource_type__resource_id=3 # Assuming resource_id 3 is for firmware version
).order_by('-timestamp_created').first()

if firmware_version:
firmware = get_object_or_404(Firmware, version=firmware_version)
devices = Endpoint.objects.filter(
resource__resource_type__object_id=3,
resource__resource_type__resource_id=3,
resource__str_value=firmware.version
).distinct()
else:
devices = Endpoint.objects.all()
device.current_firmware = firmware_resource.str_value if firmware_resource else "Unknown"

firmware_versions = Firmware.objects.values_list('version', flat=True).distinct()

ongoing_updates = FirmwareUpdate.objects.exclude(
state=FirmwareUpdate.State.STATE_IDLE
).select_related('endpoint', 'firmware')


if request.method == 'POST':
selected_devices = request.POST.getlist('device_ids')
firmware_id = request.POST.get('firmware_id')
Expand All @@ -448,7 +446,7 @@ def firmware_dashboard_view(request):
endpoint = get_object_or_404(Endpoint, id=device_id)
FirmwareUpdate.objects.create(endpoint=endpoint, firmware=firmware)

return Response({'status': 'success', 'message': 'Firmware update initiated for selected devices'})
return JsonResponse({'status': 'success', 'message': 'Firmware update initiated for selected devices'})

context = {
'devices': devices,
Expand Down

0 comments on commit 79f8104

Please sign in to comment.