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

Refactoring in progress

- UperCasing name .. not really sure.
- Support for log_level in config file
parent 472018ea
No related branches found
No related tags found
No related merge requests found
......@@ -2,6 +2,7 @@ package main
import (
"fmt"
"log/slog"
"gitlab.imt-atlantique.fr/xaal/code/go/core/uuid"
"gitlab.imt-atlantique.fr/xaal/code/go/core/xaal"
......@@ -25,7 +26,7 @@ var (
type Config struct {
brokerHost string
topic string
logLevel string
logLevel slog.Level
ignoredTopics []string
brokerPort int
baseAddr uuid.UUID
......@@ -33,7 +34,7 @@ type Config struct {
func parseConfig() (*Config, error) {
// Default configuration
config := Config{brokerHost: "mqtt", topic: "zigbee2mqtt", logLevel: "INFO", brokerPort: 1883}
config := Config{brokerHost: "mqtt", topic: "zigbee2mqtt", logLevel: slog.LevelError, brokerPort: 1883}
// Parse the configuration file
cfg, err := ini.Load(xaal.GetConfigDir() + "/z2m.ini")
if err != nil {
......@@ -56,7 +57,16 @@ func parseConfig() (*Config, error) {
// log level
level, err := sec.GetKey("log_level")
if err == nil {
config.logLevel = level.String()
switch level.String() {
case "DEBUG":
config.logLevel = slog.LevelDebug
case "INFO":
config.logLevel = slog.LevelInfo
case "WARN":
config.logLevel = slog.LevelWarn
default:
config.logLevel = slog.LevelError
}
}
// port
port, err := sec.GetKey("port")
......
......@@ -5,59 +5,59 @@ import (
"log/slog"
"slices"
MQTT "github.com/eclipse/paho.mqtt.golang"
mqtt "github.com/eclipse/paho.mqtt.golang"
"gitlab.imt-atlantique.fr/xaal/code/go/core/xaal"
)
type Gateway struct {
client MQTT.Client
engine *xaal.Engine
devices map[string]*Z2MDevice
config *Config
Client mqtt.Client
Engine *xaal.Engine
Devices map[string]*Z2MDevice
Config *Config
}
func NewGW(cfg *Config, eng *xaal.Engine) *Gateway {
gw := &Gateway{devices: make(map[string]*Z2MDevice)}
gw.config = cfg
gw.client = mqttSetup(cfg, gw.mqttPublishHander)
gw := &Gateway{Devices: make(map[string]*Z2MDevice)}
gw.Config = cfg
gw.Client = mqttSetup(cfg, gw.mqttPublishHander)
// NOTE: Wondering if we can setup engine before
gw.engine = eng
gw.Engine = eng
return gw
}
func (gw *Gateway) GetZDevice(name string) *Z2MDevice {
return gw.devices[name]
return gw.Devices[name]
}
func (gw *Gateway) AddZDevice(zDev *Z2MDevice) {
gw.devices[zDev.FriendlyName] = zDev
gw.Devices[zDev.FriendlyName] = zDev
zDev.Gateway = gw
}
func (gw *Gateway) RemoveZDevice(zDev *Z2MDevice) {
zDev.Gateway = nil
delete(gw.devices, zDev.FriendlyName)
delete(gw.Devices, zDev.FriendlyName)
}
func (gw *Gateway) GetZDevices() map[string]*Z2MDevice {
return gw.devices
return gw.Devices
}
func (gw *Gateway) GetZDeviceByTopic(topic string) *Z2MDevice {
name := topic[len(gw.config.topic+"/"):]
name := topic[len(gw.Config.topic+"/"):]
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) {
func (gw *Gateway) mqttPublishHander(client mqtt.Client, msg mqtt.Message) {
// we ignore some topics
if slices.Contains(gw.config.ignoredTopics, msg.Topic()) {
if slices.Contains(gw.Config.ignoredTopics, msg.Topic()) {
return
}
// Is it devices definitions ?
if msg.Topic() == gw.config.topic+"/bridge/devices" {
if msg.Topic() == gw.Config.topic+"/bridge/devices" {
gw.jsonParseDevices(msg)
} else {
zDev := gw.GetZDeviceByTopic(msg.Topic())
......@@ -70,7 +70,7 @@ func (gw *Gateway) mqttPublishHander(client MQTT.Client, msg MQTT.Message) {
// jsonParseDevices parses the bridge/devices json and creates new xAAL devices
// if they don't exist
func (gw *Gateway) jsonParseDevices(msg MQTT.Message) {
func (gw *Gateway) jsonParseDevices(msg mqtt.Message) {
var devices []Z2MDevice
err := json.Unmarshal(msg.Payload(), &devices)
if err != nil {
......@@ -82,7 +82,7 @@ func (gw *Gateway) jsonParseDevices(msg MQTT.Message) {
if known != nil {
continue
}
zDev.dump()
zDev.Dump()
zDev.setupXAALDevices(gw)
gw.AddZDevice(&zDev)
zDev.Sync()
......
......@@ -14,13 +14,13 @@ func main() {
}
// start xAAL stack
xaal.SetupLogger()
xaal.SetupLogger(cfg.logLevel)
eng := xaal.NewEngine()
// start the gateway
gw := NewGW(cfg, eng)
// start the xAAL engine
eng.Run()
// Engine stops, disconnect MQTT
gw.client.Disconnect(250)
gw.Client.Disconnect(250)
slog.Debug("MQTT disconnected")
}
......@@ -6,19 +6,19 @@ import (
"log/slog"
"sort"
MQTT "github.com/eclipse/paho.mqtt.golang"
mqtt "github.com/eclipse/paho.mqtt.golang"
"github.com/jedib0t/go-pretty/v6/table"
)
// mqttSetup creates a new MQTT client
func mqttSetup(cfg *Config, publishHandler MQTT.MessageHandler) MQTT.Client {
func mqttSetup(cfg *Config, publishHandler mqtt.MessageHandler) mqtt.Client {
// This JS style of creating a client is awfully verbose
opts := MQTT.NewClientOptions().
opts := mqtt.NewClientOptions().
AddBroker(fmt.Sprintf("tcp://%s:%d", cfg.brokerHost, cfg.brokerPort)).
SetClientID(mqttClientID).
SetDefaultPublishHandler(publishHandler).SetAutoReconnect(true)
client := MQTT.NewClient(opts)
client := mqtt.NewClient(opts)
if token := client.Connect(); token.Wait() && token.Error() != nil {
panic(token.Error())
}
......@@ -30,7 +30,7 @@ func mqttSetup(cfg *Config, publishHandler MQTT.MessageHandler) MQTT.Client {
}
// mqttDumpMsg displays the MQTT message
func mqttDumpMsg(msg MQTT.Message) {
func mqttDumpMsg(msg mqtt.Message) {
var data map[string]interface{}
err := json.Unmarshal(msg.Payload(), &data)
if err != nil {
......
......@@ -6,7 +6,7 @@ import (
"log/slog"
"strings"
MQTT "github.com/eclipse/paho.mqtt.golang"
mqtt "github.com/eclipse/paho.mqtt.golang"
"github.com/jedib0t/go-pretty/v6/table"
"gitlab.imt-atlantique.fr/xaal/code/go/core/uuid"
)
......@@ -75,7 +75,7 @@ func (zDev *Z2MDevice) GetExpose(name string) *Expose {
}
// updates the xAAL device with the MQTT message
func (zDev *Z2MDevice) HandleMessage(msg MQTT.Message) {
func (zDev *Z2MDevice) HandleMessage(msg mqtt.Message) {
var data map[string]interface{}
err := json.Unmarshal(msg.Payload(), &data)
if err != nil {
......@@ -91,7 +91,7 @@ func (zDev *Z2MDevice) HandleMessage(msg MQTT.Message) {
// creates new xAAL devices from a bridge device
func (zDev *Z2MDevice) setupXAALDevices(gw *Gateway) {
// TODO: Handle errors
baseAddr := gw.config.baseAddr
baseAddr := gw.Config.baseAddr
ieeeAddr, _ := hexStringToInteger(zDev.IeeeAddress)
baseAddr, _ = baseAddr.Add(int64(ieeeAddr))
......@@ -125,7 +125,7 @@ func (zDev *Z2MDevice) setupXAALDevices(gw *Gateway) {
zDev.XAALDevices = append(zDev.XAALDevices, dev)
xaalDev := dev.getXAALDevice()
xaalDev.GroupID = grpAdd
gw.engine.AddDevice(xaalDev)
gw.Engine.AddDevice(xaalDev)
}
}
}
......@@ -138,7 +138,7 @@ func (zDev *Z2MDevice) setupXAALDevices(gw *Gateway) {
func (zDev *Z2MDevice) Publish(topic string, payload interface{}) {
topic = zDev.getTopic() + "/" + topic
slog.Debug("Sending", "topic", topic, "payload", payload)
client := zDev.Gateway.client
client := zDev.Gateway.Client
if token := client.Publish(topic, 0, false, payload); token.Wait() && token.Error() != nil {
slog.Error("PUBLISH Error", ":", token.Error())
}
......@@ -161,7 +161,7 @@ func (zDev *Z2MDevice) Sync() {
zDev.Publish("dump", "{}")
}
func (zDev *Z2MDevice) dump() {
func (zDev *Z2MDevice) Dump() {
tab := table.NewWriter()
tab.SetTitle("Def:" + zDev.FriendlyName)
tab.SetStyle(table.StyleRounded)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment