MQTT rework v0.3 📩
This commit is contained in:
@@ -79,19 +79,15 @@
|
|||||||
{
|
{
|
||||||
"column": "Object_SecondaryID",
|
"column": "Object_SecondaryID",
|
||||||
"css_classes": "col-sm-2",
|
"css_classes": "col-sm-2",
|
||||||
"show": false,
|
"show": true,
|
||||||
"type": "label",
|
"type": "label",
|
||||||
"default_value":"",
|
"default_value":"",
|
||||||
"options": [],
|
"options": [],
|
||||||
"localized": ["name"],
|
"localized": ["name"],
|
||||||
"name":[{
|
"name":[{
|
||||||
"language_code": "en_us",
|
"language_code": "en_us",
|
||||||
"string" : "N/A"
|
"string" : "Sensor ID"
|
||||||
},
|
}]
|
||||||
{
|
|
||||||
"language_code": "es_es",
|
|
||||||
"string" : "N/A"
|
|
||||||
}]
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"column": "DateTimeCreated",
|
"column": "DateTimeCreated",
|
||||||
@@ -109,7 +105,7 @@
|
|||||||
{
|
{
|
||||||
"column": "DateTimeChanged",
|
"column": "DateTimeChanged",
|
||||||
"css_classes": "col-sm-2",
|
"css_classes": "col-sm-2",
|
||||||
"show": false,
|
"show": true,
|
||||||
"type": "label",
|
"type": "label",
|
||||||
"default_value":"",
|
"default_value":"",
|
||||||
"options": [],
|
"options": [],
|
||||||
@@ -133,55 +129,48 @@
|
|||||||
"localized": ["name"],
|
"localized": ["name"],
|
||||||
"name":[{
|
"name":[{
|
||||||
"language_code": "en_us",
|
"language_code": "en_us",
|
||||||
"string" : "Notification GUID"
|
"string" : "Device name"
|
||||||
}]
|
}]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"column": "Watched_Value2",
|
"column": "Watched_Value2",
|
||||||
"css_classes": "col-sm-8",
|
"css_classes": "col-sm-8",
|
||||||
"show": true,
|
"show": true,
|
||||||
"type": "textarea_readonly",
|
"type": "label",
|
||||||
"default_value":"",
|
"default_value":"",
|
||||||
"options": [],
|
"options": [],
|
||||||
"localized": ["name"],
|
"localized": ["name"],
|
||||||
"name":[{
|
"name":[{
|
||||||
"language_code": "en_us",
|
"language_code": "en_us",
|
||||||
"string" : "Result"
|
"string" : "Sensor type"
|
||||||
}]
|
}]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"column": "Watched_Value3",
|
"column": "Watched_Value3",
|
||||||
"css_classes": "col-sm-2",
|
"css_classes": "col-sm-2",
|
||||||
"show": false,
|
"show": true,
|
||||||
"type": "label",
|
"type": "label",
|
||||||
"default_value":"",
|
"default_value":"",
|
||||||
"options": [],
|
"options": [],
|
||||||
"localized": ["name"],
|
"localized": ["name"],
|
||||||
"name":[{
|
"name":[{
|
||||||
"language_code": "en_us",
|
"language_code": "en_us",
|
||||||
"string" : "N/A"
|
"string" : "Sensor name"
|
||||||
},
|
}]
|
||||||
{
|
|
||||||
"language_code": "es_es",
|
|
||||||
"string" : "N/A"
|
|
||||||
}]
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"column": "Watched_Value4",
|
"column": "Watched_Value4",
|
||||||
"css_classes": "col-sm-2",
|
"css_classes": "col-sm-2",
|
||||||
"show": false,
|
"show": true,
|
||||||
"type": "label",
|
"type": "label",
|
||||||
"default_value":"",
|
"default_value":"",
|
||||||
"options": [],
|
"options": [],
|
||||||
"localized": ["name"],
|
"localized": ["name"],
|
||||||
"name":[{
|
"name":[{
|
||||||
"language_code": "en_us",
|
"language_code": "en_us",
|
||||||
"string" : "N/A"
|
"string" : "Hash"
|
||||||
},
|
}
|
||||||
{
|
]
|
||||||
"language_code": "es_es",
|
|
||||||
"string" : "N/A"
|
|
||||||
}]
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"column": "UserData",
|
"column": "UserData",
|
||||||
@@ -203,7 +192,7 @@
|
|||||||
{
|
{
|
||||||
"column": "Status",
|
"column": "Status",
|
||||||
"css_classes": "col-sm-1",
|
"css_classes": "col-sm-1",
|
||||||
"show": false,
|
"show": true,
|
||||||
"type": "replace",
|
"type": "replace",
|
||||||
"default_value":"",
|
"default_value":"",
|
||||||
"options": [
|
"options": [
|
||||||
|
|||||||
@@ -17,7 +17,9 @@ sys.path.extend(["/home/pi/pialert/front/plugins", "/home/pi/pialert/pialert"])
|
|||||||
|
|
||||||
# PiAlert modules
|
# PiAlert modules
|
||||||
import conf
|
import conf
|
||||||
from plugin_helper import Plugin_Objects
|
from const import apiPath
|
||||||
|
from plugin_helper import getPluginObject
|
||||||
|
from plugin_utils import Plugin_Objects
|
||||||
from logger import mylog, append_line_to_file
|
from logger import mylog, append_line_to_file
|
||||||
from helper import timeNowTZ, noti_obj, get_setting_value, bytes_to_string, sanitize_string
|
from helper import timeNowTZ, noti_obj, get_setting_value, bytes_to_string, sanitize_string
|
||||||
from notification import Notification_obj
|
from notification import Notification_obj
|
||||||
@@ -27,8 +29,18 @@ from database import DB, get_all_devices, get_device_stats
|
|||||||
CUR_PATH = str(pathlib.Path(__file__).parent.resolve())
|
CUR_PATH = str(pathlib.Path(__file__).parent.resolve())
|
||||||
RESULT_FILE = os.path.join(CUR_PATH, 'last_result.log')
|
RESULT_FILE = os.path.join(CUR_PATH, 'last_result.log')
|
||||||
|
|
||||||
|
|
||||||
|
# Initialize the Plugin obj output file
|
||||||
|
plugin_objects = Plugin_Objects(RESULT_FILE)
|
||||||
|
|
||||||
pluginName = 'MQTT'
|
pluginName = 'MQTT'
|
||||||
|
|
||||||
|
# globals
|
||||||
|
|
||||||
|
mqtt_sensors = []
|
||||||
|
mqtt_connected_to_broker = False
|
||||||
|
client = None # mqtt client
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
|
|
||||||
mylog('verbose', [f'[{pluginName}](publisher) In script'])
|
mylog('verbose', [f'[{pluginName}](publisher) In script'])
|
||||||
@@ -44,6 +56,8 @@ def main():
|
|||||||
|
|
||||||
mqtt_start(db)
|
mqtt_start(db)
|
||||||
|
|
||||||
|
plugin_objects.write_result_file()
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#-------------------------------------------------------------------------------
|
#-------------------------------------------------------------------------------
|
||||||
@@ -68,6 +82,21 @@ class sensor_config:
|
|||||||
self.sensorName = sensorName
|
self.sensorName = sensorName
|
||||||
self.icon = icon
|
self.icon = icon
|
||||||
self.hash = str(hash(str(deviceId) + str(deviceName)+ str(sensorType)+ str(sensorName)+ str(icon)))
|
self.hash = str(hash(str(deviceId) + str(deviceName)+ str(sensorType)+ str(sensorName)+ str(icon)))
|
||||||
|
self.isNew = getPluginObject({"Plugin":"MQTT", "Watched_Value4":hash}) is None
|
||||||
|
|
||||||
|
# Log sensor
|
||||||
|
global plugin_objects
|
||||||
|
|
||||||
|
plugin_objects.add_object(
|
||||||
|
primaryId = pluginName,
|
||||||
|
secondaryId = deviceId,
|
||||||
|
watched1 = deviceName,
|
||||||
|
watched2 = sensorType,
|
||||||
|
watched3 = sensorName,
|
||||||
|
watched4 = hash,
|
||||||
|
extra = 'null',
|
||||||
|
foreignKey = deviceId
|
||||||
|
)
|
||||||
|
|
||||||
#-------------------------------------------------------------------------------
|
#-------------------------------------------------------------------------------
|
||||||
|
|
||||||
@@ -105,69 +134,71 @@ def create_generic_device(client):
|
|||||||
#-------------------------------------------------------------------------------
|
#-------------------------------------------------------------------------------
|
||||||
def create_sensor(client, deviceId, deviceName, sensorType, sensorName, icon):
|
def create_sensor(client, deviceId, deviceName, sensorType, sensorName, icon):
|
||||||
|
|
||||||
|
global mqtt_sensors
|
||||||
|
|
||||||
new_sensor_config = sensor_config(deviceId, deviceName, sensorType, sensorName, icon)
|
new_sensor_config = sensor_config(deviceId, deviceName, sensorType, sensorName, icon)
|
||||||
|
|
||||||
# check if config already in list and if not, add it, otherwise skip
|
# save if new
|
||||||
is_unique = True
|
if new_sensor_config.isNew:
|
||||||
|
mylog('minimal', [f"[{pluginName}] Publishing sensor number {len(mqtt_sensors)}"])
|
||||||
mylog('minimal', [f"[{pluginName}] Already previously published sensors: {len(conf.mqtt_sensors)}"])
|
|
||||||
|
|
||||||
for sensor in conf.mqtt_sensors:
|
|
||||||
if sensor.hash == new_sensor_config.hash:
|
|
||||||
is_unique = False
|
|
||||||
break
|
|
||||||
|
|
||||||
# save if unique
|
|
||||||
if is_unique:
|
|
||||||
publish_sensor(client, new_sensor_config)
|
publish_sensor(client, new_sensor_config)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#-------------------------------------------------------------------------------
|
#-------------------------------------------------------------------------------
|
||||||
def publish_sensor(client, sensorConf):
|
def publish_sensor(client, sensorConfig):
|
||||||
|
|
||||||
|
global mqtt_sensors
|
||||||
|
|
||||||
message = '{ \
|
message = '{ \
|
||||||
"name":"'+ sensorConf.deviceName +' '+sensorConf.sensorName+'", \
|
"name":"'+ sensorConfig.deviceName +' '+sensorConfig.sensorName+'", \
|
||||||
"state_topic":"system-sensors/'+sensorConf.sensorType+'/'+sensorConf.deviceId+'/state", \
|
"state_topic":"system-sensors/'+sensorConfig.sensorType+'/'+sensorConfig.deviceId+'/state", \
|
||||||
"value_template":"{{value_json.'+sensorConf.sensorName+'}}", \
|
"value_template":"{{value_json.'+sensorConfig.sensorName+'}}", \
|
||||||
"unique_id":"'+sensorConf.deviceId+'_sensor_'+sensorConf.sensorName+'", \
|
"unique_id":"'+sensorConfig.deviceId+'_sensor_'+sensorConfig.sensorName+'", \
|
||||||
"device": \
|
"device": \
|
||||||
{ \
|
{ \
|
||||||
"identifiers": ["'+sensorConf.deviceId+'_sensor"], \
|
"identifiers": ["'+sensorConfig.deviceId+'_sensor"], \
|
||||||
"manufacturer": "PiAlert", \
|
"manufacturer": "PiAlert", \
|
||||||
"name":"'+sensorConf.deviceName+'" \
|
"name":"'+sensorConfig.deviceName+'" \
|
||||||
}, \
|
}, \
|
||||||
"icon":"mdi:'+sensorConf.icon+'" \
|
"icon":"mdi:'+sensorConfig.icon+'" \
|
||||||
}'
|
}'
|
||||||
|
|
||||||
topic='homeassistant/'+sensorConf.sensorType+'/'+sensorConf.deviceId+'/'+sensorConf.sensorName+'/config'
|
topic='homeassistant/'+sensorConfig.sensorType+'/'+sensorConfig.deviceId+'/'+sensorConfig.sensorName+'/config'
|
||||||
|
|
||||||
# add the sensor to the global list to keep track of succesfully added sensors
|
# add the sensor to the global list to keep track of succesfully added sensors
|
||||||
if publish_mqtt(client, topic, message):
|
if publish_mqtt(client, topic, message):
|
||||||
# hack - delay adding to the queue in case the process is
|
# hack - delay adding to the queue in case the process is
|
||||||
time.sleep(get_setting_value('MQTT_DELAY_SEC')) # restarted and previous publish processes aborted
|
time.sleep(get_setting_value('MQTT_DELAY_SEC')) # restarted and previous publish processes aborted
|
||||||
# (it takes ~2s to update a sensor config on the broker)
|
# (it takes ~2s to update a sensor config on the broker)
|
||||||
conf.mqtt_sensors.append(sensorConf)
|
mqtt_sensors.append(sensorConfig)
|
||||||
|
|
||||||
#-------------------------------------------------------------------------------
|
#-------------------------------------------------------------------------------
|
||||||
def mqtt_create_client():
|
def mqtt_create_client():
|
||||||
def on_disconnect(client, userdata, rc):
|
def on_disconnect(client, userdata, rc):
|
||||||
conf.mqtt_connected_to_broker = False
|
|
||||||
|
global mqtt_connected_to_broker
|
||||||
|
|
||||||
|
mqtt_connected_to_broker = False
|
||||||
|
|
||||||
# not sure is below line is correct / necessary
|
# not sure is below line is correct / necessary
|
||||||
# client = mqtt_create_client()
|
# client = mqtt_create_client()
|
||||||
|
|
||||||
def on_connect(client, userdata, flags, rc):
|
def on_connect(client, userdata, flags, rc):
|
||||||
|
|
||||||
|
global mqtt_connected_to_broker
|
||||||
|
|
||||||
if rc == 0:
|
if rc == 0:
|
||||||
mylog('verbose', [f"[{pluginName}] Connected to broker"])
|
mylog('verbose', [f"[{pluginName}] Connected to broker"])
|
||||||
conf.mqtt_connected_to_broker = True # Signal connection
|
mqtt_connected_to_broker = True # Signal connection
|
||||||
else:
|
else:
|
||||||
mylog('none', [f"[{pluginName}] Connection failed"])
|
mylog('none', [f"[{pluginName}] Connection failed"])
|
||||||
conf.mqtt_connected_to_broker = False
|
mqtt_connected_to_broker = False
|
||||||
|
|
||||||
|
|
||||||
|
global client
|
||||||
|
|
||||||
client = mqtt_client.Client('PiAlert') # Set Connecting Client ID
|
client = mqtt_client.Client('PiAlert') # Set Connecting Client ID
|
||||||
client.username_pw_set(get_setting_value('MQTT_USER'), get_setting_value('MQTT_PASSWORD'))
|
client.username_pw_set(get_setting_value('MQTT_USER'), get_setting_value('MQTT_PASSWORD'))
|
||||||
client.on_connect = on_connect
|
client.on_connect = on_connect
|
||||||
@@ -180,13 +211,12 @@ def mqtt_create_client():
|
|||||||
#-------------------------------------------------------------------------------
|
#-------------------------------------------------------------------------------
|
||||||
def mqtt_start(db):
|
def mqtt_start(db):
|
||||||
|
|
||||||
#global client
|
global client, mqtt_connected_to_broker
|
||||||
|
|
||||||
if conf.mqtt_connected_to_broker == False:
|
if mqtt_connected_to_broker == False:
|
||||||
conf.mqtt_connected_to_broker = True
|
mqtt_connected_to_broker = True
|
||||||
conf.client = mqtt_create_client()
|
client = mqtt_create_client()
|
||||||
|
|
||||||
client = conf.client
|
|
||||||
# General stats
|
# General stats
|
||||||
|
|
||||||
# Create a generic device for overal stats
|
# Create a generic device for overal stats
|
||||||
|
|||||||
@@ -123,3 +123,63 @@ class AppEvent_obj:
|
|||||||
# Commit changes
|
# Commit changes
|
||||||
self.db.commitDB()
|
self.db.commitDB()
|
||||||
|
|
||||||
|
|
||||||
|
def getPluginObject(**kwargs):
|
||||||
|
|
||||||
|
# Check if nothing, end
|
||||||
|
if not any(kwargs.values()):
|
||||||
|
return None
|
||||||
|
|
||||||
|
# Optional parameters
|
||||||
|
GUID = kwargs.get("GUID", "")
|
||||||
|
Plugin = kwargs.get("Plugin", "")
|
||||||
|
MAC = kwargs.get("MAC", "")
|
||||||
|
IP = kwargs.get("IP", "")
|
||||||
|
PrimaryID = kwargs.get("PrimaryID", "")
|
||||||
|
SecondaryID = kwargs.get("SecondaryID", "")
|
||||||
|
ForeignKey = kwargs.get("ForeignKey", "")
|
||||||
|
Index = kwargs.get("Index", "")
|
||||||
|
RowID = kwargs.get("RowID", "")
|
||||||
|
|
||||||
|
# we need the plugin
|
||||||
|
if Plugin == "":
|
||||||
|
return None
|
||||||
|
|
||||||
|
plugins_objects = apiPath + 'table_plugins_objects.json'
|
||||||
|
|
||||||
|
try:
|
||||||
|
with open(plugins_objects, 'r') as json_file:
|
||||||
|
|
||||||
|
data = json.load(json_file)
|
||||||
|
|
||||||
|
for item in data.get("data",[]):
|
||||||
|
if item.get("Index") == Index:
|
||||||
|
return item
|
||||||
|
|
||||||
|
for item in data.get("data",[]):
|
||||||
|
if item.get("ObjectPrimaryID") == PrimaryID and item.get("ObjectSecondaryID") == SecondaryID:
|
||||||
|
return item
|
||||||
|
|
||||||
|
for item in data.get("data",[]):
|
||||||
|
if item.get("ObjectPrimaryID") == MAC and item.get("ObjectSecondaryID") == IP:
|
||||||
|
return item
|
||||||
|
|
||||||
|
for item in data.get("data",[]):
|
||||||
|
if item.get("ObjectPrimaryID") == PrimaryID and item.get("ObjectSecondaryID") == IP:
|
||||||
|
return item
|
||||||
|
|
||||||
|
for item in data.get("data",[]):
|
||||||
|
if item.get("ObjectPrimaryID") == MAC and item.get("ObjectSecondaryID") == IP:
|
||||||
|
return item
|
||||||
|
|
||||||
|
|
||||||
|
mylog('debug', [f'[{module_name}] Error - Object not found - GUID:{GUID} | Plugin:{Plugin} | MAC:{MAC} | IP:{IP} | PrimaryID:{PrimaryID} | SecondaryID:{SecondaryID} | ForeignKey:{ForeignKey} | Index:{Index} | RowID:{RowID} '])
|
||||||
|
|
||||||
|
return None
|
||||||
|
|
||||||
|
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}'])
|
||||||
|
|
||||||
|
return None
|
||||||
|
|
||||||
|
|||||||
@@ -24,12 +24,6 @@ last_scan_run = ''
|
|||||||
last_version_check = ''
|
last_version_check = ''
|
||||||
arpscan_devices = []
|
arpscan_devices = []
|
||||||
|
|
||||||
# for MQTT
|
|
||||||
mqtt_connected_to_broker = False
|
|
||||||
mqtt_sensors = []
|
|
||||||
client = None # mqtt client
|
|
||||||
|
|
||||||
|
|
||||||
# ACTUAL CONFIGRATION ITEMS set to defaults
|
# ACTUAL CONFIGRATION ITEMS set to defaults
|
||||||
|
|
||||||
# -------------------------------------------
|
# -------------------------------------------
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ from logger import mylog
|
|||||||
from const import pluginsPath, logPath
|
from const import pluginsPath, logPath
|
||||||
from helper import timeNowTZ, updateState, get_file_content, write_file, get_setting, get_setting_value
|
from helper import timeNowTZ, updateState, get_file_content, write_file, get_setting, get_setting_value
|
||||||
|
|
||||||
|
module_name = 'Plugin utils'
|
||||||
|
|
||||||
#-------------------------------------------------------------------------------
|
#-------------------------------------------------------------------------------
|
||||||
def logEventStatusCounts(objName, pluginEvents):
|
def logEventStatusCounts(objName, pluginEvents):
|
||||||
@@ -19,17 +20,17 @@ def logEventStatusCounts(objName, pluginEvents):
|
|||||||
status_counts[status] = 1
|
status_counts[status] = 1
|
||||||
|
|
||||||
for status, count in status_counts.items():
|
for status, count in status_counts.items():
|
||||||
mylog('debug', [f'[Plugins] In {objName} there are {count} events with the status "{status}" '])
|
mylog('debug', [f'[{module_name}] In {objName} there are {count} events with the status "{status}" '])
|
||||||
|
|
||||||
|
|
||||||
#-------------------------------------------------------------------------------
|
#-------------------------------------------------------------------------------
|
||||||
def print_plugin_info(plugin, elements = ['display_name']):
|
def print_plugin_info(plugin, elements = ['display_name']):
|
||||||
|
|
||||||
mylog('verbose', ['[Plugins] ---------------------------------------------'])
|
mylog('verbose', [f'[{module_name}] ---------------------------------------------'])
|
||||||
|
|
||||||
for el in elements:
|
for el in elements:
|
||||||
res = get_plugin_string(plugin, el)
|
res = get_plugin_string(plugin, el)
|
||||||
mylog('verbose', ['[Plugins] ', el ,': ', res])
|
mylog('verbose', [f'[{module_name}] ', el ,': ', res])
|
||||||
|
|
||||||
|
|
||||||
#-------------------------------------------------------------------------------
|
#-------------------------------------------------------------------------------
|
||||||
@@ -42,8 +43,8 @@ def get_plugin_setting(plugin, function_key):
|
|||||||
if set["function"] == function_key:
|
if set["function"] == function_key:
|
||||||
result = set
|
result = set
|
||||||
|
|
||||||
if result == None:
|
# if result == None:
|
||||||
mylog('debug', ['[Plugins] Setting with "function":"', function_key, '" is missing in plugin: ', get_plugin_string(plugin, 'display_name')])
|
# mylog('debug', [f'[{module_name}] Setting with "function":"', function_key, '" is missing in plugin: ', get_plugin_string(plugin, 'display_name')])
|
||||||
|
|
||||||
return result
|
return result
|
||||||
|
|
||||||
@@ -76,9 +77,9 @@ def list_to_csv(arr):
|
|||||||
tmp = ''
|
tmp = ''
|
||||||
arrayItemStr = ''
|
arrayItemStr = ''
|
||||||
|
|
||||||
mylog('debug', '[Plugins] Flattening the below array')
|
mylog('debug', f'[{module_name}] Flattening the below array')
|
||||||
mylog('debug', arr)
|
mylog('debug', arr)
|
||||||
mylog('debug', f'[Plugins] isinstance(arr, list) : {isinstance(arr, list)} | isinstance(arr, str) : {isinstance(arr, str)}')
|
mylog('debug', f'[{module_name}] isinstance(arr, list) : {isinstance(arr, list)} | isinstance(arr, str) : {isinstance(arr, str)}')
|
||||||
|
|
||||||
if isinstance(arr, str):
|
if isinstance(arr, str):
|
||||||
return arr.replace('[','').replace(']','').replace("'", '') # removing brackets and single quotes (not allowed)
|
return arr.replace('[','').replace(']','').replace("'", '') # removing brackets and single quotes (not allowed)
|
||||||
@@ -97,12 +98,12 @@ def list_to_csv(arr):
|
|||||||
|
|
||||||
tmp = tmp[:-1] # Remove last comma ','
|
tmp = tmp[:-1] # Remove last comma ','
|
||||||
|
|
||||||
mylog('debug', f'[Plugins] Flattened array: {tmp}')
|
mylog('debug', f'[{module_name}] Flattened array: {tmp}')
|
||||||
|
|
||||||
return tmp
|
return tmp
|
||||||
|
|
||||||
else:
|
else:
|
||||||
mylog('none', f'[Plugins] ERROR Could not convert array: {arr}')
|
mylog('none', f'[{module_name}] ERROR Could not convert array: {arr}')
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -128,7 +129,7 @@ def combine_plugin_objects(old, new):
|
|||||||
# Replace {wildcars} with parameters
|
# Replace {wildcars} with parameters
|
||||||
def resolve_wildcards_arr(commandArr, params):
|
def resolve_wildcards_arr(commandArr, params):
|
||||||
|
|
||||||
mylog('debug', ['[Plugins] Pre-Resolved CMD: '] + commandArr)
|
mylog('debug', [f'[{module_name}] Pre-Resolved CMD: '] + commandArr)
|
||||||
|
|
||||||
for param in params:
|
for param in params:
|
||||||
# mylog('debug', ['[Plugins] key : {', param[0], '}'])
|
# mylog('debug', ['[Plugins] key : {', param[0], '}'])
|
||||||
@@ -181,5 +182,36 @@ def handle_empty(value):
|
|||||||
|
|
||||||
return value
|
return value
|
||||||
|
|
||||||
|
#-------------------------------------------------------------------------------
|
||||||
|
# Get and return a plugin object based on key-value pairs
|
||||||
|
# keyValues example: getPluginObject({"Plugin":"MQTT", "Watched_Value4":"someValue"})
|
||||||
|
def getPluginObject(keyValues):
|
||||||
|
|
||||||
|
plugins_objects = apiPath + 'table_plugins_objects.json'
|
||||||
|
|
||||||
|
try:
|
||||||
|
with open(plugins_objects, 'r') as json_file:
|
||||||
|
data = json.load(json_file)
|
||||||
|
|
||||||
|
for item in data.get("data", []):
|
||||||
|
# Initialize a flag to check if all key-value pairs match
|
||||||
|
all_match = True
|
||||||
|
for key, value in keyValues.items():
|
||||||
|
if item.get(key) != value:
|
||||||
|
all_match = False
|
||||||
|
break # No need to continue checking if one pair doesn't match
|
||||||
|
|
||||||
|
if all_match:
|
||||||
|
return item
|
||||||
|
|
||||||
|
mylog('debug', [f'[{module_name}] Error - Object not found {json.dumps(keyValues)} '])
|
||||||
|
|
||||||
|
return None
|
||||||
|
|
||||||
|
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}'])
|
||||||
|
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user