Queries for historical data functional

This commit is contained in:
Andrew Scott 2022-08-06 22:16:56 -04:00
parent f91066fa34
commit 0441112a60
Signed by: a
GPG key ID: 3EB62D0BBB8DB381
4 changed files with 121 additions and 37 deletions

View file

@ -47,7 +47,7 @@ func advancedMenu(app *Application) {
getPreciseLocation(app) getPreciseLocation(app)
getCurrent(app) getCurrent(app)
if app.Forecast.Main.Temp != 0.00 { if app.Forecast.Weather.Temp != -500.00 {
validLoc = true validLoc = true
} else { } else {
fmt.Println("I couldn't get any information for that location.") fmt.Println("I couldn't get any information for that location.")
@ -55,7 +55,7 @@ func advancedMenu(app *Application) {
} }
} }
printWeather(app) printWeather(app)
app.Forecast.Main.Temp = 0.00 app.Forecast.Weather.Temp = -500.00
} else if option == "3" { } else if option == "3" {
var validLoc bool var validLoc bool
@ -64,15 +64,15 @@ func advancedMenu(app *Application) {
getDate(app) getDate(app)
getHistoricalData(app.Client, app) getHistoricalData(app.Client, app)
if app.Forecast.Main.Temp != 0.00 { if app.HistoricalForecast.Temp != -500.00 {
validLoc = true validLoc = true
} else { } else {
fmt.Println("I couldn't get any information for that location.") fmt.Println("I couldn't get any information for that location.")
fmt.Println("Are you sure the coordinates are valid?") fmt.Println("Are you sure the coordinates are valid?")
} }
} }
printWeather(app) printHistorical(app)
app.Forecast.Main.Temp = 0.00 app.Forecast.Weather.Temp = -500.00
} }
} }
} }

View file

@ -9,25 +9,44 @@ import (
pb "codeberg.org/andcscott/weather-cli/proto" pb "codeberg.org/andcscott/weather-cli/proto"
) )
type HistoricalForecast struct {
Temp float32 `json:"temp"`
FeelsLike float32 `json:"feels_like"`
Pressure uint `json:"pressure"`
Humidity uint `json:"humidity"`
Speed float32 `json:"wind_speed"`
}
type HistoricalData struct {
HistoricalForecast []HistoricalForecast `json:"data"`
}
// Get a date from the user and save it to the config // Get a date from the user and save it to the config
func getDate(app *Application) { func getDate(app *Application) {
fmt.Print("Enter 4-digit year: ") var isValid bool
_, err := fmt.Scanf("%d", &app.Config.Date.Year)
if err != nil {
fmt.Println("Invalid year")
}
fmt.Print("Enter month (1-12): ") for !isValid {
_, err = fmt.Scanf("%d", &app.Config.Date.Month) fmt.Print("\nEnter 4-digit year: ")
if err != nil { _, err := fmt.Scanf("%d", &app.Config.Date.Year)
fmt.Println("Invalid year") if err != nil {
} fmt.Println("Invalid year")
}
fmt.Print("Enter day (1-31): ") fmt.Print("\nEnter month (1-12): ")
_, err = fmt.Scanf("%d", &app.Config.Date.Day) _, err = fmt.Scanf("%d", &app.Config.Date.Month)
if err != nil { if err != nil {
fmt.Println("Invalid year") fmt.Println("Invalid month")
}
fmt.Print("\nEnter day (1-31): ")
_, err = fmt.Scanf("%d", &app.Config.Date.Day)
if err != nil {
fmt.Println("Invalid day")
} else {
fmt.Print("\n")
isValid = true
}
} }
} }
@ -65,8 +84,50 @@ func getHistoricalData(c pb.RouteGuideClient, app *Application) {
fmt.Printf("Error getting historical data: %v", err) fmt.Printf("Error getting historical data: %v", err)
} }
err = json.Unmarshal([]byte(res.Data), &app.Forecast) hData := HistoricalData{}
err = json.Unmarshal([]byte(res.Data), &hData)
if err != nil { if err != nil {
fmt.Printf("Error reading data from server: %v", err) fmt.Printf("Error reading data from server: %v", err)
} else {
app.HistoricalForecast.Temp = hData.HistoricalForecast[0].Temp
app.HistoricalForecast.FeelsLike = hData.HistoricalForecast[0].FeelsLike
app.HistoricalForecast.Pressure = hData.HistoricalForecast[0].Pressure
app.HistoricalForecast.Humidity = hData.HistoricalForecast[0].Humidity
app.HistoricalForecast.Speed = hData.HistoricalForecast[0].Speed
} }
convertUnits(app)
}
// Convert historical data from standard to user defined units
func convertUnits(app *Application) {
units := app.Config.Units
switch units {
case "imperial":
app.HistoricalForecast.Temp = convertKToF(app.HistoricalForecast.Temp)
app.HistoricalForecast.FeelsLike = convertKToF(app.HistoricalForecast.FeelsLike)
app.HistoricalForecast.Speed = convertMpsToMph(app.HistoricalForecast.Speed)
case "metric":
app.HistoricalForecast.Temp = convertKToC(app.HistoricalForecast.Temp)
app.HistoricalForecast.FeelsLike = convertKToC(app.HistoricalForecast.FeelsLike)
default:
fmt.Println("\nUnable to convert units, temperature will default to Kelvin and speed will default to meters per second.")
return
}
}
// Convert Kelvin to Fahrenheit
func convertKToF(temp float32) float32 {
return (9/5)*(temp-273) + 32
}
// Convert Kelvin to Celsius
func convertKToC(temp float32) float32 {
return temp - 273.15
}
func convertMpsToMph(speed float32) float32 {
return speed * 2.236936
} }

