statemanagement

package
v0.0.0-...-8969683 Latest Latest
Warning

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

Go to latest
Published: May 16, 2014 License: MIT Imports: 10 Imported by: 1

Documentation

Overview

OnOffMonitor Author: TheDistractor, aka-lightbulb A HouseMon Gadget to allow you to react to Boolean (0/1) inputs within a 'time' context

This Gadget came about as I wanted to react to situations when I CAN/CANNOT detect movement over a set period - I wanted to make sure lights can be turned off in a rooms where there may be no occupants, just like a traditional time-delay switch may operate, or turn heating on when a room seems to have been occupied for a while, or switch a relay off after it has been running for a set period of time etc, but with the ability to combine this output with other environmental inputs to make a bigger 'decision tree'.

Specifically useful for sensor outputs that emit a 0 or 1 [Off / On] (Inverted values also supported) The Gadget takes a Flow.Tag feed that mimic's the Tag/Msg format used with MQTTSub, where Tag is the MQTT Topic and Msg is the Value

The use-case is that input using 'sensor' topics will be the primary circuit feed (as per MQTTSub or DataSub), but any input conforming to the '<sensor>/<location>/<attribute>/<timestamp> 0|1 should work. Infact, <anything>/<filteredname>/<optionals...>/<timestamp> 0!1 should also work

A Typical use would be to listen for sensor 'moved' events (as in RoomNode), or to listen for reed switch open/close etc, and then to monitor this state and generate further time-based events such as:

On T (as soon as an On event is seen)
Off T (as soon as an Off event is seen)
On-Since T (the attribute has not changed from this state Since T)
Off-Since T (ditto)
On-For D (the attribute has been at this state for the duration defined by D)
Off-For D (ditto)

emitted messages take the form:

by/lb/oomon/<location>/<eventName>/<state>[-modifier]   UnixTimeMS | Duration

And would typically be used as input to the MQTTPub component

where:

<location> relates to the sensors 'Location' as defined by conforming input above.
(as stated above, a 'sensor' message typically looks like: sensor/garage/moved/<timestamp> 0|1 )

<eventName> is a name you (as the user) provide as a parameter. 'onoff' is used by default
I use 'motion' for roomnode 'moved' detection, but you can use anything.

<state> will be either 'On' or 'Off' (an Inverted flag can be used as feed input to invert the usage).

[-modifier] will be one of:
  '-Since' - descibed above
  '-For' - described above

The Value of each emitted message will be the 'reference' time of the event in UnixTime(Millisecond) format or standard go 'Duration' syntax for -For messages.

See the example circuits for usage

Index

Constants

View Source
const (
	MaskSince = "%s/%s/%s/%s-Since" //the mask we use to generate a 'Since'  basename/<location>/<eventname>/<direction>-Since
	MaskOnOff = "%s/%s/%s/%s"       //the mask we use to generate On/Off basename/<location>/<eventname>/<direction>
	MaskFor   = "%s/%s/%s/%s-For"   //the mask we use to generate For basename/<location>/<eventname>/<direction>-For
)

Variables

This section is empty.

Functions

func MsUnix

func MsUnix(s int64, m int64) time.Time

Create Time using Unix seconds and MilliSeconds

func UnixMs

func UnixMs(t time.Time) int64

Time to Unix with ms resolution

Types

type OnOffMonitor

type OnOffMonitor struct {
	flow.Gadget
	Filter    flow.Input //Feed to set the Filters of Messages we act upon
	Param     flow.Input //Feed to setup basic Parameters
	Threshold flow.Input //Feed to setup Duration thresholds for each .Filter input

	In  flow.Input  //Inboud Flow circuit messages
	Out flow.Output //Outbound Flow circuit messages

}

func NewOnOffMonitor

func NewOnOffMonitor() *OnOffMonitor

create a new instance of OnOffMonitor with appropriate construction

func (*OnOffMonitor) Run

func (w *OnOffMonitor) Run()

Start listening to the circuit inputs

type OnOffMonitorInst

type OnOffMonitorInst struct {
	Watched map[string]*OnOffState //we manage each of these 'watches' as a separate state.
	// contains filtered or unexported fields
}

func NewOnOffMonitorInst

func NewOnOffMonitorInst() *OnOffMonitorInst

func (*OnOffMonitorInst) NewState

func (w *OnOffMonitorInst) NewState(createDate int64, name string) *OnOffState

create a new State with correct initial construction

func (*OnOffMonitorInst) RecalcNextFor

func (s *OnOffMonitorInst) RecalcNextFor() (time.Time, error)

find the closest remaining timeslot

type OnOffState

type OnOffState struct {
	Name       string                        //a moniker for our state
	On         int64                         //last time we saw On
	Off        int64                         //last time we saw Off
	Current    float64                       //the current state of the flag
	Thresholds map[string]*ThresholdDuration //map of Durations used for this State Item
	Remaining  []int64                       //map of remaining durations
	// contains filtered or unexported fields
}

func (*OnOffState) AddThreshold

func (s *OnOffState) AddThreshold(text string) bool

attempt to add this input threshold to the state

func (*OnOffState) ExpiredThresholds

func (s *OnOffState) ExpiredThresholds(now time.Time) ([]string, error)

remove and return any durations that have 'passed' as a series of durations now is 'common' across all OnOffStates

func (*OnOffState) NextRemaining

func (s *OnOffState) NextRemaining() (int64, error)

peek the next timeslot

func (*OnOffState) ResetRemaining

func (s *OnOffState) ResetRemaining()

rebuild the Remaining Stack for the location

type ThresholdDuration

type ThresholdDuration struct {
	time.Duration
	Text string //the original string representation eg. 90s vs 1m30s
}

allows us to emit the user input duration string rather than the stringified duration

Jump to

Keyboard shortcuts

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