twomqtt

package module
v0.4.6 Latest Latest
Warning

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

Go to latest
Published: Dec 25, 2019 License: MIT Imports: 9 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 GeneralConfig

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

GeneralConfig - Configuration applicable to the genereal application

type MQTT added in v0.4.5

type MQTT struct {
	MQTTOpts
	// contains filtered or unexported fields
}

MQTT - A wrapper for the Paho MQTT

func NewMQTT added in v0.4.5

func NewMQTT(opts MQTTOpts) *MQTT

NewMQTT - Create a wrapper for Paho MQTT

func (*MQTT) Initialize added in v0.4.5

func (c *MQTT) Initialize() *MQTT

Initialize - Initialize the MQTT Client with all the appropriate settings

func (*MQTT) Publish added in v0.4.5

func (c *MQTT) Publish(topic string, payload string) MQTTMessage

Publish - Publish a topic and payload with default options

func (*MQTT) PublishWithOpts added in v0.4.5

func (c *MQTT) PublishWithOpts(topic string, payload string, opts MQTTPublishOpts) MQTTMessage

PublishWithOpts - Publish a topic and payload with specific options

func (*MQTT) Run added in v0.4.5

func (c *MQTT) Run()

Run - Connect to MQTT

func (*MQTT) SetConnectHandler added in v0.4.5

func (c *MQTT) SetConnectHandler(handler func()) *MQTT

SetConnectHandler - Sets the function to be called at the end of a successful MQTT connection

func (*MQTT) SetDisconnectHandler added in v0.4.5

func (c *MQTT) SetDisconnectHandler(handler func()) *MQTT

SetDisconnectHandler - Sets the function to be called at the end of a successful MQTT connection

func (*MQTT) SetDiscoveryHandler added in v0.4.5

func (c *MQTT) SetDiscoveryHandler(handler func() []MQTTDiscovery) *MQTT

SetDiscoveryHandler - Sets the function to be called when it's time to publish MQTT discovery messages

func (*MQTT) SetReadIncomingChannelHandler added in v0.4.5

func (c *MQTT) SetReadIncomingChannelHandler(handler func()) *MQTT

SetReadIncomingChannelHandler - Sets the function to be called when it's time to subscribe to MQTT topics

func (*MQTT) SetSubscribeHandler added in v0.4.5

func (c *MQTT) SetSubscribeHandler(handler func()) *MQTT

SetSubscribeHandler - Sets the function to be called when it's time to subscribe to MQTT topics

func (*MQTT) Subscribe added in v0.4.5

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

Subscribe - Subscribe to a topic and payload via the MQTTClientWrapperWrapper

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"`
	Device                   MQTTDiscoveryDevice `json:"device,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.

func NewMQTTDiscovery added in v0.4.5

func NewMQTTDiscovery(proxyConfig MQTTOpts, deviceName string, sensorName string, sensorType string) *MQTTDiscovery

NewMQTTDiscovery - Create a new MQTTDiscovery object

type MQTTDiscoveryDevice added in v0.3.1

type MQTTDiscoveryDevice struct {
	Identifiers  []string `json:"identifiers,omitempty"`
	Connections  []string `json:"connections,omitempty"`
	Manufacturer string   `json:"manufacturer,omitempty"`
	Model        string   `json:"model,omitempty"`
	Name         string   `json:"name,omitempty"`
	SWVersion    string   `json:"sw_version,omitempty"`
}

MQTTDiscoveryDevice - Contains the Device properties for MQTT Discovery.

type MQTTMessage added in v0.4.5

type MQTTMessage struct {
	Topic   string
	Payload string
}

MQTTMessage - The result of a publish action

type MQTTOpts added in v0.4.5

type MQTTOpts 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"`
}

MQTTOpts - Configuration applicable to the MQTTProxy

func (*MQTTOpts) AvailabilityTopic added in v0.4.5

func (c *MQTTOpts) AvailabilityTopic() string

AvailabilityTopic - Generate the availability topic

func (*MQTTOpts) CommandTopic added in v0.4.5

func (c *MQTTOpts) CommandTopic(deviceName string, sensorName string) string

CommandTopic - Generate the command topic for a named sensor

func (*MQTTOpts) StateTopic added in v0.4.5

func (c *MQTTOpts) StateTopic(deviceName string, sensorName string) string

StateTopic - Generate the state topic for a named sensor

type MQTTPublishOpts added in v0.4.5

type MQTTPublishOpts struct {
	Retained       bool
	DuplicateCheck bool
}

MQTTPublishOpts - MQTTProxy Publish Options

type MoqClient added in v0.4.6

type MoqClient struct{}

MoqClient - Aid testing MQTT callbacks

func (MoqClient) AddRoute added in v0.4.6

func (t MoqClient) AddRoute(topic string, callback mqtt.MessageHandler)

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

func (MoqClient) Connect added in v0.4.6

func (t MoqClient) Connect() mqtt.Token

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

func (MoqClient) Disconnect added in v0.4.6

func (t MoqClient) Disconnect(quiesce uint)

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

func (MoqClient) IsConnected added in v0.4.6

func (t MoqClient) IsConnected() bool

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

func (MoqClient) IsConnectionOpen added in v0.4.6

func (t MoqClient) IsConnectionOpen() bool

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

func (MoqClient) OptionsReader added in v0.4.6

func (t MoqClient) OptionsReader() mqtt.ClientOptionsReader

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

func (MoqClient) Publish added in v0.4.6

func (t MoqClient) Publish(topic string, qos byte, retained bool, payload interface{}) mqtt.Token

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

func (MoqClient) Subscribe added in v0.4.6

func (t MoqClient) Subscribe(topic string, qos byte, callback mqtt.MessageHandler) mqtt.Token

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

func (MoqClient) SubscribeMultiple added in v0.4.6

func (t MoqClient) SubscribeMultiple(filters map[string]byte, callback mqtt.MessageHandler) mqtt.Token

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

func (MoqClient) Unsubscribe added in v0.4.6

func (t MoqClient) Unsubscribe(topics ...string) mqtt.Token

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

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 MoqToken added in v0.4.6

type MoqToken struct{}

MoqToken - Aid MQTT testing

func (MoqToken) Error added in v0.4.6

func (t MoqToken) Error() error

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

func (MoqToken) Wait added in v0.4.6

func (t MoqToken) Wait() bool

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

func (MoqToken) WaitTimeout added in v0.4.6

func (t MoqToken) WaitTimeout(time.Duration) bool

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

Jump to

Keyboard shortcuts

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