-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathewelink.go
185 lines (143 loc) · 5.7 KB
/
ewelink.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
package ewelink
import (
"context"
"fmt"
"net/http"
"gopkg.in/go-playground/validator.v9"
)
// Ewelink contains the validator and Configuration context.
type Ewelink struct {
validator *validator.Validate
client Client
websocketClient WebsocketClient
}
// New returns a new instance of 'Ewelink'.
func New(options ...Option) *Ewelink {
ewelink := &Ewelink{client: newClient(), websocketClient: newWebsocketClient()}
// Apply options if there are any, can overwrite defaults
for _, option := range options {
option(ewelink)
}
return ewelink
}
// Authenticate - Authenticates a new user session using an authenticator.
func (e Ewelink) Authenticate(context context.Context, configuration *Configuration, authenticator Authenticator, options ...SessionOptionFunc) (*Session, error) {
session := &Session{MobileDevice: newIOSDevice(), Application: newApplication(), Configuration: configuration}
// newSessionWith
// Apply options if there are any, can overwrite defaults
for _, option := range options {
option(session)
}
if err := authenticator.Authenticate(context, e.client, session); err != nil {
return nil, fmt.Errorf("failed to authenticate %w", err)
}
return session, nil
}
// AuthenticateWithEmail - Authenticates a new user session using email as identifier.
func (e Ewelink) AuthenticateWithEmail(context context.Context, configuration *Configuration, email string, password string, options ...SessionOptionFunc) (*Session, error) {
return e.Authenticate(context, configuration, NewEmailAuthenticator(email, password), options...)
}
// AuthenticateWithPhoneNumber - Authenticates a new user session using phoneNumber as identifier.
func (e Ewelink) AuthenticateWithPhoneNumber(context context.Context, configuration *Configuration, phoneNumber string, password string, options ...SessionOptionFunc) (*Session, error) {
return e.Authenticate(context, configuration, NewPhoneNumberAuthenticator(phoneNumber, password), options...)
}
// GetDevices - Returns information about the devices.
func (e Ewelink) GetDevices(ctx context.Context, session *Session) (*DevicesResponse, error) {
request := newGetDevicesRequest(createDeviceQuery(session), session)
response, err := e.call(ctx, request)
if err != nil {
return nil, err
}
return response.(*DevicesResponse), nil
}
// GetDevice - Returns information about a device.
func (e Ewelink) GetDevice(ctx context.Context, session *Session, deviceID string) (*Device, error) {
devices, err := e.GetDevices(ctx, session)
if err != nil {
return nil, err
}
return devices.getDeviceByID(deviceID)
}
// SetDevicePowerState - Toggles the outlet(s) of a device.
func (e Ewelink) SetDevicePowerState(context context.Context, session *Session, device *Device, stateOn bool) (Response, error) {
numberOfOutlets, err := device.outletCount()
if err != nil {
return nil, err
}
request := newWebsocketRequest(
createUpdateActionPayload(
createUpdatePowerStateOfDeviceParameters(numberOfOutlets, stateOn), session.User.APIKey, device.DeviceID), &SetDevicePowerStateResponse{})
response, err := e.websocketCall(context, session, request)
if err != nil {
return nil, err
}
return response.(*SetDevicePowerStateResponse), nil
}
// SetDeviceOutletPowerState - Toggles an outlet of a device
// The outlet indices start at 0.
func (e Ewelink) SetDeviceOutletPowerState(context context.Context, session *Session, device *Device, stateOn bool, outletIndex int) (Response, error) {
if err := device.validOutletIndice(outletIndex); err != nil {
return nil, err
}
numberOfOutlets, err := device.outletCount()
if err != nil {
return nil, err
}
var parameters parameters // TODO action parameters
if numberOfOutlets == 1 {
parameters = createUpdatePowerStateOfDeviceParameters(numberOfOutlets, stateOn)
} else {
parameters = createUpdatePowerStateOfOutletParameters(device, outletIndex, numberOfOutlets, stateOn)
}
request := newWebsocketRequest(
createUpdateActionPayload(parameters, session.User.APIKey, device.DeviceID), &SetDeviceOutletPowerStateResponse{})
response, err := e.websocketCall(context, session, request)
if err != nil {
return nil, err
}
return response.(*SetDeviceOutletPowerStateResponse), nil
}
func (e Ewelink) websocketCall(context context.Context, session *Session, request WebsocketRequest) (Response, error) {
authenticationRequest := newWebsocketRequest(createAuthenticateActionPayload(session), &websocketResponse{})
// Always authenticate as a first step/request
responses, err := e.websocketClient.call(context, []WebsocketRequest{authenticationRequest, request}, session)
if err != nil {
return nil, fmt.Errorf("failed to send websocket request %w", err)
}
for i, response := range responses {
if response.Error != nil {
return nil, response.Error // TODO, wrap error, apiError?
}
// Skip the response of the authentication request
if i == 0 {
continue
}
return response.Response, nil
}
return nil, fmt.Errorf("websocket request is nil, not allowed")
}
func (e Ewelink) call(context context.Context, request HTTPRequest) (Response, error) {
if err := e.validate(request); err != nil {
return nil, err
}
return e.client.call(context, request)
}
func (e Ewelink) validate(request HTTPRequest) error {
if request.Payload() == nil {
return nil
}
// Validate the integrity of the payload
if err := e.validator.Struct(request.Payload()); err != nil {
return fmt.Errorf("valdation of http request failed %w", err)
}
return nil
}
// Option option definition.
type Option func(evelink *Ewelink)
// Function to create Option func to set custom http client.
// nolint:deadcode,unused
func withHTTPClient(client *http.Client) Option {
return func(ewelink *Ewelink) {
ewelink.client.withHTTPClient(client)
}
}