mqtt stuff 4
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
ci/woodpecker/tag/woodpecker Pipeline was successful

This commit is contained in:
2024-01-14 15:03:26 +01:00
parent dd394877f3
commit 0215d2efd1
4 changed files with 11 additions and 7 deletions

View File

@ -31,19 +31,21 @@ func New() *MqttHandle {
}
mqttHandle.pubTopic = os.Getenv("MQTT_TOPIC")
if mqttHandle.pubTopic != "" {
if mqttHandle.pubTopic == "" {
log.Printf("No topic set")
mqttHandle.initialized = false
}
log.Printf("MQTT connection established")
return &mqttHandle
}
func (self *MqttHandle) Publish(message string) error {
func (self *MqttHandle) Publish(topicPost string, message string) error {
if ! self.initialized {
return fmt.Errorf("MQTT connection not initialized")
}
token := self.client.Publish(self.pubTopic, 0, false, message)
topic := fmt.Sprintf("%s/%s", self.pubTopic, topicPost)
token := self.client.Publish(topic, 0, false, message)
token.Wait()
if token.Error() != nil {
return fmt.Errorf("MQTT publish failed: %v", token.Error())