AS/400 fans only ' Take it for fun - How to download data from a Web Service - Weather report from openweathermap.org

Weather report from openweathermap.org


 


first of all get your own API key on https://openweathermap.org/ , it's free.

The Key looks like '35a545c6ce3bda8347f588353aa59fbd'



Compile and run the program below to create the WBSWL00F table that will contain the weather data for a location, each row one location.

WBSWL00F.SQLRPGLE
      **free
      *********************************
      * create WBSWL00F Location list *
      *********************************
       ctl-opt
       option(*nodebugio) dftactgrp(*no) actgrp(*new);

        // The EXEC SQL is never executed. It is used at compile time.
        exec sql Set Option Commit = *None;

        // Create File
        exec sql
          create table WBSWL00F
            (
               WWIDNM numeric (9, 0) generated always as identity,
               WWORIN TIMESTAMP NOT NULL DEFAULT CURRENT TIMESTAMP,
               WWORCR TIMESTAMP GENERATED ALWAYS FOR EACH ROW ON UPDATE
                    AS ROW CHANGE TIMESTAMP NOT NULL,
               WWANN0   CHARACTER(1) NOT NULL WITH DEFAULT,
               WWLOC0       char(20) NOT NULL WITH DEFAULT,
               WWIDL0 numeric(10, 0) NOT NULL WITH DEFAULT,
               WWNAZ0       char(02) NOT NULL WITH DEFAULT,
               WWUSR0       char(10) NOT NULL WITH DEFAULT,
               WWTMP0 numeric(05, 2) NOT NULL WITH DEFAULT,
               WWDTA0 DATE DEFAULT CURRENT_DATE,
               WWTME0 TIME DEFAULT CURRENT_TIME,
               WWLAT0 numeric(06, 4) NOT NULL WITH DEFAULT,
               WWLON0 numeric(07, 4) NOT NULL WITH DEFAULT
            )
           RCDFMT WBSWL;
        *inlr = *on;
This is a RPGfree source to create the Display file.

Compile.
WBSW10V.DSPF 
                                            DSPSIZ(*DS4)
                                            CHGINPDFT(CS UL HI)
                                            INDARA
                                            CA03(03 'F3=Exit')
                                            CA05(05 'F5=Reload subfile')
                                            REF(WBSWL00F)
      *---------------------------------------------------------
                R SFL1                      SFL
      *---------------------------------------------------------
                  S1IDNM    R        H      REFFLD(WWIDNM)
                  S1LAT0    R        H      REFFLD(WWLAT0)
                  S1LON0    R        H      REFFLD(WWLON0)
                  S1OPT          1A  B  6  2
                  S1ANN0    R             +1REFFLD(WWANN0)
                  S1IDL0    R             +1REFFLD(WWIDL0) EDTWRD('         0')
                  S1LOC0    R             +1REFFLD(WWLOC0)
                  S1NAZ0    R             +1REFFLD(WWNAZ0)
                  S1TMP0    R             +1REFFLD(WWTMP0) EDTWRD(' 0 ,  -')
                  S1DTA0    R             +1REFFLD(WWDTA0)
                  S1TME0    R             +1REFFLD(WWTME0)
                  S1GOO0        51A       +1
      *---------------------------------------------------------
                R FMT01                     SFLCTL(SFL1)
      *---------------------------------------------------------
                                            SFLPAG(0015)
                                            SFLSIZ(&NBRRECS)
                                            OVERLAY
       N50                                  SFLDSP  SFLDSPCTL
        50                                  SFLCLR
        91                                  SFLEND(*MORE)
                                            RTNCSRLOC(&PM_RCD &PM_FLD)
                  PM_RCD        10A  H
                  PM_FLD        10A  H
                  SF1NUM         4S 0H
                  F1IDNM    R        H      REFFLD(WWIDNM)
                  NBRRECS        5S 0P
                                        1 21'Weather report from openweathermap-
                                            .org'
                                        2  3'New location:'
                  F1LOC0    R        B   + 1REFFLD(WWLOC0)
                  F1NAZ0    R        B    +1REFFLD(WWNAZ0)
                                          +1'<--type here a location & a nation-
                                             code, for example:'
                                          +1'NEW YORK' COLOR(YLW)
                                         +13'US' COLOR(YLW)
                  F1ANN0    R        B  3  4REFFLD(WWANN0)
                  F1IDL0    R        B    +1REFFLD(WWIDL0) EDTWRD('         0')
                                         + 1'<--Filters'

                                        4  2'2=Update' COLOR(BLU)
                                          +3'4/8=Activate/deactivate' COLOR(BLU)
                                          +3'9=Erase' COLOR(BLU)
                                        5  4'A'
                                          +1'LocationNb'
                                          +1'Location'
                                         +13'N.'
                                          +1'Temp.C '
                                          +4'Date'
                                          +6'Time'
                                         +21'DoubleClick this link'
      *---------------------------------------------------------
                R S1CMD
      *---------------------------------------------------------
                  S1MSG         78A    23  3
                                       24  3'F3=Exit' COLOR(BLU)
                                          +3'F5=Reload Subfile' COLOR(BLU) 



