handler stuff

This commit is contained in:
2023-11-27 15:21:28 +01:00
parent a1fbdb7677
commit a862b4bc2d
5 changed files with 55 additions and 5 deletions

View File

@ -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

View File

@ -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")
}

View File

@ -0,0 +1,6 @@
package handler
type Handler interface {
Handle(string, string)
}

View 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)
}

View File

@ -17,6 +17,7 @@ func main() {
config.LoadConfiguration()
dispatcher.InitDispatcher()
go dispatcher.InputDispatcher()
mqtt.StartMqttClient()