phocus_mqtt

package
v2.11.6 Latest Latest
Warning

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

Go to latest
Published: Apr 9, 2024 License: Zlib Imports: 6 Imported by: 0

Documentation

Overview

Package phocus_mqtt handles connecting to and communicating with an MQTT broker

Index

Constants

This section is empty.

Variables

View Source
var CreateClient = func(hostname string, port int, retries int, clientId string) (mqtt.Client, error) {
	var client mqtt.Client

	mqtt.ERROR = log.New(os.Stdout, "[ERROR] ", 0)
	mqtt.CRITICAL = log.New(os.Stdout, "[CRIT] ", 0)
	mqtt.WARN = log.New(os.Stdout, "[WARN]  ", 0)
	mqtt.DEBUG = log.New(os.Stdout, "[DEBUG] ", 0)
	var mqttBroker = hostname
	var mqttPort = port
	var err error
	opts := mqtt.NewClientOptions()
	opts.AddBroker(fmt.Sprintf("tcp://%s:%d", mqttBroker, mqttPort))
	opts.SetClientID(clientId)
	opts.SetDefaultPublishHandler(messagePublishedHandler)
	opts.OnConnect = connectionHandler
	opts.OnConnectionLost = connectionLostHandler
	opts.AutoReconnect = true
	opts.SetPingTimeout(5 * time.Second)
	for i := 0; i < retries; i++ {
		client = mqtt.NewClient(opts)
		token := client.Connect()
		token.WaitTimeout(30 * time.Second)
		err = token.Error()
		if err != nil {
			log.Printf("Failed to set up mqtt %d times with err: %v", i+1, err)
			time.Sleep(50 * time.Millisecond)
		} else {
			log.Printf("Succeeded to set up mqtt after %d times", i+1)
			break
		}
	}
	return client, err
}

Functions

func Error

func Error(client mqtt.Client, qos byte, retained bool, payload error, timeout time.Duration) error

Error publishes a caught error to the error stat

func Send

func Send(client mqtt.Client, topic string, qos byte, retained bool, payload interface{}, timeout time.Duration) error

Send uses the mqtt client to publish some data to a topic with a timeout

func Setup

func Setup(hostname string, port int, retries int, clientId string) (mqtt.Client, error)

Setup sets the logging and opens a connection to the broker

Types

type Client added in v2.8.0

type Client mqtt.Client

Jump to

Keyboard shortcuts

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