Documentation ¶
Overview ¶
Package mqtt defines how devices map onto an MQTT transport. It also includes a number of MQTT related helpers, like configuration flags for CLI libraries
Index ¶
Constants ¶
This section is empty.
Variables ¶
Functions ¶
func Flags ¶
func Flags(str FlagStrFunc, b FlagBoolFunc) func() (*Config, error)
Flags will use the provided callbacks to set up mqtt-specific cli arguments:
mqtt.address (localhost:1883) - Address of server (MQTT_ADDRESS) mqtt.username - Username (MQTT_USERNAME) mqtt.password - Password (MQTT_PASSWORD) mqtt.tls - Enable TLS (bool flag) (MQTT_TLS) mqtt.tls-insecure - Skip cert validation (bool flag) (MQTT_TLS_INSECURE) mqtt.cn - CN of server (i.e. hostname) (MQTT_COMMON_NAME) mqtt.ca - Path to authority certificate (MQTT_CA_PATH) mqtt.cert - Path to client certificate (MQTT_CERT_PATH) mqtt.key - Path to certificate key (MQTT_KEY_PATH) topic.announce - Topic for announcements (HEMTJANST_TOPIC_ANNOUNCE) topic.discover - Topic for discovery (HEMTJANST_TOPIC_DISCOVER) topic.leave - Topic for leaving (HEMTJANST_TOPIC_LEAVE)
The callback interfaces are compatible with the standard library flag-package. If using the flag-package, you can easily set up the mqtt transport with CLI flags by doing this
(Note that it's important to call mqtt.Flags() before flag.Parse())
package main import ( "flag" "context" "lib.hemtjan.st/transport/mqtt" ) func main() { myCustomFlag := flag.String("custom", "", "Set up your own flags here") mqCfg := mqtt.MustFlags(flag.String, flag.Bool) flag.Parse() transport, err := mqtt.New(context.TODO(), mqCfg()) }
func MustFlags ¶
func MustFlags(str FlagStrFunc, b FlagBoolFunc) func() *Config
Types ¶
type Config ¶
type Config struct { // ClientID will be used as mqtt Client ID and LastWillID ClientID string // Address is a slice of MQTT addresses (host:port / ip:port) Address []string // Username used to authenticate to MQTT Username string // Password used to authenticate to MQTT Password string // TLS configuration TLS *tls.Config // AnnounceTopic is the prefix announcements will have. Default is "announce" AnnounceTopic string // DiscoverTopic is where the library will send or listen for discoveries. Default is "discover" DiscoverTopic string // LeaveTopic is where the will is sent when the client exists. Default is "leave" LeaveTopic string // DiscoverDelay is the time between first subscribing to announcements, and sending a discover. // The delay should be long enough to be able to receive all retained announcements, but not too long // to make initial startup too slow. Default is 5 seconds DiscoverDelay time.Duration }
type FlagBoolFunc ¶
FlagBoolFunc is passed to the Flags() function, it is compatible with the standard library flag.Bool() function
type FlagStrFunc ¶
FlagStrFunc is passed to the Flags() function, it is compatible with the standard library flag.String() function
type MQTT ¶
type MQTT interface { TopicName(t EventType) string DeviceState() chan *device.State PublishMeta(topic string, payload []byte) Publish(topic string, payload []byte, retain bool) SubscribeRaw(topic string) chan *Packet Unsubscribe(topic string) bool Resubscribe(oldTopic, newTopic string) bool Subscribe(topic string) chan []byte Discover() chan struct{} LastWillID() string }
type MessageHandler ¶
type MessageHandler interface { OnRaw(p *libmqtt.PublishPacket) OnAnnounce(p *libmqtt.PublishPacket) OnLeave(p *libmqtt.PublishPacket) OnDiscover(p *libmqtt.PublishPacket) OnFeature(p *libmqtt.PublishPacket) TopicName(t EventType) string }
type Packet ¶
type Packet libmqtt.PublishPacket