From c49f244ca2c2490c7f0e4caea21a07a6f7cb1455 Mon Sep 17 00:00:00 2001 From: Richard Frith-Macdonald Date: Sun, 19 Dec 2021 11:02:02 +0000 Subject: [PATCH] Allow lookup for a recent match of an alarm --- ChangeLog | 7 +++++++ EcAlarmDestination.h | 8 ++++++++ EcAlarmDestination.m | 33 +++++++++++++++++++++++++++++++++ 3 files changed, 48 insertions(+) diff --git a/ChangeLog b/ChangeLog index cfe94e4..2480c45 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,10 @@ +2021-12-19 Richard Frith-Macdonald + + * EcAlarmDestination.h: + * EcAlarmDestination.m: + Add (-latest:) method to find the most recent match for an alarm + in the queue/active/cleared data structures. + 2021-12-16 Richard Frith-Macdonald * EcCommand.m: diff --git a/EcAlarmDestination.h b/EcAlarmDestination.h index f4b4e3f..cd5224f 100644 --- a/EcAlarmDestination.h +++ b/EcAlarmDestination.h @@ -184,6 +184,14 @@ */ - (BOOL) isRunning; +/** Finds and returns the most recent alarm in the system which matches + * (is equal to) toFind. This searches the queue of alarms to be processed, + * the set of active alarms, and the set of cleared alarms (in that order) + * returning the first match found. If no match is found the method + * returns nil. + */ +- (EcAlarm*) latest: (EcAlarm*)toFind; + /** Returns an array containing the known managed objects. */ - (NSArray*) managed; diff --git a/EcAlarmDestination.m b/EcAlarmDestination.m index b934835..9a3bc36 100644 --- a/EcAlarmDestination.m +++ b/EcAlarmDestination.m @@ -374,6 +374,39 @@ return result; } +- (EcAlarm*) latest: (EcAlarm*)toFind +{ + EcAlarm *found = nil; + NSUInteger index; + + [_alarmLock lock]; + /* First try to find the most recent match in the queue + */ + index = [_alarmQueue count]; + while (index-- > 0) + { + EcAlarm *a = [_alarmQueue objectAtIndex: index]; + + if ([a isEqual: toFind]) + { + found = RETAIN(a); + break; + } + } + if (nil == found) + { + /* Not in queue ... try active alarms or clears + */ + found = RETAIN([_alarmsActive member: toFind]); + if (nil == found) + { + found = RETAIN([_alarmsCleared member: toFind]); + } + } + [_alarmLock unlock]; + return AUTORELEASE(found); +} + - (NSArray*) managed { NSArray *a;