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

Add support for Remotes

Added support for remotes, tested w/ Ikea on/off/brigthness and actions
buttons
parent db643d4c
No related branches found
No related tags found
No related merge requests found
......@@ -26,6 +26,7 @@ func newXAALDevices(gw *Gateway, zDev *Z2MDevice) {
"linkquality": NewLinkQuality,
"battery": NewBattery,
"power": NewPowerMeter,
"action": NewButtonRemote,
}
if createFunc, ok := deviceMap[expose.Name]; ok {
......
......@@ -2,6 +2,7 @@ package main
import (
"fmt"
"log/slog"
"math"
"gitlab.imt-atlantique.fr/xaal/code/go/core/schemas"
......@@ -48,6 +49,11 @@ type Lamp struct {
Z2MDevice Z2MDevice
}
type ButtonRemote struct {
XAALDevice
Z2MDevice Z2MDevice
}
type XAALDeviceInterface interface {
update(map[string]interface{})
getXAALDevice() *xaal.Device
......@@ -258,3 +264,33 @@ func (dev *Lamp) Toggle(xaal.MessageBody) *xaal.MessageBody {
dev.Z2MDevice.Set(`{"state": "TOGGLE"}`)
return nil
}
// ----------------------------------------------------------------------------
// NetButtonRemote
// ----------------------------------------------------------------------------
func NewButtonRemote(addr uuid.UUID, zDev *Z2MDevice) XAALDeviceInterface {
dev := &ButtonRemote{XAALDevice{schemas.NewButtonRemote(addr)}, *zDev}
dev.setup(zDev)
dev.SetMethod("get_buttons", dev.GetButtons)
return dev
}
func (dev *ButtonRemote) update(payload map[string]interface{}) {
value, exists := payload["action"].(string)
if exists {
body := make(xaal.MessageBody)
body["action"] = 0
body["button"] = value
dev.SendNotification("click", body)
}
}
func (dev *ButtonRemote) GetButtons(xaal.MessageBody) *xaal.MessageBody {
slog.Info("defaultGetButtons method: get_buttons")
body := make(xaal.MessageBody)
action := dev.Z2MDevice.GetExpose("action")
if action != nil {
body["buttons"] = action.Values
}
return &body
}
......@@ -138,6 +138,16 @@ func (zDev *Z2MDevice) dump() {
}
}
// return the expose with the given name
func (zDev *Z2MDevice) GetExpose(name string) *Expose {
for _, expose := range zDev.Definition.Exposes {
if expose.Name == name {
return &expose
}
}
return nil
}
// jsonParseDevices parses the bridge/devices json and creates new xAAL devices
// if they don't exist
func jsonParseDevices(jsonData []byte) {
......@@ -158,7 +168,7 @@ func jsonParseDevices(jsonData []byte) {
// TODO: filter if device already exists
gw.AddZDevice(&zDev)
zDev.Sync()
zDev.Available()
// zDev.Available()
}
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment