Compare commits
1 Commits
Author | SHA1 | Date | |
---|---|---|---|
0215d2efd1
|
@ -25,7 +25,7 @@ spec:
|
||||
- name: MQTT_BROKER
|
||||
value: mqtt://emqx01-anonymous-cluster-internal.broker.svc.cluster.local:1883
|
||||
- name: MQTT_TOPIC
|
||||
value: Locative/Event
|
||||
value: locative/event
|
||||
ports:
|
||||
- containerPort: 8080
|
||||
protocol: TCP
|
||||
|
@ -24,7 +24,7 @@ func New() *DatabaseHandle {
|
||||
} else {
|
||||
db.dbh = conn
|
||||
db.initialized = true
|
||||
//log.Println("Database connection opened")
|
||||
log.Println("Database connection opened")
|
||||
}
|
||||
return &db
|
||||
}
|
||||
|
@ -35,7 +35,6 @@ type locativeEvent struct {
|
||||
|
||||
func main() {
|
||||
router := gin.Default()
|
||||
|
||||
ch := make(chan locativeEvent)
|
||||
dbh := database.New()
|
||||
mqtt := mqtt.New()
|
||||
@ -56,7 +55,10 @@ func main() {
|
||||
log.Printf("Unable to marshal event: %v", err2)
|
||||
} else {
|
||||
log.Printf("Message: %s", message)
|
||||
mqtt.Publish(string(message))
|
||||
err := mqtt.Publish(person, string(message))
|
||||
if err != nil {
|
||||
log.Printf("Failed to publish: %v", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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())
|
||||
|
Reference in New Issue
Block a user