-
Notifications
You must be signed in to change notification settings - Fork 1
/
leap.go
149 lines (121 loc) · 3.36 KB
/
leap.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package lutron
import (
"crypto/tls"
"crypto/x509"
"errors"
"fmt"
"net"
)
// TODO: LAP and LEAP request and responses likely look different, if they are
// then separate them into two different objects.
type LEAPAuth struct {
KeyPEM []byte `json:"key_pem"`
CertPEM []byte `json:"cert_pem"`
RootPEM []byte `json:"root_pem"`
}
// LAPClient is a client for the LAP using for pairing and other
// pre-authentication requests.
type LEAPClient struct {
addr string
conn *tls.Conn
certs LEAPAuth
}
func NewLEAPClient(addr string, certs LEAPAuth) *LEAPClient {
return &LEAPClient{
addr: addr,
certs: certs,
}
}
func (c *LEAPClient) Connect() (*tls.Conn, error) {
cert, err := tls.X509KeyPair(c.certs.CertPEM, c.certs.KeyPEM)
if err != nil {
return nil, fmt.Errorf("error loading client certs: %w", err)
}
roots := x509.NewCertPool()
if ok := roots.AppendCertsFromPEM(c.certs.RootPEM); !ok {
return nil, errors.New("failed to parse root certificate")
}
conn, err := tls.Dial("tcp", net.JoinHostPort(c.addr, LEAPPort), &tls.Config{
RootCAs: roots,
Certificates: []tls.Certificate{cert},
// We cannot verify hostname because server cert CN's are not for a specific hostname.
InsecureSkipVerify: true,
})
if err != nil {
return nil, fmt.Errorf("error connecting to bridge: %w", err)
}
c.conn = conn
return conn, nil
}
func (c *LEAPClient) SendWait(r Request) (Response, error) {
if c.conn == nil {
return Response{}, errors.New("not connected")
}
return SendWait(c.conn, r)
}
func (c *LEAPClient) Ping() error {
req := Request{
Header: Header{
RequestType: Read,
URL: "/server/status/ping",
// URL: "/server/1/status/ping",
},
CommuniqueType: &ReadRequest,
// Body: Body{},
}
rsp, err := c.SendWait(req)
if err != nil {
return err
}
if rsp.Header.StatusCode != "200 OK" {
return errors.New(rsp.Header.StatusCode)
}
// Has Body.PingResponse.LEAPVersion
return nil
}
/*
URLS:
"CreateRequest", "/zone/{zone_id}/commandprocessor",
"CreateRequest", "/virtualbutton/{scene_id}/commandprocessor",
"ReadRequest", "/zone/{device['zone']}/status"
"ReadRequest", "/server/1/status/ping"
"ReadRequest", "/device"
"ReadRequest", "/server/2/id" // LIP device, pro only
"ReadRequest", "/area"
"ReadRequest", "/occupancygroup"
"ReadRequest", "/virtualbutton"
*/
// TODO: make command structs for each command type?
// Or possibly just make constructors for each that sets the CommandType and
// accepts the proper arguments
/*
Bodies
{
"Command": {
"CommandType": "GoToDimmedLevel",
"DimmedLevelParameters": {
"Level": value,
"FadeTime": _format_duration(fade_time),
},
}
},
{
"Command": {
"CommandType": "GoToLevel",
"Parameter": [{"Type": "Level", "Value": value}],
}
},
{
"Command": {
"CommandType": "GoToFanSpeed",
"FanSpeedParameters": {"FanSpeed": value},
{"Command": {"CommandType": "PressAndRelease"}},
*/
// def _format_duration(duration: timedelta) -> str:
// """Convert a timedelta to the hh:mm:ss format used in LEAP."""
// total_seconds = math.floor(duration.total_seconds())
// seconds = int(total_seconds % 60)
// total_minutes = math.floor(total_seconds / 60)
// minutes = int(total_minutes % 60)
// hours = int(total_minutes / 60)
// return f"{hours:02}:{minutes:02}:{seconds:02}"