waqi

package
v0.0.0-...-71ac371 Latest Latest
Warning

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

Go to latest
Published: May 13, 2021 License: MIT Imports: 10 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// DefaultURL is default WAQI service root URL
	DefaultURL = "https://api.waqi.info/"

	// DefaultCacheDuration is default WAQI service cache duration
	DefaultCacheDuration = 15 * time.Minute
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Error

type Error string

Error represents a service-specific error

func (Error) Error

func (e Error) Error() string

Error retrieves an error message from an instance of Error

func (Error) String

func (e Error) String() string

String converts an instance of Error into String

type Level

type Level string

Level is an air quality level value

const (
	// GoodLevel means air quality is considered satisfactory.
	// Maps to AQI from 0 to 50.
	GoodLevel Level = "good"

	// ModerateLevel means air quality is overall satisfactory
	// but there may be a moderate health concern for a very small number of people
	// who are unusually sensitive to air pollution
	// Maps to AQI from 51 to 100
	ModerateLevel Level = "moderate"

	// PossiblyUnhealthyLevel means members of sensitive groups may experience health effects.
	// Maps to AQI from 101 to 150.
	PossiblyUnhealthyLevel = "possibly_unhealthy"

	// UnhealthyLevel means everyone may begin to experience health effects
	// and members of sensitive groups may experience more serious health effects
	// Maps to AQI from 151 to 200.
	UnhealthyLevel Level = "unhealthy"

	// VeryUnhealthyLevel is means health warnings of emergency conditions.
	// Maps to AQI from 201 to 300.
	VeryUnhealthyLevel Level = "very_unhealthy"

	// HazardousLevel means a health alert.
	// Maps to AQI from 300 and above.
	HazardousLevel Level = "hazardous"
)

func CalcAQILevel

func CalcAQILevel(value float32) Level

CalcAQILevel calculates an air quality level for raw AQI value

func CalcCOLevel

func CalcCOLevel(value float32) Level

CalcCOLevel calculates a CO level for raw CO value

func CalcNO2Level

func CalcNO2Level(value float32) Level

CalcNO2Level calculates a NO2 level for raw NO2 value

func CalcO3Level

func CalcO3Level(value float32) Level

CalcO3Level calculates an O3 level for raw O3 value

func CalcPM10Level

func CalcPM10Level(value float32) Level

CalcPM10Level calculates a PM10 level for raw PM10 value

func CalcPM25Level

func CalcPM25Level(value float32) Level

CalcPM25Level calculates a PM2.5 level for raw PM2.5 value

func CalcSO2Level

func CalcSO2Level(value float32) Level

CalcSO2Level calculates a SO2 level for raw SO2 value

func (Level) String

func (level Level) String() string

String converts a value of Level into string

type Listener

type Listener interface {
	// Update handles a weather data update
	// prevStatus will be nil on first update
	// and not nil - on subsequent ones
	Update(status *Status, prevStatus *Status) error
}

Listener receives updates on air quality status

type Option

type Option func(*options)

Option is a configuration option for NewServer function

func CacheDurationOption

func CacheDurationOption(duration time.Duration) Option

CacheDurationOption sets max cache duration

func CachePathOption

func CachePathOption(path string) Option

CachePathOption sets path to cache file

func LoggerOption

func LoggerOption(logger *log.Logger) Option

LoggerOption sets logger instance

func TokenOption

func TokenOption(token string) Option

TokenOption sets access token

func URLOption

func URLOption(url string) Option

URLOption sets root URL

type Service

type Service interface {
	// GetByCity fetches current measurements for city
	GetByCity(city string) (*Status, error)

	// GetByStation fetches current measurements for station
	GetByStation(stationID int) (*Status, error)

	// GetByGeo fetches current measurements for geo coordinates
	GetByGeo(lat, lon float32) (*Status, error)

	// Subscribe adds a listener to updates
	Subscribe(stationID int, listener Listener)

	// Unsubscribe removes a listener
	Unsubscribe(stationID int, listener Listener)

	// StartUpdates starts background data updates
	StartUpdates()

	// StopUpdates stops background data updates
	StopUpdates()

	// Close shuts down service
	Close() error
}

Service is an entry point for WAQI service

func NewService

func NewService(fn ...Option) (Service, error)

NewService creates new instance of Service

type Station

type Station struct {
	// Unique ID for the city monitoring station.
	ID int `json:"id"`

	// Name of the monitoring station
	Name string `json:"name"`

	// URL of the monitoring station website
	URL string `json:"url"`

	// Latitude of the monitoring station
	Lon float32 `json:"lon"`

	// Longitude of the monitoring station
	Lat float32 `json:"lat"`
}

Station contains weather station information

type Status

type Status struct {
	Station *Station `json:"station"`

	// Measurement time
	Time time.Time `json:"time"`

	// Air quality index value
	AQI float32 `json:"aqi"`

	// Air quality index level
	Level Level `json:"level"`

	// Particulate matter 2.5 measurement
	PM25 *float32 `json:"pm25"`

	// Particulate matter 10 measurement
	PM10 *float32 `json:"pm10"`

	// Ozone measurement
	O3 *float32 `json:"o3"`

	// Nitrogen dioxide measurement
	NO2 *float32 `json:"no2"`

	// Sulfur dioxide measurement
	SO2 *float32 `json:"so2"`

	// Carbon monoxide level measurement
	CO *float32 `json:"co"`
}

Status contains aggregated air quality status

func (*Status) Equal

func (s *Status) Equal(other *Status) bool

Equal checks two Status values for equality

func (*Status) String

func (s *Status) String() string

String converts Status to string

Jump to

Keyboard shortcuts

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