code and documentation tidied up

This commit is contained in:
Data-Monkey
2023-05-18 17:43:16 +10:00
parent a0501d88ec
commit 1474cf424b
4 changed files with 87 additions and 52 deletions

View File

@@ -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.

View File

@@ -45,7 +45,7 @@
}], }],
"description": [{ "description": [{
"language_code":"en_us", "language_code":"en_us",
"string" : "When enabled, once is the preferred option. It runs at startup and after every save of the config here.<br> 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.<br> Changes will only show in the devices <b> after the next scan!</b>"
}] }]
}, },
{ {

View File

@@ -1,10 +1,14 @@
from time import strftime
from time import sleep, time, strftime
import sys
import pathlib
# ------------------------------------------------------------------- # -------------------------------------------------------------------
class Plugin_Object: 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__( def __init__(
self, self,
primaryId="", primaryId="",
@@ -14,7 +18,7 @@ class Plugin_Object:
watched3="", watched3="",
watched4="", watched4="",
extra="", extra="",
foreignKey="" foreignKey="",
): ):
self.pluginPref = "" self.pluginPref = ""
self.primaryId = primaryId self.primaryId = primaryId
@@ -31,7 +35,11 @@ class Plugin_Object:
self.foreignKey = foreignKey self.foreignKey = foreignKey
def write(self): def write(self):
line = ("{}|{}|{}|{}|{}|{}|{}|{}|{}\n".format( """
write the object details as a string in the
format required to write the result file
"""
line = "{}|{}|{}|{}|{}|{}|{}|{}|{}\n".format(
self.primaryId, self.primaryId,
self.secondaryId, self.secondaryId,
self.created, self.created,
@@ -40,42 +48,49 @@ class Plugin_Object:
self.watched3, self.watched3,
self.watched4, self.watched4,
self.extra, self.extra,
self.foreignKey self.foreignKey,
)
) )
return line return line
class Plugin_Objects: 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): def __init__(self, result_file):
self.result_file = result_file self.result_file = result_file
self.objects = [] self.objects = []
def add_object ( self, primaryId="", def add_object(
self,
primaryId="",
secondaryId="", secondaryId="",
watched1="", watched1="",
watched2="", watched2="",
watched3="", watched3="",
watched4="", watched4="",
extra="", extra="",
foreignKey="" ): foreignKey="",
):
self.objects.append(Plugin_Object(primaryId, self.objects.append(
Plugin_Object(
primaryId,
secondaryId, secondaryId,
watched1, watched1,
watched2, watched2,
watched3, watched3,
watched4, watched4,
extra, extra,
foreignKey) foreignKey,
)
) )
def write_result_file(self): def write_result_file(self):
# print ("writing file: "+self.result_file) # 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: for obj in self.objects:
fp.write(obj.write()) fp.write(obj.write())
fp.close() fp.close()

View File

@@ -7,37 +7,37 @@ import argparse
from plugin_helper import Plugin_Objects from plugin_helper import Plugin_Objects
curPath = str(pathlib.Path(__file__).parent.resolve()) CUR_PATH = str(pathlib.Path(__file__).parent.resolve())
log_file = os.path.join(curPath , 'script.log') LOG_FILE = os.path.join(CUR_PATH , 'script.log')
result_file = os.path.join(curPath , 'last_result.log') RESULT_FILE = os.path.join(CUR_PATH , 'last_result.log')
def main(): def main():
parser = argparse.ArgumentParser(description='Import devices from dhcp.leases files') # the script expects a parameter in the format of devices=device1,device2,...
parser.add_argument('devices', action="store", help="absolute dhcp.leases file paths to check separated by ','") 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() values = parser.parse_args()
undis_devices = Plugin_Objects( result_file ) UNDIS_devices = Plugin_Objects( RESULT_FILE )
if values.devices: if values.devices:
for fake_dev in values.devices.split('=')[1].split(','): for fake_dev in values.devices.split('=')[1].split(','):
undis_devices.add_object( UNDIS_devices.add_object(
primaryId=fake_dev, # MAC primaryId=fake_dev, # MAC (Device Name)
secondaryId="0.0.0.0", # IP Address secondaryId="0.0.0.0", # IP Address (always 0.0.0.0)
watched1=fake_dev, # Device Name watched1=fake_dev, # Device Name
watched2="", watched2="",
watched3="", watched3="",
watched4="UNDIS", # used as ScanMethod watched4="",
extra="1", # used as dummy ScanCycle extra="",
foreignKey="") foreignKey="")
undis_devices.write_result_file() UNDIS_devices.write_result_file()
return 0 return 0
#=============================================================================== #===============================================================================
# BEGIN # BEGIN
#=============================================================================== #===============================================================================