mirror of
https://codeberg.org/andyscott/weather-cli.git
synced 2024-11-09 06:00:52 -05:00
Queries for historical data functional
This commit is contained in:
parent
f91066fa34
commit
0441112a60
4 changed files with 121 additions and 37 deletions
|
@ -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
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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
|
||||||
|
|
||||||
|
for !isValid {
|
||||||
|
fmt.Print("\nEnter 4-digit year: ")
|
||||||
_, err := fmt.Scanf("%d", &app.Config.Date.Year)
|
_, err := fmt.Scanf("%d", &app.Config.Date.Year)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println("Invalid year")
|
fmt.Println("Invalid year")
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Print("Enter month (1-12): ")
|
fmt.Print("\nEnter month (1-12): ")
|
||||||
_, err = fmt.Scanf("%d", &app.Config.Date.Month)
|
_, err = fmt.Scanf("%d", &app.Config.Date.Month)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println("Invalid year")
|
fmt.Println("Invalid month")
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Print("Enter day (1-31): ")
|
fmt.Print("\nEnter day (1-31): ")
|
||||||
_, err = fmt.Scanf("%d", &app.Config.Date.Day)
|
_, err = fmt.Scanf("%d", &app.Config.Date.Day)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println("Invalid year")
|
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
|
||||||
}
|
}
|
||||||
|
|
|
@ -27,8 +27,7 @@ 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"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -49,6 +48,7 @@ type Config struct {
|
||||||
|
|
||||||
type Application struct {
|
type Application struct {
|
||||||
Forecast Forecast
|
Forecast Forecast
|
||||||
|
HistoricalForecast HistoricalForecast
|
||||||
Config Config
|
Config Config
|
||||||
Version string
|
Version string
|
||||||
Client pb.RouteGuideClient
|
Client pb.RouteGuideClient
|
||||||
|
@ -75,10 +75,14 @@ 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,
|
||||||
|
HistoricalForecast: hFcst,
|
||||||
Version: version,
|
Version: version,
|
||||||
Client: pb.NewRouteGuideClient(conn),
|
Client: pb.NewRouteGuideClient(conn),
|
||||||
}
|
}
|
||||||
|
|
|
@ -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)
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue