mirror of
https://github.com/jokob-sk/NetAlertX.git
synced 2025-12-07 09:36:05 -08:00
Notification rework v0.4
This commit is contained in:
@@ -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] + "<h1>(text was truncated)</h1>"
|
||||
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
|
||||
|
||||
@@ -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()
|
||||
|
||||
@@ -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()
|
||||
|
||||
|
||||
#-------------------------------------------------------------------------------
|
||||
|
||||
Reference in New Issue
Block a user