-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathhandlers.go
executable file
·45 lines (38 loc) · 1.02 KB
/
handlers.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package mqtt
import (
"fmt"
"strings"
entities "github.com/gost/core"
"github.com/gost/server/sensorthings/models"
)
var topics = map[string]models.MQTTInternalHandler{
"Datastreams()/Observations": observationsByDatastream,
}
// MainMqttHandler handles all messages on GOST/# and maps them to the appropriate
// handler. Mapping is needed because of the ODATA (id) format
func MainMqttHandler(a *models.API, prefix, topic string, message []byte) {
topic = strings.Replace(topic, fmt.Sprintf("%s/", prefix), "", 1)
topicMapName := ""
id := ""
if strings.Contains(topic, "(") {
i := strings.Index(topic, "(")
i2 := strings.Index(topic, ")")
first := topic[0 : i+1]
id = topic[i+1 : i2]
last := topic[i2:]
topicMapName = first + last
}
h := topics[topicMapName]
if h != nil {
h(a, message, id)
}
}
func observationsByDatastream(a *models.API, message []byte, id string) {
o := entities.Observation{}
err := o.ParseEntity(message)
if err != nil {
return
}
api := *a
api.PostObservationByDatastream(id, &o)
}