|
The DSPF for the City Weather web service demo.
* _______________________________________________________________________
* ___ _ _ __ __ _ _
* | . > ___ ___ _| |_ | |_ | \ \ ___ _ _ _| |_ <_>._ _
* | . \/ . \/ . \ | | | . | | |<_> || '_> | | | || ' |
* |___/\___/\___/ |_| |_|_| |_|_|_|<___||_| |_| |_||_|_|
*
* Nov, 2019 booth@martinvt.com
* _______________________________________________________________________
* Web demo of window with weather for various cities
*
* _______________________________________________________________________
DSPSIZ(*DS3)
ERRSFL
CA03 CA10
CHGINPDFT
* -----------------------------------------------------------------------
R FMT01
1 2'System i'
2 2'WEBCITIES'
HDG5X40 200 B 1 13CNTFLD(40) CHGINPDFT
DSPATR(PR)
67 COLOR(GRN)
61 COLOR(PNK)
62 COLOR(TRQ)
63 COLOR(WHT)
64 COLOR(BLU)
65 COLOR(RED)
66 COLOR(YLW)
HDG7X23 161 B 1 56CNTFLD(23) CHGINPDFT
DSPATR(PR)
61 COLOR(GRN)
62 COLOR(PNK)
63 COLOR(TRQ)
64 COLOR(WHT)
65 COLOR(BLU)
66 COLOR(RED)
67 COLOR(YLW)
S1USERNAME 50 6 3COLOR(YLW)
23 60'(F10=Delete Entry)' COLOR(BLU)
* -----------------------------------------------------------------------
R SFL1 SFL
S1TEXT 25 3 1
S1CODE 20 H
S1KEY 6S 0H
* -----------------------------------------------------------------------
R WIN01 SFLCTL(SFL1)
SFLPAG(0009)
SFLSIZ(&NBRRECS)
OVERLAY
WINDOW(8 4 12 29 *NORSTCSR +
*NOMSGLIN)
WDWTITLE((*TEXT +
'Cities, Country') +
(*COLOR TRQ) *LEFT *TOP)
WDWBORDER((*COLOR TRQ))
PROTECT
PUTOVR
MOUBTN(*ULP ENTER)
50 SFLCLR
N50 SFLDSP SFLDSPCTL SFLEND(*SCRBAR)
RTNCSRLOC(&PM_RCD &PM_FLD)
SFLCSRRRN(&RRNA)
RTNCSRLOC(&CSRRCD &CSRFLD)
CSRRCD 10 H
CSRFLD 10 H
SFLTOP1 4S 0H SFLRCDNBR(CURSOR *TOP)
SFLTOP2 5S 0H SFLSCROLL
PM_RCD 10 H
PM_FLD 10 H
SF1NUM 4S 0H
NBRRECS 5S 0P
RRNA 5S 0H
W1TEXT 25 B 2 1CHECK(LC) DSPATR(PC UL HI)
REDX 1 B 1 28DFTVAL('X') COLOR(RED) DSPATR(PR)
W1SVTEXT 25 H
W1CODE 20 H
* -----------------------------------------------------------------------
R WIN02
OVERLAY
WINDOW(8 42 8 28 *NOMSGLIN)
USRRSTDSP
WDWBORDER((*COLOR YLW))
WDWTITLE((*TEXT &W2TITLE) +
(*COLOR TRQ) +
*LEFT *TOP)
MOUBTN(*ULP ENTER)
PROTECT
PUTOVR
W2TITLE 25 P
1 2'Weather'
W2DESC 16 +3
2 2'Temperature'
W2TEMP 8 +7
3 2' - High'
W2HIGHTEMP 8 +11
4 2' - Low'
W2LOWTEMP 8 +12
5 2'Humidity'
W2HUMIDITY 6 +12
6 2'Country'
W2COUNTRY 2 +17
7 2'Long, Lat'
W2LONGLAT 16 +1
8 2'ID #'
W2ID 9 +13
* -----------------------------------------------------------------------
R DUMMY TEXT('Prevents previous screen-
from being cleared.')
ASSUME
5 9' '
|
The SQL RPG for City Weather web service demo .
// ______________________________________________________________________
// ___ _ _ __ __ _ _
// | . > ___ ___ _| |_ | |_ | \ \ ___ _ _ _| |_ <_>._ _
// | . \/ . \/ . \ | | | . | | |<_> || '_> | | | || ' |
// |___/\___/\___/ |_| |_|_| |_|_|_|<___||_| |_| |_||_|_|
//
// Nov, 2019 booth@martinvt.com
// ______________________________________________________________________
// Comments:
// Web demo of weather for various cities
// _____________________________________________________________
ctl-opt copyright('(C) Copyright 2019, All rights reserved.')
option(*nodebugio) dftactgrp(*no) actgrp(*new);
dcl-f WEBCITIESD workstn sfile(sfl1: sf1num);
dcl-ds *n PSDS;
USERID char(10) pos(358);
end-ds;
dcl-c cTrq x'30';
dcl-s wRefillSFL ind;
// weather info:
dcl-c cURL 'http://api.openweathermap.org/data/2.5/weather?';
dcl-c cAPIKEY 'ac100566d03d8617ea3442864df88bfe';
// Get your own API key; they're free.
// https://openweathermap.org/
dcl-s URL varchar(1024);
dcl-s wTemp packed(5: 2);
dcl-s wHumidity packed(3: 0);
dcl-s wHighTemp packed(5: 2);
dcl-s wLowTemp packed(5: 2);
dcl-s wID packed(10: 0);
dcl-s wLon packed(5: 2);
dcl-s wLat packed(5: 2);
dcl-s wCity varchar(25);
dcl-s wCountry char(2);
//====================================================================*
// SQL definitions (must be first lines of all sql lines in source.) *
//====================================================================*
exec sql set option
DatFmt = *ISO,
Commit = *None,
SrtSeq = *LangIDShr; // allows sort & search with upper/lower
exec sql declare C1 cursor for // List cities
select UNQ#, CITY, COUNTRY, ID
from WEBCITYP
where USERID = :USERID
order by UNQ#;
//====================================================================*
// MAINLINE-BEGIN *
//====================================================================*
exsr GetHeading;
// Display screen.
dow *inlr = *off;
exsr ChangeColors;
write WIN02;
exfmt WIN01;
if SFLTOP1 < NBRRECS;
SFLTOP1 = SFLTOP2; // Resets screen to same position.
endif;
select;
when *inkc or CSRFLD = 'REDX'; // exit/return
*inlr = *on;
when RRNA > 0;
exsr SFLSelected;
when W1TEXT <> W1SVTEXT;
exsr GetCityCode;
when W1CODE > '';
exsr FillWin02;
endsl;
enddo;
*inlr = *on;
//====================================================================*
// MAINLINE-END *
//====================================================================*
//-------------------------------* Sub-Routine *
// FillSFL() *---------------*
// Fill the Subfile with chosen records *
//-----------------------------------------------*
begsr FillSFL;
wReFillSFL = *off;
// Clear subfile.
*in50 = *on;
write WIN01;
*in50 = *off;
SF1NUM = *zero;
SFLTOP1 = 1;
// Fill SFL.
// Read the file.
exec sql open C1;
clear sqlcode;
dow sqlcode = 0;
exec sql fetch C1 into :S1KEY, :wCity, :wCountry, :wID;
if sqlcode = 0;
S1TEXT = %trim(wCity) + ', ' + %trim(wCountry);
S1CODE = %trim(%char(wID));
SF1NUM += 1;
write SFL1;
endif;
enddo;
exec sql close C1;
// If subfile is empty, make a line
if SF1NUM = *zero;
S1TEXT = '';
S1CODE = '';
SF1NUM += 1;
write SFL1;
endif;
NBRRECS = SF1NUM;
SFLTOP1 = 1; // sets screen to record 1
endsr;
//-------------------------------* Sub-Routine *
// Get a new City *---------------*
//-----------------------------------------------*
begsr GetCityCode;
clear W1CODE;
URL = cURL
+ 'q=' + %trim(W1TEXT)
+ '&appid=' + cAPIKEY;
exec sql select * into :wID
from json_table(systools.httpgetclob(:URL, ''),
'lax $'
columns(
ID dec(10) path '$.id') empty on error);
if sqlcode = 0;
W1CODE = %trim(%char(wID));
exsr GetCity;
else;
W2TITLE = 'Not Found';
exsr ClearWIN02;
endif;
endsr;
//-------------------------------* Sub-Routine *
// Get a City's weather *---------------*
//-----------------------------------------------*
begsr GetCity;
// Execute the webservice call.
URL = cURL
+ 'id=' + %trim(W1CODE)
+ '&appid=' + cAPIKEY;
exec sql select * into :wCity, :wCountry, :wID
from json_table(systools.httpgetclob(:URL, ''),
'lax $'
columns(
CITY varchar(50) path '$.name',
COUNTRY varchar(2) path '$.sys.country',
ID dec(10) path '$.id') empty on error);
if sqlcode = 0;
W1TEXT = %trim(wCity) + ', ' + %trim(wCOUNTRY);
W1SVTEXT = W1TEXT;
W1CODE = %trim(%char(wID));
exec sql
insert into WEBCITYP
(CITY, COUNTRY, ID, USERID)
values(:wCity, :wCountry, :wID, :USERID);
exsr FillSFL;
exsr FillWIN02;
endif;
endsr;
//-------------------------------* Sub-Routine *
// Subfile record selected *---------------*
//-----------------------------------------------*
begsr SFLSelected;
chain RRNA SFL1;
if *inkj;
exec sql
delete from WEBCITYP
where UNQ# = :S1KEY;
clear W1TEXT;
clear W1SVTEXT;
clear W1CODE;
W2TITLE = '';
exsr ClearWIN02;
exsr FillSFL;
else;
W1TEXT = S1TEXT;
W1CODE = S1CODE;
W1SVTEXT = W1TEXT;
exsr FillWin02;
endif;
endsr;
//-------------------------------* Sub-Routine *
// Clear WIN02 screen *---------------*
//-----------------------------------------------*
begsr ClearWIN02;
clear W2DESC;
clear W2TEMP;
clear W2HUMIDITY;
clear W2HIGHTEMP;
clear W2LOWTEMP;
clear W2COUNTRY;
clear W2ID;
clear W2LONGLAT;
endsr;
//-------------------------------* Sub-Routine *
// Fill WIN02 *---------------*
//-----------------------------------------------*
begsr FillWIN02;
// Execute the webservice call.
URL = cURL
+ 'id=' + %trim(W1CODE)
+ '&units=imperial'
+ '&appid=' + cAPIKEY;
exec sql select * into :W2TITLE, :W2DESC, :wTemp, :wHumidity,
:wHighTemp, :wLowTemp, :W2COUNTRY, :wID, :wLon, :wLat
from json_table(systools.httpgetclob(:URL, ''),
'lax $'
columns(
CITY varchar(50) path '$.name',
DESC varchar(50) path '$.weather.description',
TEMP dec(5,2) path '$.main.temp',
HUMIDITY dec(3,0) path '$.main.humidity',
HIGHTEMP dec(5,2) path '$.main.temp_max',
LOWTEMP dec(5,2) path '$.main.temp_min',
COUNTRY CHAR(2) path '$.sys.country',
ID dec(10) path '$.id',
LONGITUTE dec(5,2) path '$.coord.lon',
LATITUDE dec(5,2) path '$.coord.lat'
) empty on error
);
if SQLCODE = 0;
// W2TITLE = wCity;
evalr W2DESC = %trim(W2DESC);
evalr W2TEMP = %trim(%editc(wTemp: 'N')) + 'f';
evalr W2HUMIDITY = %trim(%char(wHumidity)) + '%';
evalr W2HIGHTEMP = %trim(%editc(wHighTemp: 'N')) + 'f';
evalr W2LOWTEMP = %trim(%editc(wLowTemp: 'N')) + 'f';
evalr W2COUNTRY = %trim(W2COUNTRY);
evalr W2ID = %trim(%char(wID));
evalr W2LONGLAT = %trim(%editc(wLon: 'N')) + ', '
+ %trim(%editc(wLat: 'N'));
else;
W2TITLE = 'Not Found';
exsr clearWIN02;
endif;
endsr;
//-------------------------------* Sub-Routine *
// GetHeading() *---------------*
//-----------------------------------------------*
begsr GetHeading;
HDG5X40 =
' _ __ __ '
+ ' | | /| / /__ / / -=____Weather____=-'
+ ' | |/ |/ / -_) _ \ -= Various =-'
+ ' |__/|__/\__/_.__/ -= Cities =-';
HDG7X23 =
' '
+ ' '
+ ' ,,, '
+ ' (O-O) '
+ ' ----oo0-(_)-0oo---- '
+ ' '
+ ' ';
exec SQL // Get user's name to display.
select CID.ODOBTX
into :S1USERNAME
from Table( QSYS2/USERS() ) AS CID
where CID.ODOBNM = :USERID;
evalr S1USERNAME = 'with' + cTrq + %trim(S1USERNAME);
// Fill screen with first city data
exsr FillSFL;
chain 1 SFL1;
W1TEXT = S1TEXT;
W1CODE = S1CODE;
W1SVTEXT = W1TEXT;
exsr FillWin02;
endsr;
//-------------------------------* Sub-Routine *
// Change Heading Colors *---------------*
//-----------------------------------------------*
begsr ChangeColors;
select;
when *in61;
*in61 = *off;
*in62 = *on;
when *in62;
*in62 = *off;
*in63 = *on;
when *in63;
*in63 = *off;
*in64 = *on;
when *in64;
*in64 = *off;
*in65 = *on;
when *in65;
*in65 = *off;
*in66 = *on;
when *in66;
*in66 = *off;
*in67 = *on;
other;
*in67 = *off;
*in61 = *on;
endsl;
write FMT01;
endsr;
|
The SQL RPG that creates the Cities file, individualized for each user.
// ______________________________________________________________________
// ___ _ _ __ __ _ _
// | . > ___ ___ _| |_ | |_ | \ \ ___ _ _ _| |_ <_>._ _
// | . \/ . \/ . \ | | | . | | |<_> || '_> | | | || ' |
// |___/\___/\___/ |_| |_|_| |_|_|_|<___||_| |_| |_||_|_|
//
// booth@martinvt.com
// ______________________________________________________________________
// Web, create a cities file for Weather series
// Nov, 2019
// ______________________________________________________________________
ctl-opt
copyright('(C) Copyright, 2019, All rights reserved.')
option(*nodebugio) dftactgrp(*no) actgrp(*new);
/free
*inlr = *on;
// The immediately following /EXEC SQL is SQL's version of RPG's H Spec
// It is never executed. Just used at compile time.
exec sql
Set Option
Commit = *None;
// Create WEBCITYP File
exec sql
create table MARTIN/WEBCITYP
(UNQ# numeric (6, 0)
generated always as identity
(start with 1, increment by 1, no cycle),
CITY char(20) not null,
COUNTRY char(2) not null,
ID numeric(10, 0),
USERID char(10) not null);
/end-free
|
|
|