From 1474cf424b91a9444bbca9d9c2545e3a61806212 Mon Sep 17 00:00:00 2001 From: Data-Monkey Date: Thu, 18 May 2023 17:43:16 +1000 Subject: [PATCH] code and documentation tidied up --- front/plugins/undiscoverables/README.md | 20 +++++ front/plugins/undiscoverables/config.json | 2 +- .../plugins/undiscoverables/plugin_helper.py | 83 +++++++++++-------- front/plugins/undiscoverables/script.py | 34 ++++---- 4 files changed, 87 insertions(+), 52 deletions(-) create mode 100644 front/plugins/undiscoverables/README.md diff --git a/front/plugins/undiscoverables/README.md b/front/plugins/undiscoverables/README.md new file mode 100644 index 00000000..2c594243 --- /dev/null +++ b/front/plugins/undiscoverables/README.md @@ -0,0 +1,20 @@ +## Overview + +A plugin allowing for importing Un-Discoverable devices from the settings page. + + +### Usage + +- Go to settings and find Un-Discoverabe Devices in the list of plugins. +- Enable the plugin by changing the RUN parameter from disabled to `once` or `always_after_scan`. +- Add the name of your device to the list. (remove the sample entry first) +- SAVE +- wait for the next scan to finish + +#### Example: + + + +### Known Limitations + - Un-Discoverable Devices always show as offline. That is expected as they can not be discovered by Pi.Alert. + - All IPs are set to 0.0.0.0 therefore the "Random MAC" icon might show up. diff --git a/front/plugins/undiscoverables/config.json b/front/plugins/undiscoverables/config.json index c1d9b509..de03f76f 100644 --- a/front/plugins/undiscoverables/config.json +++ b/front/plugins/undiscoverables/config.json @@ -45,7 +45,7 @@ }], "description": [{ "language_code":"en_us", - "string" : "When enabled, once is the preferred option. It runs at startup and after every save of the config here.
Changes will only show in the devices after the next scan!" + "string" : "When enabled, \"once\" is the preferred option. It runs at startup and after every save of the config here.
Changes will only show in the devices after the next scan!" }] }, { diff --git a/front/plugins/undiscoverables/plugin_helper.py b/front/plugins/undiscoverables/plugin_helper.py index 68634623..e198f30a 100644 --- a/front/plugins/undiscoverables/plugin_helper.py +++ b/front/plugins/undiscoverables/plugin_helper.py @@ -1,10 +1,14 @@ - -from time import sleep, time, strftime -import sys -import pathlib +from time import strftime + # ------------------------------------------------------------------- class Plugin_Object: + """ + Plugin_Object class to manage one object introduced by the plugin + An object typically is a device but could also be a website or something + else that is monitored by the plugin. + """ + def __init__( self, primaryId="", @@ -14,7 +18,7 @@ class Plugin_Object: watched3="", watched4="", extra="", - foreignKey="" + foreignKey="", ): self.pluginPref = "" self.primaryId = primaryId @@ -29,53 +33,64 @@ class Plugin_Object: self.extra = extra self.userData = "" self.foreignKey = foreignKey - + def write(self): - line = ("{}|{}|{}|{}|{}|{}|{}|{}|{}\n".format( - self.primaryId, - self.secondaryId, - self.created, - self.watched1, - self.watched2, - self.watched3, - self.watched4, - self.extra, - self.foreignKey - ) + """ + write the object details as a string in the + format required to write the result file + """ + line = "{}|{}|{}|{}|{}|{}|{}|{}|{}\n".format( + self.primaryId, + self.secondaryId, + self.created, + self.watched1, + self.watched2, + self.watched3, + self.watched4, + self.extra, + self.foreignKey, ) return line - class Plugin_Objects: + """ + Plugin_Objects is the class that manages and holds all the objects created by the plugin. + It contains a list of Plugin_Object instances. + And can write the required result file. + """ + def __init__(self, result_file): - self.result_file = result_file + self.result_file = result_file self.objects = [] - def add_object ( self, primaryId="", + def add_object( + self, + primaryId="", secondaryId="", watched1="", watched2="", watched3="", watched4="", extra="", - foreignKey="" ): - - self.objects.append(Plugin_Object(primaryId, - secondaryId, - watched1, - watched2, - watched3, - watched4, - extra, - foreignKey) + foreignKey="", + ): + self.objects.append( + Plugin_Object( + primaryId, + secondaryId, + watched1, + watched2, + watched3, + watched4, + extra, + foreignKey, + ) ) - def write_result_file(self): # print ("writing file: "+self.result_file) - with open(self.result_file, mode='w') as fp: + with open(self.result_file, mode="w") as fp: for obj in self.objects: - fp.write ( obj.write() ) + fp.write(obj.write()) fp.close() - diff --git a/front/plugins/undiscoverables/script.py b/front/plugins/undiscoverables/script.py index 93d38310..7f17554d 100644 --- a/front/plugins/undiscoverables/script.py +++ b/front/plugins/undiscoverables/script.py @@ -7,36 +7,36 @@ import argparse from plugin_helper import Plugin_Objects -curPath = str(pathlib.Path(__file__).parent.resolve()) -log_file = os.path.join(curPath , 'script.log') -result_file = os.path.join(curPath , 'last_result.log') +CUR_PATH = str(pathlib.Path(__file__).parent.resolve()) +LOG_FILE = os.path.join(CUR_PATH , 'script.log') +RESULT_FILE = os.path.join(CUR_PATH , 'last_result.log') def main(): - parser = argparse.ArgumentParser(description='Import devices from dhcp.leases files') - parser.add_argument('devices', action="store", help="absolute dhcp.leases file paths to check separated by ','") - values = parser.parse_args() + # the script expects a parameter in the format of devices=device1,device2,... + parser = argparse.ArgumentParser(description='Import devices from settings') + parser.add_argument('devices', action="store", help="list of device names separated by ','") + values = parser.parse_args() - undis_devices = Plugin_Objects( result_file ) - - if values.devices: + UNDIS_devices = Plugin_Objects( RESULT_FILE ) + + if values.devices: for fake_dev in values.devices.split('=')[1].split(','): - undis_devices.add_object( - primaryId=fake_dev, # MAC - secondaryId="0.0.0.0", # IP Address + UNDIS_devices.add_object( + primaryId=fake_dev, # MAC (Device Name) + secondaryId="0.0.0.0", # IP Address (always 0.0.0.0) watched1=fake_dev, # Device Name watched2="", watched3="", - watched4="UNDIS", # used as ScanMethod - extra="1", # used as dummy ScanCycle + watched4="", + extra="", foreignKey="") - undis_devices.write_result_file() + UNDIS_devices.write_result_file() - return 0 - + return 0 #=============================================================================== # BEGIN