mqtt

package
v0.0.0-...-726f863 Latest Latest
Warning

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

Go to latest
Published: Dec 1, 2024 License: BSD-3-Clause Imports: 10 Imported by: 2

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ConnectHandler

func ConnectHandler(logger zerolog.Logger) func(client pmqtt.Client)

ConnectHandler returns a function that logs a message when the MQTT client successfully connects to the broker.

func ConnectLostHandler

func ConnectLostHandler(logger zerolog.Logger) func(client pmqtt.Client, err error)

ConnectLostHandler returns a function that handles MQTT connection loss by logging a warning message with the error details.

Types

type Mqtt

type Mqtt struct {
	Topic  string
	MgtUrl string

	ClientID uuid.UUID
	// contains filtered or unexported fields
}

func NewMqtt

func NewMqtt(conf MqttConf, logl int, ctx context.Context) (*Mqtt, error)

NewMqtt initializes a new Mqtt instance with given configuration, log level, and context. It sets up the logger, client options, and handles connection and reconnection behaviors. It also handles graceful disconnection upon context cancellation and returns the created Mqtt instance or error.

func (*Mqtt) Connect

func (m *Mqtt) Connect() error

Connect establishes a connection to the MQTT broker using the provided client options. If the connection fails, it logs the error and returns an aggregated error.

func (*Mqtt) ConnectHandler

func (m *Mqtt) ConnectHandler() func(client pmqtt.Client)

ConnectHandler returns a function that logs a message when the MQTT client successfully connects to the broker.

func (*Mqtt) ConnectLostHandler

func (m *Mqtt) ConnectLostHandler() func(client pmqtt.Client, err error)

ConnectLostHandler returns a function that handles MQTT connection loss by logging a warning message with the error details.

func (*Mqtt) Disconnect

func (m *Mqtt) Disconnect()

Disconnect terminates the connection to the MQTT broker, waits up to 500 milliseconds, logs the action, and sets the client to nil.

func (*Mqtt) Forward

func (m *Mqtt) Forward(b []byte, topic string) error

Forward publishes a byte slice `b` to the configured MQTT topic as a QoS 1 message. It waits for up to 200 milliseconds for the publish operation to complete. If the publish operation exceeds the timeout, the function logs an error message.

type MqttConf

type MqttConf struct {
	Connection string `yaml:"Connection"`
	Topic      string `yaml:"Topic"`
	Key        string `yaml:"Key"`
	RootCA     string `yaml:"RootCA"`
	Cert       string `yaml:"Cert"`
	PrivateKey string `yaml:"PrivateKey"`
}

Jump to

Keyboard shortcuts

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