lights

package module
v0.0.0-...-bac74aa Latest Latest
Warning

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

Go to latest
Published: Sep 23, 2017 License: ISC Imports: 14 Imported by: 0

README

Schedule

Lighting app utilities in Go library.

Documentation

Overview

Package lights provides standardized shared functionality used across all Go-based agent software running on light controller devices. Most of the agents behavior has a default setting that will be "standard" when running on devices. However, all the settings can be modified by setting a variety of environment variables. The current environment variables understood by agents include:

### INC_DEVICE_ID

Sets the device ID. The default device ID is formed using the hardware (MAC) address of the wlan0 or en0 network interfaces if they exist. For Intel Edison, wlan0 will be a reliable unique ID for each module. Override the value of the device ID by setting the `INC_DEVICE_ID` variable to any arbitrary string. Device IDs are used for creating nsq topic names and reported in logs.

### INC_NSQD

Sets the nsqd address to use when agents post messages. The default is to use the address `127.0.0.1:4150` assuming that nsqd is running locally (which is typical of an nsq deployment). You can override the setting using the INC_NSQD_TCP environment variable setting `host:port` as the value.

### INC_NSQLOOKUPD

Sets the nsqlookupd address to use when agents want to subscribe to messages. The default is to use the address `nsqlookupd.local:4161` which could be set using either rendezvous or editing the /etc/hosts file. In some cases, it is better to set the nsqlookupd address manually. The environment variable can contain more than one address separated by commas. For example:

INC_NSQLOOKUPD=192.168.0.64:4161,192.168.0.61:4161

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AddrToID

func AddrToID(addr net.HardwareAddr) string

AddrToID converts a network hardware address to a string device ID.

func AgentPort

func AgentPort(agent string) string

AgentPort looks up the correct port for an agent by name.

func CheckIf

func CheckIf(err error, messages ...string) bool

CheckIf returns true if the provided error is non-nil. If messages are provided they are logged whether the error occurred or not.

func PanicIf

func PanicIf(err error, messages ...string)

PanicIf checks if the provided error is non-nil and panics. If messages are provided they are logged whether the error occurred or not.

func PanicOrID

func PanicOrID() string

PanicOrID either produces an ID or panics if one can't be generated.

func ParseColorCode

func ParseColorCode(colorCode string) (color.Color, error)

ParseColorCode parses a color code in either #RGB or #RRGGBB formats following CSS color specifications. It returns the color found or an error if the color is not a valid color code.

func PrepPath

func PrepPath(path string) (string, error)

PrepPath prepares a path for use by cleaning and converting to an absolute path.

func ReadFileAsStringOrPanic

func ReadFileAsStringOrPanic(path string) string

ReadFileAsStringOrPanic reads a file content as utf-8 encoded string or panics if there was a problem.

func ReadFileOrPanic

func ReadFileOrPanic(path string) []byte

ReadFileOrPanic reads in a file or panics if there was a problem.

Types

type Command

type Command struct {
	Action string
	Type   string
	ID     string
	Parts  []string
}

Command represents an Inception system command. Parts are split using `|` characters.

func NewCommand

func NewCommand(cmd string) (*Command, error)

NewCommand parses a command string to obtain it's command, message, and ID. An error is returned if the command or message is not recognized.

type DeviceID

type DeviceID struct {
	ID string
}

DeviceID represents a device's ID. The class provides some extra helpers for determining equality of IDs across systems (many IDs may) be shortened to the shortest unique ID (similar to git commit IDs).

func NewID

func NewID() (*DeviceID, error)

NewID produces the device ID for an agent. The ID will be taken from the following in order of priority:

1. INC_DEVICE_ID environmental variable 2. Network MAC address

func (*DeviceID) ContainedIn

func (d *DeviceID) ContainedIn(ids []string) bool

ContainedIn returns true if this ID is matched to any of IDs in the provided string slice.

func (*DeviceID) Equals

func (d *DeviceID) Equals(id string) bool

Equals returns true if the given ID exactly equals this ID.

func (*DeviceID) Matches

func (d *DeviceID) Matches(id string) bool

Matches returns true if the given ID matchs this ID. The provided ID can be a shortened version of this ID.

func (*DeviceID) String

func (d *DeviceID) String() string

Returns this device ID as a string.

type FileStore

type FileStore struct {
	Base string // The path to the file store base
}

