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

Added support for sync

parent 45cb7234
No related branches found
No related tags found
No related merge requests found
...@@ -35,13 +35,13 @@ type Battery struct { ...@@ -35,13 +35,13 @@ type Battery struct {
type PowerRelay struct { type PowerRelay struct {
XAALDevice XAALDevice
Z2MDevice Z2MDevice Z2MDevice
Property string Property string
} }
type Lamp struct { type Lamp struct {
XAALDevice XAALDevice
Z2MDevice Z2MDevice Z2MDevice
} }
type XAALDeviceInterface interface { type XAALDeviceInterface interface {
...@@ -71,8 +71,10 @@ func NewContact(addr uuid.UUID, zDev *Z2MDevice) XAALDeviceInterface { ...@@ -71,8 +71,10 @@ func NewContact(addr uuid.UUID, zDev *Z2MDevice) XAALDeviceInterface {
} }
func (dev *Contact) update(payload map[string]interface{}) { func (dev *Contact) update(payload map[string]interface{}) {
value := payload["contact"].(bool) value, exists := payload["contact"].(bool)
dev.GetAttribute("detected").SetValue(!value) if exists {
dev.GetAttribute("detected").SetValue(!value)
}
} }
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
...@@ -85,8 +87,10 @@ func NewThermometer(addr uuid.UUID, zDev *Z2MDevice) XAALDeviceInterface { ...@@ -85,8 +87,10 @@ func NewThermometer(addr uuid.UUID, zDev *Z2MDevice) XAALDeviceInterface {
} }
func (dev *Thermometer) update(payload map[string]interface{}) { func (dev *Thermometer) update(payload map[string]interface{}) {
value := payload["temperature"].(float64) value, exists := payload["temperature"].(float64)
dev.GetAttribute("temperature").SetValue(value) if exists {
dev.GetAttribute("temperature").SetValue(value)
}
} }
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
...@@ -99,8 +103,10 @@ func NewHygrometer(addr uuid.UUID, zDev *Z2MDevice) XAALDeviceInterface { ...@@ -99,8 +103,10 @@ func NewHygrometer(addr uuid.UUID, zDev *Z2MDevice) XAALDeviceInterface {
} }
func (dev *Hygrometer) update(payload map[string]interface{}) { func (dev *Hygrometer) update(payload map[string]interface{}) {
value := payload["humidity"].(float64) value, exists := payload["humidity"].(float64)
dev.GetAttribute("humidity").SetValue(value) if exists {
dev.GetAttribute("humidity").SetValue(value)
}
} }
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
...@@ -113,9 +119,11 @@ func NewLinkQuality(addr uuid.UUID, zDev *Z2MDevice) XAALDeviceInterface { ...@@ -113,9 +119,11 @@ func NewLinkQuality(addr uuid.UUID, zDev *Z2MDevice) XAALDeviceInterface {
} }
func (dev *LinkQuality) update(payload map[string]interface{}) { func (dev *LinkQuality) update(payload map[string]interface{}) {
value := payload["linkquality"].(float64) value, exists := payload["linkquality"].(float64)
value = math.Round(value / 255 * 100) if exists {
dev.GetAttribute("level").SetValue(value) value = math.Round(value / 255 * 100)
dev.GetAttribute("level").SetValue(value)
}
} }
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
...@@ -128,8 +136,10 @@ func NewBattery(addr uuid.UUID, zDev *Z2MDevice) XAALDeviceInterface { ...@@ -128,8 +136,10 @@ func NewBattery(addr uuid.UUID, zDev *Z2MDevice) XAALDeviceInterface {
} }
func (dev *Battery) update(payload map[string]interface{}) { func (dev *Battery) update(payload map[string]interface{}) {
value := payload["battery"].(float64) value, exists := payload["battery"].(float64)
dev.GetAttribute("level").SetValue(value) if exists {
dev.GetAttribute("level").SetValue(value)
}
} }
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
...@@ -145,12 +155,14 @@ func NewPowerRelay(addr uuid.UUID, zDev *Z2MDevice, property string) XAALDeviceI ...@@ -145,12 +155,14 @@ func NewPowerRelay(addr uuid.UUID, zDev *Z2MDevice, property string) XAALDeviceI
} }
func (dev *PowerRelay) update(payload map[string]interface{}) { func (dev *PowerRelay) update(payload map[string]interface{}) {
value := payload[dev.Property].(string) value, exists := payload[dev.Property].(string)
power := dev.GetAttribute("power") if exists {
if value == "ON" { power := dev.GetAttribute("power")
power.SetValue(true) if value == "ON" {
} else if value == "OFF" { power.SetValue(true)
power.SetValue(false) } else if value == "OFF" {
power.SetValue(false)
}
} }
} }
...@@ -187,12 +199,14 @@ func NewLamp(addr uuid.UUID, zDev *Z2MDevice) XAALDeviceInterface { ...@@ -187,12 +199,14 @@ func NewLamp(addr uuid.UUID, zDev *Z2MDevice) XAALDeviceInterface {
} }
func (dev *Lamp) update(payload map[string]interface{}) { func (dev *Lamp) update(payload map[string]interface{}) {
value := payload["state"].(string) value, exists := payload["state"].(string)
light := dev.GetAttribute("light") if exists {
if value == "ON" { light := dev.GetAttribute("light")
light.SetValue(true) if value == "ON" {
} else { light.SetValue(true)
light.SetValue(false) } else {
light.SetValue(false)
}
} }
} }
......
...@@ -92,19 +92,9 @@ func (zDev *Z2MDevice) Available() { ...@@ -92,19 +92,9 @@ func (zDev *Z2MDevice) Available() {
func (zDev *Z2MDevice) Sync() { func (zDev *Z2MDevice) Sync() {
client := GetGW().client client := GetGW().client
buf := `` if token := client.Publish(zDev.GetTopic()+"/dump", 0, false, "{}"); token.Wait() && token.Error() != nil {
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()) slog.Error("REFRESH Error", ":", token.Error())
} }
} }
func (l AccessLevel) String() string { func (l AccessLevel) String() string {
...@@ -168,7 +158,7 @@ func jsonParseDevices(jsonData []byte) { ...@@ -168,7 +158,7 @@ func jsonParseDevices(jsonData []byte) {
newXAALDevices(gw, &zDev) newXAALDevices(gw, &zDev)
// TODO: filter if device already exists // TODO: filter if device already exists
gw.AddZDevice(&zDev) gw.AddZDevice(&zDev)
// zDev.Sync() zDev.Sync()
} }
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment