Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
- Dim apiKey As String = "580997entTbou3Z7n0EQfucEV6eTYZiU"
- Dim latitude As String = "12.8797" ' Philippines' latitude
- Dim longitude As String = "121.7740" ' Philippines' longitude
- Dim apiEndpoint As String = "https://api.tomorrow.io/v4/timelines?location=" & latitude & "," & longitude & "&fields=temperature,windSpeed&apikey=" & apiKey
- Try
- Using client As New HttpClient()
- Dim response = client.GetAsync(apiEndpoint).Result
- If response.IsSuccessStatusCode Then
- Dim responseData = response.Content.ReadAsStringAsync().Result
- Dim json = JObject.Parse(responseData)
- Dim weatherForecasts As New List(Of WeatherForecast)()
- ' Extract weather forecast data for 12 PM each day for the next 3 days
- Dim intervals = json.SelectToken("data.timelines[0].intervals")
- Dim filteredIntervals = intervals.Where(Function(interval) Date.Parse(interval.SelectToken("startTime").ToString()).TimeOfDay = TimeSpan.FromHours(12)).Take(5)
- ' Database connection
- WeatherDBConn = New DB2Connection("server=localhost;database=CCS6DB;" + "uid=db2admin;password=db2admin;")
- WeatherDBConn.Open()
- Dim deleteQuery As String = "DELETE FROM weatherdata "
- Dim cmdDelete As New DB2Command(deleteQuery, WeatherDBConn)
- Dim resetQuery As String = "ALTER TABLE weatherdata ALTER COLUMN ID RESTART WITH 1"
- Dim cmdReset As New DB2Command(resetQuery, WeatherDBConn)
- cmdReset.ExecuteNonQuery()
- cmdDelete.ExecuteNonQuery()
- ' Insert new data into the database
- For Each interval In filteredIntervals
- Dim weatherForecast As New WeatherForecast()
- ' Extract required data
- weatherForecast.Temperature = interval.SelectToken("values.temperature").ToObject(Of Double)()
- weatherForecast.WindSpeed = interval.SelectToken("values.windSpeed").ToObject(Of Double)()
- Dim valuesToken = interval.SelectToken("values")
- If valuesToken IsNot Nothing Then
- Dim weatherCodeToken = interval.SelectToken("weatherCode")
- If weatherCodeToken IsNot Nothing Then
- weatherForecast.weatherCode = weatherCodeToken.ToObject(Of Integer)()
- Else
- MsgBox("Weather code not found in JSON.")
- End If
- End If
- weatherForecast.DateAndTime = Date.Parse(interval.SelectToken("startTime").ToString())
- ' Add WeatherForecast object to the list
- weatherForecasts.Add(weatherForecast)
- ' Insert the data into the DB2 database
- Dim insertQuery As String = "INSERT INTO WeatherData (Temperature, WindSpeed, ForecastDate, Weathertype) VALUES (?, ?, ?, ?)"
- Using insertCmd As New DB2Command(insertQuery, WeatherDBConn)
- insertCmd.Parameters.Add("@Temperature", DB2Type.Double).Value = weatherForecast.Temperature
- insertCmd.Parameters.Add("@WindSpeed", DB2Type.Double).Value = weatherForecast.WindSpeed
- insertCmd.Parameters.Add("@ForecastDate", DB2Type.Date).Value = weatherForecast.DateAndTime
- If weatherForecast.weatherCode = 1000 Then
- Dim weathertype As String = "Sunny"
- insertCmd.Parameters.Add("@WeatherCodeFullDay", DB2Type.VarChar).Value = weathertype
- insertCmd.ExecuteNonQuery()
- ElseIf weatherForecast.weatherCode = 1001 Then
- Dim weathertype As String = "Cloudy"
- insertCmd.Parameters.Add("@WeatherCodeFullDay", DB2Type.VarChar).Value = weathertype
- insertCmd.ExecuteNonQuery()
- ElseIf weatherForecast.weatherCode = 4001 Then
- Dim weathertype As String = "Rainy"
- insertCmd.Parameters.Add("@WeatherCodeFullDay", DB2Type.VarChar).Value = weathertype
- insertCmd.ExecuteNonQuery()
- Else
- Dim weathertype As String = "unknown"
- insertCmd.Parameters.Add("@WeatherCodeFullDay", DB2Type.VarChar).Value = weathertype
- insertCmd.ExecuteNonQuery()
- End If
- End Using
- Next
- ' Use weather data (for demonstration)
- For Each forecast In weatherForecasts
- Dim displayData As String = "Temperature: " & forecast.Temperature & ", Wind Speed: " & forecast.WindSpeed & ", Date: " & forecast.DateAndTime & ", Weather Code: " & forecast.weatherCode
- MsgBox(displayData)
- Next
- Else
- MsgBox("Failed to fetch weather data. Status code: " & response.StatusCode)
- End If
- End Using
- Catch ex As Exception
- MsgBox(ex.ToString())
- Finally
- WeatherDBConn.Close()
- End Try
- End Sub
- Json -------------------------------------
- {"timelines":{"minutely":[{"time":"2023-12-16T01:16:00Z","values":{"cloudBase":null,"cloudCeiling":null,"cloudCover":1,"dewPoint":2.69,"freezingRainIntensity":0,"humidity":65,"precipitationProbability":0,"pressureSurfaceLevel":1021.67,"rainIntensity":0,"sleetIntensity":0,"snowIntensity":0,"temperature":8.88,"temperatureApparent":8.88,"uvHealthConcern":0,"uvIndex":0,"visibility":16,"weatherCode":1000,"windDirection":304.13,"windGust":6.19,"windSpeed":3}},{"time":"2023-12-16T01:17:00Z","values":{"cloudBase":null,"cloudCeiling":null,"cloudCover":1.08,"dewPoint":2.65,"freezingRainIntensity":0,"humidity":65.1,"precipitationProbability":0,"pressureSurfaceLevel":1021.66,"rainIntensity":0,"sleetIntensity":0,"snowIntensity":0,"temperature":8.82,"temperatureApparent":8.82,"uvHealthConcern":0,"uvIndex":0,"visibility":16,"weatherCode":1000,"windDirection":304.13,"windGust":6.17,"windSpeed":3}},{"time":"2023-12-16T01:18:00Z","values":{"cloudBase":null,"cloudCeiling":null,"cloudCover":1.16,"dewPoint":2.61,"freezingRainIntensity":0,"humidity":65.2,"precipitationProbability":0,"pressureSurfaceLevel":1021.64,"rainIntensity":0,"sleetIntensity":0,"snowIntensity":0,"temperature":8.76,"temperatureApparent":8.76,"uvHealthConcern":0,"uvIndex":0,"visibility":16,"weatherCode":1000,"windDirection":304.13,"windGust":6.15,"windSpeed":3}},{"time":"2023-12-16T01:19:00Z","values":{"cloudBase":null,"cloudCeiling":null,"cloudCover":1.24,"dewPoint":2.58,"freezingRainIntensity":0,"humidity":65.31,"precipitationProbability":0,"pressureSurfaceLevel":1021.62,"rainIntensity":0,"sleetIntensity":0,"snowIntensity":0,"temperature":8.7,"temperatureApparent":8.7,"uvHealthConcern":0,"uvIndex":0,"visibility":16,"weatherCode":1000,"windDirection":304.13,"windGust":6.14,"windSpeed":3}},{"time":"2023-12-16T01:20:00Z","values":{"cloudBase":null,"cloudCeiling":null,"cloudCover":1.32,"dewPoint":2.54,"freezingRainIntensity":0,"humidity":65.41,"precipitationProbability":0,"pressureSurfaceLevel":1021.61,"rainIntensity":0,"sleetIntensity":0,"snowIntensity":0,"temperature":8.65,"temperatureApparent":8.65,"uvHealthConcern":0,"uvIndex":0,"visibility":16,"weatherCode":1000,"windDirection":304.13,"windGust":6.12,"windSpeed":3}},{"time":"2023-12-16T01:21:00Z","values":{"cloudBase":null,"cloudCeiling":null,"cloudCover":1.39,"dewPoint":2.5,"freezingRainIntensity":0,"humidity":65.51,"precipitationProbability":0,"pressureSurfaceLevel":1021.59,"rainIntensity":0,"sleetIntensity":0,"snowIntensity":0,"temperature":8.59,"temperatureApparent":8.59,"uvHealthConcern":0,"uvIndex":0,"visibility":16,"weatherCode":1000,"windDirection":304.13,"windGust":6.1,"windSpeed":3}},{"time":"2023-12-16T01:22:00Z","values":{"cloudBase":null,"cloudCeiling":null,"cloudCover":1.47,"dewPoint":2.46,"freezingRainIntensity":0,"humidity":65.61,"precipitationProbability":0,"pressureSurfaceLevel":1021.58,"rainIntensity":0,"sleetIntensity":0,"snowIntensity":0,"temperature":8.53,"temperatureApparent":8.53,"uvHealthConcern":0,"uvIndex":0,"visibility":16,"weatherCode":1000,"windDirection":304.13,"windGust":6.09,"windSpeed":2.99}},{"time":"2023-12-16T01:23:00Z","values":{"cloudBase":null,"cloudCeiling":null,"cloudCover":1.55,"dewPoint":2.43,"freezingRainIntensity":0,"humidity":65.71,"precipitationProbability":0,"pressureSurfaceLevel":1021.56,"rainIntensity":0,"sleetIntensity":0,"snowIntensity":0,"temperature":8.48,"temperatureApparent":8.48,"uvHealthConcern":0,"uvIndex":0,"visibility":16,"weatherCode":1000,"windDirection":304.13,"windGust":6.07,"windSpeed":2.99}},{"time":"2023-12-16T01:24:00Z","values":{"cloudBase":null,"cloudCeiling":null,"cloudCover":1.63,"dewPoint":2.39,"freezingRainIntensity":0,"humidity":65.82,"precipitationProbability":0,"pressureSurfaceLevel":1021.54,"rainIntensity":0,"sleetIntensity":0,"snowIntensity":0,"temperature":8.42,"temperatureApparent":8.42,"uvHealthConcern":0,"uvIndex":0,"visibility":16,"weatherCode":1000,"windDirection":304.13,"windGust":6.05,"windSpeed":2.99}},
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement