Compare commits
5 Commits
Author | SHA1 | Date | |
---|---|---|---|
a7262d99cc
|
|||
6be68d4dee
|
|||
54eeb17e9b
|
|||
4a56fea33b
|
|||
0215d2efd1
|
@ -25,8 +25,44 @@ spec:
|
|||||||
- name: MQTT_BROKER
|
- name: MQTT_BROKER
|
||||||
value: mqtt://emqx01-anonymous-cluster-internal.broker.svc.cluster.local:1883
|
value: mqtt://emqx01-anonymous-cluster-internal.broker.svc.cluster.local:1883
|
||||||
- name: MQTT_TOPIC
|
- name: MQTT_TOPIC
|
||||||
value: Locative/Event
|
value: locative/event
|
||||||
ports:
|
ports:
|
||||||
- containerPort: 8080
|
- containerPort: 8080
|
||||||
protocol: TCP
|
protocol: TCP
|
||||||
|
---
|
||||||
|
apiVersion: v1
|
||||||
|
kind: Service
|
||||||
|
metadata:
|
||||||
|
name: locative
|
||||||
|
spec:
|
||||||
|
type: ClusterIP
|
||||||
|
selector:
|
||||||
|
app: locsrv
|
||||||
|
ports:
|
||||||
|
- name: http
|
||||||
|
targetPort: 8080
|
||||||
|
port: 80
|
||||||
|
---
|
||||||
|
apiVersion: networking.k8s.io/v1
|
||||||
|
kind: Ingress
|
||||||
|
metadata:
|
||||||
|
name: locative
|
||||||
|
annotations:
|
||||||
|
cert-manager.io/cluster-issuer: letsencrypt-production-http
|
||||||
|
spec:
|
||||||
|
tls:
|
||||||
|
- hosts:
|
||||||
|
- locative.hottis.de
|
||||||
|
secretName: locative-cert
|
||||||
|
rules:
|
||||||
|
- host: locative.hottis.de
|
||||||
|
http:
|
||||||
|
paths:
|
||||||
|
- path: /
|
||||||
|
pathType: Prefix
|
||||||
|
backend:
|
||||||
|
service:
|
||||||
|
name: locative
|
||||||
|
port:
|
||||||
|
number: 80
|
||||||
|
|
||||||
|
@ -24,7 +24,7 @@ func New() *DatabaseHandle {
|
|||||||
} else {
|
} else {
|
||||||
db.dbh = conn
|
db.dbh = conn
|
||||||
db.initialized = true
|
db.initialized = true
|
||||||
//log.Println("Database connection opened")
|
log.Println("Database connection opened")
|
||||||
}
|
}
|
||||||
return &db
|
return &db
|
||||||
}
|
}
|
||||||
|
@ -2,6 +2,7 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"log"
|
"log"
|
||||||
|
"time"
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"net/http"
|
"net/http"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
@ -30,12 +31,12 @@ type locativeEvent struct {
|
|||||||
Latitude string `json:"latitude"`
|
Latitude string `json:"latitude"`
|
||||||
Longitude string `json:"longitude"`
|
Longitude string `json:"longitude"`
|
||||||
Person string `json:"person"`
|
Person string `json:"person"`
|
||||||
|
Timestamp string `json:"timestamp"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
router := gin.Default()
|
router := gin.Default()
|
||||||
|
|
||||||
ch := make(chan locativeEvent)
|
ch := make(chan locativeEvent)
|
||||||
dbh := database.New()
|
dbh := database.New()
|
||||||
mqtt := mqtt.New()
|
mqtt := mqtt.New()
|
||||||
@ -47,6 +48,7 @@ func main() {
|
|||||||
person, err := dbh.GetPersonById(event.Device)
|
person, err := dbh.GetPersonById(event.Device)
|
||||||
event.Person = person
|
event.Person = person
|
||||||
event.Location = event.Id
|
event.Location = event.Id
|
||||||
|
event.Timestamp = time.Now().Format("2006-01-02 15:04:05 MST")
|
||||||
event.Id = ""
|
event.Id = ""
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("Person unknown: %v", err)
|
log.Printf("Person unknown: %v", err)
|
||||||
@ -56,7 +58,10 @@ func main() {
|
|||||||
log.Printf("Unable to marshal event: %v", err2)
|
log.Printf("Unable to marshal event: %v", err2)
|
||||||
} else {
|
} else {
|
||||||
log.Printf("Message: %s", message)
|
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")
|
mqttHandle.pubTopic = os.Getenv("MQTT_TOPIC")
|
||||||
if mqttHandle.pubTopic != "" {
|
if mqttHandle.pubTopic == "" {
|
||||||
log.Printf("No topic set")
|
log.Printf("No topic set")
|
||||||
mqttHandle.initialized = false
|
mqttHandle.initialized = false
|
||||||
}
|
}
|
||||||
|
log.Printf("MQTT connection established")
|
||||||
return &mqttHandle
|
return &mqttHandle
|
||||||
}
|
}
|
||||||
|
|
||||||
func (self *MqttHandle) Publish(message string) error {
|
func (self *MqttHandle) Publish(topicPost string, message string) error {
|
||||||
if ! self.initialized {
|
if ! self.initialized {
|
||||||
return fmt.Errorf("MQTT connection not 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, true, message)
|
||||||
token.Wait()
|
token.Wait()
|
||||||
if token.Error() != nil {
|
if token.Error() != nil {
|
||||||
return fmt.Errorf("MQTT publish failed: %v", token.Error())
|
return fmt.Errorf("MQTT publish failed: %v", token.Error())
|
||||||
|
Reference in New Issue
Block a user