48 lines
1.4 KiB
Go
Raw Normal View History

2023-11-27 13:09:41 +01:00
package dispatcher
import "log"
import "udi/mqtt"
import "udi/config"
2023-11-27 15:21:28 +01:00
import "udi/handlers/handler"
import "udi/handlers/ttn"
2023-11-27 13:09:41 +01:00
2023-11-27 15:21:28 +01:00
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)
}
}
}
2023-11-27 13:09:41 +01:00
func InputDispatcher() {
for {
select {
case message := <- mqtt.InputChannel:
log.Printf("Message arrived in inputDispatcher, topic: %s, payload: %s\n", message.Topic, message.Payload)
for _, mapping := range config.Config.TopicMappings {
2023-11-27 15:21:28 +01:00
log.Printf("Testing %s -> %s", mapping.Topics, mapping.Handler)
2023-11-27 13:09:41 +01:00
for _, subscribedTopic := range mapping.Topics {
log.Printf("Testing %s in %s", message.Topic, subscribedTopic)
if mqtt.TopicMatchesSubscription(message.Topic, subscribedTopic) {
2023-11-27 15:21:28 +01:00
log.Printf("Handle message in handler %s", mapping.Handler)
handler := handlerMap[mapping.Handler]
handler.Handle(message.Topic, string(message.Payload))
2023-11-27 13:09:41 +01:00
} else {
log.Printf("no match")
}
}
}
}
}
}