diff --git a/alarm.py b/alarm.py index f0bfdaf..638a1db 100644 --- a/alarm.py +++ b/alarm.py @@ -5,11 +5,15 @@ class alarm: def __init__(self, date, age, standort, message, quelle, bestaetigtVon, kategorie, status): self.date = date - #self.time = time self.age = age + #self.oldAge = None + self.stillActive = False self.standort = standort self.message = message self.quelle = quelle self.bestaetigtVon = bestaetigtVon self.kategorie = kategorie self.status = status + + self.firstNotification = False + self.reminderNotification = 0 diff --git a/wc_checkAlarms.py b/wc_checkAlarms.py index 0a8e3f8..d12113b 100644 --- a/wc_checkAlarms.py +++ b/wc_checkAlarms.py @@ -125,7 +125,7 @@ def main(args): sys.exit(1) checkForNewAlarmsInterval = interval - takeActionDefault = 10.0 + takeActionDefault = 20.0 errorMessage = "No time to take action specified. Provide one via '-a my_takeAction' or {..., 'takeAction':'my_takeAction', ...} " \ "or WC_ALARMS_TAKEACTION=my_takeAction. Using a default now: " + str(takeActionDefault) + " minutes" try: @@ -240,6 +240,7 @@ def main(args): # the check/solution to a prior alarm with status e.g. 'unnormal': removedAlarm = False for a in alarmsActive: + if a.quelle == quelle: # Remove alarm from running and do not append to checked removedAlarm = True @@ -258,11 +259,16 @@ def main(args): # This alarm is already known; move on to the next alarm alarmAlreadyKnown = True # Observe the age of the alarm, i.e, increase the age by the time interval in which new alarms are caught + #a.oldAge = a.age a.age += checkForNewAlarmsInterval + a.stillActive = True if not alarmAlreadyKnown: if verbosity: print('Appending alarm ' + str(date) + ' ' + quelle + ' to alarmsActive') age = 0.0 - alarmsActive.append(alarm(date, age, standort, message, quelle, bestaetigtVon, kategorie, status)) + #alarmsActive.append(alarm(date, age, standort, message, quelle, bestaetigtVon, kategorie, status)) + a = alarm(date, age, standort, message, quelle, bestaetigtVon, kategorie, status) + a.stillActive = True + alarmsActive.append(a) if verbosity: if alarmsChecked: @@ -273,18 +279,47 @@ def main(args): if alarmsActive: for a in alarmsActive: + + # Remove "Zombi alarms": + #if a.age == a.oldAge: + if not a.stillActive: + print("This alarm seems to have been solved and deleted so we're removing it from the list of active alarms:") + print(a) + alarmsActive.remove(a) + continue + if verbosity: print(a.date, a.standort, a.message, a.quelle, a.bestaetigtVon, a.kategorie, a.status) print 'Age: ' + str(a.age) + print 'a.reminderNotification= ' + str(a.reminderNotification) + print 'a.firstNotification= ' + str(a.firstNotification) + + numberOfReminders = 3 + if not a.firstNotification: + # The first notification mail about an alarm + a.firstNotification = True + a.reminderNotification = 0 + # Check the age of the still active alarms and take action in case of, for example, the age is greater than e.g. 20 min - if a.age >= takeActionTime: + #if a.age >= takeActionTime: + if a.age >= takeActionTime and (a.age % takeActionTime == 0) and a.reminderNotification < numberOfReminders+1: currentHour = datetime.datetime.now().hour mailAddress = getMailAddress(currentHour, settings['WC_ALARMS_ALERT_EMAIL'], settings['WC_ALARMS_ALERT_PERIODS']) if verbosity: print 'Sending an e-mail to ' + mailAddress sendEmail(mailAddress, a.date, a.standort, a.message, a.quelle) + + if a.firstNotification and (a.age % takeActionTime == 0) and 0 <= a.reminderNotification < numberOfReminders + 1: + # Only send a small amount of reminder mails about an alarm because we don't want the recipient to get spamed + a.reminderNotification += 1 + + for a in alarmsActive: + a.stillActive = False + else: if verbosity: print('No alarms in list alarmsActive') + if verbosity: print('-----------------------------------------------------------------------------------------') + # sleep for n minutes sleep(checkForNewAlarmsInterval*60)