From d4b590a9fc119e5b4862d473569734d35c29d230 Mon Sep 17 00:00:00 2001 From: Jokob-sk Date: Sat, 7 Oct 2023 18:04:33 +1100 Subject: [PATCH] Notification rework v0.4 --- front/plugins/_publisher_apprise/apprise.py | 16 ++++++++-------- pialert/__main__.py | 17 ++++++++++++++++- pialert/reporting.py | 16 ---------------- 3 files changed, 24 insertions(+), 25 deletions(-) diff --git a/front/plugins/_publisher_apprise/apprise.py b/front/plugins/_publisher_apprise/apprise.py index c3ddb3c8..85a61f38 100755 --- a/front/plugins/_publisher_apprise/apprise.py +++ b/front/plugins/_publisher_apprise/apprise.py @@ -15,7 +15,7 @@ sys.path.extend(["/home/pi/pialert/front/plugins", "/home/pi/pialert/pialert"]) import conf from plugin_helper import Plugin_Objects from logger import mylog, append_line_to_file -from helper import timeNowTZ, noti_obj +from helper import timeNowTZ, noti_obj, get_setting_value from notification import Notification_obj from database import DB @@ -70,7 +70,7 @@ def main(): #------------------------------------------------------------------------------- def check_config(): - if conf.APPRISE_URL == '' or conf.APPRISE_HOST == '': + if get_setting_value('APPRISE_URL') == '' or get_setting_value('APPRISE_HOST') == '': return False else: return True @@ -82,15 +82,15 @@ def send(html, text): result = '' # limit = 1024 * 1024 # 1MB limit (1024 bytes * 1024 bytes = 1MB) - limit = conf.APPRISE_SIZE + limit = get_setting_value('APPRISE_SIZE') # truncate size - if conf.APPRISE_PAYLOAD == 'html': + if get_setting_value('APPRISE_PAYLOAD') == 'html': if len(msg.html) > limit: payloadData = msg.html[:limit] + "

(text was truncated)

" else: payloadData = msg.html - if conf.APPRISE_PAYLOAD == 'text': + if get_setting_value('APPRISE_PAYLOAD') == 'text': if len(msg.text) > limit: payloadData = msg.text[:limit] + " (text was truncated)" else: @@ -99,15 +99,15 @@ def send(html, text): # Define Apprise compatible payload (https://github.com/caronc/apprise-api#stateless-solution) _json_payload = { - "urls": conf.APPRISE_URL, + "urls": get_setting_value('APPRISE_URL'), "title": "Pi.Alert Notifications", - "format": conf.APPRISE_PAYLOAD, + "format": get_setting_value('APPRISE_PAYLOAD'), "body": payloadData } try: # try runnning a subprocess - p = subprocess.Popen(["curl","-i","-X", "POST" ,"-H", "Content-Type:application/json" ,"-d", json.dumps(_json_payload), conf.APPRISE_HOST], stdout=subprocess.PIPE, stderr=subprocess.STDOUT) + p = subprocess.Popen(["curl","-i","-X", "POST" ,"-H", "Content-Type:application/json" ,"-d", json.dumps(_json_payload), get_setting_value('APPRISE_HOST')], stdout=subprocess.PIPE, stderr=subprocess.STDOUT) stdout, stderr = p.communicate() # write stdout and stderr into .log files for debugging if needed diff --git a/pialert/__main__.py b/pialert/__main__.py index fd01c6b6..8a07f430 100755 --- a/pialert/__main__.py +++ b/pialert/__main__.py @@ -154,7 +154,7 @@ def main (): notiStructure = get_notifications(db) # Write the notifications into the DB - notification = Notification_obj(db) + notification = Notification_obj(db) hasNotification = notification.create(notiStructure.json, notiStructure.text, notiStructure.html, "") # run all enabled publisher gateways @@ -162,6 +162,21 @@ def main (): pluginsState = run_plugin_scripts(db, 'on_notification', pluginsState) notification.setAllProcessed() + # Clean Pending Alert Events + sql.execute ("""UPDATE Devices SET dev_LastNotification = ? + WHERE dev_MAC IN ( + SELECT eve_MAC FROM Events + WHERE eve_PendingAlertEmail = 1 + ) + """, (timeNowTZ(),) ) + sql.execute ("""UPDATE Events SET eve_PendingAlertEmail = 0 + WHERE eve_PendingAlertEmail = 1""") + + # clear plugin events + sql.execute ("DELETE FROM Plugins_Events") + + # DEBUG - print number of rows updated + mylog('minimal', ['[Notification] Notifications changes: ', sql.rowcount]) # Commit SQL db.commitDB() diff --git a/pialert/reporting.py b/pialert/reporting.py index 7cf512a4..14d84b1d 100755 --- a/pialert/reporting.py +++ b/pialert/reporting.py @@ -315,22 +315,6 @@ def get_notifications (db): # else : # mylog('verbose', ['[Notification] No changes to report']) - # # Clean Pending Alert Events - # sql.execute ("""UPDATE Devices SET dev_LastNotification = ? - # WHERE dev_MAC IN (SELECT eve_MAC FROM Events - # WHERE eve_PendingAlertEmail = 1) - # """, (datetime.datetime.now(conf.tz),) ) - # sql.execute ("""UPDATE Events SET eve_PendingAlertEmail = 0 - # WHERE eve_PendingAlertEmail = 1""") - - # # clear plugin events - # sql.execute ("DELETE FROM Plugins_Events") - - # # DEBUG - print number of rows updated - # mylog('minimal', ['[Notification] Notifications changes: ', sql.rowcount]) - - # # Commit changes - # db.commitDB() #-------------------------------------------------------------------------------