yr

package module
v0.0.0-...-34d77a2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 15, 2024 License: MIT Imports: 14 Imported by: 0

README

yr

Yr is a norwegian weather service. This module implements its API, so you can use it.

Before you start using this module please review their terms of service.

Review the example too se an example on how to use it. In general the data is not parsed for you, this module only provide the means for you to subscribe to and get notified when the weather is updated.

You can find some details about the data in Yr's documentation.

Important things to notice:

  • Review the User-Agent, as described in the terms of service. Change the value yr.YrAppHeader to match your user-agent before using this module.
  • When initializing the weather, specify a cache file that will be used to reduce the load on the remote servers.
  • Caching/expiration of data from yr is enforced as good as possible to adhere to their tos.
  • For use in an internal microservices architecture there are methods you can use to load data from an internal origin server. (YrOriginWeatherData vs YrMicroServiceWebData).
  • In the microservice model you can refresh the data as often as you want, but the data will still be the cached data from yr.
  • The Yr data model is built on Swagger, but this is GoLand's interpretation of the download data.

Documentation

Index

Constants

View Source
const InvalidData = 9999
View Source
const ModuleVersion = "0.0.10"

Variables

View Source
var YrAppHeader = "https://bitbucket.org/HelgeOlav/yr " + ModuleVersion // our app header, sent to Yr

Functions

This section is empty.

Types

type CompactTimeSeries

type CompactTimeSeries struct {
	Time time.Time `json:"time"`
	Data struct {
		Instant struct {
			Details struct {
				AirPressureAtSeaLevel float64 `json:"air_pressure_at_sea_level"`
				AirTemperature        float64 `json:"air_temperature"`
				CloudAreaFraction     float64 `json:"cloud_area_fraction"`
				RelativeHumidity      float64 `json:"relative_humidity"`
				WindFromDirection     float64 `json:"wind_from_direction"`
				WindSpeed             float64 `json:"wind_speed"`
			} `json:"details"`
		} `json:"instant"`
		Next12Hours struct {
			Summary struct {
				SymbolCode string `json:"symbol_code"`
			} `json:"summary"`
		} `json:"next_12_hours,omitempty"`
		Next1Hours struct {
			Summary struct {
				SymbolCode string `json:"symbol_code"`
			} `json:"summary"`
			Details struct {
				PrecipitationAmount float64 `json:"precipitation_amount"`
			} `json:"details"`
		} `json:"next_1_hours,omitempty"`
		Next6Hours struct {
			Summary struct {
				SymbolCode string `json:"symbol_code"`
			} `json:"summary"`
			Details struct {
				PrecipitationAmount float64 `json:"precipitation_amount"`
			} `json:"details"`
		} `json:"next_6_hours,omitempty"`
	} `json:"data"`
}

type Weather

type Weather struct {
	Result   YrCompactData `json:"result"`            // the data from Yr
	IsValid  bool          `json:"isValid"`           // if this result is valid
	IsCached bool          `json:"-"`                 // true if data was cached from file
	Expires  time.Time     `json:"expires,omitempty"` // when the current data expires
	Error    string        `json:"error,omitempty"`   // any error
}

Weather is the internal data structure that is sent to clients

func (*Weather) GetCurrentTemperature

func (w *Weather) GetCurrentTemperature() float64

GetCurrentTemperature returns the current temperature if the data is valid, otherwise InvalidData

func (*Weather) GetIndexForTime

func (w *Weather) GetIndexForTime(t time.Time) int

GetIndexForTime returns index for time given, or InvalidData if not in range

type YrCompactData

type YrCompactData struct {
	Type     string `json:"type"`
	Geometry struct {
		Type        string    `json:"type"`
		Coordinates []float64 `json:"coordinates"`
	} `json:"geometry"`
	Properties struct {
		Meta struct {
			UpdatedAt time.Time `json:"updated_at"`
			Units     struct {
				AirPressureAtSeaLevel string `json:"air_pressure_at_sea_level"`
				AirTemperature        string `json:"air_temperature"`
				CloudAreaFraction     string `json:"cloud_area_fraction"`
				PrecipitationAmount   string `json:"precipitation_amount"`
				RelativeHumidity      string `json:"relative_humidity"`
				WindFromDirection     string `json:"wind_from_direction"`
				WindSpeed             string `json:"wind_speed"`
			} `json:"units"`
		} `json:"meta"`
		Timeseries []CompactTimeSeries `json:"timeseries"`
	} `json:"properties"`
}

YrCompactData is the data returned from yr

type YrMicroServiceWebData

type YrMicroServiceWebData struct {
	Value utils.Broadcast[Weather] // the current data, as returned from Yr
	// contains filtered or unexported fields
}

YrMicroServiceWebData is the data set when loaded from an internal microservice

func NewYrMicroServiceWebData

func NewYrMicroServiceWebData(url string) *YrMicroServiceWebData

NewYrMicroServiceWebData returns a new instance where it is loading data from the origin server

func (*YrMicroServiceWebData) AutoUpdate

func (y *YrMicroServiceWebData) AutoUpdate(ctx context.Context, interval time.Duration)

AutoUpdate is a long-lived thread that updates the data from the source at given intervals

func (*YrMicroServiceWebData) Update

func (y *YrMicroServiceWebData) Update() error

Update retrieves data from the origin server, returning error if the update failed

type YrOriginWeatherData

type YrOriginWeatherData struct {
	Value utils.Broadcast[Weather] // the current data, as returned from Yr
	// contains filtered or unexported fields
}

YrOriginWeatherData represents the exposed API for the weather data

func NewYrWeatherData

func NewYrWeatherData(lat, lon float64, cacheFile string) *YrOriginWeatherData

NewYrWeatherData initializes the weather for a given place, caching data to a cacheFile

func (*YrOriginWeatherData) AutomaticUpdate

func (y *YrOriginWeatherData) AutomaticUpdate(ctx context.Context)

AutomaticUpdate is a background task that will update the data each time it expires. Context is used to signal that updating should be stopped.

func (*YrOriginWeatherData) ServeHTTP

func (y *YrOriginWeatherData) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP is a handler that will return the current Weather as JSON data. It also implements the http.Handler interface.

func (*YrOriginWeatherData) UpdateWeather

func (y *YrOriginWeatherData) UpdateWeather() error

UpdateWeather updates the weather if it is invalid or if the data has expired. error is always returned unless the data was updated.

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL