Skip to content

Commit

Permalink
Add sun hours data point support
Browse files Browse the repository at this point in the history
Introduced a new `Duration` type for handling sun hours data points in weather forecasts. Updated the `WeatherForecastDatapoint` struct and relevant methods to support this new data point, ensuring proper retrieval and availability checks.
  • Loading branch information
wneessen committed Aug 20, 2024
1 parent ea92ee6 commit 4ae8885
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 0 deletions.
2 changes: 2 additions & 0 deletions datatype.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ const (
FieldSnowAmount
// FieldSnowHeight represents the SnowHeight data point
FieldSnowHeight
// FieldSunhours represents the sun hours data point
FieldSunhours
// FieldSunrise represents the Sunrise data point
FieldSunrise
// FieldSunset represents the Sunset data point
Expand Down
47 changes: 47 additions & 0 deletions duration.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// SPDX-FileCopyrightText: 2023 Winni Neessen <wn@neessen.dev>
//
// SPDX-License-Identifier: MIT

package meteologix

import (
"fmt"
"math"
"time"
)

// Duration is a type wrapper of an WeatherData for holding height values in WeatherData
// (based on meters a default unit)
type Duration WeatherData

// IsAvailable returns true if an Duration value was available at time of query
func (d Duration) IsAvailable() bool {
return !d.notAvailable
}

// DateTime returns the timestamp associated with the Duration value
func (d Duration) DateTime() time.Time {
return d.dateTime
}

// String satisfies the fmt.Stringer interface for the Duration type
func (d Duration) String() string {
return fmt.Sprintf("%.2fh", d.floatVal)
}

// Source returns the Source of Duration
//
// If the Source is not available it will return SourceUnknown
func (d Duration) Source() Source {
return d.source
}

// Value returns the float64 value of an Duration
//
// If the Duration is not available in the WeatherData, Value will return math.NaN instead.
func (d Duration) Value() float64 {
if d.notAvailable {
return math.NaN()
}
return d.floatVal
}
22 changes: 22 additions & 0 deletions forecast.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ type APIWeatherForecastData struct {
Dewpoint NilFloat64 `json:"dewpoint,omitempty"`
// PressureMSL represents barometric air pressure at mean sea level (at current timestamp)
PressureMSL NilFloat64 `json:"pressureMsl,omitempty"`
// SunHours represents the most probable amount of hours the sun will be visible
SunHours NilFloat64 `json:"sunHours,omitempty"`
// Temperature represents the predicted temperature at 2m height (at current timestamp)
Temperature float64 `json:"temp"`
// WindDirection represents the average direction from which the wind originates in degree
Expand All @@ -77,6 +79,7 @@ type WeatherForecastDatapoint struct {
humidity NilFloat64
isDay bool
pressureMSL NilFloat64
sunhours NilFloat64
temperature float64
winddirection NilFloat64
windgust NilFloat64
Expand Down Expand Up @@ -206,6 +209,23 @@ func (dp WeatherForecastDatapoint) PressureMSL() Pressure {
return pressure
}

// SunHours returns the sun hours data point as Duration.
//
// If the data point is not available in the WeatherForecast it will return Duration in which the
// "not available" field will be true.
func (dp WeatherForecastDatapoint) SunHours() Duration {
if dp.winddirection.IsNil() {
return Duration{notAvailable: true}
}
duration := Duration{
dateTime: dp.dateTime,
name: FieldSunhours,
source: SourceForecast,
floatVal: dp.sunhours.Get(),
}
return duration
}

// Temperature returns the temperature data point as Temperature.
func (dp WeatherForecastDatapoint) Temperature() Temperature {
return Temperature{
Expand Down Expand Up @@ -316,9 +336,11 @@ func newWeatherForecastDataPoint(data APIWeatherForecastData) WeatherForecastDat
humidity: data.Humidity,
isDay: data.IsDay,
pressureMSL: data.PressureMSL,
sunhours: data.SunHours,
temperature: data.Temperature,
winddirection: data.WindDirection,
windgust: data.WindGust,
windgust3h: data.WindGust3h,
windspeed: data.WindSpeed,
}
}

0 comments on commit 4ae8885

Please sign in to comment.