weather-cli/cmd/history.go

114 lines
3 KiB
Go
Raw Normal View History

2022-08-02 23:25:50 -04:00
package main
import (
"context"
"encoding/json"
"fmt"
2022-08-02 23:56:24 -04:00
"strconv"
2022-08-02 23:25:50 -04:00
pb "codeberg.org/andcscott/weather-cli/proto"
)
2022-08-02 23:40:40 -04:00
// Get a date from the user and save it to the config
func getDate(app *Application) {
2022-08-06 22:16:56 -04:00
var isValid bool
for !isValid {
fmt.Print("\nEnter 4-digit year: ")
_, err := fmt.Scanf("%d", &app.Config.Date.Year)
if err != nil {
fmt.Println("Invalid year")
}
2022-08-02 23:25:50 -04:00
2022-08-06 22:16:56 -04:00
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
}
2022-08-02 23:25:50 -04:00
}
}
2022-08-02 23:40:40 -04:00
// Query SimplrWeather for date in Unix time
2022-08-02 23:25:50 -04:00
func getUnixTime(c pb.RouteGuideClient) int32 {
var year, month, day int32
res, err := c.GetUnixTime(context.Background(), &pb.Date{
Year: year,
Month: month,
Day: day,
})
if err != nil {
fmt.Printf("Error getting Unix time: %v\n", err)
}
return res.Unixtime
}
2022-08-02 23:40:40 -04:00
// Query SimplrWeather for historical data
func getHistoricalData(c pb.RouteGuideClient, app *Application) {
2022-08-02 23:25:50 -04:00
2022-08-02 23:56:24 -04:00
var lat, lon int
lat, _ = strconv.Atoi(app.Config.Latitude)
lon, _ = strconv.Atoi(app.Config.Longitude)
2022-08-02 23:25:50 -04:00
res, err := c.GetHistoricalData(context.Background(), &pb.LocationDate{
2022-08-02 23:56:24 -04:00
Latitude: int32(lat),
Longitude: int32(lon),
Year: app.Config.Date.Year,
Month: app.Config.Date.Month,
Day: app.Config.Date.Day,
2022-08-02 23:25:50 -04:00
})
if err != nil {
fmt.Printf("Error getting historical data: %v", err)
}
2022-08-06 22:16:56 -04:00
hData := HistoricalData{}
err = json.Unmarshal([]byte(res.Data), &hData)
2022-08-02 23:25:50 -04:00
if err != nil {
fmt.Printf("Error reading data from server: %v", err)
2022-08-06 22:16:56 -04:00
} 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 users preferred units
2022-08-06 22:16:56 -04:00
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.")
2022-08-02 23:25:50 -04:00
}
2022-08-06 22:16:56 -04:00
}
// 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
2022-08-02 23:25:50 -04:00
}