diff --git a/config.go b/config.go index 8d46475040f7bd92a015ab2adacc5ee9e6663530..3d93bc85a06718b0ca9549b519049e432c8434ea 100644 --- a/config.go +++ b/config.go @@ -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") diff --git a/gateway.go b/gateway.go index 7341ac801b3327a8841e46dd97d77ab724e57a72..4b5de729cc7aef27c437d679b6484d12a3b7db2d 100644 --- a/gateway.go +++ b/gateway.go @@ -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() diff --git a/main.go b/main.go index 2c8b37d7814b67ccdd42dda07cb22548e6376ca6..98726ffeec327fed0645841145897907bfcbf2b2 100644 --- a/main.go +++ b/main.go @@ -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") } diff --git a/mqtt.go b/mqtt.go index 05288f5784a3f6964a9c58e88f56ea25fc2ca8d1..1e79716e15d39a4ca2d716389fec345a7c24427e 100644 --- a/mqtt.go +++ b/mqtt.go @@ -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 { diff --git a/z2m.go b/z2m.go index 7ffc34b4d2c627b4ce4280357a34454109e00d00..0c63d0826be964cc5d9d4445509c37ad9de385dc 100644 --- a/z2m.go +++ b/z2m.go @@ -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)