mqtt

package
v0.0.0-...-7cb1dad Latest Latest
Warning

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

Go to latest
Published: Dec 10, 2024 License: GPL-3.0 Imports: 11 Imported by: 0

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(ctx context.Context, wg *sync.WaitGroup, conf MqttConfig) (*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) SendAlarm

func (m *Mqtt) SendAlarm(events model.FCTSDataModel) error

SendAlarm logs an unrecoverable error and indicates that alarm sending is not available over MQTT.

func (*Mqtt) SendAlarmRaw

func (m *Mqtt) SendAlarmRaw(b []byte) error

SendAlarmRaw 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 MqttConfig

type MqttConfig struct {
	Connection string `yaml:"ConnectionString"`
	Topic      string `yaml:"Topic"`
	Key        string `yaml:"Key"`
	LogLevel   int
}

Jump to

Keyboard shortcuts

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