-
Notifications
You must be signed in to change notification settings - Fork 6
/
alarms.pas
232 lines (204 loc) · 4.94 KB
/
alarms.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
unit alarms;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, CustomTimer;
type
TAlarmEvent = procedure of object;
TAlarm = class(TObject)
private
FEnabled : Boolean;
FRunOnce : Boolean;
FOnExecute : TAlarmEvent;
public
property Enabled: Boolean read FEnabled write FEnabled;
property RunOnce: Boolean read FRunOnce write FRunOnce default False;
property OnExecute: TAlarmEvent read FOnExecute write FOnExecute;
end;
{ TIntervalAlarm }
TIntervalAlarm = class(TAlarm)
private
FInterval: Cardinal;
FNext: Cardinal;
public
constructor Create(Interval: Cardinal; AOnExecute: TAlarmEvent = nil);
destructor Destroy; override;
end;
{ TTimeAlarm }
TTimeAlarm = class(TAlarm)
private
FHour, FMinute: Word;
FExecuted: Boolean;
public
constructor Create(Hour: Word; Minute: Word; AOnExecute: TAlarmEvent = nil);
destructor Destroy; override;
end;
{ TAlarmManager }
TAlarmManager = class(TObject)
private
FIntervalAlarms: TList;
FTimeAlarms: TList;
FTimer: TCustomTimer;
FActive: Boolean;
FMode: Byte;
FInterval: Int64;
procedure CheckIntervalAlarms;
procedure CheckTimeAlarms;
procedure HandleTimer(Sender: TObject);
procedure Clear;
public
constructor Create;
destructor Destroy; override;
//
property Active: Boolean read FActive write FActive;
end;
var
AlarmManager: TAlarmManager;
implementation
{ TIntervalAlarm }
constructor TIntervalAlarm.Create(Interval: Cardinal; AOnExecute: TAlarmEvent);
begin
FInterval := Interval;
FNext := AlarmManager.FInterval + FInterval;
FEnabled := True;
FOnExecute := AOnExecute;
AlarmManager.FIntervalAlarms.Add(Self);
end;
destructor TIntervalAlarm.Destroy;
begin
FEnabled := False;
AlarmManager.FIntervalAlarms.Remove(Self);
inherited Destroy;
end;
{ TTimeAlarm }
constructor TTimeAlarm.Create(Hour: Word; Minute: Word; AOnExecute: TAlarmEvent);
begin
FEnabled := True;
FHour := Hour;
FMinute := Minute;
FOnExecute := AOnExecute;
AlarmManager.FTimeAlarms.Add(Self);
end;
destructor TTimeAlarm.Destroy;
begin
FEnabled := False;
if Assigned(AlarmManager) then
AlarmManager.FTimeAlarms.Remove(Self);
inherited Destroy;
end;
{ TAlarmManager }
constructor TAlarmManager.Create;
begin
FIntervalAlarms := TList.Create;
FTimeAlarms := TList.Create;
FTimer := TCustomTimer.Create(nil);
FTimer.Interval := 500;
FTimer.OnTimer := @HandleTimer;
FTimer.Enabled := True;
FMode := 0;
FInterval := 0;
FActive :=False;
end;
destructor TAlarmManager.Destroy;
begin
Clear;
FActive := False;
FTimer.Enabled := False;
FTimer.Free;
FIntervalAlarms.Free;
FTimeAlarms.Free;
inherited Destroy;
end;
procedure TAlarmManager.Clear;
var
X: Integer;
O: TObject;
begin
for X := FTimeAlarms.Count - 1 downto 0 do
begin
O := TObject(FTimeAlarms[X]);
if Assigned(O) then
O.Free;
end;
FTimeAlarms.Clear;
for X := FIntervalAlarms.Count - 1 downto 0 do
begin
O := TObject(FIntervalAlarms[X]);
if Assigned(O) then
O.Free;
end;
FIntervalAlarms.Clear;
end;
procedure TAlarmManager.CheckIntervalAlarms;
var
X: Integer;
A: TIntervalAlarm;
begin
FMode := 1;
for X := FIntervalAlarms.Count - 1 downto 0 do
begin
A := TIntervalAlarm(FIntervalAlarms[X]);
if Assigned(A) then
begin
if (A.Enabled) and (A.FNext <= FInterval) then
begin
try
if Assigned(A.OnExecute) then
A.OnExecute;
finally
if A.RunOnce then
A.Free
else
A.FNext := A.FNext + A.FInterval
end;
end;
end;
end;
inc(FInterval);
end;
procedure TAlarmManager.CheckTimeAlarms;
var
X: Integer;
A: TTimeAlarm;
Hour,Minute,Second,MilliSecond: Word;
begin
FMode := 0;
if FTimeAlarms.Count = 0 then Exit;
DecodeTime(Now(),Hour,Minute,Second,Millisecond);
for X := FTimeAlarms.Count - 1 downto 0 do
begin
A := TTimeAlarm(FTimeAlarms[X]);
if Assigned(A) then
begin
if (A.Enabled) and (A.FHour = Hour) and (A.FMinute = Minute) then
begin
if (not A.FExecuted) then
begin
A.FExecuted := True;
try
if Assigned(A.OnExecute) then
A.OnExecute;
finally
if A.RunOnce then
A.Free;
end;
end;
end
else
A.FExecuted := False;
end;
end;
end;
procedure TAlarmManager.HandleTimer(Sender: TObject);
begin
if FActive then
case FMode of
0: CheckIntervalAlarms;
1: CheckTimeAlarms;
end;
end;
initialization
AlarmManager := TAlarmManager.Create;
finalization
AlarmManager.Destroy;
end.