Skip to content

Commit

Permalink
Removed generics for HTTP
Browse files Browse the repository at this point in the history
Make it easier to program against it similar as websocket part
  • Loading branch information
kanimaru committed Apr 30, 2023
1 parent a8b8745 commit 8d9fac3
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 21 deletions.
8 changes: 8 additions & 0 deletions http/client.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package http

import (
"encoding/json"
"fmt"
"github.com/kanimaru/godeconz"
)
Expand Down Expand Up @@ -80,3 +81,10 @@ func (c *Client[R]) Delete(path string, container interface{}, pathArguments ...
p := c.getApiPath(path, pathArguments...)
return c.adapter.Delete(p, container)
}

func as(val json.RawMessage, data interface{}) error {
if val == nil {
return nil
}
return json.Unmarshal(val, data)
}
2 changes: 1 addition & 1 deletion http/client_configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ type ConfigResponseFullState struct {
// All groups of the gateway.
Groups map[string]GroupResponseAttribute `json:"groups"`
// All lights of the gateway.
Lights map[string]LightResponseState[LightResponseStateDetail] `json:"lights"`
Lights map[string]LightResponseState `json:"lights"`
// All rules of the gateway. (as from deconz version > 2.04.12) TODO needs to be implemented
Rules json.RawMessage `json:"rules"`
// All schedules of the gateway. TODO needs to be implemented
Expand Down
19 changes: 10 additions & 9 deletions http/client_light.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package http

import "encoding/json"

type LightAlertMode string

// LightAlertNone light is not performing an alert
Expand Down Expand Up @@ -78,7 +80,7 @@ func (l LightResponseStateDetail) GetBrightness() int {
}
}

type LightResponseState[STATE any] struct {
type LightResponseState struct {

// The color capabilities as reported by the light.
Colorcapabilities *int `json:"colorcapabilities"`
Expand Down Expand Up @@ -110,12 +112,16 @@ type LightResponseState[STATE any] struct {
// Human-readable type of the light.
Type string `json:"type"`
// The current state of the light.
State STATE `json:"state"`
State json.RawMessage `json:"state"`
// The unique id of the light. It consists of the MAC address of the light followed by a dash and a unique endpoint
// identifier in the range 01 to FF.
Uniqueid string `json:"uniqueid"`
}

func (l LightResponseState) StateAs(data interface{}) error {
return as(l.State, data)
}

type LightRequestState struct {
// Trigger a temporary alert effect. optional
Alert LightAlertMode `json:"alert,omitempty"`
Expand Down Expand Up @@ -156,17 +162,12 @@ type LightRequestDelete struct {
}

// GetAllLights Returns a list of all lights.
func (c *Client[R]) GetAllLights(container *map[string]LightResponseState[LightResponseStateDetail]) (R, error) {
func (c *Client[R]) GetAllLights(container *map[string]LightResponseState) (R, error) {
return c.Get("/lights", container)
}

// GetLightState is almost the same as Client.GetLightState but you can add generics for the response
func GetLightState[R any, S any](c *Client[R], id string, container *LightResponseState[S]) (R, error) {
return c.Get("/lights/%s", container, id)
}

// GetLightState Returns the full state of a light.
func (c *Client[R]) GetLightState(id string, container *LightResponseState[LightResponseStateDetail]) (R, error) {
func (c *Client[R]) GetLightState(id string, container *LightResponseState) (R, error) {
return c.Get("/lights/%s", container, id)
}

Expand Down
27 changes: 16 additions & 11 deletions http/client_sensor.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package http

import "github.com/kanimaru/godeconz"
import (
"encoding/json"
)

type SensorRequestCreateConfig struct {
// (default: true)
Expand Down Expand Up @@ -37,9 +39,9 @@ const SensorModeScene SensorMode = 1
const SensorModeTwoGroup SensorMode = 2
const SensorModeTemperature SensorMode = 3

type SensorResponse[C godeconz.Config, S any] struct {
type SensorResponse struct {
// The config of the sensor. Refer to Change sensor config for further details.
Config C `json:"config"`
Config json.RawMessage `json:"config"`
// The Endpoint of the sensor.
Ep int `json:"ep"`
// HTTP etag which changes whenever the sensor changes.
Expand All @@ -55,7 +57,7 @@ type SensorResponse[C godeconz.Config, S any] struct {
// The name of the sensor.
Name string `json:"name"`
// The state of the sensor.
State S `json:"state"`
State json.RawMessage `json:"state"`
// Software version of the sensor.
Swversion string `json:"swversion"`
// The type of the sensor.
Expand All @@ -64,6 +66,14 @@ type SensorResponse[C godeconz.Config, S any] struct {
Uniqueid string `json:"uniqueid"`
}

func (s SensorResponse) StateAs(data interface{}) error {
return as(s.State, data)
}

func (s SensorResponse) ConfigAs(data interface{}) error {
return as(s.Config, data)
}

type SensorRequestUpdate struct {
// The name of the sensor. optional
Name *string `json:"name,omitempty"`
Expand All @@ -78,17 +88,12 @@ func (c *Client[R]) CreateSensor(create SensorRequestCreate[any]) (R, error) {

// GetAllSensors Returns a list of all sensors. If there are no sensors in the system an empty object {} is
// returned.
func (c *Client[R]) GetAllSensors(container *map[string]SensorResponse[BaseConfig, any]) (R, error) {
func (c *Client[R]) GetAllSensors(container *map[string]SensorResponse) (R, error) {
return c.Get("/sensors", container)
}

// GetSensor Returns the sensor with the specified id. See also GetSensor.
func (c *Client[R]) GetSensor(id string, container *SensorResponse[BaseConfig, any]) (R, error) {
return c.Get("/sensors/%s", container, id)
}

// GetSensor is almost same a Client.GetSensor but you can add generics for the response
func GetSensor[R any, C godeconz.Config, S any](c *Client[R], id string, container *SensorResponse[C, S]) (R, error) {
func (c *Client[R]) GetSensor(id string, container *SensorResponse) (R, error) {
return c.Get("/sensors/%s", container, id)
}

Expand Down

0 comments on commit 8d9fac3

Please sign in to comment.