-
Notifications
You must be signed in to change notification settings - Fork 1
/
umain.pas
352 lines (334 loc) · 13.3 KB
/
umain.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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
unit umain;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ComCtrls, ExtCtrls, Grids, DBGrids, StdCtrls, XPMan, DB,
ZAbstractRODataset, ZAbstractDataset, ZDataset, ZAbstractConnection,
ZConnection, IdIOHandler, IdIOHandlerSocket, IdIOHandlerStack, IdSSL,
IdSSLOpenSSL, IdBaseComponent, IdComponent, IdTCPConnection, IdTCPClient,
IdHTTP, uLkJSON;
type
TFrmMain = class(TForm)
StatusBar1: TStatusBar;
DBGrid1: TDBGrid;
Panel1: TPanel;
Button1: TButton;
Button2: TButton;
Button3: TButton;
Button4: TButton;
ZKoneksi: TZConnection;
ZData: TZQuery;
DsData: TDataSource;
Panel2: TPanel;
Button5: TButton;
Button6: TButton;
Edit1: TEdit;
IdHTTP1: TIdHTTP;
IdSSLIOHandlerSocketOpenSSL1: TIdSSLIOHandlerSocketOpenSSL;
MLog: TMemo;
Panel3: TPanel;
Image1: TImage;
Label2: TLabel;
Label3: TLabel;
Label4: TLabel;
Label5: TLabel;
Label6: TLabel;
Label7: TLabel;
Label8: TLabel;
Label9: TLabel;
Label10: TLabel;
Label11: TLabel;
Label12: TLabel;
Label13: TLabel;
Label14: TLabel;
Label15: TLabel;
Label16: TLabel;
Label17: TLabel;
Label18: TLabel;
Label19: TLabel;
Label20: TLabel;
Label21: TLabel;
Label22: TLabel;
Label23: TLabel;
Label24: TLabel;
Label25: TLabel;
Label26: TLabel;
Label27: TLabel;
Label28: TLabel;
Label29: TLabel;
Label30: TLabel;
Label31: TLabel;
Label32: TLabel;
Label33: TLabel;
Label34: TLabel;
Label35: TLabel;
Label36: TLabel;
Label37: TLabel;
LbRegion: TLabel;
LbTime: TLabel;
LbTempC: TLabel;
LbTempF: TLabel;
LbWindMph: TLabel;
LbWindKph: TLabel;
LbWindDegree: TLabel;
LbWindDir: TLabel;
LbPressureMb: TLabel;
LbPressureIn: TLabel;
LbPrecipMm: TLabel;
LbPrecipIn: TLabel;
LbHumadity: TLabel;
LbCloud: TLabel;
LbFeelsLikeC: TLabel;
LbFeelsLikeF: TLabel;
LbVisKm: TLabel;
LbVisMiles: TLabel;
Label1: TLabel;
Label38: TLabel;
LbName: TLabel;
Label39: TLabel;
Label40: TLabel;
LbCondition: TLabel;
Label41: TLabel;
Image2: TImage;
Label42: TLabel;
Label43: TLabel;
procedure FormCreate(Sender: TObject);
procedure Button6Click(Sender: TObject);
procedure Button5Click(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure Button4Click(Sender: TObject);
private
{ Private declarations }
procedure pagingrec(vhal: Integer);
public
{ Public declarations }
end;
var
FrmMain: TFrmMain;
posisi, tothal, hal: integer;
const
APIXU_URL = 'https://api.apixu.com/v1/';
APIXU_APIKEY = ''; //silahkan signup dulu di https://www.apixu.com/ untuk mendapatkan api key nya
rec_perhalaman: integer = 100;
implementation
uses Math, DateUtils, PNGImage;
{$R *.dfm}
procedure TFrmMain.pagingrec(vhal: Integer);
begin
Screen.Cursor := crHourGlass;
if (vhal >= 1) then
begin
posisi := (vhal - 1) * rec_perhalaman;
hal := vhal;
end
else
begin
posisi := 0;
hal := 1;
end;
//
with ZData do
begin
SQL.Text := 'SELECT COUNT(*) AS total FROM indonesia';
Open;
tothal := Ceil(Fields[0].AsInteger / rec_perhalaman);
Edit1.Text := 'Halaman Ke ' + IntToStr(hal) + ' Dari ' + IntToStr(tothal) + ' Halaman';
//
SQL.Text := 'SELECT * FROM indonesia ORDER BY id LIMIT ' + IntToStr(posisi) + ',' + IntToStr(rec_perhalaman);
Open;
end;
Screen.Cursor := crDefault;
end;
procedure TFrmMain.FormCreate(Sender: TObject);
begin
IdHTTP1.Request.Accept := 'application/json';
IdHTTP1.Request.ContentType := 'application/json';
IdHTTP1.Request.CharSet := 'UTF8';
IdHTTP1.Request.UserAgent := 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:12.0) Gecko/20100101 Firefox/12.0';
//configurasi koneksi ke database
with ZKoneksi do
begin
Protocol := 'mysql';
HostName := 'localhost';
Database := 'github';
Port := 3307;
User := 'root';
Password := '';
//proses koneksi ke database
Connect;
end;
pagingrec(0);
end;
procedure TFrmMain.Button6Click(Sender: TObject);
begin
if hal < tothal then
pagingrec(hal + 1)
else
pagingrec(tothal);
end;
procedure TFrmMain.Button5Click(Sender: TObject);
begin
if hal > 1 then
pagingrec(hal - 1)
else
pagingrec(0);
end;
procedure TFrmMain.Button1Click(Sender: TObject);
var
js: TlkJSONobject;
js_location, js_current: TlkJSONbase;
jsonresponse, imgday: string;
begin
try
jsonresponse := IdHTTP1.Get(APIXU_URL + 'current.json?key=' + APIXU_APIKEY + '&q=' + ZData.FieldByName('latitude').AsString + ',' + ZData.FieldByName('longitude').AsString);
js := TlkJSON.ParseText(jsonresponse) as TlkJSONobject;
js_location := js.Field['location'];
js_current := js.Field['current'];
//decode json parent field location
if js_location.Count > 0 then
begin
LbName.Caption := js_location.Field['name'].Value;
LbRegion.Caption := js_location.Field['region'].Value;
LbTime.Caption := js_location.Field['localtime'].Value;
end;
//decode json parent field current
if js_current.Count > 0 then
begin
LbCondition.Caption := js_current.Field['condition'].Field['text'].Value;
imgday := stringreplace(stringreplace(js_current.Field['condition'].Field['icon'].Value, '//cdn.apixu.com/', ExtractFilePath(Application.ExeName),[rfReplaceAll, rfIgnoreCase]),'/','\',[rfReplaceAll, rfIgnoreCase]);
Image1.Picture.LoadFromFile(imgday);//image day
Image2.Picture.LoadFromFile(stringreplace(imgday, 'day', 'night',[rfReplaceAll, rfIgnoreCase]));//image night
LbTempC.Caption := js_current.Field['temp_c'].Value;
LbTempF.Caption := js_current.Field['temp_f'].Value;
LbWindMph.Caption := js_current.Field['wind_mph'].Value;
LbWindKph.Caption := js_current.Field['wind_kph'].Value;
LbWindDegree.Caption := js_current.Field['wind_degree'].Value;
LbWindDir.Caption := js_current.Field['wind_dir'].Value;
LbPressureMb.Caption := js_current.Field['pressure_mb'].Value;
LbPressureIn.Caption := js_current.Field['pressure_in'].Value;
LbPrecipMm.Caption := js_current.Field['precip_mm'].Value;
LbPrecipIn.Caption := js_current.Field['precip_in'].Value;
LbHumadity.Caption := js_current.Field['humidity'].Value;
LbCloud.Caption := js_current.Field['cloud'].Value;
LbFeelsLikeC.Caption := js_current.Field['feelslike_c'].Value;
LbFeelsLikeF.Caption := js_current.Field['feelslike_f'].Value;
LbVisKm.Caption := js_current.Field['vis_km'].Value;
LbVisMiles.Caption := js_current.Field['vis_miles'].Value;
end;
//MLog.Lines.Add(jsonresponse);
except
on E: Exception do
begin
MLog.Lines.Add(E.ClassName + ': ' + e.Message);
end;
end;
end;
procedure TFrmMain.Button2Click(Sender: TObject);
var
js: TlkJSONobject;
js_location, js_forecast: TlkJSONbase;
jsonresponse, nextdate, imgday: string;
begin
try
nextdate := FormatDateTime('yyyy-mm-dd', IncDay(Today,1));
jsonresponse := IdHTTP1.Get(APIXU_URL + 'forecast.json?key=' + APIXU_APIKEY + '&q=' + ZData.FieldByName('latitude').AsString + ',' + ZData.FieldByName('longitude').AsString + '&dt=' + nextdate);
js := TlkJSON.ParseText(jsonresponse) as TlkJSONobject;
js_location := js.Field['location'];
js_forecast := js.Field['forecast'].Field['forecastday'].Child[0];
//decode json parent field location
if js_location.Count > 0 then
begin
LbName.Caption := js_location.Field['name'].Value;
LbRegion.Caption := js_location.Field['region'].Value;
end;
//decode json parent field forecast
if js_forecast.Count > 0 then
begin
LbTime.Caption := js_forecast.Field['date'].Value;
LbCondition.Caption := js_forecast.Field['day'].Field['condition'].Field['text'].Value;
imgday := stringreplace(stringreplace(js_forecast.Field['day'].Field['condition'].Field['icon'].Value, '//cdn.apixu.com/', ExtractFilePath(Application.ExeName),[rfReplaceAll, rfIgnoreCase]),'/','\',[rfReplaceAll, rfIgnoreCase]);
Image1.Picture.LoadFromFile(imgday);//image day
Image2.Picture.LoadFromFile(stringreplace(imgday, 'day', 'night',[rfReplaceAll, rfIgnoreCase]));//image night
LbTempC.Caption := 'max= ' + FloatToStr(js_forecast.Field['day'].Field['maxtemp_c'].Value) + '; min= ' + FloatToStr(js_forecast.Field['day'].Field['mintemp_c'].Value) + '; avg= ' + FloatToStr(js_forecast.Field['day'].Field['avgtemp_c'].Value);
LbTempF.Caption := 'max= ' + FloatToStr(js_forecast.Field['day'].Field['maxtemp_f'].Value) + '; min= ' + FloatToStr(js_forecast.Field['day'].Field['mintemp_f'].Value) + '; avg= ' + FloatToStr(js_forecast.Field['day'].Field['avgtemp_f'].Value);
LbWindMph.Caption := 'max= ' + FloatToStr(js_forecast.Field['day'].Field['maxwind_mph'].Value);
LbWindKph.Caption := 'max= ' + FloatToStr(js_forecast.Field['day'].Field['maxwind_kph'].Value);
LbWindDegree.Caption := '-';
LbWindDir.Caption := '-';
LbPressureMb.Caption := '-';
LbPressureIn.Caption := '-';
LbPrecipMm.Caption := 'total= ' + FloatToStr(js_forecast.Field['day'].Field['totalprecip_mm'].Value);
LbPrecipIn.Caption := 'total= ' + FloatToStr(js_forecast.Field['day'].Field['totalprecip_in'].Value);
LbHumadity.Caption := 'avg= ' + FloatToStr(js_forecast.Field['day'].Field['avghumidity'].Value);
LbCloud.Caption := '-';
LbFeelsLikeC.Caption := '-';
LbFeelsLikeF.Caption := '-';
LbVisKm.Caption := 'avg= ' + FloatToStr(js_forecast.Field['day'].Field['avgvis_km'].Value);
LbVisMiles.Caption := 'avg= ' + FloatToStr(js_forecast.Field['day'].Field['avgvis_miles'].Value);
end;
//MLog.Lines.Add(jsonresponse);
except
on E: Exception do
begin
MLog.Lines.Add(E.ClassName + ': ' + e.Message);
end;
end;
end;
procedure TFrmMain.Button3Click(Sender: TObject);
var
js: TlkJSONobject;
js_location, js_forecast: TlkJSONbase;
jsonresponse, nextdate, imgday: string;
begin
try
nextdate := FormatDateTime('yyyy-mm-dd', IncDay(Today,7));
jsonresponse := IdHTTP1.Get(APIXU_URL + 'forecast.json?key=' + APIXU_APIKEY + '&q=' + ZData.FieldByName('latitude').AsString + ',' + ZData.FieldByName('longitude').AsString + '&dt=' + nextdate);
js := TlkJSON.ParseText(jsonresponse) as TlkJSONobject;
js_location := js.Field['location'];
js_forecast := js.Field['forecast'].Field['forecastday'].Child[0];
//decode json parent field location
if js_location.Count > 0 then
begin
LbName.Caption := js_location.Field['name'].Value;
LbRegion.Caption := js_location.Field['region'].Value;
end;
//decode json parent field forecast
if js_forecast.Count-1 > 0 then
begin
LbTime.Caption := js_forecast.Field['date'].Value;
LbCondition.Caption := js_forecast.Field['day'].Field['condition'].Field['text'].Value;
imgday := stringreplace(stringreplace(js_forecast.Field['day'].Field['condition'].Field['icon'].Value, '//cdn.apixu.com/', ExtractFilePath(Application.ExeName),[rfReplaceAll, rfIgnoreCase]),'/','\',[rfReplaceAll, rfIgnoreCase]);
Image1.Picture.LoadFromFile(imgday);//image day
Image2.Picture.LoadFromFile(stringreplace(imgday, 'day', 'night',[rfReplaceAll, rfIgnoreCase]));//image night
LbTempC.Caption := 'max= ' + FloatToStr(js_forecast.Field['day'].Field['maxtemp_c'].Value) + '; min= ' + FloatToStr(js_forecast.Field['day'].Field['mintemp_c'].Value) + '; avg= ' + FloatToStr(js_forecast.Field['day'].Field['avgtemp_c'].Value);
LbTempF.Caption := 'max= ' + FloatToStr(js_forecast.Field['day'].Field['maxtemp_f'].Value) + '; min= ' + FloatToStr(js_forecast.Field['day'].Field['mintemp_f'].Value) + '; avg= ' + FloatToStr(js_forecast.Field['day'].Field['avgtemp_f'].Value);
LbWindMph.Caption := 'max= ' + FloatToStr(js_forecast.Field['day'].Field['maxwind_mph'].Value);
LbWindKph.Caption := 'max= ' + FloatToStr(js_forecast.Field['day'].Field['maxwind_kph'].Value);
LbWindDegree.Caption := '-';
LbWindDir.Caption := '-';
LbPressureMb.Caption := '-';
LbPressureIn.Caption := '-';
LbPrecipMm.Caption := 'total= ' + FloatToStr(js_forecast.Field['day'].Field['totalprecip_mm'].Value);
LbPrecipIn.Caption := 'total= ' + FloatToStr(js_forecast.Field['day'].Field['totalprecip_in'].Value);
LbHumadity.Caption := 'avg= ' + FloatToStr(js_forecast.Field['day'].Field['avghumidity'].Value);
LbCloud.Caption := '-';
LbFeelsLikeC.Caption := '-';
LbFeelsLikeF.Caption := '-';
LbVisKm.Caption := 'avg= ' + FloatToStr(js_forecast.Field['day'].Field['avgvis_km'].Value);
LbVisMiles.Caption := 'avg= ' + FloatToStr(js_forecast.Field['day'].Field['avgvis_miles'].Value);
end;
//MLog.Lines.Add(jsonresponse);
except
on E: Exception do
begin
MLog.Lines.Add(E.ClassName + ': ' + e.Message);
end;
end;
end;
procedure TFrmMain.Button4Click(Sender: TObject);
begin
close;
end;
end.