diff --git a/src/udi/config/config.go b/src/udi/config/config.go index 419db6d..32b09d1 100644 --- a/src/udi/config/config.go +++ b/src/udi/config/config.go @@ -14,13 +14,13 @@ type ConfigT struct { } `json:"mqtt"` TopicMappings []struct { Topics []string `json:topics` - Plugin string `json:plugin` + Handler string `json:handler` } `json:"TopicMappings"` - Plugins []struct { + Handlers []struct { Name string `json:"name"` DatabaseConnStr string `json:"databaseConnStr"` Attributes map[string]string `json:"attributes"` - } `json:"plugins"` + } `json:"handlers"` } var Config ConfigT diff --git a/src/udi/dispatcher/dispatcher.go b/src/udi/dispatcher/dispatcher.go index a2ac2db..6cdee10 100644 --- a/src/udi/dispatcher/dispatcher.go +++ b/src/udi/dispatcher/dispatcher.go @@ -3,8 +3,26 @@ package dispatcher import "log" import "udi/mqtt" 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() { for { select { @@ -12,11 +30,13 @@ func InputDispatcher() { log.Printf("Message arrived in inputDispatcher, topic: %s, payload: %s\n", message.Topic, message.Payload) 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 { log.Printf("Testing %s in %s", 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 { log.Printf("no match") } diff --git a/src/udi/handlers/handler/handler.go b/src/udi/handlers/handler/handler.go new file mode 100644 index 0000000..0859d83 --- /dev/null +++ b/src/udi/handlers/handler/handler.go @@ -0,0 +1,6 @@ +package handler + +type Handler interface { + Handle(string, string) +} + diff --git a/src/udi/handlers/ttn/ttn.go b/src/udi/handlers/ttn/ttn.go new file mode 100644 index 0000000..0cab916 --- /dev/null +++ b/src/udi/handlers/ttn/ttn.go @@ -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) +} + + diff --git a/src/udi/main.go b/src/udi/main.go index 88e4861..3719efb 100644 --- a/src/udi/main.go +++ b/src/udi/main.go @@ -17,6 +17,7 @@ func main() { config.LoadConfiguration() + dispatcher.InitDispatcher() go dispatcher.InputDispatcher() mqtt.StartMqttClient()