ledenet_api 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: d209ac850060f4e0d6f43b75c14d65061bd928a6
4
- data.tar.gz: 1bc387beff34d221247328228db77626e095dc19
3
+ metadata.gz: fc308b3bf1a99c0a67ef519e13f84fabece00fc6
4
+ data.tar.gz: dd9f6a4032283d24b2f82ca3a00f4ee9647e864c
5
5
  SHA512:
6
- metadata.gz: eade48784872b6538dc576a9ea88278dfd6e121c9d5641c90be9be953ff7b12bf9309740d6a15ced2f721e4a43208ea34acb66599dcc0c706821b631cf9b6a07
7
- data.tar.gz: 2e8638b1c017dba772d61fad398edc4f147813beae22c58d2f8d9169ec4162d5881f10dac84c51c76518ed4a65bcd2580d6b5ecf74594c324f6e64d69bc04b0b
6
+ metadata.gz: 505fd9e3be7cfe496e4a3b1105145f7452a8df8e689f6b68e521ccbe37879bf381d66b2297e97409b5fbf6f0495d9d63865db294120bde6b6c5bbeaa599004e9
7
+ data.tar.gz: 001c8afd4e0f0228b8ace0517168992bcdb5dac57fe415fcd2044c0a0aba1fe1cd01d29e8606e2fb079b3414d90cbe2c5c995fa917e44dc885a9512d2001ba93
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- ledenet_api (0.1.0)
4
+ ledenet_api (0.1.1)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
data/lib/ledenet/api.rb CHANGED
@@ -2,16 +2,21 @@ module LEDENET
2
2
  class Api
3
3
  API_PORT = 5577
4
4
 
5
- def initialize(device_address)
5
+ DEFAULT_OPTIONS = {
6
+ reuse_connection: false
7
+ }
8
+
9
+ def initialize(device_address, options = {})
6
10
  @device_address = device_address
11
+ @options = DEFAULT_OPTIONS.merge(options)
7
12
  end
8
13
 
9
14
  def on
10
- send_bytes(0x71, 0x23, 0x0F, 0xA3)
15
+ send_bytes_action(0x71, 0x23, 0x0F, 0xA3)
11
16
  end
12
17
 
13
18
  def off
14
- send_bytes(0x71, 0x24 ,0x0F, 0xA4)
19
+ send_bytes_action(0x71, 0x24 ,0x0F, 0xA4)
15
20
  end
16
21
 
17
22
  def on?
@@ -21,7 +26,7 @@ module LEDENET
21
26
  def update_color(r, g, b)
22
27
  checksum = color_checksum(r, g, b)
23
28
 
24
- send_bytes(0x31, r, g, b, 0xFF, 0, 0x0F, checksum)
29
+ send_bytes_action(0x31, r, g, b, 0xFF, 0, 0x0F, checksum)
25
30
  end
26
31
 
27
32
  def current_color
@@ -38,16 +43,13 @@ module LEDENET
38
43
  end
39
44
 
40
45
  def status
41
- begin
46
+ socket_action do
42
47
  send_bytes(0x81, 0x8A, 0x8B, 0x96)
43
48
 
44
49
  # Example response:
45
50
  # [129, 4, 35, 97, 33, 9, 11, 22, 33, 255, 3, 0, 0, 119]
46
51
  # R G B WW ^--- LSB indicates on/off
47
52
  flush_response(14)
48
- rescue Errno::EPIPE
49
- reconnect!
50
- retry
51
53
  end
52
54
  end
53
55
 
@@ -56,19 +58,28 @@ module LEDENET
56
58
  end
57
59
 
58
60
  def send_bytes(*b)
59
- create_socket if @socket.nil?
61
+ @socket.write(b.pack('c*'))
62
+ end
60
63
 
61
- begin
62
- @socket.write(b.pack('c*'))
63
- rescue Errno::EPIPE
64
- reconnect!
65
- retry
66
- end
64
+ def send_bytes_action(*b)
65
+ socket_action { send_bytes(*b) }
67
66
  end
68
67
 
69
68
  def create_socket
70
- @socket.close unless @socket.nil?
69
+ @socket.close unless @socket.nil? or @socket.closed?
71
70
  @socket = TCPSocket.new(@device_address, API_PORT)
72
71
  end
72
+
73
+ def socket_action
74
+ begin
75
+ create_socket if @socket.nil? or @socket.closed?
76
+ yield
77
+ rescue Errno::EPIPE, IOError
78
+ reconnect!
79
+ retry
80
+ ensure
81
+ @socket.close unless @socket.closed? or @options[:reuse_connection]
82
+ end
83
+ end
73
84
  end
74
85
  end
@@ -1,3 +1,3 @@
1
1
  module LEDENET
2
- VERSION = '0.1.1'
2
+ VERSION = '0.1.2'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ledenet_api
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Christopher Mullins
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2015-10-16 00:00:00.000000000 Z
11
+ date: 2015-10-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler