ewelink 2.1.0 → 2.1.1
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 +15 -16
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: bb352abf05d6bb6ef2f3b277c6f551d436bed969086478d3e121fd2e178fe4f9
|
4
|
+
data.tar.gz: b74266f2cbc92457a3bb0dc087752299a5a9f8348d3928a79d8c3c0719653e6a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2c5bb695c7d0094436454694008dd3c05c660fb1c7dfe2436ec1fa2fae3341559f901f3b9ae4ebfa99fcc276dcd4ceee02205ed1608d03e515b8da64b52f8a25
|
7
|
+
data.tar.gz: 4713a948c736a40496b3b33fff3b83253a4559693d692cfaa7251a4fc2d3f0e67699834f6513226edcbfe6cf7759ab54b1cb6c5a24cbe1559f54a75d6cbda6fd
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
2.1.
|
1
|
+
2.1.1
|
data/lib/ewelink/api.rb
CHANGED
@@ -5,9 +5,9 @@ module Ewelink
|
|
5
5
|
APP_ID = 'oeVkj2lYFGnJu5XUtWisfW4utiN4u9Mq'
|
6
6
|
APP_SECRET = '6Nz4n0xA8s8qdxQf2GqurZj2Fs55FUvM'
|
7
7
|
DEFAULT_REGION = 'us'
|
8
|
+
REQUEST_TIMEOUT = 10.seconds
|
8
9
|
RF_BRIDGE_DEVICE_UIID = 28
|
9
10
|
SWITCH_DEVICES_UIIDS = [1, 5, 6, 24]
|
10
|
-
TIMEOUT = 10.seconds
|
11
11
|
URL = 'https://#{region}-api.coolkit.cc:8080'
|
12
12
|
UUID_NAMESPACE = 'e25750fb-3710-41af-b831-23224f4dd609';
|
13
13
|
VERSION = 8
|
@@ -21,8 +21,8 @@ module Ewelink
|
|
21
21
|
@mutexs = {}
|
22
22
|
@password = password.presence || raise(Error.new(":password must be specified"))
|
23
23
|
@phone_number = phone_number.presence.try(:strip)
|
24
|
-
@switches_statuses = {}
|
25
24
|
@web_socket_authenticated_api_keys = Set.new
|
25
|
+
@web_socket_switches_statuses = {}
|
26
26
|
raise(Error.new(":email or :phone_number must be specified")) if email.blank? && phone_number.blank?
|
27
27
|
end
|
28
28
|
|
@@ -52,7 +52,6 @@ module Ewelink
|
|
52
52
|
def reload
|
53
53
|
Ewelink.logger.debug(self.class.name) { 'Reloading API (authentication token, devices, region, connections,...)' }
|
54
54
|
dispose_web_socket
|
55
|
-
@switches_statuses.clear
|
56
55
|
[
|
57
56
|
:@api_keys,
|
58
57
|
:@authentication_token,
|
@@ -102,7 +101,7 @@ module Ewelink
|
|
102
101
|
|
103
102
|
def switch_on?(uuid)
|
104
103
|
switch = find_switch!(uuid)
|
105
|
-
if @
|
104
|
+
if @web_socket_switches_statuses[switch[:uuid]].nil?
|
106
105
|
params = {
|
107
106
|
'action' => 'query',
|
108
107
|
'apikey' => switch[:api_key],
|
@@ -116,9 +115,9 @@ module Ewelink
|
|
116
115
|
send_to_web_socket(JSON.generate(params))
|
117
116
|
end
|
118
117
|
end
|
119
|
-
web_socket_wait_for(-> { !@
|
120
|
-
Ewelink.logger.debug(self.class.name) { "Switch #{switch[:uuid].inspect} is #{@
|
121
|
-
@
|
118
|
+
web_socket_wait_for(-> { !@web_socket_switches_statuses[switch[:uuid]].nil? }) do
|
119
|
+
Ewelink.logger.debug(self.class.name) { "Switch #{switch[:uuid].inspect} is #{@web_socket_switches_statuses[switch[:uuid]]}" }
|
120
|
+
@web_socket_switches_statuses[switch[:uuid]] == 'on'
|
122
121
|
end
|
123
122
|
end
|
124
123
|
|
@@ -149,7 +148,7 @@ module Ewelink
|
|
149
148
|
on = false
|
150
149
|
end
|
151
150
|
switch = find_switch!(uuid)
|
152
|
-
@
|
151
|
+
@web_socket_switches_statuses[switch[:uuid]] = nil
|
153
152
|
web_socket_wait_for(-> { web_socket_authenticated? }) do
|
154
153
|
params = {
|
155
154
|
'action' => 'update',
|
@@ -222,7 +221,8 @@ module Ewelink
|
|
222
221
|
end
|
223
222
|
|
224
223
|
def dispose_web_socket
|
225
|
-
@web_socket_authenticated_api_keys
|
224
|
+
@web_socket_authenticated_api_keys.clear
|
225
|
+
@web_socket_switches_statuses.clear
|
226
226
|
|
227
227
|
if @web_socket_ping_thread
|
228
228
|
if Thread.current == @web_socket_ping_thread
|
@@ -272,7 +272,7 @@ module Ewelink
|
|
272
272
|
method = method.to_s.upcase
|
273
273
|
headers = (options[:headers] || {}).reverse_merge('Content-Type' => 'application/json')
|
274
274
|
Ewelink.logger.debug(self.class.name) { "#{method} #{url}" }
|
275
|
-
response = HTTParty.send(method.downcase, url, options.merge(headers: headers).reverse_merge(timeout:
|
275
|
+
response = HTTParty.send(method.downcase, url, options.merge(headers: headers).reverse_merge(timeout: REQUEST_TIMEOUT))
|
276
276
|
raise(Error.new("#{method} #{url}: #{response.code}")) unless response.success?
|
277
277
|
if response['error'] == 301 && response['region'].present?
|
278
278
|
@region = response['region']
|
@@ -307,7 +307,7 @@ module Ewelink
|
|
307
307
|
api = self
|
308
308
|
|
309
309
|
WebSocket::Client::Simple.connect(web_socket_url) do |web_socket|
|
310
|
-
Ewelink.logger.debug(self.class.name) { "Opening WebSocket to #{web_socket_url}" }
|
310
|
+
Ewelink.logger.debug(self.class.name) { "Opening WebSocket to #{web_socket_url.inspect}" }
|
311
311
|
|
312
312
|
web_socket.on(:close) do
|
313
313
|
api.instance_eval do
|
@@ -343,10 +343,9 @@ module Ewelink
|
|
343
343
|
next
|
344
344
|
end
|
345
345
|
|
346
|
-
if !@web_socket_ping_thread && response.key?('config') && response['config']
|
346
|
+
if !@web_socket_ping_thread && response.key?('config') && response['config']['hb'] == 1 && response['config']['hbInterval'].present?
|
347
347
|
@last_web_socket_pong_at = Time.now
|
348
|
-
|
349
|
-
@web_socket_ping_interval = 30.seconds
|
348
|
+
@web_socket_ping_interval = response['config']['hbInterval'] + 7
|
350
349
|
Ewelink.logger.debug(self.class.name) { "Creating thread for WebSocket ping every #{@web_socket_ping_interval} seconds" }
|
351
350
|
@web_socket_ping_thread = Thread.new do
|
352
351
|
loop do
|
@@ -365,7 +364,7 @@ module Ewelink
|
|
365
364
|
|
366
365
|
if response['deviceid'].present? && response['params'].is_a?(Hash) && response['params']['switch'].present?
|
367
366
|
switch = switches.find { |switch| switch[:device_id] == response['deviceid'] }
|
368
|
-
@
|
367
|
+
@web_socket_switches_statuses[switch[:uuid]] = response['params']['switch'] if switch.present?
|
369
368
|
end
|
370
369
|
end
|
371
370
|
end
|
@@ -428,7 +427,7 @@ module Ewelink
|
|
428
427
|
|
429
428
|
def web_socket_wait_for(condition, &block)
|
430
429
|
web_socket # Initializes WebSocket
|
431
|
-
Timeout.timeout(
|
430
|
+
Timeout.timeout(REQUEST_TIMEOUT) do
|
432
431
|
loop do
|
433
432
|
if condition.call
|
434
433
|
return yield if block_given?
|