0
0
mirror of https://github.com/crazy-max/diun.git synced 2024-11-23 23:49:35 +00:00
crazy-max_diun/internal/notif/mqtt/client.go
CrazyMax 5d583809a0
Allow to customize notification message (#415)
Co-authored-by: CrazyMax <crazy-max@users.noreply.github.com>
2021-06-25 23:24:02 +02:00

79 lines
1.7 KiB
Go

package mqtt
import (
"fmt"
"github.com/crazy-max/diun/v4/internal/model"
"github.com/crazy-max/diun/v4/internal/msg"
"github.com/crazy-max/diun/v4/internal/notif/notifier"
"github.com/crazy-max/diun/v4/pkg/utl"
MQTT "github.com/eclipse/paho.mqtt.golang"
)
// Client represents an active mqtt notification object
type Client struct {
*notifier.Notifier
cfg *model.NotifMqtt
meta model.Meta
mqttClient MQTT.Client
}
// New creates a new mqtt notification instance
func New(config *model.NotifMqtt, meta model.Meta) notifier.Notifier {
return notifier.Notifier{
Handler: &Client{
cfg: config,
meta: meta,
},
}
}
// Name returns notifier's name
func (c *Client) Name() string {
return "mqtt"
}
// Send creates and sends a mqtt notification with an entry
func (c *Client) Send(entry model.NotifEntry) error {
username, err := utl.GetSecret(c.cfg.Username, c.cfg.UsernameFile)
if err != nil {
return err
}
password, err := utl.GetSecret(c.cfg.Password, c.cfg.PasswordFile)
if err != nil {
return err
}
broker := fmt.Sprintf("%s://%s:%d", c.cfg.Scheme, c.cfg.Host, c.cfg.Port)
opts := MQTT.NewClientOptions().AddBroker(broker).SetClientID(c.cfg.Client)
opts.Username = username
opts.Password = password
if c.mqttClient == nil {
c.mqttClient = MQTT.NewClient(opts)
}
if !c.mqttClient.IsConnected() {
if token := c.mqttClient.Connect(); token.Wait() && token.Error() != nil {
return token.Error()
}
}
message, err := msg.New(msg.Options{
Meta: c.meta,
Entry: entry,
})
if err != nil {
return err
}
body, err := message.RenderJSON()
if err != nil {
return err
}
token := c.mqttClient.Publish(c.cfg.Topic, byte(c.cfg.QoS), false, body)
token.Wait()
return token.Error()
}