handler stuff
This commit is contained in:
@ -14,13 +14,13 @@ type ConfigT struct {
|
|||||||
} `json:"mqtt"`
|
} `json:"mqtt"`
|
||||||
TopicMappings []struct {
|
TopicMappings []struct {
|
||||||
Topics []string `json:topics`
|
Topics []string `json:topics`
|
||||||
Plugin string `json:plugin`
|
Handler string `json:handler`
|
||||||
} `json:"TopicMappings"`
|
} `json:"TopicMappings"`
|
||||||
Plugins []struct {
|
Handlers []struct {
|
||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
DatabaseConnStr string `json:"databaseConnStr"`
|
DatabaseConnStr string `json:"databaseConnStr"`
|
||||||
Attributes map[string]string `json:"attributes"`
|
Attributes map[string]string `json:"attributes"`
|
||||||
} `json:"plugins"`
|
} `json:"handlers"`
|
||||||
}
|
}
|
||||||
|
|
||||||
var Config ConfigT
|
var Config ConfigT
|
||||||
|
@ -3,8 +3,26 @@ package dispatcher
|
|||||||
import "log"
|
import "log"
|
||||||
import "udi/mqtt"
|
import "udi/mqtt"
|
||||||
import "udi/config"
|
import "udi/config"
|
||||||
|
import "udi/handlers/handler"
|
||||||
|
import "udi/handlers/ttn"
|
||||||
|
|
||||||
|
|
||||||
|
var handlerMap map[string]handler.Handler = make(map[string]handler.Handler)
|
||||||
|
|
||||||
|
func InitDispatcher() {
|
||||||
|
log.Printf("Initializing dispatcher")
|
||||||
|
for _, handlerEntry := range config.Config.Handlers {
|
||||||
|
log.Printf("Trying %s", handlerEntry.Name)
|
||||||
|
switch handlerEntry.Name {
|
||||||
|
case "TTN":
|
||||||
|
handlerMap[handlerEntry.Name] = ttn.NewTTNHandler()
|
||||||
|
log.Printf("TTN initialized")
|
||||||
|
default:
|
||||||
|
log.Fatalf("Handler %s not found", handlerEntry.Name)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func InputDispatcher() {
|
func InputDispatcher() {
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
@ -12,11 +30,13 @@ func InputDispatcher() {
|
|||||||
log.Printf("Message arrived in inputDispatcher, topic: %s, payload: %s\n", message.Topic, message.Payload)
|
log.Printf("Message arrived in inputDispatcher, topic: %s, payload: %s\n", message.Topic, message.Payload)
|
||||||
|
|
||||||
for _, mapping := range config.Config.TopicMappings {
|
for _, mapping := range config.Config.TopicMappings {
|
||||||
log.Printf("Testing %s -> %s", mapping.Topics, mapping.Plugin)
|
log.Printf("Testing %s -> %s", mapping.Topics, mapping.Handler)
|
||||||
for _, subscribedTopic := range mapping.Topics {
|
for _, subscribedTopic := range mapping.Topics {
|
||||||
log.Printf("Testing %s in %s", message.Topic, subscribedTopic)
|
log.Printf("Testing %s in %s", message.Topic, subscribedTopic)
|
||||||
if mqtt.TopicMatchesSubscription(message.Topic, subscribedTopic) {
|
if mqtt.TopicMatchesSubscription(message.Topic, subscribedTopic) {
|
||||||
log.Printf("Handle message in plugin %s", mapping.Plugin)
|
log.Printf("Handle message in handler %s", mapping.Handler)
|
||||||
|
handler := handlerMap[mapping.Handler]
|
||||||
|
handler.Handle(message.Topic, string(message.Payload))
|
||||||
} else {
|
} else {
|
||||||
log.Printf("no match")
|
log.Printf("no match")
|
||||||
}
|
}
|
||||||
|
6
src/udi/handlers/handler/handler.go
Normal file
6
src/udi/handlers/handler/handler.go
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
package handler
|
||||||
|
|
||||||
|
type Handler interface {
|
||||||
|
Handle(string, string)
|
||||||
|
}
|
||||||
|
|
23
src/udi/handlers/ttn/ttn.go
Normal file
23
src/udi/handlers/ttn/ttn.go
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
package ttn
|
||||||
|
|
||||||
|
import "log"
|
||||||
|
|
||||||
|
var idSeq int = 0
|
||||||
|
|
||||||
|
type TTNHandler struct {
|
||||||
|
id int
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewTTNHandler() *TTNHandler {
|
||||||
|
t := &TTNHandler {
|
||||||
|
id: idSeq,
|
||||||
|
}
|
||||||
|
idSeq += 1
|
||||||
|
return t
|
||||||
|
}
|
||||||
|
|
||||||
|
func (self *TTNHandler) Handle(topic, payload string) {
|
||||||
|
log.Printf("Handler TTN %d processing %s -> %s", self.id, topic, payload)
|
||||||
|
}
|
||||||
|
|
||||||
|
|
@ -17,6 +17,7 @@ func main() {
|
|||||||
|
|
||||||
config.LoadConfiguration()
|
config.LoadConfiguration()
|
||||||
|
|
||||||
|
dispatcher.InitDispatcher()
|
||||||
go dispatcher.InputDispatcher()
|
go dispatcher.InputDispatcher()
|
||||||
|
|
||||||
mqtt.StartMqttClient()
|
mqtt.StartMqttClient()
|
||||||
|
Reference in New Issue
Block a user