Skip to content
Snippets Groups Projects
Commit b13929ad authored by KERDREUX Jerome's avatar KERDREUX Jerome
Browse files

Refactoring in progress

mqtt Handler and device parser are now Gateway funcs..
Will get ride of the gateway singleton in the next commit, I hope.
parent 67d96e29
No related branches found
No related tags found
No related merge requests found
package main
import (
"encoding/json"
"log/slog"
"slices"
"sync"
MQTT "github.com/eclipse/paho.mqtt.golang"
......@@ -27,10 +30,12 @@ func GetGW() *Gateway {
return instance
}
func NewGW(client MQTT.Client, cfg *Config) *Gateway {
func NewGW(cfg *Config, eng *xaal.Engine) *Gateway {
gw := GetGW()
gw.baseAddr = cfg.baseAddr
gw.client = client
gw.client = mqttSetup(cfg, gw.mqttPublishHander)
// NOTE: Wondering if we can setup engine before
gw.engine = eng
return gw
}
......@@ -50,3 +55,46 @@ func (gw *Gateway) GetZDeviceByTopic(topic string) *Z2MDevice {
name := topic[len(mqttTopic+"/"):]
return gw.GetZDevice(name)
}
// mqttPublishHander handles all incoming MQTT messages
// If the topic is /bridge/devices it will parse the json and create new devices
// Else it will find the device with the topic and call the mqttDeviceHandler
func (gw *Gateway) mqttPublishHander(client MQTT.Client, msg MQTT.Message) {
// we ignore some topics
if slices.Contains(ignoredTopics, msg.Topic()) {
return
}
// slog.Debug("Received message on", "topic", msg.Topic())
// Is it devices definitions ?
if msg.Topic() == mqttTopic+"/bridge/devices" {
gw.jsonParseDevices(msg.Payload())
} else {
dev := gw.GetZDeviceByTopic(msg.Topic())
// mqttDumpMsg(msg)
if dev != nil {
dev.HandleMessage(msg)
}
}
}
// jsonParseDevices parses the bridge/devices json and creates new xAAL devices
// if they don't exist
func (gw *Gateway) jsonParseDevices(jsonData []byte) {
var devices []Z2MDevice
err := json.Unmarshal([]byte(jsonData), &devices)
if err != nil {
slog.Error("Error decoding JSON", "err", err)
}
for _, zDev := range devices {
known := gw.GetZDevice(zDev.FriendlyName)
if known != nil {
continue
}
zDev.dump()
zDev.setupXAALDevices(gw)
gw.AddZDevice(&zDev)
zDev.Sync()
// zDev.Available()
}
}
......@@ -12,15 +12,15 @@ func main() {
slog.Error("Error parsing configuration file: ", "err", err)
return
}
// spew.Dump(cfg)
// start xAAL stack
xaal.SetupLogger()
client := mqttSetup(cfg, mqttPublishHander)
eng := xaal.NewEngine()
gw := NewGW(client, cfg)
gw.engine = eng
// start the gateway
gw := NewGW(cfg, eng)
// start the xAAL engine
eng.Run()
client.Disconnect(250)
// Engine stops, disconnect MQTT
gw.client.Disconnect(250)
slog.Debug("MQTT disconnected")
}
......@@ -4,7 +4,6 @@ import (
"encoding/json"
"fmt"
"log/slog"
"slices"
"strings"
MQTT "github.com/eclipse/paho.mqtt.golang"
......@@ -226,47 +225,3 @@ func (l AccessLevel) String() string {
func (l AccessLevel) EnumIndex() int {
return int(l)
}
// jsonParseDevices parses the bridge/devices json and creates new xAAL devices
// if they don't exist
func jsonParseDevices(jsonData []byte) {
var devices []Z2MDevice
err := json.Unmarshal([]byte(jsonData), &devices)
if err != nil {
slog.Error("Error decoding JSON", "err", err)
}
gw := GetGW()
for _, zDev := range devices {
known := gw.GetZDevice(zDev.FriendlyName)
if known != nil {
continue
}
zDev.dump()
zDev.setupXAALDevices(gw)
gw.AddZDevice(&zDev)
zDev.Sync()
// zDev.Available()
}
}
// mqttPublishHander handles all incoming MQTT messages
// If the topic is /bridge/devices it will parse the json and create new devices
// Else it will find the device with the topic and call the mqttDeviceHandler
func mqttPublishHander(client MQTT.Client, msg MQTT.Message) {
// we ignore some topics
if slices.Contains(ignoredTopics, msg.Topic()) {
return
}
// slog.Debug("Received message on", "topic", msg.Topic())
// Is it devices definitions ?
if msg.Topic() == mqttTopic+"/bridge/devices" {
jsonParseDevices(msg.Payload())
} else {
dev := GetGW().GetZDeviceByTopic(msg.Topic())
// mqttDumpMsg(msg)
if dev != nil {
dev.HandleMessage(msg)
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment