twomqtt

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Aug 31, 2019 License: MIT Imports: 8 Imported by: 0

README

twomqtt

Some useful bits that I've used making a handful of Golang MQTT applications. I'm under no illusion that other people will find these useful, but just in case... enjoy!

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func MQTTDiscoveryOverride

func MQTTDiscoveryOverride(field reflect.StructField) (string, bool)

MQTTDiscoveryOverride -

func MQTTOverride

func MQTTOverride(field reflect.StructField) (string, bool)

MQTTOverride -

func SimpleKVMapParser

func SimpleKVMapParser(kvSep string, itemSep string) func(v string) (interface{}, error)

SimpleKVMapParser - Aids parsinng simple key:value,key:value,key:value mapping config items

Types

type Command

type Command = int64

Command - A type used to communicate commands between publisher and observer

type Event

type Event struct {
	Type    reflect.Type
	Payload interface{}
}

Event - A type used to communicate state between publisher and observer

type GeneralConfig

type GeneralConfig struct {
	DebugLogLevel bool `env:"DEBUG" envDefault:"false"`
}

GeneralConfig - Configuration applicable to the genereal application

type MQTTDiscovery

type MQTTDiscovery struct {
	// Used for topic generation; never output in discovery JSON
	DiscoveryName   string `json:"-"`
	DiscoveryPrefix string `json:"-"`
	Component       string `json:"-"`
	NodeID          string `json:"-"`
	ObjectID        string `json:"-"`

	// Used in discovery JSON
	AuxCommandTopic          string `json:"aux_command_topic,omitempty"`
	AuxStateTemplate         string `json:"aux_state_template,omitempty"`
	AuxStateTopic            string `json:"aux_state_topic,omitempty"`
	AvailabilityTopic        string `json:"availability_topic,omitempty"`
	AwayModeCommandTopic     string `json:"away_mode_command_topic,omitempty"`
	AwayModeStateTemplate    string `json:"away_mode_state_template,omitempty"`
	AwayModeStateTopic       string `json:"away_mode_state_topic,omitempty"`
	BrightnessCommandTopic   string `json:"brightness_command_topic,omitempty"`
	BrightnessScale          string `json:"brightness_scale,omitempty"`
	BrightnessStateTopic     string `json:"brightness_state_topic,omitempty"`
	BrightnessValueTemplate  string `json:"brightness_value_template,omitempty"`
	ColorTempCommandTopic    string `json:"color_temp_command_topic,omitempty"`
	ColorTempStateTopic      string `json:"color_temp_state_topic,omitempty"`
	ColorTempValueTemplate   string `json:"color_temp_value_template,omitempty"`
	CommandTopic             string `json:"command_topic,omitempty"`
	CurrentTemperatureTopic  string `json:"current_temperature_topic,omitempty"`
	DeviceClass              string `json:"device_class,omitempty"`
	EffectCommandTopic       string `json:"effect_command_topic,omitempty"`
	EffectList               string `json:"effect_list,omitempty"`
	EffectStateTopic         string `json:"effect_state_topic,omitempty"`
	EffectValueTemplate      string `json:"effect_value_template,omitempty"`
	ExpireAfter              string `json:"expire_after,omitempty"`
	FanModeCommandTopic      string `json:"fan_mode_command_topic,omitempty"`
	FanModeStateTemplate     string `json:"fan_mode_state_template,omitempty"`
	FanModeStateTopic        string `json:"fan_mode_state_topic,omitempty"`
	ForceUpdate              string `json:"force_update,omitempty"`
	HoldCommandTopic         string `json:"hold_command_topic,omitempty"`
	HoldStateTemplate        string `json:"hold_state_template,omitempty"`
	HoldStateTopic           string `json:"hold_state_topic,omitempty"`
	Icon                     string `json:"icon,omitempty"`
	Initial                  string `json:"initial,omitempty"`
	JSONAttributesTopic      string `json:"json_attributes_topic,omitempty"`
	JSONAttributes           string `json:"json_attributes,omitempty"`
	MaxTemp                  string `json:"max_temp,omitempty"`
	MinTemp                  string `json:"min_temp,omitempty"`
	ModeCommandTopic         string `json:"mode_command_topic,omitempty"`
	ModeStateTemplate        string `json:"mode_state_template,omitempty"`
	ModeStateTopic           string `json:"mode_state_topic,omitempty"`
	Name                     string `json:"name,omitempty"`
	OnCommandType            string `json:"on_command_type,omitempty"`
	Optimistic               string `json:"optimistic,omitempty"`
	OscillationCommandTopic  string `json:"oscillation_command_topic,omitempty"`
	OscillationStateTopic    string `json:"oscillation_state_topic,omitempty"`
	OscillationValueTemplate string `json:"oscillation_value_template,omitempty"`
	PayloadArmAway           string `json:"payload_arm_away,omitempty"`
	PayloadArmHome           string `json:"payload_arm_home,omitempty"`
	PayloadAvailable         string `json:"payload_available,omitempty"`
	PayloadClose             string `json:"payload_close,omitempty"`
	PayloadDisarm            string `json:"payload_disarm,omitempty"`
	PayloadHighSpeed         string `json:"payload_high_speed,omitempty"`
	PayloadLock              string `json:"payload_lock,omitempty"`
	PayloadLowSpeed          string `json:"payload_low_speed,omitempty"`
	PayloadMediumSpeed       string `json:"payload_medium_speed,omitempty"`
	PayloadNotAvailable      string `json:"payload_not_available,omitempty"`
	PayloadOff               string `json:"payload_off,omitempty"`
	PayloadOn                string `json:"payload_on,omitempty"`
	PayloadOpen              string `json:"payload_open,omitempty"`
	PayloadOscillationOff    string `json:"payload_oscillation_off,omitempty"`
	PayloadOscillationOn     string `json:"payload_oscillation_on,omitempty"`
	PayloadStop              string `json:"payload_stop,omitempty"`
	PayloadUnlock            string `json:"payload_unlock,omitempty"`
	PowerCommandTopic        string `json:"power_command_topic,omitempty"`
	Retain                   string `json:"retain,omitempty"`
	RgbCommandTemplate       string `json:"rgb_command_template,omitempty"`
	RgbCommandTopic          string `json:"rgb_command_topic,omitempty"`
	RgbStateTopic            string `json:"rgb_state_topic,omitempty"`
	RgbValueTemplate         string `json:"rgb_value_template,omitempty"`
	SendIfOff                string `json:"send_if_off,omitempty"`
	SetPositionTemplate      string `json:"set_position_template,omitempty"`
	SetPositionTopic         string `json:"set_position_topic,omitempty"`
	SpeedCommandTopic        string `json:"speed_command_topic,omitempty"`
	SpeedStateTopic          string `json:"speed_state_topic,omitempty"`
	SpeedValueTemplate       string `json:"speed_value_template,omitempty"`
	Speeds                   string `json:"speeds,omitempty"`
	StateClosed              string `json:"state_closed,omitempty"`
	StateOff                 string `json:"state_off,omitempty"`
	StateOn                  string `json:"state_on,omitempty"`
	StateOpen                string `json:"state_open,omitempty"`
	StateTopic               string `json:"state_topic,omitempty"`
	StateValueTemplate       string `json:"state_value_template,omitempty"`
	SwingModeCommandTopic    string `json:"swing_mode_command_topic,omitempty"`
	SwingModeStateTemplate   string `json:"swing_mode_state_template,omitempty"`
	SwingModeStateTopic      string `json:"swing_mode_state_topic,omitempty"`
	TemperatureCommandTopic  string `json:"temperature_command_topic,omitempty"`
	TemperatureStateTemplate string `json:"temperature_state_template,omitempty"`
	TemperatureStateTopic    string `json:"temperature_state_topic,omitempty"`
	TiltClosedValue          string `json:"tilt_closed_value,omitempty"`
	TiltCommandTopic         string `json:"tilt_command_topic,omitempty"`
	TiltInvertState          string `json:"tilt_invert_state,omitempty"`
	TiltMax                  string `json:"tilt_max,omitempty"`
	TiltMin                  string `json:"tilt_min,omitempty"`
	TiltOpenedValue          string `json:"tilt_opened_value,omitempty"`
	TiltStatusOptimistic     string `json:"tilt_status_optimistic,omitempty"`
	TiltStatusTopic          string `json:"tilt_status_topic,omitempty"`
	Topic                    string `json:"topic,omitempty"`
	UniqueID                 string `json:"unique_id,omitempty"`
	UnitOfMeasurement        string `json:"unit_of_measurement,omitempty"`
	ValueTemplate            string `json:"value_template,omitempty"`
	WhiteValueCommandTopic   string `json:"white_value_command_topic,omitempty"`
	WhiteValueStateTopic     string `json:"white_value_state_topic,omitempty"`
	WhiteValueTemplate       string `json:"white_value_template,omitempty"`
	XyCommandTopic           string `json:"xy_command_topic,omitempty"`
	XyStateTopic             string `json:"xy_state_topic,omitempty"`
	XyValueTemplate          string `json:"xy_value_template,omitempty"`
}

MQTTDiscovery - Aid creating/publishing Home-Assistant MQTT Discovery.

type MQTTProxy

type MQTTProxy struct {
	MQTTProxyConfig
	Client mqtt.Client
	// contains filtered or unexported fields
}

MQTTProxy - A wrapper for the Paho MQTT

func NewMQTTProxy

func NewMQTTProxy(mqttCfg MQTTProxyConfig) *MQTTProxy

NewMQTTProxy - Create a proxy for an MQTT Client

func (*MQTTProxy) AvailabilityTopic

func (c *MQTTProxy) AvailabilityTopic() string

AvailabilityTopic - Generate the availability topic

func (*MQTTProxy) CommandTopic

func (c *MQTTProxy) CommandTopic(name string, sensor string) string

CommandTopic - Generate the command topic for a named sensor

func (*MQTTProxy) Initialize

func (c *MQTTProxy) Initialize(onConn mqtt.OnConnectHandler, onLost mqtt.ConnectionLostHandler)

Initialize - Initialize the MQTT Client with all the appropriate settings

func (*MQTTProxy) LastPublishedOnTopic

func (c *MQTTProxy) LastPublishedOnTopic(topic string) string

LastPublishedOnTopic - Determine what was last published on a topic

func (*MQTTProxy) LogSettings

func (c *MQTTProxy) LogSettings()

LogSettings - Logs the current settings

func (*MQTTProxy) NewMQTTDiscovery

func (c *MQTTProxy) NewMQTTDiscovery(name string, sensor string, sensorType string) *MQTTDiscovery

NewMQTTDiscovery - Proxy a new MQTTDiscovery object

func (*MQTTProxy) Publish

func (c *MQTTProxy) Publish(topic string, payload string)

Publish - Publish a topic and payload via the MQTTClientWrapperWrapper

func (*MQTTProxy) PublishDiscovery

func (c *MQTTProxy) PublishDiscovery(mqd *MQTTDiscovery)

PublishDiscovery - Publish a MQTTDiscovery

func (*MQTTProxy) Run

func (c *MQTTProxy) Run()

Run - Connect to MQTT

func (*MQTTProxy) StateTopic

func (c *MQTTProxy) StateTopic(name string, sensor string) string

StateTopic - Generate the state topic for a named sensor

func (*MQTTProxy) Subscribe

func (c *MQTTProxy) Subscribe(topic string, qos byte, callback mqtt.MessageHandler) mqtt.Token

Subscribe - Subscribe to a topic and payload via the MQTTClientWrapperWrapper

type MQTTProxyConfig

type MQTTProxyConfig struct {
	ClientID        string `env:"MQTT_CLIENTID"`
	Broker          string `env:"MQTT_BROKER" envDefault:"tcp://test.mosquitto.org:1883"`
	Username        string `env:"MQTT_USERNAME"`
	Password        string `env:"MQTT_PASSWORD"`
	TopicPrefix     string `env:"MQTT_TOPICPREFIX"`
	Discovery       bool   `env:"MQTT_DISCOVERY" envDefault:"false"`
	DiscoveryPrefix string `env:"MQTT_DISCOVERYPREFIX" envDefault:"homeassistant"`
	DiscoveryName   string `env:"MQTT_DISCOVERYNAME"`
}

MQTTProxyConfig - Configuration applicable to the MQTTProxy

type MoqMessage

type MoqMessage struct {
	TopicSrc   string
	PayloadSrc string
}

MoqMessage - Aid testing MQTT callbacks

func (*MoqMessage) Ack

func (m *MoqMessage) Ack()

Ack - I'm just here so I don't get fined

func (*MoqMessage) Duplicate

func (m *MoqMessage) Duplicate() bool

Duplicate - I'm just here so I don't get fined

func (*MoqMessage) MessageID

func (m *MoqMessage) MessageID() uint16

MessageID - I'm just here so I don't get fined

func (*MoqMessage) Payload

func (m *MoqMessage) Payload() []byte

Payload - I'm just here so I don't get fined

func (*MoqMessage) Qos

func (m *MoqMessage) Qos() byte

Qos - I'm just here so I don't get fined

func (*MoqMessage) Retained

func (m *MoqMessage) Retained() bool

Retained - I'm just here so I don't get fined

func (*MoqMessage) Topic

func (m *MoqMessage) Topic() string

Topic - I'm just here so I don't get fined

type Observer

type Observer interface {
	ReceiveState(Event)
	ReceiveCommand(Command, Event)
}

Observer -

type Publisher

type Publisher interface {
	Register(Observer)
}

Publisher -

Jump to

Keyboard shortcuts

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