From 1b9d4223c5be466ad83d8e5da4fedc6afdf9ac7e Mon Sep 17 00:00:00 2001 From: Jokob-sk Date: Mon, 16 Oct 2023 20:28:30 +1100 Subject: [PATCH] =?UTF-8?q?MQTT,=20DHCPLSS=20work=F0=9F=94=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- front/maintenance.php | 25 ++++++++++++++++------- front/plugins/_publisher_mqtt/config.json | 2 +- front/plugins/_publisher_mqtt/mqtt.py | 16 +++++++++++---- front/plugins/dhcp_leases/script.py | 1 + front/plugins/pholus_scan/script.py | 3 +-- install/install_python.sh | 2 +- pialert/plugin_utils.py | 15 ++++++++------ 7 files changed, 43 insertions(+), 21 deletions(-) diff --git a/front/maintenance.php b/front/maintenance.php index 4b9f754a..e717fa13 100755 --- a/front/maintenance.php +++ b/front/maintenance.php @@ -395,7 +395,7 @@ $db->close();
-
+
+
+
+
+
nginx/error.log
+
+
+
+
@@ -437,7 +448,7 @@ $db->close();
-
+
@@ -454,7 +465,7 @@ $db->close();
-
+
@@ -507,7 +518,7 @@ $db->close();
-
+
diff --git a/front/plugins/_publisher_mqtt/config.json b/front/plugins/_publisher_mqtt/config.json index f9a48e3f..f3b29c3d 100755 --- a/front/plugins/_publisher_mqtt/config.json +++ b/front/plugins/_publisher_mqtt/config.json @@ -121,7 +121,7 @@ }, { "column": "Watched_Value1", - "css_classes": "col-sm-2", + "css_classes": "col-sm-3", "show": true, "type": "label", "default_value":"", diff --git a/front/plugins/_publisher_mqtt/mqtt.py b/front/plugins/_publisher_mqtt/mqtt.py index 6ecb208e..b4727969 100755 --- a/front/plugins/_publisher_mqtt/mqtt.py +++ b/front/plugins/_publisher_mqtt/mqtt.py @@ -37,6 +37,7 @@ plugin_objects = Plugin_Objects(RESULT_FILE) md5_hash = hashlib.md5() pluginName = 'MQTT' +module_name = pluginName # globals @@ -66,7 +67,6 @@ def main(): #------------------------------------------------------------------------------- # MQTT #------------------------------------------------------------------------------- - #------------------------------------------------------------------------------- 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') == '': @@ -97,7 +97,12 @@ class sensor_config: hash_value = str(md5_hash_hex) self.hash = hash_value - self.isNew = getPluginObject({"Plugin":"MQTT", "Watched_Value4":hash_value}) is None + + plugObj = getPluginObject({"Plugin":"MQTT", "Watched_Value3":hash_value}) + + mylog('verbose', [f"[{pluginName}] Previous plugin object entry: {json.dumps(plugObj)}"]) + + self.isNew = plugObj == {} # Log sensor global plugin_objects @@ -150,9 +155,10 @@ def create_generic_device(client): #------------------------------------------------------------------------------- + def create_sensor(client, deviceId, deviceName, sensorType, sensorName, icon, mac=""): - global mqtt_sensors + global mqtt_sensors new_sensor_config = sensor_config(deviceId, deviceName, sensorType, sensorName, icon, mac) @@ -268,8 +274,10 @@ def mqtt_start(db): mylog('minimal', [f"[{pluginName}] Estimated delay: ", (sec_delay), 's ', '(', round(sec_delay/60,1) , 'min)' ]) - for device in devices: + + for device in devices: + # Create devices in Home Assistant - send config messages deviceId = 'mac_' + device["dev_MAC"].replace(" ", "").replace(":", "_").lower() deviceNameDisplay = re.sub('[^a-zA-Z0-9-_\s]', '', device["dev_Name"]) diff --git a/front/plugins/dhcp_leases/script.py b/front/plugins/dhcp_leases/script.py index af044f46..a8562987 100755 --- a/front/plugins/dhcp_leases/script.py +++ b/front/plugins/dhcp_leases/script.py @@ -6,6 +6,7 @@ import subprocess import argparse import os import sys +import chardet sys.path.append("/home/pi/pialert/front/plugins") sys.path.append('/home/pi/pialert/pialert') diff --git a/front/plugins/pholus_scan/script.py b/front/plugins/pholus_scan/script.py index ec696394..bd11687e 100755 --- a/front/plugins/pholus_scan/script.py +++ b/front/plugins/pholus_scan/script.py @@ -94,8 +94,7 @@ def main(): def execute_pholus_scan(userSubnets, timeoutSec): - # output of possible multiple interfaces - arpscan_output = "" + # output of possible multiple interfaces result_list = [] timeoutPerSubnet = float(timeoutSec) / len(userSubnets) diff --git a/install/install_python.sh b/install/install_python.sh index e6188862..381a1b80 100755 --- a/install/install_python.sh +++ b/install/install_python.sh @@ -13,5 +13,5 @@ source myenv/bin/activate update-alternatives --install /usr/bin/python python /usr/bin/python3 10 # install packages thru pip3 -pip3 install requests paho-mqtt scapy cron-converter pytz json2table dhcp-leases pyunifi speedtest-cli +pip3 install requests paho-mqtt scapy cron-converter pytz json2table dhcp-leases pyunifi speedtest-cli chardet diff --git a/pialert/plugin_utils.py b/pialert/plugin_utils.py index e00fa354..2e4c6142 100755 --- a/pialert/plugin_utils.py +++ b/pialert/plugin_utils.py @@ -204,9 +204,12 @@ def getPluginObject(keyValues): with open(plugins_objects, 'r') as json_file: data = json.load(json_file) - for item in data.get("data", []): + objectEntries = data.get("data", []) + + for item in objectEntries: # Initialize a flag to check if all key-value pairs match - all_match = True + all_match = True + for key, value in keyValues.items(): if item.get(key) != value: all_match = False @@ -215,14 +218,14 @@ def getPluginObject(keyValues): if all_match: return item - mylog('debug', [f'[{module_name}] Error - Object not found {json.dumps(keyValues)} ']) + mylog('verbose', [f'[{module_name}] Error - Object not found {json.dumps(keyValues)} ']) - return None + return {} except (FileNotFoundError, json.JSONDecodeError, ValueError) as e: # Handle the case when the file is not found, JSON decoding fails, or data is not in the expected format - mylog('none', [f'[{module_name}] Error - JSONDecodeError or FileNotFoundError for file {plugins_objects}']) + mylog('verbose', [f'[{module_name}] Error - JSONDecodeError or FileNotFoundError for file {plugins_objects}']) - return None + return {}