-
Notifications
You must be signed in to change notification settings - Fork 1
/
Backend.pas
132 lines (114 loc) · 3.11 KB
/
Backend.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
unit Backend;
interface
uses
Classes, httpsend, ssl_openssl,
uLkJSON, DateUtils, Windows, SysUtils, Forms;
type
TBackend = class
protected
FJSON: string;
FOpen: boolean;
FStatus: string;
FLastChange: TDateTime;
FError: boolean;
FErrorMsg: string;
FBackendURL: string;
OldLastChange: TDateTime;
FUserAgent: string;
private
resp: TStrings;
function GetHasChanged: boolean;
public
constructor Create(aBackendURL: string);
destructor Destroy; override;
procedure Read;
published
property JSON: string read FJSON;
property Status: string read FStatus;
property LastChange: TDateTime read FLastChange;
property Open: boolean read FOpen;
property HasChanged: boolean read GetHasChanged;
property Error: boolean read FError;
property ErrorMsg: string read FErrorMsg;
property BackendURL: string read FBackendURL write FBackendURL;
property UserAgent: string read FUserAgent writeFUserAgent;
end;
implementation
function GMTToLocalTime(GMTTime: TDateTime): TDateTime;
var
GMTST: Windows.TSystemTime;
LocalST: Windows.TSystemTime;
begin
SysUtils.DateTimeToSystemTime(GMTTime, GMTST);
SysUtils.Win32Check(Windows.SystemTimeToTzSpecificLocalTime(nil, GMTST, LocalST));
Result := SysUtils.SystemTimeToDateTime(LocalST);
end;
function HttpGetText(const URL: string; const Response: TStrings; const UserAgent: string): Boolean;
var
HTTP: THTTPSend;
begin
HTTP := THTTPSend.Create;
HTTP.UserAgent := UserAgent;
try
Result := HTTP.HTTPMethod('GET', URL);
if Result then
Response.LoadFromStream(HTTP.Document);
finally
HTTP.Free;
end;
end;
constructor TBackend.Create(aBackendURL: string);
begin
inherited Create;
resp := TStringList.Create;
FBackendURL := aBackendURL;
// Initial befüllen, dabei nicht gleich eine Änderung auslösen
Read;
GetHasChanged;
end;
destructor TBackend.Destroy;
begin
inherited Destroy;
end;
function TBackend.GetHasChanged: boolean;
begin
result := (FLastChange <> OldLastChange);
OldLastChange := FLastChange;
end;
procedure TBackend.Read;
var
js: TlkJSONobject;
UserAgent: string;
begin
if HttpGetText(FBackendURL, resp, FUserAgent) then begin
FError := false;
FErrorMsg := '';
if resp.Text = '' then begin
FError := true;
FErrorMsg := 'Keine Daten vom Server erhalten.';
exit;
end;
FJSON := resp.Text;
js := TlkJSON.ParseText(FJSON) as TlkJSONobject;
if not assigned(js) then begin
FError := true;
FErrorMsg := 'Keine JSON-Struktur erkannt.';
exit;
end;
FStatus := js.getString('status');
FOpen := js.getBoolean('open');
FLastChange := GMTToLocalTime(UnixToDateTime(js.getInt('lastchange')));
js.free;
if FLastChange = 0 then begin
FError := true;
FErrorMsg := 'JSON-Struktur fehlerhaft.';
exit;
end;
end else begin
FStatus := '';
FError := true;
FErrorMsg := 'HTTPS-Verbindung fehlgeschlagen.';
exit;
end;
end;
end.