ewelink 2.2.0 → 3.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/VERSION +1 -1
- data/lib/ewelink/api.rb +122 -94
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 45f904d5e72d0038360340adc26dd7eb963f664804b43eee8235ee02d48f60ff
|
4
|
+
data.tar.gz: 98ee94c2d9187ea17250c3f32c1932f8c0df8a2123c341a47159c32e2aec66e9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 42977ef17b87c9c6710973968ca739d642108d6595861a870090ef13fbe5332b7ff99fba0dc8444e8022dfba065f4334426daf2dc423ed08314b3939cc41e69a
|
7
|
+
data.tar.gz: 4fa2068348c252aecfa1f0d55f91013a17658a3403fa7813208f34f027757b2459181323aae9a9d8c71c915ac8c83e881150c4eacee77ed85832b1adaeadb451
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
|
1
|
+
3.1.0
|
data/lib/ewelink/api.rb
CHANGED
@@ -11,7 +11,9 @@ module Ewelink
|
|
11
11
|
URL = 'https://#{region}-api.coolkit.cc:8080'
|
12
12
|
UUID_NAMESPACE = 'e25750fb-3710-41af-b831-23224f4dd609';
|
13
13
|
VERSION = 8
|
14
|
+
WEB_SOCKET_CHECK_AUTHENTICATION_TIMEOUT = 30.seconds
|
14
15
|
WEB_SOCKET_PING_TOLERANCE_FACTOR = 1.5
|
16
|
+
SWITCH_STATUS_CHANGE_CHECK_TIMEOUT = 2.seconds
|
15
17
|
WEB_SOCKET_WAIT_INTERVAL = 0.2.seconds
|
16
18
|
|
17
19
|
attr_reader :email, :password, :phone_number
|
@@ -21,15 +23,18 @@ module Ewelink
|
|
21
23
|
@mutexs = {}
|
22
24
|
@password = password.presence || raise(Error.new(":password must be specified"))
|
23
25
|
@phone_number = phone_number.presence.try(:strip)
|
24
|
-
@
|
26
|
+
@web_socket_authenticated = false
|
25
27
|
@web_socket_switches_statuses = {}
|
26
|
-
|
28
|
+
|
29
|
+
raise(Error.new(':email or :phone_number must be specified')) if email.blank? && phone_number.blank?
|
30
|
+
|
31
|
+
start_web_socket_authentication_check_thread
|
27
32
|
end
|
28
33
|
|
29
34
|
def press_rf_bridge_button!(uuid)
|
30
35
|
synchronize(:press_rf_bridge_button) do
|
31
36
|
button = find_rf_bridge_button!(uuid)
|
32
|
-
web_socket_wait_for(-> { web_socket_authenticated? }) do
|
37
|
+
web_socket_wait_for(-> { web_socket_authenticated? }, initialize_web_socket: true) do
|
33
38
|
params = {
|
34
39
|
'action' => 'update',
|
35
40
|
'apikey' => button[:api_key],
|
@@ -50,15 +55,40 @@ module Ewelink
|
|
50
55
|
end
|
51
56
|
|
52
57
|
def reload
|
53
|
-
Ewelink.logger.debug(self.class.name) { 'Reloading API (authentication token, devices, region,...)' }
|
54
|
-
|
58
|
+
Ewelink.logger.debug(self.class.name) { 'Reloading API (authentication token, api key, devices, region, connections,...)' }
|
59
|
+
|
60
|
+
@web_socket_authenticated = false
|
61
|
+
@web_socket_switches_statuses.clear
|
62
|
+
|
63
|
+
[@web_socket_ping_thread, @web_socket_thread].each do |thread|
|
64
|
+
next unless thread
|
65
|
+
if Thread.current == thread
|
66
|
+
thread[:stop] = true
|
67
|
+
else
|
68
|
+
thread.kill
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
if @web_socket.present?
|
73
|
+
begin
|
74
|
+
@web_socket.close if @web_socket.open?
|
75
|
+
rescue
|
76
|
+
# Ignoring close errors
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
55
80
|
[
|
56
|
-
:@
|
57
|
-
:@authentication_token,
|
81
|
+
:@authentication_infos,
|
58
82
|
:@devices,
|
83
|
+
:@last_web_socket_pong_at,
|
59
84
|
:@region,
|
60
85
|
:@rf_bridge_buttons,
|
61
86
|
:@switches,
|
87
|
+
:@web_socket_ping_interval,
|
88
|
+
:@web_socket_ping_thread,
|
89
|
+
:@web_socket_thread,
|
90
|
+
:@web_socket_url,
|
91
|
+
:@web_socket,
|
62
92
|
].each do |variable|
|
63
93
|
remove_instance_variable(variable) if instance_variable_defined?(variable)
|
64
94
|
end
|
@@ -102,7 +132,7 @@ module Ewelink
|
|
102
132
|
def switch_on?(uuid)
|
103
133
|
switch = find_switch!(uuid)
|
104
134
|
if @web_socket_switches_statuses[switch[:uuid]].nil?
|
105
|
-
web_socket_wait_for(-> { web_socket_authenticated? }) do
|
135
|
+
web_socket_wait_for(-> { web_socket_authenticated? }, initialize_web_socket: true) do
|
106
136
|
Ewelink.logger.debug(self.class.name) { "Checking switch #{switch[:uuid].inspect} status" }
|
107
137
|
params = {
|
108
138
|
'action' => 'query',
|
@@ -115,7 +145,7 @@ module Ewelink
|
|
115
145
|
send_to_web_socket(JSON.generate(params))
|
116
146
|
end
|
117
147
|
end
|
118
|
-
web_socket_wait_for(-> { !@web_socket_switches_statuses[switch[:uuid]].nil? }) do
|
148
|
+
web_socket_wait_for(-> { !@web_socket_switches_statuses[switch[:uuid]].nil? }, initialize_web_socket: true) do
|
119
149
|
@web_socket_switches_statuses[switch[:uuid]] == 'on'
|
120
150
|
end
|
121
151
|
end
|
@@ -148,7 +178,7 @@ module Ewelink
|
|
148
178
|
end
|
149
179
|
switch = find_switch!(uuid)
|
150
180
|
@web_socket_switches_statuses[switch[:uuid]] = nil
|
151
|
-
web_socket_wait_for(-> { web_socket_authenticated? }) do
|
181
|
+
web_socket_wait_for(-> { web_socket_authenticated? }, initialize_web_socket: true) do
|
152
182
|
params = {
|
153
183
|
'action' => 'update',
|
154
184
|
'apikey' => switch[:api_key],
|
@@ -163,43 +193,40 @@ module Ewelink
|
|
163
193
|
Ewelink.logger.debug(self.class.name) { "Turning switch #{switch[:uuid].inspect} #{on ? 'on' : 'off'}" }
|
164
194
|
send_to_web_socket(JSON.generate(params))
|
165
195
|
end
|
196
|
+
sleep(SWITCH_STATUS_CHANGE_CHECK_TIMEOUT)
|
166
197
|
switch_on?(switch[:uuid]) # Waiting for switch status update
|
167
198
|
true
|
168
199
|
end
|
169
200
|
|
170
201
|
private
|
171
202
|
|
172
|
-
def
|
173
|
-
|
174
|
-
@api_keys ||= Set.new(devices.map { |device| device['apikey'] })
|
175
|
-
end
|
203
|
+
def api_key
|
204
|
+
authentication_infos[:api_key]
|
176
205
|
end
|
177
206
|
|
178
|
-
def
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
send_to_web_socket(JSON.generate(params))
|
193
|
-
end
|
207
|
+
def authenticate_web_socket_api_key
|
208
|
+
params = {
|
209
|
+
'action' => 'userOnline',
|
210
|
+
'apikey' => api_key,
|
211
|
+
'appid' => APP_ID,
|
212
|
+
'at' => authentication_token,
|
213
|
+
'nonce' => nonce,
|
214
|
+
'sequence' => web_socket_sequence,
|
215
|
+
'ts' => Time.now.to_i,
|
216
|
+
'userAgent' => 'app',
|
217
|
+
'version' => VERSION,
|
218
|
+
}
|
219
|
+
Ewelink.logger.debug(self.class.name) { "Authenticating WebSocket API key: #{api_key.truncate(16).inspect}" }
|
220
|
+
send_to_web_socket(JSON.generate(params))
|
194
221
|
end
|
195
222
|
|
196
223
|
def authentication_headers
|
197
224
|
{ 'Authorization' => "Bearer #{authentication_token}" }
|
198
225
|
end
|
199
226
|
|
200
|
-
def
|
201
|
-
synchronize(:
|
202
|
-
@
|
227
|
+
def authentication_infos
|
228
|
+
synchronize(:authentication_infos) do
|
229
|
+
@authentication_infos ||= begin
|
203
230
|
params = {
|
204
231
|
'appid' => APP_ID,
|
205
232
|
'imei' => SecureRandom.uuid.upcase,
|
@@ -216,11 +243,19 @@ module Ewelink
|
|
216
243
|
body = JSON.generate(params)
|
217
244
|
response = rest_request(:post, '/api/user/login', { body: body, headers: { 'Authorization' => "Sign #{Base64.encode64(OpenSSL::HMAC.digest('SHA256', APP_SECRET, body))}" } })
|
218
245
|
raise(Error.new('Authentication token not found')) if response['at'].blank?
|
219
|
-
|
246
|
+
raise(Error.new('API key not found')) if response['user'].blank? || response['user']['apikey'].blank?
|
247
|
+
{
|
248
|
+
authentication_token: response['at'].tap { Ewelink.logger.debug(self.class.name) { 'Authentication token found' } },
|
249
|
+
api_key: response['user']['apikey'].tap { Ewelink.logger.debug(self.class.name) { 'API key found' } },
|
250
|
+
}
|
220
251
|
end
|
221
252
|
end
|
222
253
|
end
|
223
254
|
|
255
|
+
def authentication_token
|
256
|
+
authentication_infos[:authentication_token]
|
257
|
+
end
|
258
|
+
|
224
259
|
def devices
|
225
260
|
synchronize(:devices) do
|
226
261
|
@devices ||= begin
|
@@ -237,40 +272,6 @@ module Ewelink
|
|
237
272
|
end
|
238
273
|
end
|
239
274
|
|
240
|
-
def dispose_web_socket
|
241
|
-
Ewelink.logger.debug(self.class.name) { 'Dispose WebSocket' }
|
242
|
-
@web_socket_authenticated_api_keys.clear
|
243
|
-
@web_socket_switches_statuses.clear
|
244
|
-
|
245
|
-
[@web_socket_ping_thread, @web_socket_thread].each do |thread|
|
246
|
-
next unless thread
|
247
|
-
if Thread.current == thread
|
248
|
-
thread[:stop] = true
|
249
|
-
else
|
250
|
-
thread.kill
|
251
|
-
end
|
252
|
-
end
|
253
|
-
|
254
|
-
if @web_socket.present?
|
255
|
-
begin
|
256
|
-
@web_socket.close if @web_socket.open?
|
257
|
-
rescue
|
258
|
-
# Ignoring close errors
|
259
|
-
end
|
260
|
-
end
|
261
|
-
|
262
|
-
[
|
263
|
-
:@last_web_socket_pong_at,
|
264
|
-
:@web_socket_ping_interval,
|
265
|
-
:@web_socket_ping_thread,
|
266
|
-
:@web_socket_thread,
|
267
|
-
:@web_socket_url,
|
268
|
-
:@web_socket,
|
269
|
-
].each do |variable|
|
270
|
-
remove_instance_variable(variable) if instance_variable_defined?(variable)
|
271
|
-
end
|
272
|
-
end
|
273
|
-
|
274
275
|
def find_rf_bridge_button!(uuid)
|
275
276
|
rf_bridge_buttons.find { |button| button[:uuid] == uuid } || raise(Error.new("No such RF bridge button with UUID: #{uuid.inspect}"))
|
276
277
|
end
|
@@ -299,7 +300,7 @@ module Ewelink
|
|
299
300
|
Ewelink.logger.debug(self.class.name) { "Switched to region #{region.inspect}" }
|
300
301
|
return rest_request(method, path, options)
|
301
302
|
end
|
302
|
-
remove_instance_variable(:@
|
303
|
+
remove_instance_variable(:@authentication_infos) if instance_variable_defined?(:@authentication_infos) && [401, 403].include?(response['error'])
|
303
304
|
raise(Error.new("#{method} #{url}: #{response['error']} #{response['msg']}".strip)) if response['error'].present? && response['error'] != 0
|
304
305
|
response.to_h
|
305
306
|
rescue Errno::ECONNREFUSED, OpenSSL::OpenSSLError, SocketError, Timeout::Error => e
|
@@ -307,16 +308,30 @@ module Ewelink
|
|
307
308
|
end
|
308
309
|
|
309
310
|
def send_to_web_socket(message)
|
310
|
-
if web_socket_outdated_ping?
|
311
|
-
Ewelink.logger.warn(self.class.name) { 'WebSocket ping is outdated' }
|
312
|
-
dispose_web_socket
|
313
|
-
end
|
314
311
|
web_socket.send(message)
|
315
312
|
rescue => e
|
316
|
-
|
313
|
+
reload
|
317
314
|
raise Error.new(e)
|
318
315
|
end
|
319
316
|
|
317
|
+
def start_web_socket_authentication_check_thread
|
318
|
+
raise Error.new('WebSocket authentication check must only be started once') if @web_socket_authentication_check_thread.present?
|
319
|
+
|
320
|
+
@web_socket_authentication_check_thread = Thread.new do
|
321
|
+
loop do
|
322
|
+
Ewelink.logger.debug(self.class.name) { 'Checking if WebSocket is authenticated' }
|
323
|
+
begin
|
324
|
+
web_socket_wait_for(-> { web_socket_authenticated? }, initialize_web_socket: true) do
|
325
|
+
Ewelink.logger.debug(self.class.name) { 'WebSocket is authenticated' }
|
326
|
+
end
|
327
|
+
rescue => e
|
328
|
+
Ewelink.logger.error(self.class.name) { e }
|
329
|
+
end
|
330
|
+
sleep(WEB_SOCKET_CHECK_AUTHENTICATION_TIMEOUT)
|
331
|
+
end
|
332
|
+
end
|
333
|
+
end
|
334
|
+
|
320
335
|
def start_web_socket_ping_thread(interval)
|
321
336
|
@last_web_socket_pong_at = Time.now
|
322
337
|
@web_socket_ping_interval = interval
|
@@ -336,24 +351,34 @@ module Ewelink
|
|
336
351
|
end
|
337
352
|
|
338
353
|
def web_socket
|
354
|
+
if web_socket_outdated_ping?
|
355
|
+
Ewelink.logger.warn(self.class.name) { 'WebSocket ping is outdated' }
|
356
|
+
reload
|
357
|
+
end
|
358
|
+
|
339
359
|
synchronize(:web_socket) do
|
340
360
|
next @web_socket if @web_socket
|
341
361
|
|
362
|
+
# Initializes caches before opening WebSocket: important in order to
|
363
|
+
# NOT cumulate requests Timeouts from #web_socket_wait_for.
|
364
|
+
api_key
|
365
|
+
web_socket_url
|
366
|
+
|
367
|
+
Ewelink.logger.debug(self.class.name) { "Opening WebSocket to #{web_socket_url.inspect}" }
|
368
|
+
|
342
369
|
@web_socket_thread = Thread.new do
|
343
370
|
EventMachine.run do
|
344
|
-
|
345
|
-
|
346
|
-
@web_socket = Faye::WebSocket::Client.new('wss://as-pconnect3.coolkit.cc:8080/api/ws')
|
371
|
+
@web_socket = Faye::WebSocket::Client.new(web_socket_url)
|
347
372
|
|
348
373
|
@web_socket.on(:close) do |event|
|
349
374
|
Ewelink.logger.debug(self.class.name) { 'WebSocket closed' }
|
350
|
-
|
375
|
+
reload
|
351
376
|
end
|
352
377
|
|
353
378
|
@web_socket.on(:open) do |event|
|
354
379
|
Ewelink.logger.debug(self.class.name) { 'WebSocket opened' }
|
355
380
|
@last_web_socket_pong_at = Time.now
|
356
|
-
|
381
|
+
authenticate_web_socket_api_key
|
357
382
|
end
|
358
383
|
|
359
384
|
@web_socket.on(:message) do |event|
|
@@ -369,11 +394,13 @@ module Ewelink
|
|
369
394
|
json = JSON.parse(message)
|
370
395
|
rescue => e
|
371
396
|
Ewelink.logger.error(self.class.name) { 'WebSocket JSON parse error' }
|
397
|
+
reload
|
372
398
|
next
|
373
399
|
end
|
374
400
|
|
375
401
|
if json.key?('error') && json['error'] != 0
|
376
402
|
Ewelink.logger.error(self.class.name) { "WebSocket message error: #{message.inspect}" }
|
403
|
+
reload
|
377
404
|
next
|
378
405
|
end
|
379
406
|
|
@@ -381,8 +408,8 @@ module Ewelink
|
|
381
408
|
start_web_socket_ping_thread(json['config']['hbInterval'] + 7)
|
382
409
|
end
|
383
410
|
|
384
|
-
if json['apikey'].present? && !@
|
385
|
-
@
|
411
|
+
if json['apikey'].present? && !@web_socket_authenticated && json['apikey'] == api_key
|
412
|
+
@web_socket_authenticated = true
|
386
413
|
Ewelink.logger.debug(self.class.name) { "WebSocket successfully authenticated API key: #{json['apikey'].truncate(16).inspect}" }
|
387
414
|
end
|
388
415
|
|
@@ -397,18 +424,14 @@ module Ewelink
|
|
397
424
|
end
|
398
425
|
end
|
399
426
|
|
400
|
-
|
401
|
-
|
402
|
-
sleep(WEB_SOCKET_WAIT_INTERVAL)
|
403
|
-
end
|
427
|
+
web_socket_wait_for(-> { @web_socket.present? }) do
|
428
|
+
@web_socket
|
404
429
|
end
|
405
|
-
|
406
|
-
@web_socket
|
407
430
|
end
|
408
431
|
end
|
409
432
|
|
410
433
|
def web_socket_authenticated?
|
411
|
-
|
434
|
+
@web_socket_authenticated.present?
|
412
435
|
end
|
413
436
|
|
414
437
|
def web_socket_outdated_ping?
|
@@ -438,13 +461,18 @@ module Ewelink
|
|
438
461
|
end
|
439
462
|
end
|
440
463
|
|
441
|
-
def web_socket_wait_for(condition, &block)
|
442
|
-
web_socket
|
443
|
-
|
444
|
-
|
445
|
-
|
464
|
+
def web_socket_wait_for(condition, initialize_web_socket: false, &block)
|
465
|
+
web_socket if initialize_web_socket
|
466
|
+
begin
|
467
|
+
Timeout.timeout(REQUEST_TIMEOUT) do
|
468
|
+
while !condition.call
|
469
|
+
sleep(WEB_SOCKET_WAIT_INTERVAL)
|
470
|
+
end
|
471
|
+
block_given? ? yield : true
|
446
472
|
end
|
447
|
-
|
473
|
+
rescue => e
|
474
|
+
reload
|
475
|
+
raise Error.new(e)
|
448
476
|
end
|
449
477
|
end
|
450
478
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ewelink
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 3.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Alexis Toulotte
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2020-
|
11
|
+
date: 2020-11-12 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|