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

Added more test for refresh

parent fd2edcb2
No related branches found
No related tags found
No related merge requests found
......@@ -7,8 +7,8 @@ import (
"gitlab.imt-atlantique.fr/xaal/code/go/core/uuid"
)
// newDevices creates new xAAL devices from a bridge device
func newDevices(gw *Gateway, zDev *Z2MDevice) {
// newXAALDevices creates new xAAL devices from a bridge device
func newXAALDevices(gw *Gateway, zDev *Z2MDevice) {
// TODO: Handle errors
baseAddr := gw.baseAddr
ieeeAddr, _ := hexStringToInteger(zDev.IeeeAddress)
......@@ -25,9 +25,6 @@ func newDevices(gw *Gateway, zDev *Z2MDevice) {
"linkquality": NewLinkQuality,
"battery": NewBattery,
}
// "switch": schemas.NewPowerrelay,
// "power": schemas.NewPowermeter,
// }
if createFunc, ok := deviceMap[expose.Name]; ok {
dev = createFunc(addr, zDev)
......@@ -53,40 +50,7 @@ func newDevices(gw *Gateway, zDev *Z2MDevice) {
func updateXAALDevice(zDev *Z2MDevice, payload map[string]interface{}) {
slog.Info("Updating device:", "name", zDev.FriendlyName)
for _, dev := range zDev.XAALDevices {
dev.update(payload)
}
// for _, dev := range zDev.XAALDevice {
// for key, value := range payload {
// if key == "contact" && dev.DevType == "contact.basic" {
// dev.GetAttribute("detected").SetValue(!value.(bool))
// }
// if key == "battery" && dev.DevType == "battery.basic" {
// dev.GetAttribute("level").SetValue(value)
// }
// if key == "linkquality" && dev.DevType == "linkquality.basic" {
// value = math.Round(value.(float64) / 255 * 100)
// dev.GetAttribute("level").SetValue(value)
// }
// if key == "temperature" && dev.DevType == "thermometer.basic" {
// dev.GetAttribute("temperature").SetValue(value)
// }
// if key == "humidity" && dev.DevType == "hygrometer.basic" {
// dev.GetAttribute("humidity").SetValue(value)
// }
// if key == "power" && dev.DevType == "powermeter.basic" {
// dev.GetAttribute("power").SetValue(value)
// }
// if key == "state" && dev.DevType == "powerrelay.basic" {
// switch value {
// case "ON":
// dev.GetAttribute("power").SetValue(true)
// case "OFF":
// dev.GetAttribute("power").SetValue(false)
// }
// }
// }
// }
}
......@@ -79,10 +79,34 @@ func (zDev *Z2MDevice) Send(payload interface{}) {
slog.Debug("Sending", "topic", topic, "payload", payload)
client := GetGW().client
if token := client.Publish(topic, 0, false, payload); token.Wait() && token.Error() != nil {
slog.Error("Error sending", ":", token.Error())
slog.Error("SEND Error", ":", token.Error())
}
}
func (zDev *Z2MDevice) Available() {
client := GetGW().client
if token := client.Publish(zDev.GetTopic()+"/availability", 0, false, `{"state": "online"}`); token.Wait() && token.Error() != nil {
slog.Error("REFRESH Error", ":", token.Error())
}
}
func (zDev *Z2MDevice) Sync() {
client := GetGW().client
buf := ``
for _, expose := range zDev.Definition.Exposes {
if expose.Access == 1 || expose.Access == 3 || expose.Access == 5 || expose.Access == 7 {
buf += fmt.Sprintf(`"%s": "",`, expose.Name)
}
}
buf = strings.TrimSuffix(buf, ",")
buf = `{` + buf + `}`
fmt.Println(buf)
if token := client.Publish(zDev.GetTopic()+"/get", 0, false, buf); token.Wait() && token.Error() != nil {
slog.Error("REFRESH Error", ":", token.Error())
}
}
func (l AccessLevel) String() string {
return [...]string{"--", "R", "W", "RW", "Err", "RN", "Err", "RWN"}[l]
// return fmt.Sprintf("%d", l)
......@@ -141,9 +165,10 @@ func jsonParseDevices(jsonData []byte) {
continue
}
zDev.dump()
newDevices(gw, &zDev)
newXAALDevices(gw, &zDev)
// TODO: filter if device already exists
gw.AddZDevice(&zDev)
// zDev.Sync()
}
}
......@@ -192,8 +217,8 @@ func mqttPublishHander(client MQTT.Client, msg MQTT.Message) {
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())
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment