ewelink 2.2.2 → 2.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/VERSION +1 -1
  3. data/lib/ewelink/api.rb +36 -8
  4. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b7b1ea698c178aeed55aa90970659bb230f84e611a1bff87d135e301a81bca0d
4
- data.tar.gz: 73e86511d022f0481d0156b130ac08f679c5e06a1549ab1c903bb9b795187ee9
3
+ metadata.gz: d17e40d48a819572e83369500edbe6295a991626c3333ce4ff0765876f25ed51
4
+ data.tar.gz: 9caca646a1355592a276e5e4595ab486e407ecaecb105a9e7134028ecab4cb42
5
5
  SHA512:
6
- metadata.gz: 141e43b9082542fe3c6a598754b9d0e9e69f6b5612da8df2e56921e1b1df8c96517ac97059d782c7539177bf42889dbfd70b6f94186e99f8d22649071066ecf7
7
- data.tar.gz: 445ce9af569e5b997eff1ff20fcafec709cc3edf0a60e278262ba66c56341cf389f6d133c4650f8916af995e42f61a132a56ba822455e8a6595003aca6e92a48
6
+ metadata.gz: 6e8a2717428e2b57c7a30a7d4e9a1658032b1155a2f9bd2ba9b8b0cb2309748d248f24d66babfbe2212af9d8096e6be18ebdc3c60d799654571526edff1204d5
7
+ data.tar.gz: 9b9e517a3c8629d6a69b8195cab906d12f8f4d4231ab22311083deb75e5db97b02ec9c13a8509710017bb17cfcf1750c46919f52b393b73a0d453c423f120153
data/VERSION CHANGED
@@ -1 +1 @@
1
- 2.2.2
1
+ 2.3.0
@@ -11,6 +11,7 @@ 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
15
16
  SWITCH_STATUS_CHANGE_CHECK_TIMEOUT = 2.seconds
16
17
  WEB_SOCKET_WAIT_INTERVAL = 0.2.seconds
@@ -24,7 +25,10 @@ module Ewelink
24
25
  @phone_number = phone_number.presence.try(:strip)
25
26
  @web_socket_authenticated_api_keys = Set.new
26
27
  @web_socket_switches_statuses = {}
27
- raise(Error.new(":email or :phone_number must be specified")) if email.blank? && phone_number.blank?
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
28
32
  end
29
33
 
30
34
  def press_rf_bridge_button!(uuid)
@@ -301,16 +305,30 @@ module Ewelink
301
305
  end
302
306
 
303
307
  def send_to_web_socket(message)
304
- if web_socket_outdated_ping?
305
- Ewelink.logger.warn(self.class.name) { 'WebSocket ping is outdated' }
306
- reload
307
- end
308
308
  web_socket.send(message)
309
309
  rescue => e
310
310
  reload
311
311
  raise Error.new(e)
312
312
  end
313
313
 
314
+ def start_web_socket_authentication_check_thread
315
+ raise Error.new('WebSocket authentication check must only be started once') if @web_socket_authentication_check_thread.present?
316
+
317
+ @web_socket_authentication_check_thread = Thread.new do
318
+ loop do
319
+ Ewelink.logger.debug(self.class.name) { 'Checking if WebSocket is authenticated' }
320
+ begin
321
+ web_socket_wait_for(-> { web_socket_authenticated? }, initialize_web_socket: true) do
322
+ Ewelink.logger.debug(self.class.name) { 'WebSocket is authenticated' }
323
+ end
324
+ rescue => e
325
+ Ewelink.logger.error(self.class.name) { e }
326
+ end
327
+ sleep(WEB_SOCKET_CHECK_AUTHENTICATION_TIMEOUT)
328
+ end
329
+ end
330
+ end
331
+
314
332
  def start_web_socket_ping_thread(interval)
315
333
  @last_web_socket_pong_at = Time.now
316
334
  @web_socket_ping_interval = interval
@@ -330,14 +348,24 @@ module Ewelink
330
348
  end
331
349
 
332
350
  def web_socket
351
+ if web_socket_outdated_ping?
352
+ Ewelink.logger.warn(self.class.name) { 'WebSocket ping is outdated' }
353
+ reload
354
+ end
355
+
333
356
  synchronize(:web_socket) do
334
357
  next @web_socket if @web_socket
335
358
 
359
+ # Initializes caches before opening WebSocket: important in order to
360
+ # NOT cumulate requests Timeouts from #web_socket_wait_for.
361
+ api_keys
362
+ web_socket_url
363
+
364
+ Ewelink.logger.debug(self.class.name) { "Opening WebSocket to #{web_socket_url.inspect}" }
365
+
336
366
  @web_socket_thread = Thread.new do
337
367
  EventMachine.run do
338
- Ewelink.logger.debug(self.class.name) { "Opening WebSocket to #{web_socket_url.inspect}" }
339
-
340
- @web_socket = Faye::WebSocket::Client.new('wss://as-pconnect3.coolkit.cc:8080/api/ws')
368
+ @web_socket = Faye::WebSocket::Client.new(web_socket_url)
341
369
 
342
370
  @web_socket.on(:close) do |event|
343
371
  Ewelink.logger.debug(self.class.name) { 'WebSocket closed' }
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: 2.2.2
4
+ version: 2.3.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-09-04 00:00:00.000000000 Z
11
+ date: 2020-09-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport