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 ¶
Types ¶
Click to show internal directories.
Click to hide internal directories.