rivers

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jun 20, 2022 License: MIT Imports: 12 Imported by: 0

README

Go Go Report Card Maintainability Test Coverage

rivers

rivers is a Go library and a cli utility for reading water level and temperature data from stations located in rivers in Ireland. It allows to get data from more than 450 sensors located in 28 rivers.

Using the Go library

Import the library using:

import "github.com/qba73/rivers" 

Creating a client

Creat a new Client object by calling rivers.NewClient():

client := rivers.NewClient()

Retrieving latest water level redings

client.GetLatestWaterLevels()

or

rivers.GetLatestWaterLevels()

A complete example program

You can see an example programs which retrieves water level data in the examples/stations folder.

Bugs and feature requests

If you find a bug in the rivers client or library, please open an issue. Similarly, if you'd like a feature added or improved, let me know via an issue.

Not all the functionality of the water level is implemented yet.

Pull requests welcome!

Using the CLI utility for reading water levels

Get readings from stations:

$ waterlevel
time: 2022-06-20 05:30:00 +0000 UTC, station: Sandy Mills, id: 0000001041, regionid: 3, level: 0.25
time: 2022-06-20 05:15:00 +0000 UTC, station: Ballybofey, id: 0000001043, regionid: 3, level: 0.58
time: 2022-06-20 05:30:00 +0000 UTC, station: Glaslough, id: 0000003055, regionid: 10, level: 0.43
time: 2022-06-20 05:30:00 +0000 UTC, station: Cappog Bridge, id: 0000003058, regionid: 10, level: 0.45
time: 2022-06-20 05:30:00 +0000 UTC, station: Moyles Mill, id: 0000006011, regionid: 10, level: 0.39
[...]
time: 2022-06-20 05:30:00 +0000 UTC, station: Malin Head, id: 0000040060, regionid: 3, level: 1.81

Save readings to a file:

$ waterlevel > levels.txt

Documentation

Overview

Package rivers provides functionality to read water temperature and water level data recorded by sensors installed in rivers in Ireland.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func RunCLI

func RunCLI()

RunCLI executes program and prints out latest recorded water levels.

func RunPuller

func RunPuller()

RunPuller holds all required machinery to run the river data puller.

func RunServer

func RunServer()

Types

type Client

type Client struct {
	UserAgent  string
	BaseURL    string
	HTTPClient *http.Client
}

Client holds data required to communicate with the web service.

func NewClient

func NewClient() *Client

NewClient knows how to construct a new default rivers client. The client will be used to retrieve information about various measures recorded by sensors.

func (*Client) GetDayLevel

func (c *Client) GetDayLevel(stationID string) ([]Reading, error)

GetDayLevel knows how to return water level readings recorded for last 24hr period for the given stationID number.

func (*Client) GetDayTemperature

func (c *Client) GetDayTemperature(stationID string) ([]Reading, error)

GetDayTemperature knows how to return water temperature recorded for last 24hr period for the given stationID number.

func (*Client) GetDayVoltage

func (c *Client) GetDayVoltage(stationID string) ([]Reading, error)

GetDayVoltage knows how to return sensor voltage data recorded over last 24h.

func (*Client) GetGroupWaterLevel

func (c *Client) GetGroupWaterLevel(groupID int) ([]Reading, error)

GetGroupWaterLevel returns water level readings for stations that belong to provided groupID. The value of roupID should be between 1 and 28.

func (*Client) GetLatestWaterLevels

func (c *Client) GetLatestWaterLevels() ([]StationWaterLevelReading, error)

GetLatestWaterLevels returns latest water level readings from sensors.

func (*Client) GetMonthLevel

func (c *Client) GetMonthLevel(stationID string) ([]Reading, error)

GetMonthLevel knows how to return water level readings recorded for last 4 weeks period for the given stationID number.

func (*Client) GetMonthTemperature

func (c *Client) GetMonthTemperature(stationID string) ([]Reading, error)

GetMonthTemperature knows how to return water temperature recorded for last 4 weeks period for the given stationID number.

