mqtt stuff 3
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 13:59:01 +01:00
parent 9624d5d53d
commit dd394877f3
3 changed files with 74 additions and 4 deletions

View File

@ -21,6 +21,11 @@ spec:
envFrom: envFrom:
- secretRef: - secretRef:
name: locsrv-db-cred name: locsrv-db-cred
env:
- name: MQTT_BROKER
value: mqtt://emqx01-anonymous-cluster-internal.broker.svc.cluster.local:1883
- name: MQTT_TOPIC
value: Locative/Event
ports: ports:
- containerPort: 8080 - containerPort: 8080
protocol: TCP protocol: TCP

View File

@ -4,6 +4,7 @@ import (
"log" "log"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"net/http" "net/http"
"encoding/json"
"locsrv/database" "locsrv/database"
"locsrv/mqtt" "locsrv/mqtt"
) )
@ -24,10 +25,11 @@ import (
type locativeEvent struct { type locativeEvent struct {
Trigger string `json:"trigger"` Trigger string `json:"trigger"`
Device string `json:"device"` Device string `json:"device"`
Location string `json:"id"` Id string `json:"id,omitempty"`
Location string `json:"location"`
Latitude string `json:"latitude"` Latitude string `json:"latitude"`
Longitude string `json:"longitude"` Longitude string `json:"longitude"`
Person string Person string `json:"person"`
} }
@ -44,11 +46,18 @@ func main() {
case event := <- ch: case event := <- ch:
person, err := dbh.GetPersonById(event.Device) person, err := dbh.GetPersonById(event.Device)
event.Person = person event.Person = person
event.Location = event.Id
event.Id = ""
if err != nil { if err != nil {
log.Printf("Person unknown: %v", err) log.Printf("Person unknown: %v", err)
} }
log.Printf("Trigger: %s, Device: %s, Location: %s, Person: %s, Latitude: %s, Longitude: %s", event.Trigger, event.Device, event.Location, event.Person, event.Latitude, event.Longitude) message, err2 := json.Marshal(event)
mqtt.Publish("bla") if err2 != nil {
log.Printf("Unable to marshal event: %v", err2)
} else {
log.Printf("Message: %s", message)
mqtt.Publish(string(message))
}
} }
} }
}() }()

56
src/locsrv/mqtt/mqtt.go Normal file
View File

@ -0,0 +1,56 @@
package mqtt
import (
"log"
"os"
"fmt"
MQTT "github.com/eclipse/paho.mqtt.golang"
"github.com/google/uuid"
)
type MqttHandle struct {
initialized bool
pubTopic string
client MQTT.Client
}
func New() *MqttHandle {
var mqttHandle MqttHandle
mqttHandle.initialized = true
mqttOpts := MQTT.NewClientOptions().
AddBroker(os.Getenv("MQTT_BROKER")).
SetClientID(fmt.Sprintf("locsrv-%s", uuid.New())).
SetConnectRetry(true)
mqttHandle.client= MQTT.NewClient(mqttOpts)
if token := mqttHandle.client.Connect(); token.Wait() && token.Error() != nil {
log.Printf("Unable to connect to broker, error %v", token.Error())
mqttHandle.initialized = false
}
mqttHandle.pubTopic = os.Getenv("MQTT_TOPIC")
if mqttHandle.pubTopic != "" {
log.Printf("No topic set")
mqttHandle.initialized = false
}
return &mqttHandle
}
func (self *MqttHandle) Publish(message string) error {
if ! self.initialized {
return fmt.Errorf("MQTT connection not initialized")
}
token := self.client.Publish(self.pubTopic, 0, false, message)
token.Wait()
if token.Error() != nil {
return fmt.Errorf("MQTT publish failed: %v", token.Error())
}
return nil
}