package mqtt import ( "fmt" "time" paho "github.com/eclipse/paho.mqtt.golang" "git.piskot.si/SeminarM2/lambdaiot-core/internal/config" ) type Client struct { client paho.Client } func Connect(cfg config.MQTTConfig) (*Client, error) { opts := paho.NewClientOptions() opts.AddBroker(cfg.Broker) if cfg.ClientID != "" { opts.SetClientID(cfg.ClientID) } if cfg.Username != "" { opts.SetUsername(cfg.Username) opts.SetPassword(cfg.Password) } opts.SetAutoReconnect(true) opts.SetConnectRetry(true) opts.SetConnectTimeout(5 * time.Second) c := paho.NewClient(opts) token := c.Connect() if ok := token.WaitTimeout(10 * time.Second); !ok { return nil, fmt.Errorf("mqtt connect timeout") } if err := token.Error(); err != nil { return nil, err } return &Client{client: c}, nil } func (c *Client) Publish(topic string, payload []byte) error { if c == nil || c.client == nil { return fmt.Errorf("mqtt client not connected") } token := c.client.Publish(topic, 0, false, payload) if ok := token.WaitTimeout(5 * time.Second); !ok { return fmt.Errorf("publish timeout") } return token.Error() } func (c *Client) Close() { if c == nil || c.client == nil { return } c.client.Disconnect(250) }