added lights test and time interface

This commit is contained in:
Wlad Meixner 2019-02-11 23:01:08 +01:00
parent 2b3d33eccc
commit 36b8bab1d8
5 changed files with 145 additions and 10 deletions

View File

@ -16,8 +16,7 @@ import (
// Bridge is the hue bridge interface
type Bridge struct {
Config *Config
lightMonitors map[string]*Monitor
Config *Config
}
// BridgeUserConfig is the config provided for hue for a user

View File

@ -7,7 +7,7 @@ import (
// SwUpdate provides the current sw state and last install
type SwUpdate struct {
State string `json:"state,omitempty"`
LastInstall string `json:"lastinstall,omitempty"`
LastInstall Time `json:"lastinstall,omitempty"`
}
// LightCapabilities type providing control and certification settings
@ -18,6 +18,7 @@ type LightCapabilities struct {
// Light hue object
type Light struct {
State *LightState `json:"state,omitempty"`
SwUpdate *SwUpdate `json:"swupdate,omitempty"`
Type string `json:"type,omitempty"`
Name string `json:"name,omitempty"`
ModelID string `json:"modelid,omitempty"`
@ -25,7 +26,6 @@ type Light struct {
Productname string `json:"productname,omitempty"`
SwVersion string `json:"swversion,omitempty"`
UniqueID string `json:"uniqueID,omitempty"`
SwUpdate *SwUpdate `json:"swupdate,omitempty"`
// "capabilities": {
// "certified": true,
// "control": {
@ -66,15 +66,16 @@ type Light struct {
// LightState is the hue light>state object
type LightState struct {
On bool `json:"on"`
BridgeID int `json:"bridgeid,omitempty"`
BridgeID int `json:"bri,omitempty"`
Hue uint16 `json:"hue,omitempty"`
Sat uint8 `json:"sat,omitempty"`
Effect string `json:"effect,omitempty"`
XY []float32 `json:"xy,omitempty"`
Ct uint16 `json:"ct,omitempty"`
Alert string `json:"alert,omitempty"`
Effect string `json:"effect,omitempty"`
TransitionTime uint16 `json:"transitiontime,omitempty"`
ColorMode string `json:"colormode,omitempty"`
Mode string `json:"mode,omitempty"`
Reachable bool `json:"reachable,omitempty"`
}

101
lights_test.go Normal file
View File

@ -0,0 +1,101 @@
package hue
import (
"bytes"
"encoding/json"
"fmt"
"testing"
)
func TestParseLights(t *testing.T) {
target := make(map[string]*Light)
buffer := new(bytes.Buffer)
// Remove all spaces
json.Compact(buffer, []byte(LightsTestData))
bytes := buffer.Bytes()
// Unmarshal the data
json.Unmarshal(bytes, &target)
// Marshal it again
outputData, _ := json.Marshal(target)
old := string(bytes)
new := string(outputData)
if old != new {
fmt.Println("String do not match!")
fmt.Println("OLD: \n " + old)
fmt.Println("----------------------------- ")
fmt.Println("NEW: \n " + new)
t.Fail()
}
t.Log("Completed")
}
const LightsTestData = `{
"1": {
"state": {
"on": false,
"bri": 1,
"hue": 33761,
"sat": 254,
"effect": "none",
"xy": [
0.3171,
0.3366
],
"ct": 159,
"alert": "none",
"colormode": "xy",
"mode": "homeautomation",
"reachable": true
},
"swupdate": {
"state": "noupdates",
"lastinstall": "2018-01-02T19:24:20"
},
"type": "Extended color light",
"name": "Hue color lamp 7",
"modelid": "LCT007",
"manufacturername": "Philips",
"productname": "Hue color lamp",
"capabilities": {
"certified": true,
"control": {
"mindimlevel": 5000,
"maxlumen": 600,
"colorgamuttype": "B",
"colorgamut": [
[
0.675,
0.322
],
[
0.409,
0.518
],
[
0.167,
0.04
]
],
"ct": {
"min": 153,
"max": 500
}
},
"streaming": {
"renderer": true,
"proxy": false
}
},
"config": {
"archetype": "sultanbulb",
"function": "mixed",
"direction": "omnidirectional"
},
"uniqueid": "00:17:88:01:00:bd:c7:b9-0b",
"swversion": "5.105.0.21169"
}
}`

View File

@ -1,6 +1,8 @@
package hue
package monitor
import "reflect"
import (
"reflect"
)
// Monitor is a interface which is used for checking events
type Monitor struct {
@ -18,6 +20,6 @@ func (m *Monitor) Update(newState interface{}) {
}
}
func (b *Bridge) AddMonitor() {
// func (b *hue.Bridge) AddMonitor() {
}
// }

32
time.go Normal file
View File

@ -0,0 +1,32 @@
package hue
import (
"fmt"
"strings"
"time"
)
type Time struct {
time.Time
}
const TimeFormat = "2006-01-02T15:04:05"
// UnmarshalJSON unmarshals mite time from json
func (t *Time) UnmarshalJSON(b []byte) (err error) {
s := strings.Trim(string(b), "\"")
if s == "null" {
t.Time = time.Time{}
return
}
t.Time, err = time.Parse(TimeFormat, s)
return
}
// MarshalJSON marshals mite time to json
func (t *Time) MarshalJSON() ([]byte, error) {
if t.Time.UnixNano() == (time.Time{}).UnixNano() {
return []byte("null"), nil
}
return []byte(fmt.Sprintf("\"%s\"", t.Time.Format(TimeFormat))), nil
}