Skip to content

Commit

Permalink
Read time zone from Nightscout data
Browse files Browse the repository at this point in the history
  • Loading branch information
Sergey Oleynikov committed Nov 30, 2019
1 parent b0028e6 commit 744f3ff
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 2 deletions.
8 changes: 8 additions & 0 deletions forms/ufMain.lfm
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ object fMain: TfMain
object miSetCheckInterval: TMenuItem
Action = actSetCheckInterval
end
object miSetTimeZoneCorrection: TMenuItem
Action = actSetTimeZoneCorrection
end
object miShowSettings: TMenuItem
Action = actShowSettings
end
Expand Down Expand Up @@ -634,6 +637,11 @@ object fMain: TfMain
Caption = 'Enable sound alarms'
OnExecute = actEnableAudioAlarmsExecute
end
object actSetTimeZoneCorrection: TAction
Category = 'Other'
Caption = 'Set TimeZone correction'
OnExecute = actSetTimeZoneCorrectionExecute
end
end
object TrayIcon: TTrayIcon
BalloonFlags = bfInfo
Expand Down
23 changes: 23 additions & 0 deletions forms/ufMain.pas
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ TfMain = class(TForm)
actEnableGlucoseLevelAlarms: TAction;
actEnableStaleDataAlarms: TAction;
actEnableAudioAlarms: TAction;
actSetTimeZoneCorrection: TAction;
actSnoozeAlarmsCustom: TAction;
actShowIconInTray: TAction;
actShowIconOnTaskbar: TAction;
Expand All @@ -46,6 +47,7 @@ TfMain = class(TForm)
actSnoozeAlarms30mins: TAction;
actSnoozeAlarms10mins: TAction;
actStayOnTop: TAction;
miSetTimeZoneCorrection: TMenuItem;
miEnableAudioAlarms: TMenuItem;
miEnableStaleDataAlarms: TMenuItem;
miEnableGlucoseLevelAlarms: TMenuItem;
Expand Down Expand Up @@ -148,6 +150,7 @@ TfMain = class(TForm)
procedure actEnableGlucoseLevelAlarmsExecute(Sender: TObject);
procedure actEnableAudioAlarmsExecute(Sender: TObject);
procedure actEnableStaleDataAlarmsExecute(Sender: TObject);
procedure actSetTimeZoneCorrectionExecute(Sender: TObject);
procedure actShowIconInTrayExecute(Sender: TObject);
procedure actShowIconOnTaskbarExecute(Sender: TObject);
procedure alUpdate(AAction: TBasicAction; var Handled: Boolean);
Expand Down Expand Up @@ -985,6 +988,26 @@ procedure TfMain.actEnableStaleDataAlarmsExecute(Sender: TObject);
Settings.EnableStaleDataAlarms := TAction(Sender).Checked;
end;

procedure TfMain.actSetTimeZoneCorrectionExecute(Sender: TObject);
const
cMsg = 'Type in time zone correction (in hours). Sometimes it is needed when Nightscout site is not set up correctly.';
var
TimeZoneCorrectionStr, Msg: string;
begin
al.State := asSuspended;
TimeZoneCorrectionStr := IntToStr(Settings.TimeZoneCorrection);
if TfTimerDialog.Execute(Self, 'Time zone correction', cMsg, TimeZoneCorrectionStr, [pbOK, pbCancel]) = mrOK then
begin
if not TryStrToInt(TimeZoneCorrectionStr, Settings.TimeZoneCorrection) then
begin
Msg := 'You must type in time zone correction in hours (int value)';
if MessageDlg(Msg, mtWarning, [mbYes, mbCancel], -1) = mrYes then
actSetTimeZoneCorrectionExecute(Sender);
end;
end;
al.State := asNormal;
end;

procedure TfMain.FormMouseEnter(Sender: TObject);
begin
WasAlphaBlend := AlphaBlend;
Expand Down
18 changes: 16 additions & 2 deletions units/uNightscout.pas
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ interface
{ TNightscoutEntry }

TNightscoutEntry = class
private
TimeZoneForParsing: Integer;
public
Date: TDateTime;
Glucose: Integer;
Expand Down Expand Up @@ -237,6 +239,18 @@ function TNightscoutEntry.GetArrowCountOfSlope(): Byte;
end;

function TNightscoutEntry.ParseRow(Row: string): Boolean;

procedure ParseTimeZoneFromDateTime(DateText: string);
var
PlusPos: Integer;
TimeZoneStr: String;
begin
// Value = '2019-11-30T22:10:23.684+0400'
PlusPos := Pos('+', DateText) + 1;
TimeZoneStr := Copy(DateText, PlusPos, Length(DateText) - PlusPos - 1);
TimeZoneForParsing := StrToInt(TimeZoneStr);
end;

var
Columns: TStringList;
i: Integer;
Expand All @@ -252,7 +266,7 @@ function TNightscoutEntry.ParseRow(Row: string): Boolean;
begin
Value := Columns[i];
case i of
0: Continue;
0: ParseTimeZoneFromDateTime(Value);
1: SetDate(Value);
2: SetGlucose(Value);
3: Slope := Value;
Expand All @@ -274,7 +288,7 @@ procedure TNightscoutEntry.SetDate(UnixDateStr: string);
// Remove microsecs
UnixDateStr := Copy(UnixDateStr, 1, 10);
UnixDate := StrToInt64(UnixDateStr);
Date := UnixToDateTime(UnixDate);
Date := UnixToDateTime(UnixDate) + TimeZoneForParsing / HoursPerDay;
end;

procedure TNightscoutEntry.SetGlucose(Value: Double; IsMmolL: Boolean);
Expand Down

0 comments on commit 744f3ff

Please sign in to comment.