From 7cac7697952fd53da618a0eb69b42436a1aecc15 Mon Sep 17 00:00:00 2001 From: PaulVua Date: Fri, 7 Feb 2025 15:04:11 +0100 Subject: [PATCH] update --- SARA/check_running.py | 19 +- html/launcher.php | 6 + html/logs.html | 36 +++- html/saraR4.html | 413 +++++++++++++++++++++++------------------- 4 files changed, 279 insertions(+), 195 deletions(-) diff --git a/SARA/check_running.py b/SARA/check_running.py index fcda7e0..b41a83c 100644 --- a/SARA/check_running.py +++ b/SARA/check_running.py @@ -3,10 +3,10 @@ Check if the main loop is running /usr/bin/python3 /var/www/nebuleair_pro_4g/tests/check_running.py ''' import psutil -import subprocess +import json def is_script_running(script_name): - """Check if a given Python script is running.""" + """Check if a given Python script is currently running.""" for process in psutil.process_iter(['pid', 'cmdline']): if process.info['cmdline'] and script_name in " ".join(process.info['cmdline']): return True # Script is running @@ -14,7 +14,14 @@ def is_script_running(script_name): script_to_check = "/var/www/nebuleair_pro_4g/loop/SARA_send_data_v2.py" -if is_script_running(script_to_check): - print(f"{script_to_check} is still running.❌❌❌") -else: - print(f"{script_to_check} is NOT running.✅✅✅") +# Determine script status +is_running = is_script_running(script_to_check) + +# Create JSON response +response = { + "message": "The script is still running.❌❌❌" if is_running else "The script is NOT running.✅✅✅", + "running": is_running +} + +# Print JSON output +print(json.dumps(response, indent=4)) # Pretty print for readability diff --git a/html/launcher.php b/html/launcher.php index e698ab6..7d07375 100755 --- a/html/launcher.php +++ b/html/launcher.php @@ -44,6 +44,12 @@ if ($type == "update_config") { echo "Config updated!"; } +if ($type == "getModem_busy") { + $command = '/usr/bin/python3 /var/www/nebuleair_pro_4g/SARA/check_running.py'; + $output = shell_exec($command); + echo $output; +} + if ($type == "RTC_time") { $time = shell_exec("date '+%d/%m/%Y %H:%M:%S'"); echo $time; diff --git a/html/logs.html b/html/logs.html index d73fdcf..3840845 100755 --- a/html/logs.html +++ b/html/logs.html @@ -57,7 +57,7 @@
Master logs - +
@@ -166,6 +166,9 @@ window.onload = function() { + getModem_busy_status(); + setInterval(getModem_busy_status, 2000); + fetch('../config.json') // Replace 'deviceID.txt' with 'config.json' .then(response => response.json()) // Parse response as JSON .then(data => { @@ -221,6 +224,37 @@ function clear_loopLogs(){ } }); } + +function getModem_busy_status() { + console.log("Getting modem busy status"); + + const script_is_running = document.getElementById("script_running"); + + $.ajax({ + url: 'launcher.php?type=getModem_busy', + dataType: 'json', // Expecting JSON response + method: 'GET', + success: function(response) { + console.log(response); + + if (response.running) { + // Script is running → Show the Bootstrap spinner + script_is_running.innerHTML = ` +
+ Modem is busy... +
+ `; + } else { + // Script is NOT running → Show a success message (no spinner) + script_is_running.innerHTML = ``; + } + }, + error: function(xhr, status, error) { + console.error('AJAX request failed:', status, error); + script_is_running.innerHTML = `Error checking status ⚠️`; + } + }); +} diff --git a/html/saraR4.html b/html/saraR4.html index f358748..168e774 100755 --- a/html/saraR4.html +++ b/html/saraR4.html @@ -51,7 +51,7 @@

Modem 4G

Votre capteur est équipé d'un modem 4G et d'une carte SIM afin d'envoyer les mesures sur internet.

-

Verification utilisation du Modem:

+

Status Loading... @@ -391,211 +391,248 @@ function getData_saraR4(port, command, timeout){ }); } - function connectNetwork_saraR4(port, networkID, timeout){ - console.log(" Connect to network (port "+port+" and network id "+networkID+"):"); - $("#loading_"+port+"_AT_COPS_Connect").show(); - $.ajax({ - url: 'launcher.php?type=sara_connectNetwork&port='+port+'&networkID='+encodeURIComponent(networkID)+'&timeout='+timeout, - dataType:'text', - //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); - $("#loading_"+port+"_AT_COPS_Connect").hide(); - // Replace newline characters with
tags - const formattedResponse = response.replace(/\n/g, "
"); - $("#response_"+port+"_AT_COPS_Connect").html(formattedResponse); +function connectNetwork_saraR4(port, networkID, timeout){ + console.log(" Connect to network (port "+port+" and network id "+networkID+"):"); + $("#loading_"+port+"_AT_COPS_Connect").show(); + $.ajax({ + url: 'launcher.php?type=sara_connectNetwork&port='+port+'&networkID='+encodeURIComponent(networkID)+'&timeout='+timeout, + dataType:'text', + //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); + $("#loading_"+port+"_AT_COPS_Connect").hide(); + // Replace newline characters with
tags + const formattedResponse = response.replace(/\n/g, "
"); + $("#response_"+port+"_AT_COPS_Connect").html(formattedResponse); - }, - error: function(xhr, status, error) { - console.error('AJAX request failed:', status, error); - } - }); - } + }, + error: function(xhr, status, error) { + console.error('AJAX request failed:', status, error); + } + }); + } - function mqtt_getConfig_saraR4(port, timeout){ - console.log("GET MQTT config (port "+port+"):"); - $("#loading_mqtt_getConfig").show(); - $.ajax({ - url: 'launcher.php?type=sara_getMQTT_config&port='+port+'&timeout='+timeout, - dataType:'text', - //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); - $("#loading_mqtt_getConfig").hide(); - // Replace newline characters with
tags - const formattedResponse = response.replace(/\n/g, "
"); - $("#response_mqtt_getConfig").html(formattedResponse); - }, - error: function(xhr, status, error) { - console.error('AJAX request failed:', status, error); - } - }); - } +function mqtt_getConfig_saraR4(port, timeout){ + console.log("GET MQTT config (port "+port+"):"); + $("#loading_mqtt_getConfig").show(); + $.ajax({ + url: 'launcher.php?type=sara_getMQTT_config&port='+port+'&timeout='+timeout, + dataType:'text', + //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); + $("#loading_mqtt_getConfig").hide(); + // Replace newline characters with
tags + const formattedResponse = response.replace(/\n/g, "
"); + $("#response_mqtt_getConfig").html(formattedResponse); + }, + error: function(xhr, status, error) { + console.error('AJAX request failed:', status, error); + } + }); + } - function mqtt_login_logout(port, login_logout, timeout){ - console.log("GET MQTT login / logout (port "+port+"):"); - $("#loading_mqtt_login_logout").show(); - $.ajax({ - url: 'launcher.php?type=sara_getMQTT_login_logout&port='+port+'&login_logout='+login_logout+'&timeout='+timeout, - dataType:'text', - //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); - $("#loading_mqtt_login_logout").hide(); - const formattedResponse = response.replace(/\n/g, "
"); - $("#response_mqtt_login_logout").html(formattedResponse); +function mqtt_login_logout(port, login_logout, timeout){ + console.log("GET MQTT login / logout (port "+port+"):"); + $("#loading_mqtt_login_logout").show(); + $.ajax({ + url: 'launcher.php?type=sara_getMQTT_login_logout&port='+port+'&login_logout='+login_logout+'&timeout='+timeout, + dataType:'text', + //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); + $("#loading_mqtt_login_logout").hide(); + const formattedResponse = response.replace(/\n/g, "
"); + $("#response_mqtt_login_logout").html(formattedResponse); - const regex = /^\+UMQTTC:\s*(\d+),(\d+)/m; // Match "+UMQTTC:", followed by two numbers separated by a comma - const match = response.match(regex); + const regex = /^\+UMQTTC:\s*(\d+),(\d+)/m; // Match "+UMQTTC:", followed by two numbers separated by a comma + const match = response.match(regex); - if (match) { - const firstNumber = match[1]; // The first number after ":" - const secondNumber = match[2]; // The second number after the "," - if (firstNumber == 0) { - console.log("MQTT LOGOUT:"); - $("#response_mqtt_login_logout").append("

logout

"); - } - if (firstNumber == 1) { - console.log("MQTT LOGIN:"); - $("#response_mqtt_login_logout").append("

login

"); - } - if (secondNumber == 0) { - console.log("ERROR"); - $("#response_mqtt_login_logout").append("

error

"); - } - if (secondNumber == 1) { - console.log("SUCCESS"); - $("#response_mqtt_login_logout").append("

success

"); - } + if (match) { + const firstNumber = match[1]; // The first number after ":" + const secondNumber = match[2]; // The second number after the "," + if (firstNumber == 0) { + console.log("MQTT LOGOUT:"); + $("#response_mqtt_login_logout").append("

logout

"); + } + if (firstNumber == 1) { + console.log("MQTT LOGIN:"); + $("#response_mqtt_login_logout").append("

login

"); + } + if (secondNumber == 0) { + console.log("ERROR"); + $("#response_mqtt_login_logout").append("

error

"); + } + if (secondNumber == 1) { + console.log("SUCCESS"); + $("#response_mqtt_login_logout").append("

success

"); + } - } else { - console.log("No matching line found"); -} - - }, - error: function(xhr, status, error) { - console.error('AJAX request failed:', status, error); - } - }); - } + } else { + console.log("No matching line found"); + } + + }, + error: function(xhr, status, error) { + console.error('AJAX request failed:', status, error); + } + }); + } - function mqtt_publish(port, message, timeout){ - console.log(" MQTT publish (port "+port+"):"); - $("#loading_mqtt_publish").show(); - $.ajax({ - url: 'launcher.php?type=sara_MQTT_publish&port='+port+'&timeout='+timeout+'&message='+message, - dataType: 'text', - //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); - $("#loading_mqtt_publish").hide(); - // Replace newline characters with
tags - const formattedResponse = response.replace(/\n/g, "
"); - $("#response_mqtt_publish").html(formattedResponse); - }, - error: function(xhr, status, error) { - console.error('AJAX request failed:', status, error); - } - }); - } +function mqtt_publish(port, message, timeout){ + console.log(" MQTT publish (port "+port+"):"); + $("#loading_mqtt_publish").show(); + $.ajax({ + url: 'launcher.php?type=sara_MQTT_publish&port='+port+'&timeout='+timeout+'&message='+message, + dataType: 'text', + //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); + $("#loading_mqtt_publish").hide(); + // Replace newline characters with
tags + const formattedResponse = response.replace(/\n/g, "
"); + $("#response_mqtt_publish").html(formattedResponse); + }, + error: function(xhr, status, error) { + console.error('AJAX request failed:', status, error); + } + }); + } - function setURL_saraR4(port, url){ - console.log("Set URL for HTTP (port "+port+" and URL "+url+"):"); - $("#loading_"+port+"_setURL").show(); - $.ajax({ - url: 'launcher.php?type=sara_setURL&port='+port+'&url='+encodeURIComponent(url), - dataType: 'text', - //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); - $("#loading_"+port+"_setURL").hide(); - // Replace newline characters with
tags - const formattedResponse = response.replace(/\n/g, "
"); - $("#response_"+port+"_setURL").html(formattedResponse); - }, - error: function(xhr, status, error) { - console.error('AJAX request failed:', status, error); - } - }); - } +function setURL_saraR4(port, url){ + console.log("Set URL for HTTP (port "+port+" and URL "+url+"):"); + $("#loading_"+port+"_setURL").show(); + $.ajax({ + url: 'launcher.php?type=sara_setURL&port='+port+'&url='+encodeURIComponent(url), + dataType: 'text', + //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); + $("#loading_"+port+"_setURL").hide(); + // Replace newline characters with
tags + const formattedResponse = response.replace(/\n/g, "
"); + $("#response_"+port+"_setURL").html(formattedResponse); + }, + error: function(xhr, status, error) { + console.error('AJAX request failed:', status, error); + } + }); + } - function writeMessage_saraR4(port, message, type){ - console.log(type +" message to SARA R4 memory (port "+port+" and message "+message+"):"); - $("#loading_"+port+"_message_write").show(); - $.ajax({ - url: 'launcher.php?type=sara_writeMessage&port='+port+'&message='+encodeURIComponent(message)+'&type2='+type, - dataType: 'text', - //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); - $("#loading_"+port+"_message_write").hide(); - // Replace newline characters with
tags - const formattedResponse = response.replace(/\n/g, "
"); - $("#response_"+port+"_message_write").html(formattedResponse); - }, - error: function(xhr, status, error) { - console.error('AJAX request failed:', status, error); - } - }); - } +function writeMessage_saraR4(port, message, type){ + console.log(type +" message to SARA R4 memory (port "+port+" and message "+message+"):"); + $("#loading_"+port+"_message_write").show(); + $.ajax({ + url: 'launcher.php?type=sara_writeMessage&port='+port+'&message='+encodeURIComponent(message)+'&type2='+type, + dataType: 'text', + //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); + $("#loading_"+port+"_message_write").hide(); + // Replace newline characters with
tags + const formattedResponse = response.replace(/\n/g, "
"); + $("#response_"+port+"_message_write").html(formattedResponse); + }, + error: function(xhr, status, error) { + console.error('AJAX request failed:', status, error); + } + }); + } - function sendMessage_saraR4(port, endpoint){ - - console.log("Send message from SaraR4 (port "+port+" and endpoint "+endpoint+"):"); +function sendMessage_saraR4(port, endpoint){ + + console.log("Send message from SaraR4 (port "+port+" and endpoint "+endpoint+"):"); - $("#loading_"+port+"_message_send").show(); + $("#loading_"+port+"_message_send").show(); - $.ajax({ - url: 'launcher.php?type=sara_sendMessage&port='+port+'&endpoint='+encodeURIComponent(endpoint), - dataType: 'text', - //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); - $("#loading_"+port+"_message_send").hide(); - // Replace newline characters with
tags - const formattedResponse = response.replace(/\n/g, "
"); - $("#response_"+port+"_message_send").html(formattedResponse); - }, - error: function(xhr, status, error) { - console.error('AJAX request failed:', status, error); - } - }); - } - - function connectAPN_saraR4(port, APN_address, timeout){ - - console.log(" Set APN (port "+port+" and adress "+APN_address+"):"); - - $("#loading_"+port+"_APN").show(); - $.ajax({ - url: 'launcher.php?type=sara_APN&port='+port+'&APN_address='+encodeURIComponent(APN_address)+'&timeout='+timeout, - //dataType: 'json', // Specify that you expect a JSON response - dataType: 'text', - method: 'GET', // Use GET or POST depending on your needs - success: function(response) { - console.log(response); - $("#loading_"+port+"_APN").hide(); - // Replace newline characters with
tags - const formattedResponse = response.replace(/\n/g, "
"); - $("#response_"+port+"_APN").html(formattedResponse); + url: 'launcher.php?type=sara_sendMessage&port='+port+'&endpoint='+encodeURIComponent(endpoint), + dataType: 'text', + //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); + $("#loading_"+port+"_message_send").hide(); + // Replace newline characters with
tags + const formattedResponse = response.replace(/\n/g, "
"); + $("#response_"+port+"_message_send").html(formattedResponse); + }, + error: function(xhr, status, error) { + console.error('AJAX request failed:', status, error); + } + }); + } - }, - error: function(xhr, status, error) { - console.error('AJAX request failed:', status, error); - } - }); +function connectAPN_saraR4(port, APN_address, timeout){ + + console.log(" Set APN (port "+port+" and adress "+APN_address+"):"); + + $("#loading_"+port+"_APN").show(); + + $.ajax({ + url: 'launcher.php?type=sara_APN&port='+port+'&APN_address='+encodeURIComponent(APN_address)+'&timeout='+timeout, + //dataType: 'json', // Specify that you expect a JSON response + dataType: 'text', + method: 'GET', // Use GET or POST depending on your needs + success: function(response) { + console.log(response); + $("#loading_"+port+"_APN").hide(); + // Replace newline characters with
tags + const formattedResponse = response.replace(/\n/g, "
"); + $("#response_"+port+"_APN").html(formattedResponse); + + }, + error: function(xhr, status, error) { + console.error('AJAX request failed:', status, error); + } + }); +} + +function getModem_busy_status() { + console.log("Getting modem busy status"); + + const SARA_busy_message = document.getElementById("modem_status_message"); + + $.ajax({ + url: 'launcher.php?type=getModem_busy', + dataType: 'json', // Expecting JSON response + method: 'GET', + success: function(response) { + console.log(response); + + if (response.running) { + // Script is running → Red button, "Modem is busy" + + SARA_busy_message.innerHTML= ` ` + } else { + // Script is NOT running → Green button, "Modem is available" + + SARA_busy_message.innerHTML= ` ` } + }, + error: function(xhr, status, error) { + console.error('AJAX request failed:', status, error); + SARA_busy_status.textContent = "Error checking status"; + SARA_busy_status.className = "btn text-bg-warning"; // Yellow button for errors + } + }); +} window.onload = function() { + getModem_busy_status(); + setInterval(getModem_busy_status, 1000); + fetch('../config.json') // Replace 'deviceID.txt' with 'config.json' .then(response => response.json()) // Parse response as JSON .then(data => {