From 95a7dcc7fc672e1650727656f6fcbe77704519d9 Mon Sep 17 00:00:00 2001 From: Data-Monkey Date: Wed, 17 May 2023 12:55:34 +1000 Subject: [PATCH] undiscoverables initial version --- front/plugins/undiscoverables/config.json | 158 ++++++++++++++++++ .../plugins/undiscoverables/plugin_helper.py | 81 +++++++++ front/plugins/undiscoverables/script.py | 38 +++++ 3 files changed, 277 insertions(+) create mode 100644 front/plugins/undiscoverables/config.json create mode 100644 front/plugins/undiscoverables/plugin_helper.py create mode 100644 front/plugins/undiscoverables/script.py diff --git a/front/plugins/undiscoverables/config.json b/front/plugins/undiscoverables/config.json new file mode 100644 index 00000000..b997d1e6 --- /dev/null +++ b/front/plugins/undiscoverables/config.json @@ -0,0 +1,158 @@ +{ + "code_name": "undiscoverables", + "unique_prefix": "UNDIS", + "enabled": true, + "data_source": "python-script", + "localized": ["display_name", "description", "icon"], + + "display_name": [ + { + "language_code": "en_us", + "string": "Un-Discoverable Devices" + } + ], + "icon": [ + { + "language_code": "en_us", + "string": "" + } + ], + "description": [ + { + "language_code": "en_us", + "string": "This plugin is to import undiscoverable devices from a file." + } + ], + + "settings": [ + { + "function": "RUN", + "type": "selecttext", + "default_value": "once", + "options": [ + "disabled", + "once", + "schedule", + "always_after_scan", + "on_new_device" + ], + "localized": ["name", "description"], + "name": [ + { + "language_code": "en_us", + "string": "When to run" + } + ], + "description": [ + { + "language_code": "en_us", + "string": "Enable import of devices from a file. If you select schedule the scheduling settings from below are applied. If you select once the scan is run only once on start of the application (container) or after you update your settings." + } + ] + }, + { + "function": "CMD", + "type": "text", + "default_value": "python3 /home/pi/pialert/front/plugins/undiscoverables/script.py", + "options": [], + "localized": ["name", "description"], + "name": [ + { + "language_code": "en_us", + "string": "Command" + } + ], + "description": [ + { + "language_code": "en_us", + "string": "Command to run" + } + ] + }, + { + "function": "RUN_SCHD", + "type": "text", + "default_value": "0 2 * * *", + "options": [], + "localized": ["name", "description"], + "name": [ + { + "language_code": "en_us", + "string": "Schedule" + } + ], + "description": [ + { + "language_code": "en_us", + "string": "Only enabled if you select schedule in the DHCPLSS_RUN setting. Make sure you enter the schedule in the correct cron-like format (e.g. validate at crontab.guru). For example entering 0 4 * * * will run the scan after 4 am in the TIMEZONE you set above. Will be run NEXT time the time passes." + } + ] + }, + { + "function": "RUN_TIMEOUT", + "type": "integer", + "default_value": 5, + "options": [], + "localized": ["name", "description"], + "name": [ + { + "language_code": "en_us", + "string": "Run timeout" + }, + { + "language_code": "de_de", + "string": "Wartezeit" + } + ], + "description": [ + { + "language_code": "en_us", + "string": "Maximum time in seconds to wait for the script to finish. If this time is exceeded the script is aborted." + } + ] + }, + { + "function": "WATCH", + "type": "multiselect", + "default_value": ["Watched_Value1", "Watched_Value4"], + "options": [ + "Watched_Value1", + "Watched_Value2", + "Watched_Value3", + "Watched_Value4" + ], + "localized": ["name", "description"], + "name": [ + { + "language_code": "en_us", + "string": "Watched" + } + ], + "description": [ + { + "language_code": "en_us", + "string": "Send a notification if selected values change. Use CTRL + Click to select/deselect. " + } + ] + }, + { + "function": "REPORT_ON", + "type": "multiselect", + "default_value": ["new", "watched-changed"], + "options": ["new", "watched-changed", "watched-not-changed"], + "localized": ["name", "description"], + "name": [ + { + "language_code": "en_us", + "string": "Report on" + } + ], + "description": [ + { + "language_code": "en_us", + "string": "Send a notification only on these statuses. new means a new unique (unique combination of PrimaryId and SecondaryId) object was discovered. watched-changed means that selected Watched_ValueN columns changed." + } + ] + } + ] +} diff --git a/front/plugins/undiscoverables/plugin_helper.py b/front/plugins/undiscoverables/plugin_helper.py new file mode 100644 index 00000000..31b46ae7 --- /dev/null +++ b/front/plugins/undiscoverables/plugin_helper.py @@ -0,0 +1,81 @@ + +from time import sleep, time, strftime +import sys +import pathlib + +# ------------------------------------------------------------------- +class Plugin_Object: + def __init__( + self, + primaryId="", + secondaryId="", + watched1="", + watched2="", + watched3="", + watched4="", + extra="", + foreignKey="" + ): + self.pluginPref = "" + self.primaryId = primaryId + self.secondaryId = secondaryId + self.created = strftime("%Y-%m-%d %H:%M:%S") + self.changed = "" + self.watched1 = watched1 + self.watched2 = watched2 + self.watched3 = watched3 + self.watched4 = watched4 + self.status = "" + 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 + ) + ) + return line + + + +class Plugin_Objects: + def __init__(self, result_file): + self.result_file = result_file + self.objects = [] + + 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) + ) + + + def write_result_file(self): + + with open(self.result_file, mode='w') as fp: + for obj in self.objects: + fp.write ( obj.write() ) + fp.close() + diff --git a/front/plugins/undiscoverables/script.py b/front/plugins/undiscoverables/script.py new file mode 100644 index 00000000..6737ce1d --- /dev/null +++ b/front/plugins/undiscoverables/script.py @@ -0,0 +1,38 @@ +#!/usr/bin/env python +# Based on the work of https://github.com/leiweibau/Pi.Alert + +# python3 /home/pi/pialert/front/plugins/website_monitor/script.py urls=http://google.com,http://bing.com + +import sys +import pathlib + +from plugin_helper import Plugin_Objects, Plugin_Object + +sys.dont_write_bytecode = True + +curPath = str(pathlib.Path(__file__).parent.resolve()) +log_file = curPath + '/script.log' +result_file = curPath + '\last_result.log' + +FAKE_DEVICES = ["routerXX","hubZZ"] + + +def main(): + print("Hello") + + devices = Plugin_Objects( result_file ) + + + for fake_dev in FAKE_DEVICES: + devices.add_object(fake_dev, fake_dev, fake_dev, fake_dev, "", "", "", "") + + devices.write_result_file() + + return devices + + +#=============================================================================== +# BEGIN +#=============================================================================== +if __name__ == '__main__': + d = main() \ No newline at end of file