': This program uses
': InForm - GUI library for QB64 - v1.1
': Fellippe Heitor, 2016-2019 - fellippe@qb64.org - @fellippeheitor
': https://github.com/FellippeHeitor/InForm
'-----------------------------------------------------------

': Controls' IDs: ------------------------------------------------------------------
DIM SHARED WeatherMakerV10 AS LONG
DIM SHARED logojpgPX AS LONG
DIM SHARED ProgressBar1 AS LONG
DIM SHARED GetWeatherBT AS LONG

': External modules: ---------------------------------------------------------------
'$INCLUDE:'InForm\InForm.ui'
'$INCLUDE:'InForm\xp.uitheme'
'$INCLUDE:'weather.frm'

': Event procedures: ---------------------------------------------------------------
SUB __UI_BeforeInit

END SUB

SUB __UI_OnLoad

END SUB

SUB __UI_BeforeUpdateDisplay
    'This event occurs at approximately 30 frames per second.
    'You can change the update frequency by calling SetFrameRate DesiredRate%

    SHARED GettingWeather AS _BYTE
    SHARED GetWeatherStep AS INTEGER

    IF GettingWeather THEN
        GetWeatherStep = GetWeatherStep + 1
        Control(ProgressBar1).Value = GetWeatherStep
        SELECT EVERYCASE GetWeatherStep
            CASE 1 'Read INI File
                ' Weather station v1.0
                DIM Img1 AS STRING
                DIM Img2 AS STRING
                DIM Img3 AS STRING
                DIM Img4 AS STRING
                DIM Img5 AS STRING
                DIM Img6 AS STRING
                DIM Img7 AS STRING
                DIM Img8 AS STRING
                DIM Img9 AS STRING
                DIM Img10 AS STRING
                DIM Img11 AS STRING
                DIM LineTxt AS STRING

                OPEN "radar.txt" FOR INPUT AS #1

                INPUT #1, Img1, Img2, Img3, Img4, Img5, Img6
                INPUT #1, Img7, Img8, Img9, Img10, Img11

                CLOSE #1


                ' Getting NOAA 7 day weather text forecast

                ' SHELL "curl -k https://tgftp.nws.noaa.gov/data/forecasts/zone/ms/msz024.txt > forecast.txt"
                SHELL "go.bat"
            CASE 2
                ' Correcting forecast.txt format for writting to file

                CLS
                OPEN "forecast.txt" FOR INPUT AS #1
                OPEN "test.txt" FOR OUTPUT AS #2
                WHILE NOT EOF(1)
                    INPUT #1, LineTxt
                    IF LEFT$(LineTxt, 1) = "." THEN
                        PRINT CHR$(13); CHR$(13)
                        PRINT #2, " "
                        PRINT MID$(LineTxt, 2); " ";
                        PRINT #2, MID$(LineTxt, 2); " ";
                    ELSE
                        PRINT #2, LineTxt 'write output to new file test.txt
                        PRINT LineTxt; 'prints output to screen
                    END IF
                WEND
                CLOSE #1
                CLOSE #2
            CASE 3
                DIM DownloadResult AS _BYTE
                DownloadResult = Download(Img1, "images\radar.jpg", 10)
            CASE 4
                DownloadResult = Download(Img2, "images\radarn.jpg", 10)
            CASE 5
                DownloadResult = Download(Img3, "images\warning.jpg", 10)
            CASE 6
                DownloadResult = Download(Img4, "images\7day.jpg", 10)
            CASE 7
                DownloadResult = Download(Img5, "images\cur_temp.jpg", 10)
            CASE 8
                DownloadResult = Download(Img6, "images\temp_hi.jpg", 10)
            CASE 9
                DownloadResult = Download(Img7, "images\temp_lo.jpg", 10)
            CASE 10
                DownloadResult = Download(Img8, "images\humidity.jpg", 10)
            CASE 11
                DownloadResult = Download(Img9, "images\dewpoint.jpg", 10)
            CASE 12
                DownloadResult = Download(Img10, "images\winds.jpg", 10)
            CASE 13
                DownloadResult = Download(Img11, "images\almanac.jpg", 10)
            CASE 3 TO 13
                IF DownloadResult = 0 THEN
                    GettingWeather = 0
                    DIM Answer AS _BYTE
                    Answer = MessageBox("Download failed.", "", MsgBox_OkOnly + MsgBox_Exclamation)
                    Control(GetWeatherBT).Disabled = False
                END IF
            CASE ELSE
                'Finished
                GettingWeather = 0
                Control(GetWeatherBT).Disabled = False
        END SELECT
    END IF

END SUB

