2023-05-22 17:13:36 +02:00
|
|
|
// SPDX-FileCopyrightText: 2023 Winni Neessen <wn@neessen.dev>
|
|
|
|
//
|
|
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
|
|
|
|
package meteologix
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"math"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2023-06-23 16:42:06 +02:00
|
|
|
const (
|
|
|
|
// MultiplierKnots is the multiplier for converting the base unit to knots
|
|
|
|
MultiplierKnots = 1.9438444924
|
|
|
|
// MultiplierKPH is the multiplier for converting the base unit to kilometers per hour
|
|
|
|
MultiplierKPH = 3.6
|
|
|
|
// MultiplierMPH is the multiplier for converting the base unit to miles per hour
|
|
|
|
MultiplierMPH = 2.236936
|
|
|
|
)
|
|
|
|
|
2023-05-22 17:13:36 +02:00
|
|
|
// Speed is a type wrapper of an WeatherData for holding speed
|
|
|
|
// values in WeatherData
|
|
|
|
type Speed WeatherData
|
|
|
|
|
|
|
|
// IsAvailable returns true if an Speed value was
|
|
|
|
// available at time of query
|
2023-05-23 20:00:42 +02:00
|
|
|
func (s Speed) IsAvailable() bool {
|
|
|
|
return !s.na
|
2023-05-22 17:13:36 +02:00
|
|
|
}
|
|
|
|
|
2023-06-27 19:12:35 +02:00
|
|
|
// DateTime returns the DateTime when the Speed was checked
|
2023-05-23 20:00:42 +02:00
|
|
|
func (s Speed) DateTime() time.Time {
|
|
|
|
return s.dt
|
2023-05-22 17:13:36 +02:00
|
|
|
}
|
|
|
|
|
2023-06-23 11:52:36 +02:00
|
|
|
// Value returns the float64 value of an Speed in meters
|
|
|
|
// per second.
|
2023-06-26 12:21:36 +02:00
|
|
|
// If the Speed is not available in the WeatherData
|
2023-05-22 17:13:36 +02:00
|
|
|
// Vaule will return math.NaN instead.
|
2023-05-23 20:00:42 +02:00
|
|
|
func (s Speed) Value() float64 {
|
|
|
|
if s.na {
|
2023-05-22 17:13:36 +02:00
|
|
|
return math.NaN()
|
|
|
|
}
|
2023-05-24 22:00:47 +02:00
|
|
|
return s.fv
|
2023-05-22 17:13:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// String satisfies the fmt.Stringer interface for the Speed type
|
2023-05-23 20:00:42 +02:00
|
|
|
func (s Speed) String() string {
|
2023-06-23 11:52:36 +02:00
|
|
|
return fmt.Sprintf("%.1fm/s", s.fv)
|
2023-05-23 20:00:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Source returns the Source of Speed
|
|
|
|
// If the Source is not available it will return SourceUnknown
|
|
|
|
func (s Speed) Source() Source {
|
|
|
|
return s.s
|
2023-05-22 17:13:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// KMH returns the Speed value in km/h
|
2023-05-23 20:00:42 +02:00
|
|
|
func (s Speed) KMH() float64 {
|
2023-06-23 16:42:06 +02:00
|
|
|
return s.fv * MultiplierKPH
|
2023-05-22 17:13:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// KMHString returns the Speed value as formatted string in km/h
|
2023-05-23 20:00:42 +02:00
|
|
|
func (s Speed) KMHString() string {
|
|
|
|
return fmt.Sprintf("%.1fkm/h", s.KMH())
|
2023-05-22 17:13:36 +02:00
|
|
|
}
|
|
|
|
|
2023-06-23 11:52:36 +02:00
|
|
|
// Knots returns the Speed value in kn
|
|
|
|
func (s Speed) Knots() float64 {
|
2023-06-23 16:42:06 +02:00
|
|
|
return s.fv * MultiplierKnots
|
2023-06-23 11:52:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// KnotsString returns the Speed value as formatted string in kn
|
|
|
|
func (s Speed) KnotsString() string {
|
|
|
|
return fmt.Sprintf("%.0fkn", s.Knots())
|
|
|
|
}
|
|
|
|
|
2023-05-22 17:13:36 +02:00
|
|
|
// MPH returns the Speed value in mi/h
|
2023-05-23 20:00:42 +02:00
|
|
|
func (s Speed) MPH() float64 {
|
2023-06-23 16:42:06 +02:00
|
|
|
return s.fv * MultiplierMPH
|
2023-05-22 17:13:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// MPHString returns the Speed value as formatted string in mi/h
|
2023-05-23 20:00:42 +02:00
|
|
|
func (s Speed) MPHString() string {
|
|
|
|
return fmt.Sprintf("%.1fmi/h", s.MPH())
|
2023-05-22 17:13:36 +02:00
|
|
|
}
|