View file

@ -27,9 +27,8 @@ type Wind struct {
} }
type Forecast struct { type Forecast struct {
//Overview weatherOverview `json:"weather"` Weather Weather `json:"main"`
Main Weather `json:"main"` Wind Wind `json:"wind"`
Wind Wind `json:"wind"`
} }
type Date struct { type Date struct {
@ -48,10 +47,11 @@ type Config struct {
} }
type Application struct { type Application struct {
Forecast Forecast Forecast Forecast
Config Config HistoricalForecast HistoricalForecast
Version string Config Config
Client pb.RouteGuideClient Version string
Client pb.RouteGuideClient
} }
func main() { func main() {
@ -75,12 +75,16 @@ func main() {
} }
fcst := Forecast{} fcst := Forecast{}
hFcst := HistoricalForecast{}
fcst.Weather.Temp = -500.00
hFcst.Temp = -500.00
app := Application{ app := Application{
Config: cfg, Config: cfg,
Forecast: fcst, Forecast: fcst,
Version: version, HistoricalForecast: hFcst,
Client: pb.NewRouteGuideClient(conn), Version: version,
Client: pb.NewRouteGuideClient(conn),
} }
mainMenu(&app) mainMenu(&app)

View file

@ -14,12 +14,31 @@ func printWeather(app *Application) {
fmt.Println("\nNote! A value of 0 or 0.00 indicates the data is not available at this time.") fmt.Println("\nNote! A value of 0 or 0.00 indicates the data is not available at this time.")
fmt.Printf("Units: %s (%s)\n\n", app.Config.Units, unitStr) fmt.Printf("Units: %s (%s)\n\n", app.Config.Units, unitStr)
fmt.Printf("Current temperature: %.2f\n", app.Forecast.Main.Temp) fmt.Printf("Current temperature: %.2f\n", app.Forecast.Weather.Temp)
fmt.Printf("Feels like: %.2f\n", app.Forecast.Main.FeelsLike) fmt.Printf("Feels like: %.2f\n", app.Forecast.Weather.FeelsLike)
fmt.Printf("High: %.2f\n", app.Forecast.Main.HighTemp) fmt.Printf("High: %.2f\n", app.Forecast.Weather.HighTemp)
fmt.Printf("Low: %.2f\n", app.Forecast.Main.LowTemp) fmt.Printf("Low: %.2f\n", app.Forecast.Weather.LowTemp)
fmt.Printf("Pressure (hPa): %d\n", app.Forecast.Main.Pressure) fmt.Printf("Pressure (hPa): %d\n", app.Forecast.Weather.Pressure)
fmt.Printf("Humidity (%%): %d\n", app.Forecast.Main.Humidity) fmt.Printf("Humidity (%%): %d\n", app.Forecast.Weather.Humidity)
fmt.Printf("Wind speed: %.2f\n", app.Forecast.Wind.Speed) fmt.Printf("Wind speed: %.2f\n", app.Forecast.Wind.Speed)
fmt.Printf("Gust: %.2f\n", app.Forecast.Wind.Gust) fmt.Printf("Gust: %.2f\n", app.Forecast.Wind.Gust)
} }
// Prints saved historical data to the terminal
func printHistorical(app *Application) {
var unitStr string
if app.Config.Units == "imperial" {
unitStr = "Farhenheit/mph"
} else {
unitStr = "Celsius/kph"
}
fmt.Println("\nNote! A value of 0 or 0.00 indicates the data is not available at this time.")
fmt.Printf("Units: %s (%s)\n\n", app.Config.Units, unitStr)
fmt.Printf("Temperature: %.2f\n", app.HistoricalForecast.Temp)
fmt.Printf("Felt like: %.2f\n", app.HistoricalForecast.FeelsLike)
fmt.Printf("Pressure (hPa): %d\n", app.HistoricalForecast.Pressure)
fmt.Printf("Humidity (%%): %d\n", app.HistoricalForecast.Humidity)
fmt.Printf("Wind speed: %.2f\n", app.HistoricalForecast.Speed)
}