ewelink 2.2.1 → 2.2.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/VERSION +1 -1
  3. data/lib/ewelink/api.rb +50 -57
  4. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 957d00b0d8171519a48b1cf828cbd78c21dbde34b93f2c1930273ad460d5a770
4
- data.tar.gz: 0c636a47ac05f8428d9e099d952a0b89a512f8e199a3a85a72624c10a76590b3
3
+ metadata.gz: b7b1ea698c178aeed55aa90970659bb230f84e611a1bff87d135e301a81bca0d
4
+ data.tar.gz: 73e86511d022f0481d0156b130ac08f679c5e06a1549ab1c903bb9b795187ee9
5
5
  SHA512:
6
- metadata.gz: c20dfab19c133d80913716e7be2b20e47958e841e61d7b401f28b73e642358d80ce698d5fd22410ede0bb783114c0e91dfc7a1b4760ebab3738aaba3081275af
7
- data.tar.gz: 07664117dfacf9f32fa7e0965199bed938da619b75b313207d702d5ad83ebc2ceacbb76b266e40f3e995114542a110061ad271630a3b007fc2e4f1cbb2bccc58
6
+ metadata.gz: 141e43b9082542fe3c6a598754b9d0e9e69f6b5612da8df2e56921e1b1df8c96517ac97059d782c7539177bf42889dbfd70b6f94186e99f8d22649071066ecf7
7
+ data.tar.gz: 445ce9af569e5b997eff1ff20fcafec709cc3edf0a60e278262ba66c56341cf389f6d133c4650f8916af995e42f61a132a56ba822455e8a6595003aca6e92a48
data/VERSION CHANGED
@@ -1 +1 @@
1
- 2.2.1
1
+ 2.2.2
@@ -30,7 +30,7 @@ module Ewelink
30
30
  def press_rf_bridge_button!(uuid)
31
31
  synchronize(:press_rf_bridge_button) do
32
32
  button = find_rf_bridge_button!(uuid)
33
- web_socket_wait_for(-> { web_socket_authenticated? }) do
33
+ web_socket_wait_for(-> { web_socket_authenticated? }, initialize_web_socket: true) do
34
34
  params = {
35
35
  'action' => 'update',
36
36
  'apikey' => button[:api_key],
@@ -51,15 +51,41 @@ module Ewelink
51
51
  end
52
52
 
53
53
  def reload
54
- Ewelink.logger.debug(self.class.name) { 'Reloading API (authentication token, devices, region,...)' }
55
- dispose_web_socket
54
+ Ewelink.logger.debug(self.class.name) { 'Reloading API (authentication token, devices, region, connections,...)' }
55
+
56
+ @web_socket_authenticated_api_keys.clear
57
+ @web_socket_switches_statuses.clear
58
+
59
+ [@web_socket_ping_thread, @web_socket_thread].each do |thread|
60
+ next unless thread
61
+ if Thread.current == thread
62
+ thread[:stop] = true
63
+ else
64
+ thread.kill
65
+ end
66
+ end
67
+
68
+ if @web_socket.present?
69
+ begin
70
+ @web_socket.close if @web_socket.open?
71
+ rescue
72
+ # Ignoring close errors
73
+ end
74
+ end
75
+
56
76
  [
57
77
  :@api_keys,
58
78
  :@authentication_token,
59
79
  :@devices,
80
+ :@last_web_socket_pong_at,
60
81
  :@region,
61
82
  :@rf_bridge_buttons,
62
83
  :@switches,
84
+ :@web_socket_ping_interval,
85
+ :@web_socket_ping_thread,
86
+ :@web_socket_thread,
87
+ :@web_socket_url,
88
+ :@web_socket,
63
89
  ].each do |variable|
64
90
  remove_instance_variable(variable) if instance_variable_defined?(variable)
65
91
  end
@@ -103,7 +129,7 @@ module Ewelink
103
129
  def switch_on?(uuid)
104
130
  switch = find_switch!(uuid)
105
131
  if @web_socket_switches_statuses[switch[:uuid]].nil?
106
- web_socket_wait_for(-> { web_socket_authenticated? }) do
132
+ web_socket_wait_for(-> { web_socket_authenticated? }, initialize_web_socket: true) do
107
133
  Ewelink.logger.debug(self.class.name) { "Checking switch #{switch[:uuid].inspect} status" }
108
134
  params = {
109
135
  'action' => 'query',
@@ -116,7 +142,7 @@ module Ewelink
116
142
  send_to_web_socket(JSON.generate(params))
117
143
  end
118
144
  end
119
- web_socket_wait_for(-> { !@web_socket_switches_statuses[switch[:uuid]].nil? }) do
145
+ web_socket_wait_for(-> { !@web_socket_switches_statuses[switch[:uuid]].nil? }, initialize_web_socket: true) do
120
146
  @web_socket_switches_statuses[switch[:uuid]] == 'on'
121
147
  end
122
148
  end
@@ -149,7 +175,7 @@ module Ewelink
149
175
  end
150
176
  switch = find_switch!(uuid)
151
177
  @web_socket_switches_statuses[switch[:uuid]] = nil
152
- web_socket_wait_for(-> { web_socket_authenticated? }) do
178
+ web_socket_wait_for(-> { web_socket_authenticated? }, initialize_web_socket: true) do
153
179
  params = {
154
180
  'action' => 'update',
155
181
  'apikey' => switch[:api_key],
@@ -239,40 +265,6 @@ module Ewelink
239
265
  end
240
266
  end
241
267
 
242
- def dispose_web_socket
243
- Ewelink.logger.debug(self.class.name) { 'Dispose WebSocket' }
244
- @web_socket_authenticated_api_keys.clear
245
- @web_socket_switches_statuses.clear
246
-
247
- [@web_socket_ping_thread, @web_socket_thread].each do |thread|
248
- next unless thread
249
- if Thread.current == thread
250
- thread[:stop] = true
251
- else
252
- thread.kill
253
- end
254
- end
255
-
256
- if @web_socket.present?
257
- begin
258
- @web_socket.close if @web_socket.open?
259
- rescue
260
- # Ignoring close errors
261
- end
262
- end
263
-
264
- [
265
- :@last_web_socket_pong_at,
266
- :@web_socket_ping_interval,
267
- :@web_socket_ping_thread,
268
- :@web_socket_thread,
269
- :@web_socket_url,
270
- :@web_socket,
271
- ].each do |variable|
272
- remove_instance_variable(variable) if instance_variable_defined?(variable)
273
- end
274
- end
275
-
276
268
  def find_rf_bridge_button!(uuid)
277
269
  rf_bridge_buttons.find { |button| button[:uuid] == uuid } || raise(Error.new("No such RF bridge button with UUID: #{uuid.inspect}"))
278
270
  end
@@ -311,11 +303,11 @@ module Ewelink
311
303
  def send_to_web_socket(message)
312
304
  if web_socket_outdated_ping?
313
305
  Ewelink.logger.warn(self.class.name) { 'WebSocket ping is outdated' }
314
- dispose_web_socket
306
+ reload
315
307
  end
316
308
  web_socket.send(message)
317
309
  rescue => e
318
- dispose_web_socket
310
+ reload
319
311
  raise Error.new(e)
320
312
  end
321
313
 
@@ -349,7 +341,7 @@ module Ewelink
349
341
 
350
342
  @web_socket.on(:close) do |event|
351
343
  Ewelink.logger.debug(self.class.name) { 'WebSocket closed' }
352
- dispose_web_socket
344
+ reload
353
345
  end
354
346
 
355
347
  @web_socket.on(:open) do |event|
@@ -371,11 +363,13 @@ module Ewelink
371
363
  json = JSON.parse(message)
372
364
  rescue => e
373
365
  Ewelink.logger.error(self.class.name) { 'WebSocket JSON parse error' }
366
+ reload
374
367
  next
375
368
  end
376
369
 
377
370
  if json.key?('error') && json['error'] != 0
378
371
  Ewelink.logger.error(self.class.name) { "WebSocket message error: #{message.inspect}" }
372
+ reload
379
373
  next
380
374
  end
381
375
 
@@ -399,13 +393,9 @@ module Ewelink
399
393
  end
400
394
  end
401
395
 
402
- Timeout.timeout(REQUEST_TIMEOUT) do
403
- while @web_socket.blank?
404
- sleep(WEB_SOCKET_WAIT_INTERVAL)
405
- end
396
+ web_socket_wait_for(-> { @web_socket.present? }) do
397
+ @web_socket
406
398
  end
407
-
408
- @web_socket
409
399
  end
410
400
  end
411
401
 
@@ -440,15 +430,18 @@ module Ewelink
440
430
  end
441
431
  end
442
432
 
443
- def web_socket_wait_for(condition, &block)
444
- web_socket # Initializes WebSocket
445
- Timeout.timeout(REQUEST_TIMEOUT) do
446
- while !condition.call
447
- sleep(WEB_SOCKET_WAIT_INTERVAL)
433
+ def web_socket_wait_for(condition, initialize_web_socket: false, &block)
434
+ web_socket if initialize_web_socket
435
+ begin
436
+ Timeout.timeout(REQUEST_TIMEOUT) do
437
+ while !condition.call
438
+ sleep(WEB_SOCKET_WAIT_INTERVAL)
439
+ end
440
+ block_given? ? yield : true
448
441
  end
449
- block_given? ? yield : true
450
- rescue
451
- dispose_web_socket
442
+ rescue => e
443
+ reload
444
+ raise Error.new(e)
452
445
  end
453
446
  end
454
447
 
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.1
4
+ version: 2.2.2
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-03 00:00:00.000000000 Z
11
+ date: 2020-09-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport