mirror of
https://github.com/ferdzo/iotDashboard.git
synced 2026-04-05 09:06:26 +00:00
Removed unused files, updated templates, added gpt service and small fixes on Django
This commit is contained in:
@@ -10,10 +10,15 @@ from datetime import datetime
|
||||
@dataclass
|
||||
class DeviceRegistrationResponse:
|
||||
device_id: str
|
||||
certificate_id: str
|
||||
certificate: str
|
||||
private_key: str
|
||||
ca_certificate: str
|
||||
protocol: str
|
||||
certificate_id: Optional[str] = None
|
||||
ca_certificate_pem: Optional[str] = None
|
||||
certificate_pem: Optional[str] = None
|
||||
private_key_pem: Optional[str] = None
|
||||
expires_at: Optional[datetime] = None
|
||||
credential_id: Optional[str] = None
|
||||
api_key: Optional[str] = None
|
||||
webhook_secret: Optional[str] = None
|
||||
|
||||
|
||||
@dataclass
|
||||
@@ -21,9 +26,9 @@ class DeviceInfo:
|
||||
id: str
|
||||
name: str
|
||||
location: Optional[str]
|
||||
is_active: bool
|
||||
protocol: str
|
||||
connection_config: Optional[Dict[str, Any]]
|
||||
created_at: datetime
|
||||
certificates: List[Dict[str, Any]]
|
||||
|
||||
|
||||
class DeviceManagerAPIError(Exception):
|
||||
@@ -63,20 +68,33 @@ class DeviceManagerClient:
|
||||
status_code=0, message=f"Connection error: {str(e)}"
|
||||
)
|
||||
|
||||
def register_device(self, name: str, location: Optional[str] = None) -> DeviceRegistrationResponse:
|
||||
payload = {"name": name}
|
||||
def register_device(
|
||||
self,
|
||||
name: str,
|
||||
location: Optional[str] = None,
|
||||
protocol: str = "mqtt",
|
||||
connection_config: Optional[Dict[str, Any]] = None
|
||||
) -> DeviceRegistrationResponse:
|
||||
payload = {"name": name, "protocol": protocol}
|
||||
if location:
|
||||
payload["location"] = location
|
||||
if connection_config:
|
||||
payload["connection_config"] = connection_config
|
||||
|
||||
response = self._request("POST", "/devices/register", json=payload)
|
||||
data = response.json()
|
||||
|
||||
return DeviceRegistrationResponse(
|
||||
device_id=data["device_id"],
|
||||
certificate_id=data["certificate_id"],
|
||||
certificate=data["certificate"],
|
||||
private_key=data["private_key"],
|
||||
ca_certificate=data["ca_certificate"],
|
||||
protocol=data["protocol"],
|
||||
certificate_id=data.get("certificate_id"),
|
||||
ca_certificate_pem=data.get("ca_certificate_pem"),
|
||||
certificate_pem=data.get("certificate_pem"),
|
||||
private_key_pem=data.get("private_key_pem"),
|
||||
expires_at=datetime.fromisoformat(data["expires_at"].replace("Z", "+00:00")) if data.get("expires_at") else None,
|
||||
credential_id=data.get("credential_id"),
|
||||
api_key=data.get("api_key"),
|
||||
webhook_secret=data.get("webhook_secret"),
|
||||
)
|
||||
|
||||
def get_device(self, device_id: str) -> DeviceInfo:
|
||||
@@ -87,9 +105,9 @@ class DeviceManagerClient:
|
||||
id=data["id"],
|
||||
name=data["name"],
|
||||
location=data.get("location"),
|
||||
is_active=data["is_active"],
|
||||
protocol=data["protocol"],
|
||||
connection_config=data.get("connection_config"),
|
||||
created_at=datetime.fromisoformat(data["created_at"].replace("Z", "+00:00")),
|
||||
certificates=data.get("certificates", []),
|
||||
)
|
||||
|
||||
def list_devices(self) -> List[DeviceInfo]:
|
||||
@@ -101,11 +119,11 @@ class DeviceManagerClient:
|
||||
id=device["id"],
|
||||
name=device["name"],
|
||||
location=device.get("location"),
|
||||
is_active=device["is_active"],
|
||||
protocol=device["protocol"],
|
||||
connection_config=device.get("connection_config"),
|
||||
created_at=datetime.fromisoformat(
|
||||
device["created_at"].replace("Z", "+00:00")
|
||||
),
|
||||
certificates=device.get("certificates", []),
|
||||
)
|
||||
for device in data
|
||||
]
|
||||
@@ -114,9 +132,22 @@ class DeviceManagerClient:
|
||||
response = self._request("POST", f"/devices/{device_id}/revoke")
|
||||
return response.json()
|
||||
|
||||
def renew_certificate(self, device_id: str) -> Dict[str, Any]:
|
||||
def renew_certificate(self, device_id: str) -> DeviceRegistrationResponse:
|
||||
response = self._request("POST", f"/devices/{device_id}/renew")
|
||||
return response.json()
|
||||
data = response.json()
|
||||
|
||||
return DeviceRegistrationResponse(
|
||||
device_id=data["device_id"],
|
||||
protocol=data["protocol"],
|
||||
certificate_id=data.get("certificate_id"),
|
||||
ca_certificate_pem=data.get("ca_certificate_pem"),
|
||||
certificate_pem=data.get("certificate_pem"),
|
||||
private_key_pem=data.get("private_key_pem"),
|
||||
expires_at=datetime.fromisoformat(data["expires_at"].replace("Z", "+00:00")) if data.get("expires_at") else None,
|
||||
credential_id=data.get("credential_id"),
|
||||
api_key=data.get("api_key"),
|
||||
webhook_secret=data.get("webhook_secret"),
|
||||
)
|
||||
|
||||
def get_ca_certificate(self) -> str:
|
||||
response = self._request("GET", "/ca_certificate")
|
||||
@@ -137,8 +168,13 @@ class DeviceManagerClient:
|
||||
default_client = DeviceManagerClient()
|
||||
|
||||
|
||||
def register_device(name: str, location: Optional[str] = None) -> DeviceRegistrationResponse:
|
||||
return default_client.register_device(name, location)
|
||||
def register_device(
|
||||
name: str,
|
||||
location: Optional[str] = None,
|
||||
protocol: str = "mqtt",
|
||||
connection_config: Optional[Dict[str, Any]] = None
|
||||
) -> DeviceRegistrationResponse:
|
||||
return default_client.register_device(name, location, protocol, connection_config)
|
||||
|
||||
|
||||
def get_device(device_id: str) -> DeviceInfo:
|
||||
|
||||
@@ -1,65 +1,40 @@
|
||||
"""
|
||||
Django forms for the IoT Dashboard.
|
||||
|
||||
Note: Device registration is handled through the device_manager API.
|
||||
These forms are used for the legacy Django UI only.
|
||||
"""
|
||||
|
||||
from django import forms
|
||||
from iotDashboard.models import Device, Sensor, SensorType
|
||||
from iotDashboard.models import Device
|
||||
|
||||
|
||||
class DeviceForm(forms.ModelForm):
|
||||
"""
|
||||
Form for creating/editing devices.
|
||||
|
||||
Note: This is for the Django UI only. Actual device registration
|
||||
happens through the device_manager microservice API.
|
||||
"""
|
||||
|
||||
protocol = forms.ChoiceField(
|
||||
choices=[
|
||||
("mqtt", "MQTT"),
|
||||
("http", "HTTP"),
|
||||
("webhook", "Webhook"),
|
||||
],
|
||||
initial="mqtt",
|
||||
help_text="Communication protocol for this device",
|
||||
)
|
||||
|
||||
class Meta:
|
||||
model = Device
|
||||
fields = ["name", "ip", "protocol"] # Exclude sensors from the fields
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
# No need to handle sensors in the form
|
||||
super(DeviceForm, self).__init__(*args, **kwargs)
|
||||
|
||||
def save(self, commit=True):
|
||||
# Save the device instance
|
||||
device = super(DeviceForm, self).save(commit=False)
|
||||
|
||||
if commit:
|
||||
device.save()
|
||||
|
||||
return device
|
||||
|
||||
|
||||
class SensorWithTypeForm(forms.ModelForm):
|
||||
# Add fields for SensorType directly in the form
|
||||
type_name = forms.CharField(max_length=50, label="Sensor Type Name")
|
||||
unit = forms.CharField(max_length=20, label="Unit", required=False)
|
||||
protocol = forms.ChoiceField(
|
||||
choices=[("mqtt", "MQTT"), ("http", "HTTP")], label="Protocol"
|
||||
)
|
||||
topic = forms.CharField(max_length=100, label="Topic", required=False)
|
||||
endpoint = forms.CharField(max_length=100, label="Endpoint", required=False)
|
||||
|
||||
class Meta:
|
||||
model = Sensor
|
||||
fields = ["enabled"] # Exclude 'device' from the form fields
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
self.device = kwargs.pop("device", None) # Get the device from kwargs
|
||||
super(SensorWithTypeForm, self).__init__(*args, **kwargs)
|
||||
|
||||
def save(self, commit=True):
|
||||
# Create or get the SensorType
|
||||
try:
|
||||
sensor_type = SensorType.objects.get(name=self.cleaned_data["type_name"])
|
||||
except SensorType.DoesNotExist:
|
||||
sensor_type = SensorType(
|
||||
name=self.cleaned_data["type_name"],
|
||||
unit=self.cleaned_data["unit"],
|
||||
protocol=self.cleaned_data["protocol"],
|
||||
topic=self.cleaned_data["topic"],
|
||||
endpoint=self.cleaned_data["endpoint"],
|
||||
)
|
||||
if commit:
|
||||
sensor_type.save()
|
||||
|
||||
# Create Sensor with the SensorType found or created
|
||||
sensor = super(SensorWithTypeForm, self).save(commit=False)
|
||||
sensor.type = sensor_type
|
||||
sensor.device = self.device # Associate the sensor with the device
|
||||
|
||||
if commit:
|
||||
sensor.save()
|
||||
|
||||
return sensor
|
||||
fields = ["name", "location", "protocol"]
|
||||
widgets = {
|
||||
"name": forms.TextInput(attrs={"class": "form-control", "placeholder": "Device name"}),
|
||||
"location": forms.TextInput(attrs={"class": "form-control", "placeholder": "Device location (optional)"}),
|
||||
}
|
||||
help_texts = {
|
||||
"name": "Unique identifier for this device",
|
||||
"location": "Physical location or description",
|
||||
}
|
||||
|
||||
6
iotDashboard/main.py
Normal file
6
iotDashboard/main.py
Normal file
@@ -0,0 +1,6 @@
|
||||
def main():
|
||||
print("Hello from iotdashboard!")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
5
iotDashboard/pyproject.toml
Normal file
5
iotDashboard/pyproject.toml
Normal file
@@ -0,0 +1,5 @@
|
||||
[project]
|
||||
name = "dashboard"
|
||||
version = "0.1.0"
|
||||
requires-python = ">=3.13"
|
||||
dependencies = []
|
||||
@@ -3,15 +3,19 @@
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>Add Sensor and Type</title>
|
||||
<title>Renew Certificate</title>
|
||||
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
|
||||
</head>
|
||||
<body class="bg-light">
|
||||
<nav class="navbar navbar-expand-lg navbar-light bg-light">
|
||||
<nav class="navbar navbar-expand-lg navbar-light bg-light">
|
||||
<div class="container-fluid">
|
||||
<a class="navbar-brand" href="#">IoT Dashboard</a>
|
||||
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav">
|
||||
<span class="navbar-toggler-icon"></span>
|
||||
</button>
|
||||
<div class="collapse navbar-collapse" id="navbarNav">
|
||||
<ul class="navbar-nav me-auto mb-2 mb-lg-0">
|
||||
<li class="nav-item"><a class="nav-link" href="{% url 'index' %}">Chart</a></li>
|
||||
<li class="nav-item"><a class="nav-link" href="{% url 'device_list' %}">Devices</a></li>
|
||||
</ul>
|
||||
</div>
|
||||
@@ -19,24 +23,19 @@
|
||||
</nav>
|
||||
|
||||
<div class="container mt-5">
|
||||
<h1 class="text-center mb-4">Add Sensor and Sensor Type</h1>
|
||||
<form method="post">
|
||||
<h1 class="text-center mb-4">Renew Certificate</h1>
|
||||
|
||||
<div class="alert alert-info" role="alert">
|
||||
<strong>Info:</strong> This will generate a new certificate for device "{{ device.name }}" and revoke the old one.
|
||||
</div>
|
||||
|
||||
<p class="text-center">Do you want to renew the certificate now?</p>
|
||||
|
||||
<form method="post" class="text-center">
|
||||
{% csrf_token %}
|
||||
{{ form.as_p }} <!-- Renders the inputs for all fields you added in your form -->
|
||||
<input type="hidden" name="device_id" value="{{ device.id }}"> <!-- Hidden field for device ID -->
|
||||
<button type="submit" class="btn btn-success">Save</button>
|
||||
<button type="submit" class="btn btn-primary">Renew Certificate</button>
|
||||
<a href="{% url 'device_list' %}" class="btn btn-secondary">Cancel</a>
|
||||
</form>
|
||||
|
||||
{% if form.errors %}
|
||||
<div class="alert alert-danger">
|
||||
<ul>
|
||||
{% for field, errors in form.errors.items %}
|
||||
<li>{{ field }}: {{ errors|join:', ' }}</li>
|
||||
{% endfor %}
|
||||
</ul>
|
||||
</div>
|
||||
{% endif %}
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
42
iotDashboard/templates/certificate_revoke_confirm.html
Normal file
42
iotDashboard/templates/certificate_revoke_confirm.html
Normal file
@@ -0,0 +1,42 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>Revoke Certificate</title>
|
||||
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
|
||||
</head>
|
||||
<body class="bg-light">
|
||||
<nav class="navbar navbar-expand-lg navbar-light bg-light">
|
||||
<div class="container-fluid">
|
||||
<a class="navbar-brand" href="#">IoT Dashboard</a>
|
||||
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav">
|
||||
<span class="navbar-toggler-icon"></span>
|
||||
</button>
|
||||
<div class="collapse navbar-collapse" id="navbarNav">
|
||||
<ul class="navbar-nav me-auto mb-2 mb-lg-0">
|
||||
<li class="nav-item"><a class="nav-link" href="{% url 'index' %}">Chart</a></li>
|
||||
<li class="nav-item"><a class="nav-link" href="{% url 'device_list' %}">Devices</a></li>
|
||||
</ul>
|
||||
</div>
|
||||
</div>
|
||||
</nav>
|
||||
|
||||
<div class="container mt-5">
|
||||
<h1 class="text-center mb-4">Revoke Certificate</h1>
|
||||
|
||||
<div class="alert alert-warning" role="alert">
|
||||
<strong>Warning!</strong> This action will revoke the certificate for device "{{ device.name }}".
|
||||
The device will no longer be able to connect until a new certificate is issued.
|
||||
</div>
|
||||
|
||||
<p class="text-center">Are you sure you want to revoke the certificate?</p>
|
||||
|
||||
<form method="post" class="text-center">
|
||||
{% csrf_token %}
|
||||
<button type="submit" class="btn btn-danger">Revoke Certificate</button>
|
||||
<a href="{% url 'device_list' %}" class="btn btn-secondary">Cancel</a>
|
||||
</form>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
@@ -93,19 +93,19 @@
|
||||
<!-- Device Selector Dropdown -->
|
||||
<div class="row mb-4">
|
||||
<div class="col-md-4 offset-md-4">
|
||||
<select id="deviceSelector" class="form-select" onchange="updateSensorOptions()">
|
||||
<select id="deviceSelector" class="form-select" onchange="updateMetricOptions()">
|
||||
<option value="">-- Select a Device --</option>
|
||||
<script>
|
||||
const devices = JSON.parse('{{ devices_json|escapejs }}');
|
||||
|
||||
const deviceSelector = document.getElementById('deviceSelector');
|
||||
const sensorSelector = document.getElementById('sensorSelector');
|
||||
const metricSelector = document.getElementById('metricSelector');
|
||||
|
||||
// Populate device selector
|
||||
devices.forEach(device => {
|
||||
const option = document.createElement('option');
|
||||
option.value = device.name; // Device name used for identification
|
||||
option.textContent = device.name;
|
||||
option.value = device.id; // Use device ID
|
||||
option.textContent = device.name + ' (' + device.protocol.toUpperCase() + ')';
|
||||
deviceSelector.appendChild(option);
|
||||
});
|
||||
</script>
|
||||
@@ -113,11 +113,11 @@
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- Sensor Selector Dropdown -->
|
||||
<!-- Metric Selector Dropdown -->
|
||||
<div class="row mb-4">
|
||||
<div class="col-md-4 offset-md-4">
|
||||
<select id="sensorSelector" class="form-select">
|
||||
<option value="">-- Select a Sensor --</option>
|
||||
<select id="metricSelector" class="form-select">
|
||||
<option value="">-- Select a Metric --</option>
|
||||
</select>
|
||||
</div>
|
||||
</div>
|
||||
@@ -157,39 +157,39 @@
|
||||
var ctx = document.getElementById('conditionsChart').getContext('2d');
|
||||
var conditionsChart;
|
||||
|
||||
function updateSensorOptions() {
|
||||
const selectedDeviceName = document.getElementById('deviceSelector').value;
|
||||
function updateMetricOptions() {
|
||||
const selectedDeviceId = document.getElementById('deviceSelector').value;
|
||||
|
||||
// Find the selected device in the devices array
|
||||
const selectedDevice = devices.find(device => device.name === selectedDeviceName);
|
||||
const selectedDevice = devices.find(device => device.id === selectedDeviceId);
|
||||
|
||||
// Clear the existing options in the sensorSelector
|
||||
const sensorSelector = document.getElementById('sensorSelector');
|
||||
sensorSelector.innerHTML = '<option value="">-- Select a Sensor --</option>';
|
||||
// Clear the existing options in the metricSelector
|
||||
const metricSelector = document.getElementById('metricSelector');
|
||||
metricSelector.innerHTML = '<option value="">-- Select a Metric --</option>';
|
||||
|
||||
if (selectedDevice) {
|
||||
// Populate the sensor selector with the sensors for the selected device
|
||||
selectedDevice.sensors.forEach(sensor => {
|
||||
if (selectedDevice && selectedDevice.metrics) {
|
||||
// Populate the metric selector with the metrics for the selected device
|
||||
selectedDevice.metrics.forEach(metric => {
|
||||
const option = document.createElement('option');
|
||||
option.value = sensor.id; // Set the sensor ID as the value
|
||||
option.textContent = sensor.type; // Set the sensor type as the display text
|
||||
sensorSelector.appendChild(option);
|
||||
option.value = metric;
|
||||
option.textContent = metric.charAt(0).toUpperCase() + metric.slice(1);
|
||||
metricSelector.appendChild(option);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
function fetchDeviceData() {
|
||||
var device = document.getElementById('deviceSelector').value;
|
||||
var sensor = document.getElementById('sensorSelector').value; // Get selected sensor
|
||||
var deviceId = document.getElementById('deviceSelector').value;
|
||||
var metric = document.getElementById('metricSelector').value;
|
||||
var startDate = document.getElementById('startDate').value;
|
||||
var endDate = document.getElementById('endDate').value;
|
||||
|
||||
if (!device || !sensor) {
|
||||
alert("Please select both a device and a sensor.");
|
||||
if (!deviceId || !metric) {
|
||||
alert("Please select both a device and a metric.");
|
||||
return;
|
||||
}
|
||||
|
||||
fetch(`/fetch_device_data/?device=${device}&sensor=${sensor}&start_date=${startDate}&end_date=${endDate}`)
|
||||
fetch(`/fetch_device_data/?device_id=${deviceId}&metric=${metric}&start_date=${startDate}&end_date=${endDate}`)
|
||||
.then(response => {
|
||||
if (!response.ok) {
|
||||
throw new Error("Network response was not ok: " + response.statusText);
|
||||
@@ -212,9 +212,9 @@ function fetchDeviceData() {
|
||||
labels: data.times,
|
||||
datasets: [
|
||||
{
|
||||
label: `${sensor} (°C or %)`,
|
||||
label: metric.charAt(0).toUpperCase() + metric.slice(1),
|
||||
data: data.values,
|
||||
borderColor: sensor === 'Temperature' ? 'red' : 'blue',
|
||||
borderColor: metric === 'temperature' ? 'red' : (metric === 'humidity' ? 'blue' : 'green'),
|
||||
fill: false,
|
||||
}
|
||||
]
|
||||
|
||||
@@ -29,7 +29,7 @@
|
||||
</li>
|
||||
{% else %}
|
||||
<li class="nav-item">
|
||||
<a class="nav-link" href="{% url 'login' %}">Login</a>
|
||||
<a class="nav-link" href="/admin">Login</a>
|
||||
</li>
|
||||
{% endif %}
|
||||
</ul>
|
||||
|
||||
172
iotDashboard/templates/device_credentials.html
Normal file
172
iotDashboard/templates/device_credentials.html
Normal file
@@ -0,0 +1,172 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>Device Credentials - {{ device_name }}</title>
|
||||
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
|
||||
<style>
|
||||
.credential-box {
|
||||
background-color: #f8f9fa;
|
||||
border: 1px solid #dee2e6;
|
||||
border-radius: 5px;
|
||||
padding: 15px;
|
||||
margin-bottom: 20px;
|
||||
font-family: monospace;
|
||||
white-space: pre-wrap;
|
||||
word-wrap: break-word;
|
||||
max-height: 300px;
|
||||
overflow-y: auto;
|
||||
}
|
||||
.copy-btn {
|
||||
margin-top: 10px;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body class="bg-light">
|
||||
<nav class="navbar navbar-expand-lg navbar-light bg-light">
|
||||
<div class="container-fluid">
|
||||
<a class="navbar-brand" href="#">IoT Dashboard</a>
|
||||
<div class="collapse navbar-collapse">
|
||||
<ul class="navbar-nav me-auto mb-2 mb-lg-0">
|
||||
<li class="nav-item"><a class="nav-link" href="{% url 'index' %}">Chart</a></li>
|
||||
<li class="nav-item"><a class="nav-link" href="{% url 'device_list' %}">Devices</a></li>
|
||||
</ul>
|
||||
</div>
|
||||
</div>
|
||||
</nav>
|
||||
|
||||
<div class="container mt-5">
|
||||
<div class="alert alert-success" role="alert">
|
||||
<h4 class="alert-heading">✅ Device Registered Successfully!</h4>
|
||||
<p>Device <strong>{{ device_name }}</strong> has been registered.</p>
|
||||
</div>
|
||||
|
||||
<div class="alert alert-warning" role="alert">
|
||||
<strong>⚠️ Important:</strong> Save these credentials now! They will not be shown again.
|
||||
You'll need them to configure your device.
|
||||
</div>
|
||||
|
||||
<div class="card mb-4">
|
||||
<div class="card-header">
|
||||
<h5>Device Information</h5>
|
||||
</div>
|
||||
<div class="card-body">
|
||||
<table class="table">
|
||||
<tr>
|
||||
<th>Device ID:</th>
|
||||
<td><code>{{ response.device_id }}</code></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<th>Protocol:</th>
|
||||
<td><span class="badge bg-info">{{ response.protocol|upper }}</span></td>
|
||||
</tr>
|
||||
{% if response.certificate_id %}
|
||||
<tr>
|
||||
<th>Certificate ID:</th>
|
||||
<td><code>{{ response.certificate_id }}</code></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<th>Expires At:</th>
|
||||
<td>{{ response.expires_at|date:"Y-m-d H:i:s" }}</td>
|
||||
</tr>
|
||||
{% endif %}
|
||||
</table>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{% if response.protocol == 'mqtt' and response.certificate_pem %}
|
||||
<div class="card mb-4">
|
||||
<div class="card-header">
|
||||
<h5>CA Certificate</h5>
|
||||
</div>
|
||||
<div class="card-body">
|
||||
<div class="credential-box" id="ca-cert">{{ response.ca_certificate_pem }}</div>
|
||||
<button class="btn btn-sm btn-secondary copy-btn" onclick="copyToClipboard('ca-cert')">Copy CA Certificate</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="card mb-4">
|
||||
<div class="card-header">
|
||||
<h5>Device Certificate</h5>
|
||||
</div>
|
||||
<div class="card-body">
|
||||
<div class="credential-box" id="device-cert">{{ response.certificate_pem }}</div>
|
||||
<button class="btn btn-sm btn-secondary copy-btn" onclick="copyToClipboard('device-cert')">Copy Device Certificate</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="card mb-4">
|
||||
<div class="card-header">
|
||||
<h5>Private Key</h5>
|
||||
</div>
|
||||
<div class="card-body">
|
||||
<div class="credential-box" id="private-key">{{ response.private_key_pem }}</div>
|
||||
<button class="btn btn-sm btn-secondary copy-btn" onclick="copyToClipboard('private-key')">Copy Private Key</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="alert alert-info">
|
||||
<h6>MQTT Configuration:</h6>
|
||||
<ul>
|
||||
<li>Broker: <code>localhost:8883</code></li>
|
||||
<li>Topic: <code>devices/{{ response.device_id }}/<metric></code></li>
|
||||
<li>Example: <code>devices/{{ response.device_id }}/temperature</code></li>
|
||||
</ul>
|
||||
</div>
|
||||
{% endif %}
|
||||
|
||||
{% if response.protocol == 'http' and response.api_key %}
|
||||
<div class="card mb-4">
|
||||
<div class="card-header">
|
||||
<h5>API Key</h5>
|
||||
</div>
|
||||
<div class="card-body">
|
||||
<div class="credential-box" id="api-key">{{ response.api_key }}</div>
|
||||
<button class="btn btn-sm btn-secondary copy-btn" onclick="copyToClipboard('api-key')">Copy API Key</button>
|
||||
</div>
|
||||
</div>
|
||||
{% endif %}
|
||||
|
||||
{% if response.protocol == 'webhook' and response.webhook_secret %}
|
||||
<div class="card mb-4">
|
||||
<div class="card-header">
|
||||
<h5>Webhook Secret</h5>
|
||||
</div>
|
||||
<div class="card-body">
|
||||
<div class="credential-box" id="webhook-secret">{{ response.webhook_secret }}</div>
|
||||
<button class="btn btn-sm btn-secondary copy-btn" onclick="copyToClipboard('webhook-secret')">Copy Webhook Secret</button>
|
||||
</div>
|
||||
</div>
|
||||
{% endif %}
|
||||
|
||||
<div class="text-center mt-4">
|
||||
<a href="{% url 'device_list' %}" class="btn btn-primary">Go to Device List</a>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<script>
|
||||
function copyToClipboard(elementId) {
|
||||
const element = document.getElementById(elementId);
|
||||
const text = element.textContent;
|
||||
|
||||
navigator.clipboard.writeText(text).then(() => {
|
||||
// Change button text temporarily
|
||||
const btn = event.target;
|
||||
const originalText = btn.textContent;
|
||||
btn.textContent = '✓ Copied!';
|
||||
btn.classList.remove('btn-secondary');
|
||||
btn.classList.add('btn-success');
|
||||
|
||||
setTimeout(() => {
|
||||
btn.textContent = originalText;
|
||||
btn.classList.remove('btn-success');
|
||||
btn.classList.add('btn-secondary');
|
||||
}, 2000);
|
||||
}).catch(err => {
|
||||
alert('Failed to copy: ' + err);
|
||||
});
|
||||
}
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
101
iotDashboard/templates/device_detail.html
Normal file
101
iotDashboard/templates/device_detail.html
Normal file
@@ -0,0 +1,101 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>Device Details - {{ device.name }}</title>
|
||||
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
|
||||
</head>
|
||||
<body class="bg-light">
|
||||
<nav class="navbar navbar-expand-lg navbar-light bg-light">
|
||||
<div class="container-fluid">
|
||||
<a class="navbar-brand" href="#">IoT Dashboard</a>
|
||||
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav">
|
||||
<span class="navbar-toggler-icon"></span>
|
||||
</button>
|
||||
<div class="collapse navbar-collapse" id="navbarNav">
|
||||
<ul class="navbar-nav me-auto mb-2 mb-lg-0">
|
||||
<li class="nav-item"><a class="nav-link" href="{% url 'index' %}">Chart</a></li>
|
||||
<li class="nav-item"><a class="nav-link" href="{% url 'device_list' %}">Devices</a></li>
|
||||
</ul>
|
||||
</div>
|
||||
</div>
|
||||
</nav>
|
||||
|
||||
<div class="container mt-5">
|
||||
<h1 class="text-center mb-4">Device Details</h1>
|
||||
|
||||
{% if messages %}
|
||||
{% for message in messages %}
|
||||
<div class="alert alert-{{ message.tags }} alert-dismissible fade show" role="alert">
|
||||
{{ message }}
|
||||
<button type="button" class="btn-close" data-bs-dismiss="alert"></button>
|
||||
</div>
|
||||
{% endfor %}
|
||||
{% endif %}
|
||||
|
||||
<div class="card">
|
||||
<div class="card-header">
|
||||
<h3>{{ device.name }}</h3>
|
||||
</div>
|
||||
<div class="card-body">
|
||||
<table class="table">
|
||||
<tr>
|
||||
<th>Device ID:</th>
|
||||
<td><code>{{ device.id }}</code></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<th>Location:</th>
|
||||
<td>{{ device.location|default:"—" }}</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<th>Protocol:</th>
|
||||
<td><span class="badge bg-info">{{ device.protocol|upper }}</span></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<th>Created:</th>
|
||||
<td>{{ device.created_at|date:"Y-m-d H:i:s" }}</td>
|
||||
</tr>
|
||||
</table>
|
||||
|
||||
{% if device.protocol == 'mqtt' and certificate %}
|
||||
<h4 class="mt-4">Certificate Information</h4>
|
||||
<table class="table">
|
||||
<tr>
|
||||
<th>Certificate ID:</th>
|
||||
<td><code>{{ certificate.id }}</code></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<th>Issued At:</th>
|
||||
<td>{{ certificate.issued_at|date:"Y-m-d H:i:s" }}</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<th>Expires At:</th>
|
||||
<td>{{ certificate.expires_at|date:"Y-m-d H:i:s" }}</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<th>Status:</th>
|
||||
<td>
|
||||
{% if certificate.revoked_at %}
|
||||
<span class="badge bg-danger">Revoked</span>
|
||||
{% else %}
|
||||
<span class="badge bg-success">Active</span>
|
||||
{% endif %}
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
{% endif %}
|
||||
|
||||
<div class="mt-4">
|
||||
<a href="{% url 'device_list' %}" class="btn btn-secondary">Back to List</a>
|
||||
{% if device.protocol == 'mqtt' %}
|
||||
<a href="{% url 'renew_certificate' device.id %}" class="btn btn-warning">Renew Certificate</a>
|
||||
<a href="{% url 'revoke_certificate' device.id %}" class="btn btn-danger">Revoke Certificate</a>
|
||||
{% endif %}
|
||||
<a href="{% url 'delete_device' device.id %}" class="btn btn-danger">Delete Device</a>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
@@ -23,40 +23,42 @@
|
||||
</nav>
|
||||
|
||||
<div class="container mt-5">
|
||||
<h1 class="text-center mb-4">{% if form.instance.pk %}Edit{% else %}Add{% endif %} Device</h1>
|
||||
<h1 class="text-center mb-4">Add New Device</h1>
|
||||
|
||||
{% if messages %}
|
||||
{% for message in messages %}
|
||||
<div class="alert alert-{{ message.tags }} alert-dismissible fade show" role="alert">
|
||||
{{ message }}
|
||||
<button type="button" class="btn-close" data-bs-dismiss="alert"></button>
|
||||
</div>
|
||||
{% endfor %}
|
||||
{% endif %}
|
||||
|
||||
<form method="post">
|
||||
{% csrf_token %}
|
||||
<div class="mb-3">
|
||||
<!-- Display the form fields -->
|
||||
{{ form.name.label_tag }} {{ form.name }}<br>
|
||||
{{ form.ip.label_tag }} {{ form.ip }}<br>
|
||||
{{ form.protocol.label_tag }} {{ form.protocol }}<br>
|
||||
|
||||
<!-- If there are errors, display them -->
|
||||
{% if form.errors %}
|
||||
<div class="alert alert-danger">
|
||||
<ul>
|
||||
{% for field, errors in form.errors.items %}
|
||||
<li>{{ field }}: {{ errors|join:", " }}</li>
|
||||
{% endfor %}
|
||||
</ul>
|
||||
</div>
|
||||
{% endif %}
|
||||
<label for="name" class="form-label">Device Name *</label>
|
||||
<input type="text" class="form-control" id="name" name="name" value="{{ name }}" required>
|
||||
</div>
|
||||
|
||||
<!-- Submit and Cancel buttons -->
|
||||
<button type="submit" class="btn btn-success">Save</button>
|
||||
<div class="mb-3">
|
||||
<label for="location" class="form-label">Location</label>
|
||||
<input type="text" class="form-control" id="location" name="location" value="{{ location }}">
|
||||
</div>
|
||||
|
||||
<div class="mb-3">
|
||||
<label for="protocol" class="form-label">Protocol *</label>
|
||||
<select class="form-select" id="protocol" name="protocol" required>
|
||||
<option value="mqtt" {% if protocol == 'mqtt' %}selected{% endif %}>MQTT (with mTLS)</option>
|
||||
<option value="http" {% if protocol == 'http' %}selected{% endif %}>HTTP (coming soon)</option>
|
||||
<option value="webhook" {% if protocol == 'webhook' %}selected{% endif %}>Webhook (coming soon)</option>
|
||||
</select>
|
||||
<div class="form-text">MQTT devices will receive X.509 certificates for secure communication.</div>
|
||||
</div>
|
||||
|
||||
<button type="submit" class="btn btn-success">Register Device</button>
|
||||
<a href="{% url 'device_list' %}" class="btn btn-secondary">Cancel</a>
|
||||
</form>
|
||||
|
||||
<!-- Links for adding/editing sensors for this specific device -->
|
||||
<div class="mt-4">
|
||||
{% if form.instance.pk %}
|
||||
<a href="{% url 'add_sensor' form.instance.pk %}" class="btn btn-primary">Add Sensor</a>
|
||||
<a href="{% url 'sensor_list' form.instance.pk %}" class="btn btn-info">Edit Sensors</a>
|
||||
{% endif %}
|
||||
</div>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
|
||||
@@ -23,7 +23,7 @@
|
||||
{% if user.is_authenticated %}
|
||||
<li class="nav-item"><a class="nav-link" href="{% url 'logout' %}">Logout</a></li>
|
||||
{% else %}
|
||||
<li class="nav-item"><a class="nav-link" href="{% url 'login' %}">Login</a></li>
|
||||
<li class="nav-item"><a class="nav-link" href="/admin">Login</a></li>
|
||||
{% endif %}
|
||||
</ul>
|
||||
</div>
|
||||
@@ -33,41 +33,61 @@
|
||||
<!-- Device Management -->
|
||||
<div class="container mt-5">
|
||||
<h1 class="text-center mb-4">Manage Devices</h1>
|
||||
|
||||
{% if messages %}
|
||||
{% for message in messages %}
|
||||
<div class="alert alert-{{ message.tags }} alert-dismissible fade show" role="alert">
|
||||
{{ message }}
|
||||
<button type="button" class="btn-close" data-bs-dismiss="alert"></button>
|
||||
</div>
|
||||
{% endfor %}
|
||||
{% endif %}
|
||||
|
||||
<a href="{% url 'add_device' %}" class="btn btn-primary mb-3">Add Device</a>
|
||||
|
||||
<table class="table table-striped">
|
||||
<thead>
|
||||
<tr>
|
||||
<th>Name</th>
|
||||
<th>IP Address</th>
|
||||
<th>Location</th>
|
||||
<th>Protocol</th>
|
||||
<th>Sensor Types</th> <!-- Adjusted to Sensor Types -->
|
||||
<th>Certificate Status</th>
|
||||
<th>Certificate Expiry</th>
|
||||
<th>Actions</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{% for device in devices %}
|
||||
{% for device_data in devices %}
|
||||
<tr>
|
||||
<td>{{ device.name }}</td>
|
||||
<td>{{ device.ip }}</td>
|
||||
<td>{{ device.protocol }}</td>
|
||||
<!-- Collect and display sensor types -->
|
||||
<td>{{ device_data.device.name }}</td>
|
||||
<td>{{ device_data.device.location|default:"—" }}</td>
|
||||
<td><span class="badge bg-info">{{ device_data.device.protocol|upper }}</span></td>
|
||||
<td>
|
||||
{% for sensor in device.sensors.all %}
|
||||
{{ sensor.type.name }}{% if not forloop.last %}, {% endif %}
|
||||
{% empty %}
|
||||
No sensors
|
||||
{% endfor %}
|
||||
{% if device_data.device.protocol == 'mqtt' %}
|
||||
<span class="badge bg-success">{{ device_data.certificate_status }}</span>
|
||||
{% else %}
|
||||
<span class="badge bg-secondary">N/A</span>
|
||||
{% endif %}
|
||||
</td>
|
||||
<!-- You may further expand other sensor-related data -->
|
||||
<td>
|
||||
<a href="{% url 'edit_device' device.pk %}" class="btn btn-warning btn-sm">Edit</a>
|
||||
<a href="{% url 'delete_device' device.pk %}" class="btn btn-danger btn-sm">Delete</a>
|
||||
{% if device_data.active_certificate %}
|
||||
{{ device_data.active_certificate.expires_at|date:"Y-m-d H:i" }}
|
||||
{% else %}
|
||||
—
|
||||
{% endif %}
|
||||
</td>
|
||||
<td>
|
||||
<a href="{% url 'view_device' device_data.device.id %}" class="btn btn-info btn-sm">View</a>
|
||||
<a href="{% url 'delete_device' device_data.device.id %}" class="btn btn-danger btn-sm">Delete</a>
|
||||
{% if device_data.device.protocol == 'mqtt' %}
|
||||
<a href="{% url 'renew_certificate' device_data.device.id %}" class="btn btn-warning btn-sm">Renew Cert</a>
|
||||
<a href="{% url 'revoke_certificate' device_data.device.id %}" class="btn btn-secondary btn-sm">Revoke</a>
|
||||
{% endif %}
|
||||
</td>
|
||||
</tr>
|
||||
{% empty %}
|
||||
<tr>
|
||||
<td colspan="5" class="text-center">No devices found.</td>
|
||||
<td colspan="6" class="text-center">No devices found.</td>
|
||||
</tr>
|
||||
{% endfor %}
|
||||
</tbody>
|
||||
|
||||
@@ -1,48 +0,0 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>Confirm Delete Sensor</title>
|
||||
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
|
||||
</head>
|
||||
<body class="bg-light">
|
||||
<!-- Navbar -->
|
||||
<nav class="navbar navbar-expand-lg navbar-light bg-light">
|
||||
<div class="container-fluid">
|
||||
<a class="navbar-brand" href="#">IoT Dashboard</a>
|
||||
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
|
||||
<span class="navbar-toggler-icon"></span>
|
||||
</button>
|
||||
<div class="collapse navbar-collapse" id="navbarNav">
|
||||
<ul class="navbar-nav me-auto mb-2 mb-lg-0">
|
||||
<li class="nav-item"><a class="nav-link" href="{% url 'index' %}">Chart</a></li>
|
||||
<li class="nav-item"><a class="nav-link" href="{% url 'device_list' %}">Devices</a></li>
|
||||
</ul>
|
||||
<ul class="navbar-nav">
|
||||
{% if user.is_authenticated %}
|
||||
<li class="nav-item"><a class="nav-link" href="{% url 'logout' %}">Logout</a></li>
|
||||
{% else %}
|
||||
<li class="nav-item"><a class="nav-link" href="{% url 'login' %}">Login</a></li>
|
||||
{% endif %}
|
||||
</ul>
|
||||
</div>
|
||||
</div>
|
||||
</nav>
|
||||
|
||||
<!-- Delete Confirmation -->
|
||||
<div class="container mt-5">
|
||||
<h1 class="text-center mb-4">Confirm Delete Sensor</h1>
|
||||
<div class="alert alert-warning">
|
||||
<strong>Warning!</strong> Are you sure you want to delete the sensor "{{ sensor.type.name }}"? This action cannot be undone.
|
||||
</div>
|
||||
<form method="POST">
|
||||
{% csrf_token %}
|
||||
<div class="text-center">
|
||||
<button type="submit" class="btn btn-danger">Delete</button>
|
||||
<a href="{% url 'sensor_list' sensor.device.pk %}" class="btn btn-secondary">Cancel</a>
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
@@ -1,64 +0,0 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>Manage Sensors for {{ device.name }}</title>
|
||||
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
|
||||
</head>
|
||||
<body class="bg-light">
|
||||
<!-- Navbar -->
|
||||
<nav class="navbar navbar-expand-lg navbar-light bg-light">
|
||||
<div class="container-fluid">
|
||||
<a class="navbar-brand" href="#">IoT Dashboard</a>
|
||||
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
|
||||
<span class="navbar-toggler-icon"></span>
|
||||
</button>
|
||||
<div class="collapse navbar-collapse" id="navbarNav">
|
||||
<ul class="navbar-nav me-auto mb-2 mb-lg-0">
|
||||
<li class="nav-item"><a class="nav-link" href="{% url 'index' %}">Chart</a></li>
|
||||
<li class="nav-item"><a class="nav-link" href="{% url 'device_list' %}">Devices</a></li>
|
||||
</ul>
|
||||
<ul class="navbar-nav">
|
||||
{% if user.is_authenticated %}
|
||||
<li class="nav-item"><a class="nav-link" href="{% url 'logout' %}">Logout</a></li>
|
||||
{% else %}
|
||||
<li class="nav-item"><a class="nav-link" href="{% url 'login' %}">Login</a></li>
|
||||
{% endif %}
|
||||
</ul>
|
||||
</div>
|
||||
</div>
|
||||
</nav>
|
||||
|
||||
<!-- Sensor Management -->
|
||||
<div class="container mt-5">
|
||||
<h1>Sensors for {{ device.name }}</h1>
|
||||
<table class="table">
|
||||
<thead>
|
||||
<tr>
|
||||
<th>Sensor Type</th>
|
||||
<th>Enabled</th>
|
||||
<th>Actions</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{% for sensor in sensors %}
|
||||
<tr>
|
||||
<td>{{ sensor.type.name }}</td>
|
||||
<td>{{ sensor.enabled }}</td>
|
||||
<td>
|
||||
<a href="{% url 'edit_sensor' sensor.pk %}" class="btn btn-warning btn-sm">Edit</a>
|
||||
<a href="{% url 'delete_sensor' sensor.pk %}" class="btn btn-danger btn-sm">Delete</a>
|
||||
</td>
|
||||
</tr>
|
||||
{% empty %}
|
||||
<tr>
|
||||
<td colspan="3">No sensors found for this device.</td>
|
||||
</tr>
|
||||
{% endfor %}
|
||||
</tbody>
|
||||
</table>
|
||||
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
@@ -21,18 +21,25 @@ from iotDashboard import views
|
||||
|
||||
urlpatterns = [
|
||||
path("admin/", admin.site.urls),
|
||||
path("devices_api/", views.devices_api),
|
||||
|
||||
# Main dashboard
|
||||
path("", views.chart, name="index"),
|
||||
path("fetch_device_data/", views.fetch_device_data, name="fetch_device_data"),
|
||||
path("chart/", views.chart, name="chart"),
|
||||
|
||||
# Device management
|
||||
path("devices/", views.device_list, name="device_list"),
|
||||
path("devices/add/", views.add_device, name="add_device"),
|
||||
path("devices/edit/<int:pk>/", views.edit_device, name="edit_device"),
|
||||
path("devices/delete/<int:pk>/", views.delete_device, name="delete_device"),
|
||||
path("devices/<str:device_id>/", views.view_device, name="view_device"),
|
||||
path("devices/<str:device_id>/delete/", views.delete_device, name="delete_device"),
|
||||
|
||||
# Certificate management (MQTT devices only)
|
||||
path("devices/<str:device_id>/certificate/revoke/", views.revoke_certificate, name="revoke_certificate"),
|
||||
path("devices/<str:device_id>/certificate/renew/", views.renew_certificate, name="renew_certificate"),
|
||||
|
||||
# Telemetry data API
|
||||
path("fetch_device_data/", views.fetch_device_data, name="fetch_device_data"),
|
||||
|
||||
# Legacy/utility endpoints
|
||||
path("devices_api/", views.devices_api, name="devices_api"),
|
||||
path("logout/", views.logout_view, name="logout"),
|
||||
path("sensors/<int:device_id>/", views.sensor_list, name="sensor_list"),
|
||||
path("sensor/add/", views.add_sensor_with_type, name="add_sensor_with_type"),
|
||||
# path('devices/<int:device_id>/sensors/', views.sensor_list, name='sensor_list'),
|
||||
path("device/<int:device_id>/add_sensor/", views.add_sensor, name="add_sensor"),
|
||||
path("sensor/edit/<int:pk>/", views.edit_sensor, name="edit_sensor"),
|
||||
path("sensor/delete/<int:pk>/", views.delete_sensor, name="delete_sensor"),
|
||||
]
|
||||
|
||||
@@ -1,235 +1,263 @@
|
||||
import redis
|
||||
import json
|
||||
from django.db import connections
|
||||
from django.http import JsonResponse, HttpResponse
|
||||
from django.shortcuts import render, redirect, get_object_or_404
|
||||
from django.http import JsonResponse
|
||||
from django.shortcuts import render, redirect
|
||||
from django.contrib import messages
|
||||
|
||||
from .forms import DeviceForm, SensorWithTypeForm
|
||||
from iotDashboard.models import Device, Sensor
|
||||
from iotDashboard.models import Device, Telemetry
|
||||
from iotDashboard.device_manager_client import DeviceManagerClient, DeviceManagerAPIError
|
||||
|
||||
redis_client = redis.StrictRedis(host="10.10.0.1", port=6379, db=0)
|
||||
device_manager = DeviceManagerClient()
|
||||
|
||||
|
||||
def fetch_gpt_data():
|
||||
return (
|
||||
redis_client.get("gpt")
|
||||
.decode("utf-8")
|
||||
.strip('b"')
|
||||
.replace('\\"', '"')
|
||||
.replace("\\n", "")
|
||||
.replace("\\", "")
|
||||
.replace("\\u00b0", "°")
|
||||
)
|
||||
# def index(request):
|
||||
# """Redirect to chart page."""
|
||||
# if request.user.is_authenticated:
|
||||
# return redirect("/chart/")
|
||||
# return HttpResponse("NOT AUTHENTICATED!!!")
|
||||
|
||||
|
||||
def chart(request):
|
||||
# Fetch devices and their related sensors
|
||||
devices = Device.objects.prefetch_related(
|
||||
"sensors__type"
|
||||
).all() # Prefetch related sensors and their types
|
||||
|
||||
# Create a list of devices and associated sensors
|
||||
devices_json = [
|
||||
{
|
||||
"name": device.name,
|
||||
"sensors": [
|
||||
{"id": sensor.id, "type": sensor.type.name}
|
||||
for sensor in device.sensors.all()
|
||||
],
|
||||
}
|
||||
for device in devices
|
||||
]
|
||||
|
||||
"""Main dashboard showing telemetry charts."""
|
||||
try:
|
||||
gpt_data = fetch_gpt_data()
|
||||
gpt = json.loads(gpt_data)
|
||||
except (redis.RedisError, json.JSONDecodeError) as e:
|
||||
gpt = {"summary": "Error fetching data", "recommendations": {}}
|
||||
print(f"Error fetching or parsing GPT data: {e}")
|
||||
devices = Device.objects.all()
|
||||
|
||||
context = {
|
||||
"devices_json": json.dumps(devices_json), # Convert to a JSON string
|
||||
"gpt": gpt,
|
||||
}
|
||||
devices_data = []
|
||||
for device in devices:
|
||||
# Get unique metrics for this device from telemetry
|
||||
metrics = (
|
||||
Telemetry.objects
|
||||
.filter(device_id=device.id)
|
||||
.values_list('metric', flat=True)
|
||||
.distinct()
|
||||
)
|
||||
|
||||
return render(request, "chart.html", context)
|
||||
devices_data.append({
|
||||
"id": device.id,
|
||||
"name": device.name,
|
||||
"protocol": device.protocol,
|
||||
"metrics": list(metrics),
|
||||
})
|
||||
|
||||
context = {
|
||||
"devices_json": json.dumps(devices_data),
|
||||
}
|
||||
|
||||
return render(request, "chart.html", context)
|
||||
|
||||
except Exception as e:
|
||||
messages.error(request, f"Error loading dashboard: {str(e)}")
|
||||
return render(request, "chart.html", {"devices_json": "[]"})
|
||||
|
||||
|
||||
def fetch_device_data(request):
|
||||
device_name = request.GET.get("device", "Livingroom")
|
||||
sensor_name = request.GET.get("sensor") # This will be the actual sensor name
|
||||
"""Fetch telemetry data for chart visualization."""
|
||||
from datetime import datetime, timedelta
|
||||
from django.utils import timezone
|
||||
|
||||
device_id = request.GET.get("device_id")
|
||||
metric = request.GET.get("metric")
|
||||
start_date = request.GET.get("start_date")
|
||||
end_date = request.GET.get("end_date")
|
||||
|
||||
# Log the parameters to ensure they are correct
|
||||
sensor_name = Sensor.objects.get(id=sensor_name).type.name
|
||||
if not device_id:
|
||||
return JsonResponse({"error": "device_id is required"}, status=400)
|
||||
|
||||
print("Device Name:", device_name)
|
||||
print("Sensor Name:", sensor_name) # Log sensor name
|
||||
print("Start Date:", start_date)
|
||||
print("End Date:", end_date)
|
||||
try:
|
||||
# Build query using Django ORM
|
||||
queryset = Telemetry.objects.filter(device_id=device_id)
|
||||
|
||||
# Get the specific device by name
|
||||
device = get_object_or_404(Device, name=device_name)
|
||||
# Filter by metric if provided
|
||||
if metric:
|
||||
queryset = queryset.filter(metric=metric)
|
||||
|
||||
# Initialize lists to store times and values
|
||||
times = []
|
||||
values = []
|
||||
# Parse and filter by date range (default to last 24 hours)
|
||||
if start_date:
|
||||
start_dt = datetime.fromisoformat(start_date.replace('Z', '+00:00'))
|
||||
queryset = queryset.filter(time__gte=start_dt)
|
||||
else:
|
||||
# Default: last 24 hours
|
||||
queryset = queryset.filter(time__gte=timezone.now() - timedelta(hours=24))
|
||||
|
||||
# Prepare SQL query and parameters for the device
|
||||
query = """
|
||||
SELECT time, metric, value
|
||||
FROM sensor_readings
|
||||
WHERE device_name = %s
|
||||
"""
|
||||
params = [device.name]
|
||||
if end_date:
|
||||
end_dt = datetime.fromisoformat(end_date.replace('Z', '+00:00'))
|
||||
queryset = queryset.filter(time__lte=end_dt)
|
||||
|
||||
# If a specific sensor is specified, filter by that sensor name (converted to lowercase)
|
||||
if sensor_name:
|
||||
query += " AND metric = LOWER(%s)" # Convert to lowercase for comparison
|
||||
params.append(sensor_name.lower()) # Convert sensor name to lowercase
|
||||
# Order by time and get values
|
||||
results = queryset.order_by('time').values_list('time', 'value')
|
||||
|
||||
# Add time filtering to the query
|
||||
if start_date:
|
||||
query += " AND time >= %s::timestamptz"
|
||||
params.append(start_date)
|
||||
times = []
|
||||
values = []
|
||||
for time, value in results:
|
||||
times.append(time.strftime("%Y-%m-%d %H:%M:%S"))
|
||||
values.append(float(value))
|
||||
|
||||
if end_date:
|
||||
query += " AND time <= %s::timestamptz"
|
||||
params.append(end_date)
|
||||
return JsonResponse({"times": times, "values": values})
|
||||
|
||||
# Log the final query and params
|
||||
print("Final Query:", query)
|
||||
print("Params Before Execution:", params)
|
||||
|
||||
# Fetch data from the database
|
||||
with connections["data"].cursor() as cursor:
|
||||
cursor.execute(query, params)
|
||||
rows = cursor.fetchall()
|
||||
|
||||
# Log the number of rows returned
|
||||
print("Number of Rows Returned:", len(rows))
|
||||
|
||||
# Process the results and extract times and values
|
||||
for row in rows:
|
||||
time, metric, value = row
|
||||
formatted_time = time.strftime("%Y-%m-%d %H:%M:%S")
|
||||
|
||||
times.append(formatted_time)
|
||||
values.append(value)
|
||||
|
||||
# If no data is found, return empty arrays
|
||||
if not times and not values:
|
||||
print("No data found for the specified device and sensor.")
|
||||
return JsonResponse({"times": [], "values": []})
|
||||
|
||||
# Return the response in the expected format
|
||||
return JsonResponse({"times": times, "values": values})
|
||||
|
||||
|
||||
def index(request):
|
||||
if request.user.is_authenticated:
|
||||
return redirect("/chart/")
|
||||
return HttpResponse("NOT AUTHENTICATED!!!")
|
||||
except Exception as e:
|
||||
return JsonResponse({"error": str(e)}, status=500)
|
||||
|
||||
|
||||
def device_list(request):
|
||||
devices = Device.objects.all()
|
||||
return render(request, "device_list.html", {"devices": devices})
|
||||
"""List all devices with their certificate status."""
|
||||
try:
|
||||
devices = Device.objects.all()
|
||||
|
||||
# Enrich devices with certificate information
|
||||
devices_with_certs = []
|
||||
for device in devices:
|
||||
device_data = {
|
||||
"device": device,
|
||||
"certificate_status": device.certificate_status if device.protocol == "mqtt" else "N/A",
|
||||
"active_certificate": device.active_certificate if device.protocol == "mqtt" else None,
|
||||
}
|
||||
devices_with_certs.append(device_data)
|
||||
|
||||
return render(request, "device_list.html", {"devices": devices_with_certs})
|
||||
|
||||
except Exception as e:
|
||||
messages.error(request, f"Error loading devices: {str(e)}")
|
||||
return render(request, "device_list.html", {"devices": []})
|
||||
|
||||
|
||||
def add_device(request):
|
||||
"""Register a new device via device_manager API."""
|
||||
if request.method == "POST":
|
||||
form = DeviceForm(request.POST)
|
||||
if form.is_valid():
|
||||
form.save()
|
||||
return redirect("device_list")
|
||||
else:
|
||||
form = DeviceForm()
|
||||
return render(request, "device_form.html", {"form": form})
|
||||
name = request.POST.get("name")
|
||||
location = request.POST.get("location")
|
||||
protocol = request.POST.get("protocol", "mqtt")
|
||||
|
||||
if not name:
|
||||
messages.error(request, "Device name is required")
|
||||
return render(request, "device_form.html")
|
||||
|
||||
try:
|
||||
response = device_manager.register_device(
|
||||
name=name,
|
||||
location=location,
|
||||
protocol=protocol
|
||||
)
|
||||
|
||||
# Show credentials page (one-time view)
|
||||
return render(request, "device_credentials.html", {
|
||||
"device_name": name,
|
||||
"response": response,
|
||||
})
|
||||
|
||||
except DeviceManagerAPIError as e:
|
||||
messages.error(request, f"Failed to register device: {e.message}")
|
||||
return render(request, "device_form.html", {
|
||||
"name": name,
|
||||
"location": location,
|
||||
"protocol": protocol,
|
||||
})
|
||||
|
||||
return render(request, "device_form.html")
|
||||
|
||||
|
||||
def edit_device(request, pk):
|
||||
device = get_object_or_404(Device, pk=pk)
|
||||
if request.method == "POST":
|
||||
form = DeviceForm(request.POST, instance=device)
|
||||
if form.is_valid():
|
||||
form.save()
|
||||
return redirect("device_list")
|
||||
else:
|
||||
form = DeviceForm(instance=device)
|
||||
return render(request, "device_form.html", {"form": form})
|
||||
def view_device(request, device_id):
|
||||
"""View device details and certificate information."""
|
||||
try:
|
||||
device = Device.objects.get(id=device_id)
|
||||
|
||||
# Get certificate if MQTT device
|
||||
certificate = None
|
||||
if device.protocol == "mqtt":
|
||||
certificate = device.active_certificate
|
||||
|
||||
def delete_device(request, pk):
|
||||
device = get_object_or_404(Device, pk=pk)
|
||||
if request.method == "POST":
|
||||
device.delete()
|
||||
context = {
|
||||
"device": device,
|
||||
"certificate": certificate,
|
||||
}
|
||||
|
||||
return render(request, "device_detail.html", context)
|
||||
|
||||
except Device.DoesNotExist:
|
||||
messages.error(request, f"Device {device_id} not found")
|
||||
return redirect("device_list")
|
||||
except Exception as e:
|
||||
messages.error(request, f"Error loading device: {str(e)}")
|
||||
return redirect("device_list")
|
||||
return render(request, "device_confirm_delete.html", {"device": device})
|
||||
|
||||
|
||||
def add_sensor_with_type(request):
|
||||
if request.method == "POST":
|
||||
form = SensorWithTypeForm(request.POST)
|
||||
if form.is_valid():
|
||||
form.save() # This will save both Sensor and SensorType as needed
|
||||
return redirect("device_list") # Adjust this to your specific URL name
|
||||
else:
|
||||
form = SensorWithTypeForm()
|
||||
def delete_device(request, device_id):
|
||||
"""Delete a device."""
|
||||
try:
|
||||
device = Device.objects.get(id=device_id)
|
||||
|
||||
context = {"form": form}
|
||||
return render(request, "sensor_form.html", context)
|
||||
if request.method == "POST":
|
||||
device_name = device.name
|
||||
device.delete()
|
||||
messages.success(request, f"Device '{device_name}' deleted successfully")
|
||||
return redirect("device_list")
|
||||
|
||||
return render(request, "device_confirm_delete.html", {"device": device})
|
||||
|
||||
except Device.DoesNotExist:
|
||||
messages.error(request, f"Device {device_id} not found")
|
||||
return redirect("device_list")
|
||||
|
||||
|
||||
def revoke_certificate(request, device_id):
|
||||
"""Revoke a device's certificate via device_manager API."""
|
||||
try:
|
||||
device = Device.objects.get(id=device_id)
|
||||
|
||||
if device.protocol != "mqtt":
|
||||
messages.error(request, "Only MQTT devices have certificates to revoke")
|
||||
return redirect("device_list")
|
||||
|
||||
if request.method == "POST":
|
||||
try:
|
||||
device_manager.revoke_certificate(device_id)
|
||||
messages.success(request, f"Certificate for device '{device.name}' revoked successfully")
|
||||
except DeviceManagerAPIError as e:
|
||||
messages.error(request, f"Failed to revoke certificate: {e.message}")
|
||||
|
||||
return redirect("device_list")
|
||||
|
||||
return render(request, "certificate_revoke_confirm.html", {"device": device})
|
||||
|
||||
except Device.DoesNotExist:
|
||||
messages.error(request, f"Device {device_id} not found")
|
||||
return redirect("device_list")
|
||||
|
||||
|
||||
def renew_certificate(request, device_id):
|
||||
"""Renew a device's certificate via device_manager API."""
|
||||
try:
|
||||
device = Device.objects.get(id=device_id)
|
||||
|
||||
if device.protocol != "mqtt":
|
||||
messages.error(request, "Only MQTT devices have certificates to renew")
|
||||
return redirect("device_list")
|
||||
|
||||
if request.method == "POST":
|
||||
try:
|
||||
response = device_manager.renew_certificate(device_id)
|
||||
|
||||
# Show the new credentials (one-time view)
|
||||
return render(request, "device_credentials.html", {
|
||||
"device_name": device.name,
|
||||
"response": response,
|
||||
"is_renewal": True,
|
||||
})
|
||||
except DeviceManagerAPIError as e:
|
||||
messages.error(request, f"Failed to renew certificate: {e.message}")
|
||||
return redirect("device_list")
|
||||
|
||||
return render(request, "certificate_renew_confirm.html", {"device": device})
|
||||
|
||||
except Device.DoesNotExist:
|
||||
messages.error(request, f"Device {device_id} not found")
|
||||
return redirect("device_list")
|
||||
|
||||
|
||||
def logout_view(request):
|
||||
"""Redirect to admin logout."""
|
||||
return redirect("/admin")
|
||||
|
||||
|
||||
def devices_api(request):
|
||||
devices = list(Device.objects.all().values("name", "sensors__type__name"))
|
||||
"""JSON API endpoint for devices."""
|
||||
devices = list(Device.objects.all().values("id", "name", "protocol", "location"))
|
||||
return JsonResponse(devices, safe=False)
|
||||
|
||||
|
||||
def sensor_list(request, device_id):
|
||||
device = get_object_or_404(Device, id=device_id)
|
||||
sensors = device.sensors.all() # Get sensors for this specific device
|
||||
return render(request, "sensor_list.html", {"device": device, "sensors": sensors})
|
||||
|
||||
|
||||
def edit_sensor(request, pk):
|
||||
sensor = get_object_or_404(Sensor, pk=pk)
|
||||
if request.method == "POST":
|
||||
form = SensorWithTypeForm(request.POST, instance=sensor)
|
||||
if form.is_valid():
|
||||
form.save()
|
||||
return redirect("sensor_list", device_id=sensor.device.pk)
|
||||
else:
|
||||
form = SensorWithTypeForm(instance=sensor)
|
||||
return render(request, "sensor_form.html", {"form": form})
|
||||
|
||||
|
||||
def delete_sensor(request, pk):
|
||||
sensor = get_object_or_404(Sensor, pk=pk)
|
||||
if request.method == "POST":
|
||||
device_id = sensor.device.pk
|
||||
sensor.delete()
|
||||
return redirect("sensor_list", device_id=device_id)
|
||||
return render(request, "sensor_confirm_delete.html", {"sensor": sensor})
|
||||
|
||||
|
||||
def add_sensor(request, device_id):
|
||||
device = get_object_or_404(Device, pk=device_id)
|
||||
if request.method == "POST":
|
||||
form = SensorWithTypeForm(request.POST)
|
||||
if form.is_valid():
|
||||
sensor = form.save(commit=False)
|
||||
sensor.device = device # Associate the sensor with the device
|
||||
sensor.save()
|
||||
return redirect(
|
||||
"device_list"
|
||||
) # Redirect to device list or appropriate view
|
||||
else:
|
||||
form = SensorWithTypeForm()
|
||||
|
||||
return render(request, "sensor_form.html", {"form": form, "device": device})
|
||||
|
||||
@@ -23,4 +23,6 @@ dev = [
|
||||
[tool.uv.workspace]
|
||||
members = [
|
||||
"db_migrations",
|
||||
"iotDashboard",
|
||||
"services/gpt_service",
|
||||
]
|
||||
|
||||
@@ -158,14 +158,20 @@ async def revoke_device_certificate(device_id: str):
|
||||
"""
|
||||
try:
|
||||
with get_db_context() as db:
|
||||
# Get the active (non-revoked) certificate for the device
|
||||
device_cert = (
|
||||
db.query(DeviceCertificate).filter(DeviceCertificate.device_id == device_id).first()
|
||||
db.query(DeviceCertificate)
|
||||
.filter(
|
||||
DeviceCertificate.device_id == device_id,
|
||||
DeviceCertificate.revoked_at.is_(None)
|
||||
)
|
||||
.first()
|
||||
)
|
||||
if not device_cert:
|
||||
raise HTTPException(status_code=404, detail="Device certificate not found")
|
||||
|
||||
if device_cert.revoked_at:
|
||||
raise HTTPException(status_code=400, detail="Certificate already revoked")
|
||||
raise HTTPException(
|
||||
status_code=404,
|
||||
detail="No active certificate found for this device"
|
||||
)
|
||||
|
||||
cert_manager.revoke_certificate(device_cert.certificate_pem)
|
||||
|
||||
|
||||
6
services/gpt_service/.env.sample
Normal file
6
services/gpt_service/.env.sample
Normal file
@@ -0,0 +1,6 @@
|
||||
API_KEY =
|
||||
PROVIDER_NAME = os.getenv("PROVIDER_NAME", "openai")
|
||||
MODEL_NAME = os.getenv("MODEL_NAME", "gpt-4.1")
|
||||
# HOST_URL = os.getenv("MODEL_URL")
|
||||
LOG_LEVEL=INFO
|
||||
|
||||
0
services/gpt_service/README.md
Normal file
0
services/gpt_service/README.md
Normal file
13
services/gpt_service/config.py
Normal file
13
services/gpt_service/config.py
Normal file
@@ -0,0 +1,13 @@
|
||||
import os
|
||||
from dotenv import load_dotenv
|
||||
|
||||
load_dotenv()
|
||||
|
||||
class Config:
|
||||
"""Configuration settings for the GPT Service."""
|
||||
|
||||
API_KEY = os.getenv("API_KEY")
|
||||
PROVIDER_NAME = os.getenv("PROVIDER_NAME", "openai")
|
||||
MODEL_NAME = os.getenv("MODEL_NAME", "gpt-4")
|
||||
HOST_URL = os.getenv("HOST_URL")
|
||||
LOG_LEVEL = os.getenv("LOG_LEVEL", "INFO")
|
||||
42
services/gpt_service/gpt_service.py
Normal file
42
services/gpt_service/gpt_service.py
Normal file
@@ -0,0 +1,42 @@
|
||||
import openai
|
||||
|
||||
from config import API_KEY, MODEL_NAME, PROVIDER_NAME, HOST_URL, LOG_LEVEL
|
||||
import logging
|
||||
|
||||
class GPTService:
|
||||
def __init__(self):
|
||||
self.api_key = API_KEY
|
||||
self.model_name = MODEL_NAME
|
||||
self.provider_name = PROVIDER_NAME
|
||||
self.host_url = HOST_URL
|
||||
|
||||
logging.basicConfig(level=getattr(logging, LOG_LEVEL.upper(), logging.INFO))
|
||||
self.logger = logging.getLogger(__name__)
|
||||
|
||||
if self.provider_name == "openai":
|
||||
openai.api_key = self.api_key
|
||||
if self.host_url:
|
||||
openai.api_base = self.host_url
|
||||
self.logger.info(f"Initialized OpenAI GPTService with model {self.model_name}")
|
||||
else:
|
||||
self.logger.error(f"Unsupported provider: {self.provider_name}")
|
||||
raise ValueError(f"Unsupported provider: {self.provider_name}")
|
||||
|
||||
def analyze_metrics(self, metrics: dict) -> str:
|
||||
"""Analyze given metrics using GPT model and return insights."""
|
||||
prompt = f"Analyze the following metrics and provide insights:\n{metrics}"
|
||||
try:
|
||||
response = openai.Completion.create(
|
||||
engine=self.model_name,
|
||||
prompt=prompt,
|
||||
max_tokens=150,
|
||||
n=1,
|
||||
stop=None,
|
||||
temperature=0.7,
|
||||
)
|
||||
insights = response.choices[0].text.strip()
|
||||
self.logger.info("Successfully obtained insights from GPT model")
|
||||
return insights
|
||||
except Exception as e:
|
||||
self.logger.error(f"Error during GPT analysis: {e}")
|
||||
raise
|
||||
6
services/gpt_service/main.py
Normal file
6
services/gpt_service/main.py
Normal file
@@ -0,0 +1,6 @@
|
||||
def main():
|
||||
print("Hello from gpt-service!")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
7
services/gpt_service/pyproject.toml
Normal file
7
services/gpt_service/pyproject.toml
Normal file
@@ -0,0 +1,7 @@
|
||||
[project]
|
||||
name = "gpt-service"
|
||||
version = "0.1.0"
|
||||
description = "Add your description here"
|
||||
readme = "README.md"
|
||||
requires-python = ">=3.13"
|
||||
dependencies = []
|
||||
6
uv.lock
generated
6
uv.lock
generated
@@ -4,6 +4,7 @@ requires-python = ">=3.13"
|
||||
|
||||
[manifest]
|
||||
members = [
|
||||
"dashboard",
|
||||
"db-migrations",
|
||||
"iotdashboard",
|
||||
]
|
||||
@@ -112,6 +113,11 @@ wheels = [
|
||||
{ url = "https://files.pythonhosted.org/packages/d1/d6/3965ed04c63042e047cb6a3e6ed1a63a35087b6a609aa3a15ed8ac56c221/colorama-0.4.6-py2.py3-none-any.whl", hash = "sha256:4f1d9991f5acc0ca119f9d443620b77f9d6b33703e51011c16baf57afb285fc6", size = 25335, upload-time = "2022-10-25T02:36:20.889Z" },
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "dashboard"
|
||||
version = "0.1.0"
|
||||
source = { virtual = "iotDashboard" }
|
||||
|
||||
[[package]]
|
||||
name = "db-migrations"
|
||||
version = "0.1.0"
|
||||
|
||||
Reference in New Issue
Block a user