Paho MQTT Version slection support + JS/CSS fixes #580 🩹

This commit is contained in:
Jokob-sk
2024-03-05 09:34:13 +11:00
parent 5559194617
commit 8e85abfda4
9 changed files with 163 additions and 81 deletions

View File

@@ -1055,6 +1055,11 @@ input[readonly] {
}
.pia-top-left-logo
{
height:50px;
}
/* -----------------------------------------------------------------------------
Donations
----------------------------------------------------------------------------- */

View File

@@ -757,18 +757,7 @@ function getGuid() {
// UI
// -----------------------------------------------------------------------------
// -----------------------------------------------------------------------------
// Generate work-in-progress icons
function workInProgress() {
if($(".work-in-progress").html().trim() == "")
{
$(".work-in-progress").append(`
<a href="https://github.com/jokob-sk/Pi.Alert/issues" target="_blank">
<b class="pointer" title="${getString("Gen_Work_In_Progress")}">🦺</b>
</a>
`)
}
}
// -----------------------------------------------------------------------------
// Loading Spinner overlay
@@ -876,7 +865,6 @@ function executeOnce() {
cacheSettings();
cacheStrings();
initDeviceListAll_JSON();
workInProgress();
// Set the flag in sessionStorage to indicate that the code has been executed and save time when last time the page for initialized
sessionStorage.setItem(sessionStorageKey, "true");

View File

@@ -111,7 +111,7 @@ if ($ENABLED_DARKMODE === True) {
<!-- ----------------------------------------------------------------------- -->
<!-- Layout Boxed Yellow -->
<body class="hold-transition <?php echo $pia_skin_selected;?> sidebar-mini" <?php echo $BACKGROUND_IMAGE_PATCH;?> onLoad="show_pia_servertime();" >
<body class="hold-transition fixed <?php echo $pia_skin_selected;?> sidebar-mini" <?php echo $BACKGROUND_IMAGE_PATCH;?> onLoad="show_pia_servertime();" >
<!-- Site wrapper -->
<div class="wrapper">
@@ -122,9 +122,14 @@ if ($ENABLED_DARKMODE === True) {
<!-- Logo -->
<a href="devices.php" class="logo">
<!-- mini logo for sidebar mini 50x50 pixels -->
<span class="logo-mini">P<b>a</b></span>
<span class="logo-mini">
<img src="img/pialertLogoWhite.png" class="pia-top-left-logo" alt="Pi.Alert Logo"/>
</span>
<!-- logo for regular state and mobile devices -->
<span class="logo-lg">Pi<b>.Alert</b></span>
<span class="logo-lg">Pi<b>.Alert</b>
</span>
</a>
<!-- ----------------------------------------------------------------------- -->
@@ -183,7 +188,7 @@ if ($ENABLED_DARKMODE === True) {
<img src="img/pialertLogoWhite.png" class="img-circle" alt="Pi.Alert Logo" style="border-color:transparent; height: 50px; width: 50px; margin-top:15px;">
<p style="float: right; width: 200px">
<?= lang('About_Title');?>
<small><?= lang('About_Design');?> Raspberry Pi</small>
<small><?= lang('About_Design');?> Docker</small>
</p>
</li>
@@ -208,12 +213,6 @@ if ($ENABLED_DARKMODE === True) {
<!-- sidebar: style can be found in sidebar.less -->
<section class="sidebar">
<!-- Sidebar user panel (optional) -->
<div class="user-panel"> <a href="." class="logo">
<img src="img/pialertLogoGray80.png" class="img-responsive" alt="Pi.Alert Logo"/>
</a>
</div>
<!-- search form (Optional) -->
<!-- DELETED -->
@@ -248,9 +247,38 @@ if ($ENABLED_DARKMODE === True) {
<div class="info-icon-nav myhidden" id="version" data-build-time="<?php echo file_get_contents( "buildtimestamp.txt");?>">🆕</div>
<a href="maintenance.php"><i class="fa fa-wrench "></i> <span><?= lang('Navigation_Maintenance');?></span></a>
</li>
<li class=" <?php if (in_array (basename($_SERVER['SCRIPT_NAME']), array('settings.php') ) ){ echo 'active'; } ?>">
<a href="settings.php"><i class="fa fa-cog"></i> <span><?= lang('Navigation_Settings');?></span></a>
<!-- <li class="treeview menu-open" style="height: auto;"> -->
<li class=" treeview <?php if (in_array (basename($_SERVER['SCRIPT_NAME']), array('settings.php') ) ){ echo 'active menu-open'; } ?>">
<a href="#">
<i class="fa fa-cog"></i> <span><?= lang('Navigation_Settings');?></span>
<span class="pull-right-container">
<i class="fa fa-angle-left pull-right"></i>
</span>
</a>
<ul class="treeview-menu" style="display: <?php if (in_array (basename($_SERVER['SCRIPT_NAME']), array('settings.php') ) ){ echo 'block'; } else {echo 'none';} ?>;">
<li>
<a href="settings.php"> <?= lang("settings_enabled");?> </a>
</li>
<li>
<a href="settings.php#core_content_header"> <?= lang("settings_core_label");?> </a>
</li>
<li>
<a href="settings.php#system_content_header"> <?= lang("settings_system_label");?> </a>
</li>
<li>
<a href="settings.php#device_scanner_content_header"> <?= lang("settings_device_scanners_label");?> </a>
</li>
<li>
<a href="settings.php#other_content_header"> <?= lang("settings_other_scanners_label");?> </a>
</li>
<li>
<a href="settings.php#publisher_content_header"> <?= lang("settings_publishers_label");?> </a>
</li>
</ul>
</li>
<li class=" <?php if (in_array (basename($_SERVER['SCRIPT_NAME']), array('workflows.php') ) ){ echo 'active'; } ?>">
<div class="info-icon-nav work-in-progress"> </div>
<a href="workflows.php"><i class="fa fa-shuffle"></i> <span><?= lang('Navigation_Workflows');?></span></a>
@@ -274,6 +302,19 @@ if ($ENABLED_DARKMODE === True) {
<script src="js/pialert_common.js"></script>
<script defer>
// Generate work-in-progress icons
function workInProgress() {
if($(".work-in-progress").html().trim() == "")
{
$(".work-in-progress").append(`
<a href="https://github.com/jokob-sk/Pi.Alert/issues" target="_blank">
<b class="pointer" title="${getString("Gen_Work_In_Progress")}">🦺</b>
</a>
`)
}
}
//--------------------------------------------------------------
//--------------------------------------------------------------
@@ -295,5 +336,6 @@ if ($ENABLED_DARKMODE === True) {
// Update server state in the header
updateState()
workInProgress()
</script>

View File

@@ -5,7 +5,7 @@
"API_icon": "<i class=\"fa fa-arrow-down-up-across-line\"></i>",
"About_Design": "Designed for:",
"About_Exit": "Sign out",
"About_Title": "Open Source Network Guard",
"About_Title": "Network security scanner & notification framework",
"AppEvents_DateTimeCreated": "Logged",
"AppEvents_Extra": "Extra",
"AppEvents_GUID": "Application Event GUID",

0
front/php/templates/language/es_es.json Normal file → Executable file
View File

View File

@@ -464,13 +464,28 @@
}],
"description": [{
"language_code": "en_us",
"string" : "Quality of service setting for MQTT message sending. <code>0</code> - Low quality to <code>2</code> - High quality. The higher the quality the longer the delay."
"string" : "Quality of service setting for MQTT message sending. The higher the quality the longer the delay. <br/> <code>0</code> - Low quality to <code>2</code> - High quality."
},
{
"language_code": "es_es",
"string" : "Configuración de calidad de servicio para el envío de mensajes MQTT. <code>0</code>: baja calidad a <code>2</code>: alta calidad. Cuanto mayor sea la calidad, mayor será el retraso."
}]
},
{
"function": "VERSION",
"type": "integer.select",
"default_value": 1,
"options": [ 1, 2 ],
"localized": ["name", "description"],
"name" : [{
"language_code": "en_us",
"string" : "Version"
}],
"description": [{
"language_code": "en_us",
"string" : "Paho MQTT API version. Depends on the MQTT <a href=\"https://eclipse.dev/paho/files/paho.mqtt.python/html/index.html#callbacks\" target=\"_blank\">version supported by the MQTT broker</a>. Usually set to <code>1</code>."
}]
},
{
"function": "DELAY_SEC",
"type": "integer",

View File

@@ -39,7 +39,6 @@ plugin_objects = Plugin_Objects(RESULT_FILE)
md5_hash = hashlib.md5()
pluginName = 'MQTT'
module_name = pluginName
# globals
@@ -53,7 +52,7 @@ def main():
# Check if basic config settings supplied
if check_config() == False:
mylog('none', [f'[{pluginName}] ⚠ ERROR: Publisher notification gateway not set up correctly. Check your pialert.conf {pluginName}_* variables.'])
mylog('verbose', [f'[{pluginName}] ⚠ ERROR: Publisher notification gateway not set up correctly. Check your pialert.conf {pluginName}_* variables.'])
return
# Create a database connection
@@ -72,7 +71,7 @@ def main():
#-------------------------------------------------------------------------------
def check_config():
if get_setting_value('MQTT_BROKER') == '' or get_setting_value('MQTT_PORT') == '' or get_setting_value('MQTT_USER') == '' or get_setting_value('MQTT_PASSWORD') == '':
mylog('none', ['[Check Config] ⚠ ERROR: MQTT service not set up correctly. Check your pialert.conf MQTT_* variables.'])
mylog('verbose', ['[Check Config] ⚠ ERROR: MQTT service not set up correctly. Check your pialert.conf MQTT_* variables.'])
return False
else:
return True
@@ -135,23 +134,38 @@ class sensor_config:
def publish_mqtt(mqtt_client, topic, message):
status = 1
message = json.dumps(message).replace("'",'"')
qos = get_setting_value('MQTT_QOS')
mylog('verbose', [f"[{pluginName}] Sending MQTT topic: {topic}"])
mylog('verbose', [f"[{pluginName}] Sending MQTT message: {message}"])
# mylog('verbose', [f"[{pluginName}] get_setting_value('MQTT_QOS'): {qos}"])
if mqtt_connected_to_broker == False:
mylog('verbose', [f"[{pluginName}] ⚠ ERROR: Not connected to broker, aborting."])
return False
while status != 0:
# mylog('verbose', [f"[{pluginName}] mqtt_client.publish "])
# mylog('verbose', [f"[{pluginName}] mqtt_client.is_connected(): {mqtt_client.is_connected()} "])
result = mqtt_client.publish(
topic=topic,
payload=message,
qos=get_setting_value('MQTT_QOS'),
qos=qos,
retain=True,
)
status = result[0]
# mylog('verbose', [f"[{pluginName}] status: {status}"])
# mylog('verbose', [f"[{pluginName}] result: {result}"])
if status != 0:
mylog('minimal', [f"[{pluginName}] Waiting to reconnect to MQTT broker"])
mylog('verbose', [f"[{pluginName}] Waiting to reconnect to MQTT broker"])
time.sleep(0.1)
return True
@@ -179,7 +193,7 @@ def create_sensor(mqtt_client, deviceId, deviceName, sensorType, sensorName, ico
# save if new
if new_sensor_config.isNew:
mylog('minimal', [f"[{pluginName}] Publishing sensor number {len(mqtt_sensors)}"])
mylog('verbose', [f"[{pluginName}] Publishing sensor number {len(mqtt_sensors)}"])
publish_sensor(mqtt_client, new_sensor_config)
@@ -190,19 +204,19 @@ def publish_sensor(mqtt_client, sensorConfig):
global mqtt_sensors
message = '{ \
"name":"'+sensorConfig.sensorName+'", \
"state_topic":"system-sensors/'+sensorConfig.sensorType+'/'+sensorConfig.deviceId+'/state", \
"value_template":"{{value_json.'+sensorConfig.sensorName+'}}", \
"unique_id":"'+sensorConfig.deviceId+'_sensor_'+sensorConfig.sensorName+'", \
"device": \
{ \
"identifiers": ["'+sensorConfig.deviceId+'_sensor"], \
"manufacturer": "PiAlert", \
"name":"'+sensorConfig.deviceName+'" \
}, \
"icon":"mdi:'+sensorConfig.icon+'" \
}'
message = {
"name" : sensorConfig.sensorName,
"state_topic" : "system-sensors/"+sensorConfig.sensorType+'/'+sensorConfig.deviceId+"/state",
"value_template" : "{{value_json."+sensorConfig.sensorName+"}}",
"unique_id" : sensorConfig.deviceId+'_sensor_'+sensorConfig.sensorName,
"device":
{
"identifiers" : [sensorConfig.deviceId+"_sensor"],
"manufacturer" : "PiAlert",
"name" : sensorConfig.deviceName
},
"icon":"mdi:'+sensorConfig.icon+'"
}
topic='homeassistant/'+sensorConfig.sensorType+'/'+sensorConfig.deviceId+'/'+sensorConfig.sensorName+'/config'
@@ -232,13 +246,18 @@ def mqtt_create_client():
mylog('verbose', [f"[{pluginName}] Connected to broker"])
mqtt_connected_to_broker = True # Signal connection
else:
mylog('none', [f"[{pluginName}] Connection failed, reason_code: {reason_code}"])
mylog('verbose', [f"[{pluginName}] Connection failed, reason_code: {reason_code}"])
mqtt_connected_to_broker = False
global mqtt_client
mqtt_client = mqtt.Client(mqtt.CallbackAPIVersion.VERSION2)
if get_setting_value('MQTT_VERSION') == 1:
mqtt_client = mqtt.Client(mqtt.CallbackAPIVersion.VERSION1)
else:
mqtt_client = mqtt.Client(mqtt.CallbackAPIVersion.VERSION2)
mqtt_client.username_pw_set(get_setting_value('MQTT_USER'), get_setting_value('MQTT_PASSWORD'))
mqtt_client.on_connect = on_connect
mqtt_client.on_disconnect = on_disconnect
@@ -266,19 +285,16 @@ def mqtt_start(db):
# Get the data
row = get_device_stats(db)
columns = ["online","down","all","archived","new","unknown"]
payload = ""
# Update the values
for column in columns:
payload += '"'+column+'": ' + str(row[column]) +','
# Publish (wrap into {} and remove last ',' from above)
publish_mqtt(mqtt_client, "system-sensors/sensor/pialert/state",
'{ \
'+ payload[:-1] +'\
}'
{
"online": row[0],
"down": row[1],
"all": row[2],
"archived": row[3],
"new": row[4],
"unknown": row[5]
}
)
# Generate device-specific MQTT messages if enabled
@@ -291,11 +307,11 @@ def mqtt_start(db):
sec_delay = len(devices) * int(get_setting_value('MQTT_DELAY_SEC'))*5
mylog('minimal', [f"[{pluginName}] Estimated delay: ", (sec_delay), 's ', '(', round(sec_delay/60,1) , 'min)' ])
mylog('verbose', [f"[{pluginName}] Estimated delay: ", (sec_delay), 's ', '(', round(sec_delay/60,1) , 'min)' ])
# debug_index = 0
for device in devices:
# Create devices in Home Assistant - send config messages
deviceId = 'mac_' + device["dev_MAC"].replace(" ", "").replace(":", "_").lower()
@@ -310,18 +326,18 @@ def mqtt_start(db):
# update device sensors in home assistant
publish_mqtt(mqtt_client, 'system-sensors/sensor/'+deviceId+'/state',
'{ \
"last_ip": "' + device["dev_LastIP"] +'", \
"is_new": "' + str(device["dev_NewDevice"]) +'", \
"vendor": "' + sanitize_string(device["dev_Vendor"]) +'", \
"mac_address": "' + str(device["dev_MAC"]) +'" \
}'
{
"last_ip": device["dev_LastIP"],
"is_new": str(device["dev_NewDevice"]),
"vendor": sanitize_string(device["dev_Vendor"]),
"mac_address": str(device["dev_MAC"])
}
)
publish_mqtt(mqtt_client, 'system-sensors/binary_sensor/'+deviceId+'/state',
'{ \
"is_present": "' + to_binary_sensor(str(device["dev_PresentLastScan"])) +'"\
}'
{
"is_present": to_binary_sensor(str(device["dev_PresentLastScan"]))
}
)
# delete device / topic

View File

@@ -98,35 +98,35 @@ while ($row = $result -> fetchArray (SQLITE3_ASSOC)) {
<div class="content settingswrap " id="accordion_gen">
<div class ="bg-grey-dark color-palette box panel panel-default col-sm-12 box-default box-info" >
<div class ="bg-grey-dark color-palette box panel panel-default col-sm-12 box-default box-info" id="core_content_header" >
<div class ="settings-group col-sm-12">
<i class="<?= lang("settings_core_icon");?>"></i> <?= lang("settings_core_label");?>
</div>
<div class =" col-sm-12" id="core_content"></div>
</div>
<div class ="bg-grey-dark color-palette box panel panel-default col-sm-12 box-default box-info" >
<div class ="bg-grey-dark color-palette box panel panel-default col-sm-12 box-default box-info" id="system_content_header" >
<div class ="settings-group col-sm-12">
<i class="<?= lang("settings_system_icon");?>"></i> <?= lang("settings_system_label");?>
</div>
<div class =" col-sm-12" id="system_content"></div>
</div>
<div class ="bg-grey-dark color-palette box panel panel-default col-sm-12 box-default box-info" >
<div class ="bg-grey-dark color-palette box panel panel-default col-sm-12 box-default box-info" id="device_scanner_content_header" >
<div class ="settings-group col-sm-12">
<i class="<?= lang("settings_device_scanners_icon");?>"></i> <?= lang("settings_device_scanners_label");?>
</div>
<div class =" col-sm-12" id="device_scanner_content"></div>
</div>
<div class ="bg-grey-dark color-palette box panel panel-default col-sm-12 box-default box-info" >
<div class ="bg-grey-dark color-palette box panel panel-default col-sm-12 box-default box-info" id="other_content_header">
<div class ="settings-group col-sm-12">
<i class="<?= lang("settings_other_scanners_icon");?>"></i> <?= lang("settings_other_scanners_label");?>
</div>
<div class =" col-sm-12" id="other_content"></div>
</div>
<div class ="bg-grey-dark color-palette box panel panel-default col-sm-12 box-default box-info" >
<div class ="bg-grey-dark color-palette box panel panel-default col-sm-12 box-default box-info" id="publisher_content_header" >
<div class ="settings-group col-sm-12">
<i class="<?= lang("settings_publishers_icon");?>"></i> <?= lang("settings_publishers_label");?>
</div>
@@ -757,6 +757,22 @@ while ($row = $result -> fetchArray (SQLITE3_ASSOC)) {
document.getElementById('lastImportedTime').innerHTML = humanReadable;
// Scroll down if ID in URL
// Get the ID from the URL
var url = window.location.href;
var id = url.substring(url.indexOf("#") + 1);
// Check if the ID exists in the document
if ($("#" + id).length > 0) {
setTimeout(function() {
// Scroll to the element
$('html, body').animate({
scrollTop: $("#" + id).offset().top - 50
}, 1000);
},200);
}
})
}

View File

@@ -96,17 +96,17 @@ $total_memorymb = number_format($total_memorymb, 0, '.', '.');
$mem_used = round(memory_get_usage() / 1048576 * 100, 2);
$memory_usage_percent = round(($mem_used / $total_memorymb), 2);
//HDD stats
$hdd_result = shell_exec("df | awk '{print $1}'");
$hdd_result = shell_exec(" df -P | awk '{print $1}'");
$hdd_devices = explode("\n", trim($hdd_result));
$hdd_result = shell_exec("df | awk '{print $2}'");
$hdd_result = shell_exec(" df -P | awk '{print $2}'");
$hdd_devices_total = explode("\n", trim($hdd_result));
$hdd_result = shell_exec("df | awk '{print $3}'");
$hdd_result = shell_exec(" df -P | awk '{print $3}'");
$hdd_devices_used = explode("\n", trim($hdd_result));
$hdd_result = shell_exec("df | awk '{print $4}'");
$hdd_result = shell_exec(" df -P | awk '{print $4}'");
$hdd_devices_free = explode("\n", trim($hdd_result));
$hdd_result = shell_exec("df | awk '{print $5}'");
$hdd_result = shell_exec(" df -P | awk '{print $5}'");
$hdd_devices_percent = explode("\n", trim($hdd_result));
$hdd_result = shell_exec("df | awk '{print $6}'");
$hdd_result = shell_exec(" df -P | awk '{print $6}'");
$hdd_devices_mount = explode("\n", trim($hdd_result));
//Network stats
// Check Server name