-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathuLog.pas
290 lines (253 loc) · 5.72 KB
/
uLog.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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
unit uLog;
interface
uses
// Delphi
System.Classes,
Generics.Collections,
// FireDACE
FireDAC.Comp.Client,
// Project
uDatabase;
type
TLogItem = class
private
FIdent : Integer;
FUnits : Integer;
FInsulin : Integer;
FDateTime: TDateTime;
public
constructor Create(AIdent, AUnits, AInsulin: Integer; ADateTime: TDateTime);
property Ident : Integer read FIdent;
property Units : Integer read FUnits;
property Insulin : Integer read FInsulin;
property DateTime: TDateTime read FDateTime;
end;
TLog = class
private
FQuery: TFDQuery;
FDatabase: TDatabase;
FOnChange: TNotifyEvent;
procedure Change;
function GetQuery: TFDQuery;
function GetDatabase: TDatabase;
procedure AfterConnect(Sender: TObject);
property Query: TFDQuery read GetQuery;
property Database: TDatabase read GetDatabase;
public
constructor Create;
destructor Destroy; override;
function GetItems: TObjectList<TLogItem>;
procedure Add(nUnits, InsulinIdent: Integer);
procedure Delete(AIndex: Integer);
procedure DeleteExpired;
function IOB: REAL;
function Remaining: TDateTime;
property OnChange: TNotifyEvent read FOnChange write FOnChange;
end;
implementation
uses
// Delphi
System.SysUtils,
System.DateUtils,
// Project
uSettings,
uInsulins;
{ TLogItem }
constructor TLogItem.Create(AIdent, AUnits, AInsulin: Integer; ADateTime: TDateTime);
begin
inherited Create;
FIdent := AIdent;
FUnits := AUnits;
FInsulin := AInsulin;
FDateTime := ADateTime;
end;
{ TLog }
constructor TLog.Create;
begin
inherited;
FQuery := nil;
FDatabase := nil;
end;
destructor TLog.Destroy;
begin
if Assigned(FQuery) then
try
if FQuery.Active then
FQuery.Close;
finally
FQuery.Free;
end;
if Assigned(FDatabase) then
FDatabase.Free;
inherited Destroy;
end;
function TLog.GetDatabase: TDatabase;
begin
if not Assigned(FDatabase) then
begin
FDatabase := TDatabase.Create;
FDatabase.Connection.AfterConnect := AfterConnect;
end;
Result := FDatabase;
end;
function TLog.GetQuery: TFDQuery;
begin
if not Assigned(FQuery) then
begin
FQuery := TFDQuery.Create(nil);
FQuery.Connection := Database.Connection;
FQuery.Open('SELECT * FROM LOG');
end;
Result := FQuery;
end;
procedure TLog.AfterConnect;
begin
if not Database.TableExists('LOG') then
Database.Execute('CREATE TABLE LOG (ID INTEGER PRIMARY KEY, DATETIME REAL, UNITS INTEGER, TYPE INTEGER);');
end;
function TLog.GetItems: TObjectList<TLogItem>;
begin
Result := TObjectList<TLogItem>.Create;
Query.First;
while not Query.EOF do
begin
Result.Add(TLogItem.Create(
Query.FieldByName('ID').AsInteger,
Query.FieldByName('UNITS').AsInteger,
Query.FieldByName('TYPE').AsInteger,
Query.FieldByName('DATETIME').AsFloat
));
Query.Next;
end;
end;
procedure TLog.Change;
begin
if Assigned(FOnChange) then FOnChange(Self);
end;
procedure TLog.Add(nUnits, InsulinIdent: Integer);
begin
if Database.Execute(Format('INSERT INTO LOG (DATETIME, UNITS, TYPE) VALUES (%s, %d, %d);', [FloatToStr(Now, FormatSettingsFloat), nUnits, InsulinIdent])) > 0 then
begin
Query.Refresh;
Change;
end;
end;
procedure TLog.Delete(AIndex: Integer);
var
LL: TObjectList<TLogItem>;
begin
if AIndex > -1 then
begin
LL := GetItems;
try
if AIndex < LL.Count then
if Database.Execute(Format('DELETE FROM LOG WHERE ID = %d;', [LL[AIndex].Ident])) > 0 then
begin
Query.Refresh;
Change;
end;
finally
LL.Free;
end;
end;
end;
procedure TLog.DeleteExpired;
var
B : Boolean;
ID: Integer;
I : TInsulin;
DT: TDateTime;
begin
B := False;
Query.First;
while not Query.EOF do
begin
ID := Query.FieldByName('TYPE').AsInteger;
if ID > 0 then
begin
I := Insulins.GetItemByIdent(ID);
if Assigned(I) then
try
DT := IncMinute(Query.FieldByName('DATETIME').AsFloat, I.Duration);
if Now > DT then
begin
Query.Delete;
B := True;
CONTINUE;
end;
finally
I.Free;
end;
end;
Query.Next;
end;
if B then
Change;
end;
function TLog.IOB: REAL;
var
I: TInsulin;
DT1, DT2: TDateTime;
nUnits, nID: Integer;
nDelta1, nDelta2: Integer;
begin
Result := 0;
Query.First;
while not Query.EOF do
begin
nUnits := Query.FieldByName('UNITS').AsInteger;
if nUnits > 0 then
begin
nID := Query.FieldByName('TYPE').AsInteger;
if nID > 0 then
begin
I := Insulins.GetItemByIdent(nID);
if Assigned(I) then
try
DT1 := Query.FieldByName('DATETIME').AsFloat;
DT2 := IncMinute(DT1, I.Duration);
nDelta1 := MinutesBetween(DT2, DT1);
nDelta2 := MinutesBetween(DT2, Now) + 1;
Result := Result + ((nUnits / nDelta1) * nDelta2);
finally
I.Free;
end;
end;
end;
Query.Next;
end;
end;
function TLog.Remaining: TDateTime;
var
I: TInsulin;
Delta: Double;
DT1, DT2: TDateTime;
nUnits, nID: Integer;
begin
Result := 0;
Query.First;
while not Query.EOF do
begin
nUnits := Query.FieldByName('UNITS').AsInteger;
if nUnits > 0 then
begin
nID := Query.FieldByName('TYPE').AsInteger;
if nID > 0 then
begin
I := Insulins.GetItemByIdent(nID);
if Assigned(I) then
try
DT1 := Query.FieldByName('DATETIME').AsFloat;
DT2 := IncMinute(DT1, I.Duration);
Delta := IncMinute(DT2 - Now, 1);
if Delta > Result then
Result := Delta;
finally
I.Free;
end;
end;
end;
Query.Next;
end;
end;
end.