weatherlink

package module
v1.0.9 Latest Latest
Warning

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

Go to latest
Published: Feb 28, 2023 License: MIT Imports: 11 Imported by: 1

README

Push

Go package for working with Davis Instruments Weatherlink protocol over a Weatherlink IP, serial, or USB interface.

Features:

  • Should work with any Davis station made after 2002. Developed using a Vantage Pro 2 Plus with all sensor types.
  • Device support for Weatherlink IP, serial, USB (genuine or clone).
  • Device simulator.
  • Decodes DMP (archive), EEPROM (configuration), HILOWS, LPS 1 (loop 1), and LPS 2 (loop 2) events and writes them to a channel.
  • Partial encoding (work in progress).
  • Sync console time.
  • Command broker that coordinates commands. Use the standard idler or define a custom one.

Installation

$ go get github.com/ebarkie/weatherlink

Usage

See USAGE.

Example

package main

import (
	"log"
	"os"
	"time"

	"github.com/ebarkie/weatherlink"
	"github.com/ebarkie/weatherlink/data"
)

func main() {
	// Enable weatherlink logging to standard output
	weatherlink.Error.SetOutput(os.Stdout)
	weatherlink.Warn.SetOutput(os.Stdout)
	weatherlink.Info.SetOutput(os.Stdout)

	// Open station
	w, err := weatherlink.Dial("192.168.1.254:22222")
	if err != nil {
		log.Fatal(err)
	}
	defer w.Close()
	log.Println("Opened station")

	// Goroutine to receive station events
	go func() {
		// Standard idler which reads loop packets and new archive
		// records when they're available.
		ec := w.Start(weatherlink.StdIdle)
		log.Println("Command broker started")

		// Keep retrieving events until the channel is closed
		for e := range ec {
			switch e.(type) {
			case data.Archive:
				log.Printf("Received archive record: %+v", e)
			case data.EEPROM:
				log.Printf("Received EEPROM configuration: %+v", e)
			case data.HiLows:
				log.Printf("Received record high and lows: %+v", e)
			case data.Loop:
				log.Printf("Received loop packet: %+v", e)
			default:
				log.Printf("Received unknown event of type: %T", e)
			}
		}
		log.Println("Command broker stopped")
	}()

	// Send an explicit command
	w.Q <- weatherlink.GetHiLows

	// Run for a period of time and then send a stop signal
	runTime := 6 * time.Minute
	log.Printf("Receiving events for %s", runTime)
	time.Sleep(runTime)
	log.Println("Stopping command broker")
	w.Stop()
}

License

Copyright (c) 2016-2020 Eric Barkie. All rights reserved.
Use of this source code is governed by the MIT license that can be found in the LICENSE file.

Documentation

Overview

Package weatherlink implements the Davis Instruments serial, USB, and TCP/IP communication protocol.

Index

Constants

View Source
const (
	GetDmps cmd = iota
	GetEEPROM
	GetHiLows
	GetLoops
	LampsOff
	LampsOn
	Stop
	SyncConsTime
)

Commands.

Variables

View Source
var (
	Trace = log.New(io.Discard, "[TRCE]", log.LstdFlags|log.Lmicroseconds|log.Lshortfile)
	Debug = log.New(io.Discard, "[DBUG]", log.LstdFlags|log.Lshortfile)
	Info  = log.New(io.Discard, "[INFO]", log.LstdFlags)
	Warn  = log.New(io.Discard, "[WARN]", log.LstdFlags|log.Lshortfile)
	Error = log.New(io.Discard, "[ERRO]", log.LstdFlags|log.Lshortfile)
)

Loggers.

View Source
var (
	ConsTimeSyncFreq = 24 * time.Hour
)

Tunables.

View Source
var (
	ErrCmdFailed = errors.New("command failed")
)

Errors.

View Source
var Sdump = func(i ...interface{}) (s string) {
	return fmt.Sprintf(strings.Repeat("%+v\n", len(i)), i...)
}

Sdump returns a variable as a string. It includes field names and pointers, if applicable.

Functions

func StdIdle

func StdIdle(c *Conn, ec chan<- interface{}) (err error)

StdIdle is the standard idler which reads loop packets and new archive records when they're available.

Types

type Conn

type Conn struct {
	LastDmp   time.Time // Time of the last downloaded archive record
	NewArcRec bool      // Indicates a new archive record is available

	Q chan cmd // Command queue
	// contains filtered or unexported fields
}

Conn holds the weatherlink connnection context.

func Dial

func Dial(addr string) (c Conn, err error)

Dial establishes the weatherlink connection.

func (Conn) Close

func (c Conn) Close() error

Close closes the weatherlink connection.

func (Conn) GetConsTime

func (c Conn) GetConsTime() (time.Time, error)

GetConsTime gets the console time.

func (Conn) GetDmps

func (c Conn) GetDmps(ec chan<- interface{}, lastRec time.Time) (newLastRec time.Time, err error)

GetDmps downloads all archive records *after* lastRec and sends them to the event channel ordered from oldest to newest. It returns the time of the last record it read.

If lastRec does not match an existing archive timestamp (which is the case if left uninitialized) then all records in memory are returned.

func (Conn) GetEEPROM

func (c Conn) GetEEPROM(ec chan<- interface{}) error

GetEEPROM retrieves the entire EEPROM configuration.

func (Conn) GetFirmBuildTime added in v1.0.7

func (c Conn) GetFirmBuildTime() (time.Time, error)

GetFirmBuildTime gets the firmware build time.

func (Conn) GetFirmVer added in v1.0.5

func (c Conn) GetFirmVer() (string, error)

GetFirmVer gets the firmware version number.

func (Conn) GetHiLows

func (c Conn) GetHiLows(ec chan<- interface{}) error

GetHiLows retrieves the record high and lows.

func (*Conn) GetLoops

func (c *Conn) GetLoops(ec chan<- interface{}) (err error)

GetLoops starts a stream of loop packets and sends them to the event channel. It exits when numLoops is hit, an archive record was written, or a command is pending.

func (Conn) SetLamps

func (c Conn) SetLamps(on bool) (err error)

SetLamps sets the console lamps state.

func (*Conn) Start

func (c *Conn) Start(idle Idler) <-chan interface{}

Start starts the command broker. If no commands are pending it runs the idler.

func (Conn) Stop

func (c Conn) Stop()

Stop stops the command broker.

func (Conn) SyncConsTime

func (c Conn) SyncConsTime() error

SyncConsTime synchronizes the console time with the local system time if the offset exceeds 10 seconds.

type Idler

type Idler func(*Conn, chan<- interface{}) error

Idler is the idle function the command broker executes when there are no pending commands in the queue.

Directories

Path Synopsis
Package calc implements weather calculations.
Package calc implements weather calculations.
Package data implements encoding and decoding of Davis Instruments binary data types.
Package data implements encoding and decoding of Davis Instruments binary data types.
internal
Package packet implements getters and setters for Davis Instruments packets.
Package packet implements getters and setters for Davis Instruments packets.
Package units implements simple unit conversion functions.
Package units implements simple unit conversion functions.

Jump to

Keyboard shortcuts

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