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": [{
"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 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()

View File

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