From 49be391eb3ae071dbda57049841d7af9d9684a15 Mon Sep 17 00:00:00 2001 From: PaulVua Date: Wed, 5 Feb 2025 15:09:15 +0100 Subject: [PATCH] database --- .gitignore | 2 +- config.json.dist | 4 + html/database.html | 175 +++++++++++++++++++++++++++++++++++++++++ html/index.html | 70 +++++++++++++++-- html/launcher.php | 55 ++++++++++++- html/sidebar.html | 7 ++ logs/master.log | 0 logs/master_errors.log | 0 sqlite/create_db.py | 2 +- 9 files changed, 306 insertions(+), 9 deletions(-) create mode 100644 html/database.html delete mode 100644 logs/master.log delete mode 100644 logs/master_errors.log diff --git a/.gitignore b/.gitignore index cfd2a16..6041bda 100755 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,4 @@ -logs/app.log +logs/*.log logs/loop.log deviceID.txt loop/loop.log diff --git a/config.json.dist b/config.json.dist index b1277c2..6b32a88 100755 --- a/config.json.dist +++ b/config.json.dist @@ -2,9 +2,13 @@ "loop_activation": true, "loop_log": true, "boot_log": true, + "NPM/get_data.py": true, + "tests/script2.py": false, + "tests/script3.py": true, "deviceID": "XXXX", "deviceName": "NebuleAir-proXXX", "SaraR4_baudrate": 115200, + "NPM_solo_port": "/dev/ttyAMA5", "NextPM_ports": [ "ttyAMA5" ], diff --git a/html/database.html b/html/database.html new file mode 100644 index 0000000..eecdc60 --- /dev/null +++ b/html/database.html @@ -0,0 +1,175 @@ + + + + + + NebuleAir + + + + + + + + +
+
+
NebuleAir
+ +
+ +
+ +
+
+ + +
+

Base de données

+

Le capteur enregistre en local les données de mesures. Vous pouvez ici les consulter et les télécharger.

+ +
+ +
+
+
+
Consulter Base de donnée
+

General information.

+ + + +
+
+
+
+ +
+
+
+
Télécharger les données
+

Scan des réseaux WIFI disponibles.

+ + + +
+ +
+
+
+ + + +
+ +
+
+
+ + + + + + + + + + + + diff --git a/html/index.html b/html/index.html index 56012d9..fe1aa80 100755 --- a/html/index.html +++ b/html/index.html @@ -64,6 +64,7 @@

Memory usage (total size Mb)

+

Database size:

@@ -73,10 +74,7 @@
Mesures PM
-

Particules Fines

- - - +
@@ -152,6 +150,34 @@ window.onload = function() { console.error('AJAX request failed:', status, error); } }); + + //get database size + $.ajax({ + url: 'launcher.php?type=database_size', + dataType: 'json', // Specify that you expect a JSON response + method: 'GET', // Use GET or POST depending on your needs + success: function(response) { + console.log(response); + + if (response.size_megabytes !== undefined) { + // Extract and format the size in MB + const databaseSizeMB = response.size_megabytes + " MB"; + + // Update the HTML element with the database size + const databaseSizeElement = document.getElementById("database_size"); + databaseSizeElement.textContent = databaseSizeMB; + + console.log("Database size:", databaseSizeMB); + } else if (response.error) { + // Handle errors from the PHP response + console.error("Error from server:", response.error); + } + }, + error: function(xhr, status, error) { + console.error('AJAX request failed:', status, error); + } + }); + //get disk free space $.ajax({ @@ -289,7 +315,41 @@ window.onload = function() { }, options: { responsive: true, - maintainAspectRatio: false + maintainAspectRatio: true, + plugins: { + legend: { + position: 'top' + } + }, + scales: { + x: { + title: { + display: true, + text: 'Time' + }, + ticks: { + autoSkip: true, + maxTicksLimit: 5, + callback: function(value, index) { + // Access the correct label from the `labels` array + const label = labels[index]; // Use the original `labels` array + if (label && typeof label === 'string' && label.includes(' ')) { + return label.split(' ')[1].slice(0, 5); // Extract "HH:MM" + } + return value; // Fallback for invalid labels + } + } + + + }, + y: { + title: { + display: true, + text: 'Values (µg/m³)' + } + } + } + } }); } else { diff --git a/html/launcher.php b/html/launcher.php index de6988c..2352be6 100755 --- a/html/launcher.php +++ b/html/launcher.php @@ -13,8 +13,8 @@ if ($type == "get_npm_sqlite_data") { $db = new PDO("sqlite:$database_path"); $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); - // Fetch the last 10 records - $stmt = $db->query("SELECT timestamp, PM1, PM25, PM10 FROM data ORDER BY timestamp DESC LIMIT 10"); + // Fetch the last 30 records + $stmt = $db->query("SELECT timestamp, PM1, PM25, PM10 FROM data ORDER BY timestamp DESC LIMIT 30"); $data = $stmt->fetchAll(PDO::FETCH_ASSOC); echo json_encode($data); @@ -92,6 +92,57 @@ if ($type == "clear_loopLogs") { echo $output; } +if ($type == "database_size") { + +// Path to the SQLite database file +$databasePath = '/var/www/nebuleair_pro_4g/sqlite/sensors.db'; + +// Check if the file exists +if (file_exists($databasePath)) { + try { + // Connect to the SQLite database + $db = new PDO("sqlite:$databasePath"); + + // Get the file size in bytes + $fileSizeBytes = filesize($databasePath); + + // Convert the file size to human-readable formats + $fileSizeKilobytes = $fileSizeBytes / 1024; // KB + $fileSizeMegabytes = $fileSizeKilobytes / 1024; // MB + + // Query the number of records in the `data` table + $query = "SELECT COUNT(*) AS total_records FROM data"; + $result = $db->query($query); + $recordCount = $result ? $result->fetch(PDO::FETCH_ASSOC)['total_records'] : 0; + + // Prepare the JSON response + $data = [ + 'path' => $databasePath, + 'size_bytes' => $fileSizeBytes, + 'size_kilobytes' => round($fileSizeKilobytes, 2), + 'size_megabytes' => round($fileSizeMegabytes, 2), + 'data_table_records' => $recordCount + ]; + + // Output the JSON response + echo json_encode($data, JSON_PRETTY_PRINT); + } catch (PDOException $e) { + // Handle database connection errors + echo json_encode([ + 'error' => 'Database query failed: ' . $e->getMessage() + ]); + } +} else { + // Handle error if the file doesn't exist + echo json_encode([ + 'error' => 'Database file not found', + 'path' => $databasePath + ]); +} + + +} + if ($type == "linux_disk") { $command = 'df -h /'; $output = shell_exec($command); diff --git a/html/sidebar.html b/html/sidebar.html index f94d94e..fe150bf 100755 --- a/html/sidebar.html +++ b/html/sidebar.html @@ -13,6 +13,13 @@ Capteurs + + + + + + DataBase + diff --git a/logs/master.log b/logs/master.log deleted file mode 100644 index e69de29..0000000 diff --git a/logs/master_errors.log b/logs/master_errors.log deleted file mode 100644 index e69de29..0000000 diff --git a/sqlite/create_db.py b/sqlite/create_db.py index 8ea1856..b0bbe25 100755 --- a/sqlite/create_db.py +++ b/sqlite/create_db.py @@ -11,7 +11,7 @@ Script to create a sqlite database import sqlite3 -# Connect to (or create) the database +# Connect to (or create if not existent) the database conn = sqlite3.connect("/var/www/nebuleair_pro_4g/sqlite/sensors.db") cursor = conn.cursor()