func (*Client) GetWeekLevel

func (c *Client) GetWeekLevel(stationID string) ([]Reading, error)

GetWeekLevel knows how to return water level readings recorded for last week period for the given stationID number.

func (*Client) GetWeekTemperature

func (c *Client) GetWeekTemperature(stationID string) ([]Reading, error)

GetWeekTemperature knows how to return water temperature recorded for last week period for the given stationID number.

type FileStore

type FileStore struct {
	// contains filtered or unexported fields
}

FileStore represents a data store.

func NewFileStore

func NewFileStore(path string) (*FileStore, error)

NewFileStore takes a path and creates a new file store. It errors if the filepath is empty.

func (*FileStore) Records

func (fs *FileStore) Records() ([]StationWaterLevelReading, error)

Records returns records stored in a file.

func (*FileStore) Save

func (fs *FileStore) Save(records []StationWaterLevelReading) error

Save takes a slice of records and saves them in a file.

type Puller

type Puller struct {
	Client   *Client
	Store    Saver
	Interval time.Duration
}

func NewPuller

func NewPuller() (*Puller, error)

func (*Puller) Run

func (p *Puller) Run() error

type Reading

type Reading struct {
	Name      string
	RefID     string
	Timestamp time.Time
	Value     float64
}

Reading represents water level recorded by a gauge at the particular time.

func LoadCSV

func LoadCSV(path string) ([]Reading, error)

LoadCSV knows how to open and read given csv file. Upon successful run it returns a slice of level structs.

func ReadCSV

func ReadCSV(r io.Reader) ([]Reading, error)

ReadCSV knows how to read a csv file containing readings from a gauge in a format: timestamp,level

func ReadGroupCSV

func ReadGroupCSV(r io.Reader) ([]Reading, error)

func (Reading) String

func (rd Reading) String() string

String prints out information about the reading.

type Saver

type Saver interface {
	Save(records []StationWaterLevelReading) error
}

Saver is the interface that wraps the basic Save method.

Save takes records and stores them in a store.

type Sensor

type Sensor struct {
	StationID   string `json:"station_id"`
	StationName string `json:"station_name"`
	Type        string `json:"type"`
	Value       string `json:"value"`
	Timestamp   string `json:"timestamp"`
	ErrorCode   int    `json:"err_code"`
	RegionID    string `json:"region_id"`
}

Sensor holds data from a station.

type SensorReading

type SensorReading struct {
	StationID   string
	StationName string
	SensorID    string
	RegionID    int
	Value       float64
	Timestamp   time.Time
	ErrCode     int
}

SensorReading represents data received from a sensor.

type Station

type Station struct {
	ID         string   `json:"id"`
	Name       string   `json:"name"`
	RegionID   int      `json:"region_id"`
	RegionName string   `json:"region_name"`
	Lat        float64  `json:"lat"`
	Long       float64  `json:"long"`
	Sensors    []Sensor `json:"sensors"`
}

Station represents a station with multiple sensors.

type StationWaterLevelReading

type StationWaterLevelReading struct {
	StationID  string    `json:"station_id"`
	Name       string    `json:"name,omitempty"`
	RegionID   int       `json:"region_id,omitempty"`
	Readtime   time.Time `json:"readtime"`
	WaterLevel float64   `json:"water_level"`
}

StationWaterLevelReading represents data received from a water level sensor.

func GetLatestWaterLevels

func GetLatestWaterLevels() ([]StationWaterLevelReading, error)

GetLatestWaterLevels returns latests readings from all stations.

This func uses default rivers' client under the hood.

type WaterLevelProvider

type WaterLevelProvider interface {
	GetLatestWaterLevels() []StationWaterLevelReading
}

WaterLevelProvider is the interface that wraps the GetLatestWaterLevels method.

GetLatestWaterLevels returns a slice of water level readings from sensors.

Directories

Path Synopsis
cmd
api
examples

Jump to

Keyboard shortcuts

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