This is a RPGfree source to manage the WBSWL00F table.

Compile and Run.

WBSW10.SQLRPGLE
      **FREE
       ctl-opt option(*nodebugio:*srcstmt:*nounref) dftactgrp(*no);

       dcl-f WBSW10V workstn indds(Dspf) sfile(SFL1 : SF1NUM);

       dcl-c MaxSFLrec 9999 ;

       dcl-s ReLoadSFL  ind         inz(*on)    ;

       // String filters
       dcl-s F1ANN0lke  varchar(35) inz('%')    ;
       dcl-s F1LOC0lke  varchar(35) inz('%')    ;
       dcl-s F1NAZ0lke  varchar(35) inz('%')    ;
       dcl-s F1USR0lke  varchar(35) inz('%')    ;

       dcl-s F1ANN0prv  varchar(35) inz(*blanks);
       dcl-s F1LOC0prv  varchar(35) inz(*blanks);
       dcl-s F1NAZ0prv  varchar(35) inz(*blanks);
       dcl-s F1USR0prv  varchar(35) inz(*blanks);

       // Numeric filters
       dcl-s wF1IDNMstr zoned(09)   inz(*zeros);
       dcl-s wF1IDNMend zoned(09)   inz(*hival);
       dcl-s F1IDNMprv  zoned(09)   inz(*loval);
       dcl-s wF1IDL0str zoned(10)   inz(*zeros);
       dcl-s wF1IDL0end zoned(10)   inz(*hival);
       dcl-s F1IDL0prv  zoned(10)   inz(*loval);

       // Standalone
       dcl-s PPANN0 like(WWANN0);
       dcl-s PPLOC0 like(WWLOC0);
       dcl-s PPNAZ0 like(WWNAZ0);
       dcl-s PPIDL0 like(WWIDL0);
       dcl-s PPTMP0 like(WWTMP0);
       dcl-s PPDTA0 like(WWDTA0);
       dcl-s PPTME0 like(WWTME0);
       dcl-s PPLAT0 like(WWLAT0);
       dcl-s PPLON0 like(WWLON0);

       // Command keys indicators
       dcl-ds Dspf qualified ;
         Exit            ind pos(03) inz(*off);
         ReLoadSFL       ind pos(05) inz(*off);
         SflClr          ind pos(50) inz(*off);
         SflEnd          ind pos(91) inz(*off);
         end-ds ;

       //***********************************************************
       // weather info:
       dcl-c cURL 'http://api.openweathermap.org/data/2.5/weather?';
       dcl-c cAPIKEY '76a614c6ce3bda8347f588353aa59fbd';
       //  Get your own API key; they're free.
       //    https://openweathermap.org/
       // you can freely use my key, but you can also
       // get your own, it's free.

       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 wLat      packed(6: 4);
       dcl-s wLon      packed(7: 4);
       dcl-s wCity     varchar(25);
       dcl-s wCountry  char(2);
       //***********************************************************

       dcl-ds RecordDs;
         WWIDNM packed(09:0);
         WWANN0   char(  01);
         WWIDL0 packed(10:0);
         WWLOC0   char(  20);
         WWNAZ0   char(  02);
         WWTMP0 packed(05:2);
         WWDTA0 date;
         WWTME0 time;
         WWLAT0 packed(06:4);
         WWLON0 packed(07:4);
       end-ds;
       // Both "exec sql" is not executed but needed at compile time.
       // It must be placed just after the last "dcl" declaration.
       // LANGIDSHR defines the sort sequence as a "shared-weight sort table",
       // upper/lower insensitive, special characters are treated
       // "à" as "A"
       // "é" as "E" and so on.
       exec sql set option Commit = *None;
       exec sql set option SRTSEQ = *LANGIDSHR;
       // preapre the data recordset
       exec sql declare C1 cursor for
         select     WBSWL00F.WWIDNM,
                    WBSWL00F.WWANN0,
                    WBSWL00F.WWIDL0,
                    WBSWL00F.WWLOC0,
                    WBSWL00F.WWNAZ0,
                    WBSWL00F.WWTMP0,
                    WBSWL00F.WWDTA0,
                    WBSWL00F.WWTME0,
                    WBSWL00F.WWLAT0,
                    WBSWL00F.WWLON0
         from       WBSWL00F
         where      WWIDNM between :wF1IDNMstr and :wF1IDNMend
         and        WWANN0 like :F1ANN0lke
         and        WWIDL0 between :wF1IDL0str and :wF1IDL0end
         and        WWLOC0 like :F1LOC0lke
         and        WWNAZ0 like :F1NAZ0lke
         order by   WWIDL0
         for read only;
       //*********************************************************************
       // Start working...
       //*********************************************************************
       ReLoadSFL = *on;
       dow Dspf.Exit = *off;
         if ReLoadSFL;
           LoadSFL();
         endif;
         write S1CMD;
         exfmt FMT01;
         select;
         when Dspf.Exit;              // F3=Exit
           leave;
         when Dspf.ReLoadSFL;
           F1LOC0 = *blanks;
           F1NAZ0 = *blanks;
           LoadSFL();
         when F1LOC0 <> *blanks;
           webData();
         other; // Read user options
           dow 1 = 1;
             readc SFL1;
             if Not %eof;
               if S1OPT = '1';
                 // call a program or a subroutine
               endif;
               if S1OPT = '2';
                 // call a program or a subroutine
                 F1LOC0 = S1LOC0;
                 F1NAZ0 = S1NAZ0;
                 webData();
               endif;
               if S1OPT = '4';
                 PPANN0 = 'A';
                 exec sql
                 UPDATE WBSWL00F
                 SET WWANN0 = :PPANN0
                 WHERE WWIDNM = :S1IDNM;
                 S1ANN0 = 'A';
                 // call a program or a subroutine
               endif;
               if S1OPT = '8';
                 PPANN0 = ' ';
                 exec sql
                 UPDATE WBSWL00F
                 SET WWANN0 = :PPANN0
                 WHERE WWIDNM = :S1IDNM;
                 S1ANN0 = ' ';
                 // call a program or a subroutine
               endif;
               if S1OPT = '9' and S1ANN0 = 'A';
                 exec sql
                 DELETE from WBSWL00F
                 WHERE WWIDNM = :S1IDNM;
                 S1IDNM = *zeros;
                 S1ANN0 = *blanks;
                 S1IDL0 = *zeros;
                 S1LOC0 = *blanks;
                 S1NAZ0 = *blanks;
                 S1TMP0 = *zeros;
                 S1DTA0 = *loval;
                 S1TME0 = *loval;
                 S1LAT0 = *zeros;
                 S1LON0 = *zeros;
                 S1GOO0 = *blanks;
                 // call a program or a subroutine
               endif;
               S1OPT = '>';
               update SFL1;
               S1OPT = ' ';
             else;
               leave;
             endif;
           enddo;
         endsl;
         SetFilters();
       enddo;
       exec sql close C1;
       *inlr = *on;
       //*********************************************************************
       dcl-proc webData; // Load Location data from web service
       //*********************************************************************
         URL = cURL
             + 'q=' + %trim(F1LOC0) + ',' + %trim(F1NAZ0)
             + '&appid=' + cAPIKEY + '&units=metric';

         exec sql select * into :wCity, :wCountry, :wID, :wTemp,
                                :wLat,  :wLon
           from json_table(systools.httpgetclob(:URL, ''),
             'lax $'
             columns(
               CITY    varchar(50    ) path '$.name'            ,
               COUNTRY varchar( 2    ) path '$.sys.country'     ,
               ID          dec(10    ) path '$.id'              ,
               TEMP        dec( 5 , 2) path '$.main.temp'       ,
               LATITUDE    dec( 6 , 4) path '$.coord.lat'       ,
               LONGITUTE   dec( 7 , 4) path '$.coord.lon'
           ) empty on error
         );

         if sqlcode = 0;
           PPLOC0 = %trim(%char(wCity));
           F1LOC0 = %trim(%char(wCity));
           PPNAZ0 = %trim(%char(wCOUNTRY));
           F1NAZ0 = %trim(%char(wCOUNTRY));
           PPIDL0 = wID;
           PPTMP0 = wTEMP; // - 273,15; // 289 K - 273,15 = 15,85 °C
           PPDTA0 = %date();
           PPTME0 = %time();
           PPLAT0 = wLat;
           PPLON0 = wLon;

           S1TMP0 = PPTMP0;
           S1DTA0 = PPDTA0;
           S1TME0 = PPTME0;
           ReLoadSFL = *on;

           exec sql select WWIDL0 INTO :PPIDL0 from WBSWL00F
           WHERE WWIDL0 = :wID;
           if sqlcode = 0;
             exec sql
             UPDATE WBSWL00F
             SET WWLOC0 = :PPLOC0, WWNAZ0 = :PPNAZ0,
                 WWIDL0 = :PPIDL0, WWTMP0 = :PPTMP0,
                 WWDTA0 = :PPDTA0, WWTME0 = :PPTME0,
                 WWLAT0 = :PPLAT0, WWLON0 = :PPLON0
             WHERE WWIDL0 = :PPIDL0;
           else;
             exec sql
             INSERT INTO WBSWL00F ( WWLOC0,  WWNAZ0,  WWIDL0,
                                    WWTMP0,  WWDTA0,  WWTME0,
                                    WWLAT0,  WWLON0         )
                           VALUES (:PPLOC0, :PPNAZ0, :PPIDL0,
                                   :PPTMP0, :PPDTA0, :PPTME0,
                                   :PPLAT0, :PPLON0         );
           endif;
         endif;
       end-proc;
       //*********************************************************************
       dcl-proc LoadSFL; // Load subfile
       //*********************************************************************
         ReLoadSFL = *off;
         // Clear subfile
         Dspf.SflClr = *on;
         write FMT01;
         Dspf.SflClr = *off;
         // Load SFL
         SF1NUM = *zero;
         exec sql close C1;
         exec sql open C1;
         // Read recordset
         dow Sqlcode = 0 and SF1NUM < MaxSFLrec;
           exec sql fetch C1 into :RecordDS;
           if sqlcode = 0;
             S1IDNM = WWIDNM;
             S1ANN0 = WWANN0;
             S1IDL0 = WWIDL0;
             S1LOC0 = WWLOC0;
             S1NAZ0 = WWNAZ0;
             S1TMP0 = WWTMP0;
             S1DTA0 = WWDTA0;
             S1TME0 = WWTME0;
             S1LAT0 = WWLAT0;
             S1LON0 = WWLON0;
             S1GOO0 = 'https://www.yr.no/en/forecast/graph/2-' +
                      %trim(%editw(S1IDL0:'          '));
             SF1NUM += 1;
             write SFL1;
           else;
             Dspf.SflEnd = *on; // SFLEND *END
         endif;
       enddo;

       NBRRECS = SF1NUM;
       S1MSG = %Editc(NBRRECS:'X') + ' record loaded. No more records +
       to load';
       if sqlcode = 0 and NBRRECS >= MaxSFLrec; // MaxSFLrec reached
         S1MSG = %Editc(NBRRECS:'X') + ' records loaded. Use filters field +
         to limit the records to be loaded.';
       endif;

       if NBRRECS = 0;
         clear SFL1;
         SF1NUM += 1;
         S1ANN0 = '*';
         S1MSG = 'No records loaded';
         write SFL1;
       endif;

       end-proc;
       //*********************************************************************
       dcl-proc SetFilters; // Set filters filed
       //*********************************************************************
         if (F1IDNM <>   F1IDNMprv); // numeric filter need str & end
             F1IDNMprv = F1IDNM;
            wF1IDNMstr = F1IDNM;
            wF1IDNMend = F1IDNM;
           ReLoadSFL = *on;
         endif;
         if (F1IDNM = 0);          // numeric filter: if 0 set from 0 to HiVal
             F1IDNMprv = F1IDNM;
            wF1IDNMstr = *zeros;
            wF1IDNMend = *HiVal;
         endif;

         if (F1ANN0               <> F1ANN0prv); // char filter
             F1ANN0prv =             F1ANN0;
             F1ANN0lke = '%' + %trim(F1ANN0) + '%';
           ReLoadSFL = *on;
         endif;

         if (F1IDL0 <>   F1IDL0prv); // numeric filter need str & end
             F1IDL0prv = F1IDL0;
            wF1IDL0str = F1IDL0;
            wF1IDL0end = F1IDL0;
           ReLoadSFL = *on;
         endif;
         if (F1IDL0 = 0);          // numeric filter: if 0 set from 0 to HiVal
             F1IDL0prv = F1IDL0;
            wF1IDL0str = *zeros;
            wF1IDL0end = *HiVal;
         endif;

         if (F1LOC0               <> F1LOC0prv); // char filter
             F1LOC0prv =             F1LOC0;
             F1LOC0lke = '%' + %trim(F1LOC0) + '%';
           ReLoadSFL = *on;
         endif;

         if (F1NAZ0               <> F1NAZ0prv); // char filter
             F1NAZ0prv =             F1NAZ0;
             F1NAZ0lke = '%' + %trim(F1NAZ0) + '%';
           ReLoadSFL = *on;
         endif;

       end-proc;


Have fun! 

I appreciate all the comments made on this blog.

#IBMiSample #sql #RPGfree

Comments

  1. Hi, where is the function to call the internet address please!

    ReplyDelete
    Replies
    1. dcl-proc webData; // Load Location data from web service

      In this Proc the program connect the web service (systools.httpgetclob(:URL, '')

      The URL is

      URL = cURL + 'q=' + %trim(F1LOC0) + ',' + %trim(F1NAZ0) + '&appid=' + cAPIKEY + '&units=metric';

      For example, if you type "LONDON" the url is:
      http://api.openweathermap.org/data/2.5/weather?q=LONDON,&appid=76a614c6ce3bda8347f588353aa59fbd&units=metric

      Delete
  2. hello.
    I am looking for something similar, to extract distances between cities.
    Where can I search?

    ReplyDelete

Post a Comment

Popular posts from this blog

IBMi (AS400) fans only ‘ Memories (IBM Coding Forms)

IBMi (AS400) fans only ' SQLCODE values: Dear readers, unleash your suggestions!