SUB __UI_BeforeUnload
    'If you set __UI_UnloadSignal = False here you can
    'cancel the user's request to close.
END SUB

SUB __UI_Click (id AS LONG)
    SELECT CASE id
        CASE WeatherMakerV10

        CASE logojpgPX

        CASE ProgressBar1

        CASE GetWeatherBT
            SHARED GettingWeather AS _BYTE
            SHARED GetWeatherStep AS INTEGER
            IF GettingWeather = 0 THEN
                GettingWeather = -1 'set flag
                GetWeatherStep = 0 'start download process

                'reset ProgressBar1
                Control(ProgressBar1).Min = 0
                Control(ProgressBar1).Max = MaxSteps
                Control(ProgressBar1).Value = 0

                Control(GetWeatherBT).Disabled = True
            END IF
    END SELECT
END SUB

SUB __UI_MouseEnter (id AS LONG)
    SELECT CASE id
        CASE WeatherMakerV10

        CASE logojpgPX

        CASE ProgressBar1

        CASE GetWeatherBT

    END SELECT
END SUB

SUB __UI_MouseLeave (id AS LONG)
    SELECT CASE id
        CASE WeatherMakerV10

        CASE logojpgPX

        CASE ProgressBar1

        CASE GetWeatherBT

    END SELECT
END SUB

SUB __UI_FocusIn (id AS LONG)
    SELECT CASE id
        CASE GetWeatherBT

    END SELECT
END SUB

SUB __UI_FocusOut (id AS LONG)
    'This event occurs right before a control loses focus.
    'To prevent a control from losing focus, set __UI_KeepFocus = True below.
    SELECT CASE id
        CASE GetWeatherBT

    END SELECT
END SUB

SUB __UI_MouseDown (id AS LONG)
    SELECT CASE id
        CASE WeatherMakerV10

        CASE logojpgPX

        CASE ProgressBar1

        CASE GetWeatherBT

    END SELECT
END SUB

SUB __UI_MouseUp (id AS LONG)
    SELECT CASE id
        CASE WeatherMakerV10

        CASE logojpgPX

        CASE ProgressBar1

        CASE GetWeatherBT

    END SELECT
END SUB

SUB __UI_KeyPress (id AS LONG)
    'When this event is fired, __UI_KeyHit will contain the code of the key hit.
    'You can change it and even cancel it by making it = 0
    SELECT CASE id
        CASE GetWeatherBT

    END SELECT
END SUB

SUB __UI_TextChanged (id AS LONG)
    SELECT CASE id
    END SELECT
END SUB

SUB __UI_ValueChanged (id AS LONG)
    SELECT CASE id
    END SELECT
END SUB

SUB __UI_FormResized

END SUB




'SLEEP
'SYSTEM
' ---------- program end -----------





FUNCTION Download (url$, file$, timelimit) ' returns -1 if successful, 0 if not
    url2$ = url$
    x = INSTR(url2$, "/")
    IF x THEN url2$ = LEFT$(url$, x - 1)
    client = _OPENCLIENT("TCP/IP:80:" + url2$)
    IF client = 0 THEN EXIT FUNCTION
    e$ = CHR$(13) + CHR$(10) ' end of line characters
    url3$ = RIGHT$(url$, LEN(url$) - x + 1)
    x$ = "GET " + url3$ + " HTTP/1.1" + e$
    x$ = x$ + "Host: " + url2$ + e$ + e$
    PUT #client, , x$
    t! = TIMER ' start time
    DO
        _DELAY 0.05 ' 50ms delay (20 checks per second)
        GET #client, , a2$
        a$ = a$ + a2$
        i = INSTR(a$, "Content-Length:")
        IF i THEN
            i2 = INSTR(i, a$, e$)
            IF i2 THEN
                l = VAL(MID$(a$, i + 15, i2 - i - 14))
                i3 = INSTR(i2, a$, e$ + e$)
                IF i3 THEN
                    i3 = i3 + 4 'move i3 to start of data
                    IF (LEN(a$) - i3 + 1) = l THEN
                        CLOSE client ' CLOSE CLIENT
                        d$ = MID$(a$, i3, l)
                        fh = FREEFILE
                        OPEN file$ FOR OUTPUT AS #fh: CLOSE #fh 'Warning! Clears data from existing file
                        OPEN file$ FOR BINARY AS #fh
                        PUT #fh, , d$
                        CLOSE #fh
                        Download = -1 'indicates download was successfull
                        EXIT FUNCTION
                    END IF ' availabledata = l
                END IF ' i3
            END IF ' i2
        END IF ' i
    LOOP UNTIL TIMER > t! + timelimit ' (in seconds)
    CLOSE client
END FUNCTION






