-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathlayout.go
112 lines (87 loc) · 2.52 KB
/
layout.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package nanoleaf
import (
"encoding/json"
"fmt"
"net/http"
)
// NanoLayout layouts
type NanoLayout struct {
nano *Nanoleaf
endpoint string
}
// GlobalOrientation globalOrientation as a go struct
type GlobalOrientation MinMaxValue
// PanelPositionData positionData as a go struct
type PanelPositionData struct {
ID int `json:"panelId"`
X int `json:"x"`
Y int `json:"y"`
Z int `json:"z"`
}
// PanelLayout panelLayout as a go struct
type PanelLayout struct {
Panels int `json:"numPanels"`
SideLength int `json:"sideLength"`
PositionData []PanelPositionData `json:"positionData"`
}
// newNanoLayout returns a new instance of NanoLayout
func newNanoLayout(nano *Nanoleaf) *NanoLayout {
return &NanoLayout{
nano: nano,
endpoint: fmt.Sprintf("%s/%s/panelLayout", nano.url, nano.token),
}
}
// GetGlobalOrientation returns the global orientation
func (l *NanoLayout) GetGlobalOrientation() (*GlobalOrientation, error) {
url := fmt.Sprintf("%s/globalOrientation", l.endpoint)
resp, err := l.nano.client.R().Get(url)
if err != nil {
return nil, err
}
if resp.StatusCode() == http.StatusUnauthorized {
return nil, ErrUnauthorized
}
if resp.StatusCode() != http.StatusOK {
return nil, ErrUnexpectedResponse
}
var globalOrientation GlobalOrientation
if err := json.Unmarshal(resp.Body(), &globalOrientation); err != nil {
return nil, ErrParsingJSON
}
return &globalOrientation, nil
}
// SetGlobalOrientation sets the global orientation
func (l *NanoLayout) SetGlobalOrientation(value int) error {
url := fmt.Sprintf("%s/globalOrientation", l.endpoint)
body := jsonPayload{"globalOrientation": jsonPayload{"value": value}}
resp, err := l.nano.client.R().SetHeader("Content-Type", "application/json").SetBody(body).Put(url)
if err != nil {
return err
}
if resp.StatusCode() == http.StatusUnauthorized {
return ErrUnauthorized
}
if resp.StatusCode() != http.StatusNoContent {
return ErrUnexpectedResponse
}
return nil
}
// GetLayout returns the layout of nanoleafs
func (l *NanoLayout) GetLayout() (*PanelLayout, error) {
url := fmt.Sprintf("%s/layout", l.endpoint)
resp, err := l.nano.client.R().Get(url)
if err != nil {
return nil, err
}
if resp.StatusCode() == http.StatusUnauthorized {
return nil, ErrUnauthorized
}
if resp.StatusCode() != http.StatusOK {
return nil, ErrUnexpectedResponse
}
var panelLayout PanelLayout
if err := json.Unmarshal(resp.Body(), &panelLayout); err != nil {
return nil, ErrParsingJSON
}
return &panelLayout, nil
}