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

View file

@ -9,25 +9,44 @@ import (
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
func getDate(app *Application) {
fmt.Print("Enter 4-digit year: ")
_, err := fmt.Scanf("%d", &app.Config.Date.Year)
if err != nil {
fmt.Println("Invalid year")
}
var isValid bool
fmt.Print("Enter month (1-12): ")
_, err = fmt.Scanf("%d", &app.Config.Date.Month)
if err != nil {
fmt.Println("Invalid year")
}
for !isValid {
fmt.Print("\nEnter 4-digit year: ")
_, err := fmt.Scanf("%d", &app.Config.Date.Year)
if err != nil {
fmt.Println("Invalid year")
}
fmt.Print("Enter day (1-31): ")
_, err = fmt.Scanf("%d", &app.Config.Date.Day)
if err != nil {
fmt.Println("Invalid year")
fmt.Print("\nEnter month (1-12): ")
_, err = fmt.Scanf("%d", &app.Config.Date.Month)
if err != nil {
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)
}
err = json.Unmarshal([]byte(res.Data), &app.Forecast)
hData := HistoricalData{}
err = json.Unmarshal([]byte(res.Data), &hData)
if err != nil {
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 {
//Overview weatherOverview `json:"weather"`
Main Weather `json:"main"`
Wind Wind `json:"wind"`
Weather Weather `json:"main"`
Wind Wind `json:"wind"`
}
type Date struct {
@ -48,10 +47,11 @@ type Config struct {
}
type Application struct {
Forecast Forecast
Config Config
Version string
Client pb.RouteGuideClient
Forecast Forecast
HistoricalForecast HistoricalForecast
Config Config
Version string
Client pb.RouteGuideClient
}
func main() {
@ -75,12 +75,16 @@ func main() {
}
fcst := Forecast{}
hFcst := HistoricalForecast{}
fcst.Weather.Temp = -500.00
hFcst.Temp = -500.00
app := Application{
Config: cfg,
Forecast: fcst,
Version: version,
Client: pb.NewRouteGuideClient(conn),
Config: cfg,
Forecast: fcst,
HistoricalForecast: hFcst,
Version: version,
Client: pb.NewRouteGuideClient(conn),
}
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.Printf("Units: %s (%s)\n\n", app.Config.Units, unitStr)
fmt.Printf("Current temperature: %.2f\n", app.Forecast.Main.Temp)
fmt.Printf("Feels like: %.2f\n", app.Forecast.Main.FeelsLike)
fmt.Printf("High: %.2f\n", app.Forecast.Main.HighTemp)
fmt.Printf("Low: %.2f\n", app.Forecast.Main.LowTemp)
fmt.Printf("Pressure (hPa): %d\n", app.Forecast.Main.Pressure)
fmt.Printf("Humidity (%%): %d\n", app.Forecast.Main.Humidity)
fmt.Printf("Current temperature: %.2f\n", app.Forecast.Weather.Temp)
fmt.Printf("Feels like: %.2f\n", app.Forecast.Weather.FeelsLike)
fmt.Printf("High: %.2f\n", app.Forecast.Weather.HighTemp)
fmt.Printf("Low: %.2f\n", app.Forecast.Weather.LowTemp)
fmt.Printf("Pressure (hPa): %d\n", app.Forecast.Weather.Pressure)
fmt.Printf("Humidity (%%): %d\n", app.Forecast.Weather.Humidity)
fmt.Printf("Wind speed: %.2f\n", app.Forecast.Wind.Speed)
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)
}