FileStore implements the Store interface by storing each value in a file named after the item ID and folders for each collection. Note that IDs and collections must be file name friendly. On disk, the files will have a `.txt` file extension added.

func NewFileStore

func NewFileStore(base ...string) (*FileStore, error)

NewFileStore creates a new file-based Store implementation. Pass in an optional base path to use for data storage (otherwise the user's home directory is used).

func (*FileStore) Load

func (f *FileStore) Load(collection string) ([]string, error)

Load all the values for a collection.

func (*FileStore) Read

func (f *FileStore) Read(collection, id string) (string, error)

Read a value from the provided collection and ID.

func (*FileStore) Remove

func (f *FileStore) Remove(collection, id string) error

Remove a value from the provided collection and ID.

func (*FileStore) RemoveAll

func (f *FileStore) RemoveAll(collection string) error

RemoveAll removes all items from a collection.

func (*FileStore) Write

func (f *FileStore) Write(collection, id, value string) error

Write a value to the provided collection and ID.

type MockStore

type MockStore struct {
	Data map[string]map[string]string
}

MockStore is used to test services that rely on Store implementations.

func (*MockStore) Load

func (s *MockStore) Load(collection string) ([]string, error)

Load all the values from a collection.

func (*MockStore) Read

func (s *MockStore) Read(collection, id string) (string, error)

Read a value from the provided collection with a given ID.

func (*MockStore) Remove

func (s *MockStore) Remove(collection, id string) error

Remove a value from the provided collection with a given ID.

func (*MockStore) RemoveAll

func (s *MockStore) RemoveAll(collection string) error

RemoveAll clears all items from a collection.

func (*MockStore) Reset

func (s *MockStore) Reset()

Reset removes all data from the store.

func (*MockStore) Write

func (s *MockStore) Write(collection, id, value string) error

Write a value to the provided collection with a given ID.

type Pattern

type Pattern struct {
	ID    string
	Loops int
	Slots []*Slot
}

Pattern captures all data needed for a light pattern.

func NewPattern

func NewPattern(pattern string) (*Pattern, error)

NewPattern creates a pattern from a pattern specification string.

type Slot

type Slot struct {
	Color      color.Color
	Fade       time.Duration
	Hold       time.Duration
	Transition string
}

Slot captures the information about a single slot in a pattern.

func NewSlot

func NewSlot(slot string) (s *Slot, err error)

NewSlot creates a slot from a slot specification.

type Store

type Store interface {
	// Read a value from the provided collection with a given ID.
	Read(collection, id string) (string, error)
	// Write a value to the provided collection with a given ID.
	Write(collection, id, value string) error
	// Remove a value from the provided collection with a given ID.
	Remove(collection, id string) error
	// Remove all values from the provided collection.
	RemoveAll(collection string) error
	// Load reads all the values out of a collection.
	Load(colection string) ([]string, error)
}

Store is implemented by data storage providers for persistent configuration information.

type Worker

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

Worker takes care of the common worker tasks that all message driven agents must carry out. The worker takes care of bootstrapping the system.

func NewWorker

func NewWorker(agent string) (*Worker, error)

NewWorker creates a new worker ready for configuration. Call Start() on the worker to begin processing messages. Returns an error if there was a problem creating the worker. If an ID is provided the worker will use it, otherwise an ID will automatically be generated using lights.NewID().

func (*Worker) Consumer

func (w *Worker) Consumer(handler WorkerFunc) error

Consumer creates a new API /command Consumer for the worker.

func (*Worker) Handler

func (w *Worker) Handler(route string, handler WorkerFunc) error

Handler registers a new API route handler for the worker.

func (*Worker) Send

func (w *Worker) Send(agent, message string)

Send transmits a message to an agent.

func (*Worker) Start

func (w *Worker) Start() error

Start begins processing commands blocking the thread.

func (*Worker) Status

func (w *Worker) Status(agent, message string)

Status transmits a status update to an agent.

func (*Worker) Transmit

func (w *Worker) Transmit(agent, route, message string, consolidate bool)

Transmit reliably sends a message to another agent using HTTP. Set consolidate to true if messages sent to the same agent and route should only transmit the last message when an agent is offline. If consolidate is false, messages queued for later delivery will all be delivered when the agent is reachable again.

type WorkerFunc

type WorkerFunc func(message string) error

WorkerFunc handles incoming string messages returning an error if the message could not be handled.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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