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

Cleanup / Better Access display

parent f20cba1b
Branches
No related tags found
No related merge requests found
......@@ -10,22 +10,22 @@ import (
)
// setupDevice sets up the xAAL device with the bridge device information
func setupDevice(dev *xaal.Device, bDevice *BridgeDevice) {
dev.VendorID = bDevice.Definition.Vendor
dev.ProductID = bDevice.Definition.Model
dev.HWID = bDevice.IeeeAddress
dev.Version = bDevice.SwBuildID
dev.Info = "z2m:" + bDevice.Type + ":" + bDevice.FriendlyName
func setupDevice(dev *xaal.Device, bDev *BridgeDevice) {
dev.VendorID = bDev.Definition.Vendor
dev.ProductID = bDev.Definition.Model
dev.HWID = bDev.IeeeAddress
dev.Version = bDev.SwBuildID
dev.Info = "z2m:" + bDev.Type + ":" + bDev.FriendlyName
}
// newDevices creates new xAAL devices from a bridge device
func newDevices(gw *gateway, bDevice *BridgeDevice) {
func newDevices(gw *gateway, bDev *BridgeDevice) {
// TODO: Handle errors
baseAddr := gw.baseAddr
ieeeAddr, _ := hexStringToInteger(bDevice.IeeeAddress)
ieeeAddr, _ := hexStringToInteger(bDev.IeeeAddress)
baseAddr, _ = baseAddr.Add(int64(ieeeAddr))
for i, expose := range bDevice.Definition.Exposes {
for i, expose := range bDev.Definition.Exposes {
addr, _ := baseAddr.Add(int64(i))
var dev *xaal.Device
......@@ -48,17 +48,17 @@ func newDevices(gw *gateway, bDevice *BridgeDevice) {
}
if dev != nil {
setupDevice(dev, bDevice)
setupDevice(dev, bDev)
dev.Dump()
bDevice.XAALDevice = append(bDevice.XAALDevice, dev)
bDev.XAALDevice = append(bDev.XAALDevice, dev)
gw.engine.AddDevice(dev)
}
}
}
func updateXAALDevice(bDevice *BridgeDevice, payload map[string]interface{}) {
slog.Info("Updating device:", "name", bDevice.FriendlyName)
for _, dev := range bDevice.XAALDevice {
func updateXAALDevice(bDev *BridgeDevice, payload map[string]interface{}) {
slog.Info("Updating device:", "name", bDev.FriendlyName)
for _, dev := range bDev.XAALDevice {
for key, value := range payload {
if key == "contact" && dev.DevType == "contact.basic" {
dev.GetAttribute("detected").SetValue(!value.(bool))
......
......@@ -55,30 +55,46 @@ type Feature struct {
Access int `json:"access,omitempty"`
}
func (bDevice *BridgeDevice) GetTopic() string {
return mqttTopic + "/" + bDevice.FriendlyName
func (bDev *BridgeDevice) GetTopic() string {
return mqttTopic + "/" + bDev.FriendlyName
}
func (bDevice *BridgeDevice) SetTopic() string {
return bDevice.GetTopic() + "/set"
func (bDev *BridgeDevice) SetTopic() string {
return bDev.GetTopic() + "/set"
}
func (bDevices *BridgeDevice) dump() {
func getAccessType(level int) string {
switch level {
case 1:
return "R"
case 2:
return "W"
case 3:
return "RW"
case 5:
return "RN"
case 7:
return "RWN"
}
return ""
}
func (bDev *BridgeDevice) dump() {
tab := table.NewWriter()
tab.SetTitle("Def:" + bDevices.FriendlyName)
tab.SetTitle("Def:" + bDev.FriendlyName)
tab.SetStyle(table.StyleRounded)
tab.AppendRow(table.Row{"IeeeAddr", bDevices.IeeeAddress})
tab.AppendRow(table.Row{"Vendor", bDevices.Definition.Vendor})
tab.AppendRow(table.Row{"Model", bDevices.Definition.Model})
tab.AppendRow(table.Row{"Type", bDevices.Type})
tab.AppendRow(table.Row{"IeeeAddr", bDev.IeeeAddress})
tab.AppendRow(table.Row{"Vendor", bDev.Definition.Vendor})
tab.AppendRow(table.Row{"Model", bDev.Definition.Model})
tab.AppendRow(table.Row{"Type", bDev.Type})
fmt.Println(tab.Render())
if len(bDevices.Definition.Exposes) > 0 {
if len(bDev.Definition.Exposes) > 0 {
expTab := table.NewWriter()
expTab.SetTitle("Exp:" + bDevices.FriendlyName)
expTab.SetTitle("Exp:" + bDev.FriendlyName)
expTab.SetStyle(table.StyleRounded)
expTab.AppendHeader(table.Row{"Name", "Type", "Acc", "Unit", "Values", "Features: Name[Type]-Acc-(Unit){Property}"})
for _, expose := range bDevices.Definition.Exposes {
for _, expose := range bDev.Definition.Exposes {
values := ""
if len(expose.Values) > 0 {
values = strings.Join(expose.Values, "\n")
......@@ -86,16 +102,18 @@ func (bDevices *BridgeDevice) dump() {
features := ""
if len(expose.Features) > 0 {
for _, feature := range expose.Features {
features += fmt.Sprintf("- %s[%s]-%d-(%s){%s}\n", feature.Name, feature.Type, feature.Access, feature.Unit, feature.Property)
features += fmt.Sprintf("- %s[%s]-%s-(%s){%s}\n", feature.Name, feature.Type, getAccessType(feature.Access), feature.Unit, feature.Property)
}
features = strings.TrimSuffix(features, "\n")
}
expTab.AppendRow(table.Row{expose.Name, expose.Type, expose.Access, expose.Unit, values, features})
expTab.AppendRow(table.Row{expose.Name, expose.Type, getAccessType(expose.Access), expose.Unit, values, features})
}
fmt.Println(expTab.Render())
}
}
// jsonParseDevices parses the bridge/devices json and creates new xAAL devices
// if they don't exist
func jsonParseDevices(jsonData []byte) {
var devices []BridgeDevice
err := json.Unmarshal([]byte(jsonData), &devices)
......@@ -104,7 +122,6 @@ func jsonParseDevices(jsonData []byte) {
}
gw := GetGW()
for _, device := range devices {
newFlag := true
for _, known := range gw.devices {
......@@ -123,16 +140,18 @@ func jsonParseDevices(jsonData []byte) {
}
}
func mqttDeviceHandler(device *BridgeDevice, msg MQTT.Message) {
// mqttDeviceHandler updates the xAAL device with the MQTT message
func mqttDeviceHandler(bDev *BridgeDevice, msg MQTT.Message) {
var data map[string]interface{}
err := json.Unmarshal(msg.Payload(), &data)
if err != nil {
slog.Error("Error decoding JSON", "err", err)
} else {
updateXAALDevice(device, data)
updateXAALDevice(bDev, data)
}
}
// mqttDumpMsg displays the MQTT message
func mqttDumpMsg(msg MQTT.Message) {
var data map[string]interface{}
err := json.Unmarshal(msg.Payload(), &data)
......@@ -158,13 +177,17 @@ func mqttDumpMsg(msg MQTT.Message) {
fmt.Println(tab.Render())
}
// 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 {
......@@ -182,6 +205,7 @@ func mqttPublishHander(client MQTT.Client, msg MQTT.Message) {
}
}
// mqttSetup creates a new MQTT client
func mqttSetup(mqttBroker string, port int) MQTT.Client {
// This JS style of creating a client is awfully verbose
opts := MQTT.NewClientOptions().
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment