2019-12-29 17:39:08 +00:00
|
|
|
package service
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
mqtt "github.com/eclipse/paho.mqtt.golang"
|
2022-06-09 10:30:53 +00:00
|
|
|
"go.uber.org/zap"
|
2019-12-29 17:39:08 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func StopService(name string, client mqtt.Client, topics ...string) {
|
2022-06-09 10:30:53 +00:00
|
|
|
zap.S().Infof("Stop %s service", name)
|
2019-12-29 17:39:08 +00:00
|
|
|
token := client.Unsubscribe(topics...)
|
|
|
|
token.Wait()
|
|
|
|
if token.Error() != nil {
|
2022-06-09 10:30:53 +00:00
|
|
|
zap.S().Errorf("unable to unsubscribe service: %v", token.Error())
|
2019-12-29 17:39:08 +00:00
|
|
|
}
|
|
|
|
client.Disconnect(50)
|
|
|
|
}
|
|
|
|
|
|
|
|
func RegisterCallback(client mqtt.Client, topic string, callback mqtt.MessageHandler) error {
|
2022-06-09 10:30:53 +00:00
|
|
|
zap.S().Infof("Register callback on topic %v", topic)
|
2019-12-29 17:39:08 +00:00
|
|
|
token := client.Subscribe(topic, 0, callback)
|
|
|
|
token.Wait()
|
|
|
|
if token.Error() != nil {
|
|
|
|
return fmt.Errorf("unable to register callback on topic %s: %v", topic, token.Error())
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type Part interface {
|
|
|
|
Start() error
|
|
|
|
Stop()
|
|
|
|
}
|