wifly 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ .rvmrc
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format progress
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in wifly.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Billy Reisinger
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,49 @@
1
+ # Wifly
2
+
3
+ This Ruby gem can be used to talk to a [WiFly RN-XV](http://www.rovingnetworks.com/products/RN171XV) device at a specified address. The gem has no dependencies other than the socket libraries that ship with any Ruby installation. The gem takes advantage of the predictable size of the response strings from the WiFly by using blocking IO operations. Using blocking operations significantly reduces the complexity of the code. There are a couple of caveats with using blocking reads, though:
4
+ * Unexpected output from the WiFly could cause the client to deadlock on a socket read, or could cause corrupt data in subsequent reads. I've tried to find the variants of output that I'm concerned with in my project; however, you might find some edge cases I haven't covered. In that case, pull requests are welcome.
5
+ * The version of the firmware must be passed into the client so that the size of the response strings can be predicted.
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ gem 'wifly'
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install wifly
20
+
21
+ ## Usage
22
+ Create a client by passing in the host, port, and firmware version:
23
+
24
+ control = Wifly::Control.new('192.168.1.45', 2000, '2.31.7')
25
+
26
+ Read the high pins:
27
+
28
+ control.high_pins # [3,7,9,15]
29
+
30
+ Set a pin to high or low:
31
+
32
+ control.set_high(5)
33
+ control.high_pins # [3,5,7,9,15]
34
+
35
+ control.set_low(7)
36
+ control.high_pins # [3,5,9,15]
37
+
38
+ Read a single pin:
39
+
40
+ control.read_pin(7) # 0
41
+ control.read_pin(5) # 1
42
+
43
+ ## Contributing
44
+
45
+ 1. Fork it
46
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
47
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
48
+ 4. Push to the branch (`git push origin my-new-feature`)
49
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,9 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec) do |config|
5
+
6
+ end
7
+
8
+ task :default => :spec
9
+
@@ -0,0 +1,53 @@
1
+ module Wifly
2
+ class Connection
3
+ HELLO = '*HELLO*CMD\r\n'
4
+
5
+ attr_accessor :address, :port, :version
6
+
7
+ ##
8
+ # address => the hostname or IP address of the wifly device
9
+ # port => the port for communicating with the wifly
10
+ # version => the firmware version of the device
11
+ def initialize(address, port, version)
12
+ self.address = address
13
+ self.port = port
14
+ self.version = version
15
+ end
16
+
17
+ ##
18
+ # str => the command to send to the wifly, without any carriage return
19
+ # [return_len] => the expected length of the return string; defaults to 0
20
+ #
21
+ # The wifly will echo back the command (with carriage return)
22
+ # along with another CRLF and the command prompt string.
23
+ # Something like "lites\r\r\n<2.32> "
24
+ # Since the string has a predictable length, we can do a blocking read.
25
+ #
26
+ def send_command(str, return_len=0)
27
+ str += '\r'
28
+ socket.write(str)
29
+ expected_return_length = str.length + '\r\n#{prompt}'.length + return_len
30
+ socket.read(expected_return_length).gsub(prompt,'')
31
+ end
32
+
33
+ def close
34
+ socket.close
35
+ end
36
+
37
+ def socket
38
+ @socket ||= initialize_socket
39
+ end
40
+
41
+ private
42
+ def prompt
43
+ "<#{version}> "
44
+ end
45
+
46
+ def initialize_socket
47
+ sock = Socket.tcp(address, port)
48
+ sock.write('$$$\r') # enter command mode
49
+ sock.read(HELLO.length) # read off the response, "*HELLO*CMD\r\n"
50
+ sock
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,86 @@
1
+ module Wifly
2
+ class Control
3
+ AOK = "\r\nAOK"
4
+
5
+ attr_accessor :connection
6
+
7
+ ##
8
+ # address - the hostname or IP address
9
+ # port - defaults to 2000
10
+ # version - the firmware version (a string)
11
+ # connection - the object to use for talking to the wifly. Responds to "send_command."
12
+ # Defaults to Wifly::Connection.
13
+ #
14
+ def initialize(address, port, version, connection=nil)
15
+ self.connection = connection || Connection.new(address, port, version)
16
+ end
17
+
18
+ ##
19
+ # Toggle the blinking lights on the device
20
+ #
21
+ def lites
22
+ connection.send_command("lites")
23
+ end
24
+
25
+ ##
26
+ # Return an array containing the numbers of the pins that are in HIGH state
27
+ #
28
+ def high_pins
29
+ io=read_io
30
+ #"8d08" 36104 "1000110100001000" make it 16 bits
31
+ binary_string = io .hex .to_s(2) .rjust(io.size*4, '0')
32
+ binary_string.reverse.split("").each_with_index.map do |value, pin|
33
+ pin if value == "1"
34
+ end.compact
35
+ end
36
+
37
+ ##
38
+ # set a pin high
39
+ #
40
+ def set_high(pin)
41
+ hex = pin_to_hex(pin)
42
+ connection.send_command "set sys output #{hex} #{hex}", AOK.length
43
+ end
44
+
45
+ ##
46
+ # set a pin low
47
+ #
48
+ def set_low(pin)
49
+ hex = pin_to_hex(pin)
50
+ connection.send_command "set sys output 0x00 #{hex}", AOK.length
51
+ end
52
+
53
+ ##
54
+ # given a pin number, return 1 if high or 0 if low
55
+ #
56
+ def read_pin(pin)
57
+ high_pins.include?(pin) ? 1 : 0
58
+ end
59
+
60
+ ##
61
+ # Given a pin number, return the hex code that corresponds to it
62
+ #
63
+ def pin_to_hex(pin)
64
+ return "0x0" if pin == 0
65
+ binstr = "0b1" + ("0" * pin) # make a binary string with a 1 in `pin` position
66
+ base10 = binstr.to_i(2) # convert to base 10
67
+ "0x" + base10.to_s(16) # return hexadecimal string
68
+ end
69
+
70
+ private
71
+
72
+ ##
73
+ # Gets the status of all IO pins on wifly. Returns a hex string.
74
+ #
75
+ def read_io
76
+ cmd = "show io\r"
77
+
78
+ # wifly spits back something like 'show io\r\r\n8d08\r\n<2.32> '
79
+ # crucially, the middle part "8d08\r\n" is always the same length
80
+ str = connection.send_command("show io", "8d08\r\n".length)
81
+
82
+ # Return only the middle part, which is the io state
83
+ str.gsub(cmd,'').strip
84
+ end
85
+ end
86
+ end
@@ -0,0 +1,3 @@
1
+ module Wifly
2
+ VERSION = "0.0.1"
3
+ end
data/lib/wifly.rb ADDED
@@ -0,0 +1,4 @@
1
+ require 'socket'
2
+ require 'wifly/connection'
3
+ require 'wifly/control'
4
+ require 'wifly/version'
@@ -0,0 +1,59 @@
1
+ require 'wifly'
2
+ class Wifly::TestServer
3
+ attr_accessor :input
4
+
5
+ def initialize(port)
6
+ @socket = TCPServer.new(port)
7
+ end
8
+
9
+ def simple_connect
10
+ @client = @socket.accept
11
+ self.input = @client.read('$$$\r'.length)
12
+ @client.write(Wifly::Connection::HELLO)
13
+ @client.close
14
+ end
15
+
16
+ def receive_command(command, version)
17
+ @client = @socket.accept
18
+
19
+ @client.write(Wifly::Connection::HELLO)
20
+ @client.read((command + '\r').length)
21
+ @client.write(command + '\r\r\n<' + version + '> ')
22
+ @client.close
23
+ end
24
+ end
25
+
26
+ describe Wifly::Connection do
27
+ before(:all) do
28
+ @server = Wifly::TestServer.new(2000)
29
+ end
30
+ it 'should set stuff on initialize' do
31
+ connection = Wifly::Connection.new('localhost', 2000, '2.3.13')
32
+ connection.address.should eq('localhost')
33
+ connection.port.should eq(2000)
34
+ connection.version.should eq('2.3.13')
35
+ end
36
+
37
+ it 'should enter command mode' do
38
+ t = Thread.new do
39
+ @server.simple_connect
40
+ end
41
+ connection = Wifly::Connection.new('localhost', 2000, '2.3.13')
42
+ connection.socket
43
+ t.join
44
+ @server.input.should eq('$$$\r')
45
+ connection.close
46
+ end
47
+
48
+ it 'should send commands correctly' do
49
+ t = Thread.new do
50
+ @server.receive_command("lites", '123')
51
+ end
52
+ connection = Wifly::Connection.new('localhost', 2000, '123')
53
+
54
+ result = connection.send_command("lites")
55
+ t.join
56
+ result.should eq('lites\r\r\n')
57
+ connection.close
58
+ end
59
+ end
@@ -0,0 +1,27 @@
1
+ require 'wifly'
2
+ describe Wifly::Control do
3
+ it 'should convert pin to hex' do
4
+ control = Wifly::Control.new('localhost', 2000, '1.2', {})
5
+ control.pin_to_hex(5).should eq("0x20")
6
+ control.pin_to_hex(0).should eq("0x0")
7
+ end
8
+
9
+ it 'should get high pins' do
10
+ connection = double('connection')
11
+ connection.should_receive(:send_command).exactly(3).times.and_return("show io\r\r\n8d08\r\n")
12
+ control = Wifly::Control.new('localhost', 2000, '1.2', connection)
13
+ result = control.high_pins
14
+ result.should eq([3, 8, 10, 11, 15])
15
+ control.read_pin(8).should eq(1)
16
+ control.read_pin(7).should eq(0)
17
+ end
18
+
19
+ it 'should set high and low' do
20
+ connection = double('connection')
21
+ connection.should_receive(:send_command).with("set sys output 0x20 0x20", Wifly::Control::AOK.length)
22
+ connection.should_receive(:send_command).with("set sys output 0x00 0x20", Wifly::Control::AOK.length)
23
+ control = Wifly::Control.new('localhost', 2000, '1.2', connection)
24
+ control.set_high(5)
25
+ control.set_low(5)
26
+ end
27
+ end
@@ -0,0 +1,17 @@
1
+ # This file was generated by the `rspec --init` command. Conventionally, all
2
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
3
+ # Require this file using `require "spec_helper"` to ensure that it is only
4
+ # loaded once.
5
+ #
6
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
7
+ RSpec.configure do |config|
8
+ config.treat_symbols_as_metadata_keys_with_true_values = true
9
+ config.run_all_when_everything_filtered = true
10
+ config.filter_run :focus
11
+
12
+ # Run specs in random order to surface order dependencies. If you find an
13
+ # order dependency and want to debug it, you can fix the order by providing
14
+ # the seed, which is printed after each run.
15
+ # --seed 1234
16
+ config.order = 'random'
17
+ end
data/wifly.gemspec ADDED
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'wifly/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "wifly"
8
+ spec.version = Wifly::VERSION
9
+ spec.authors = ["Billy Reisinger"]
10
+ spec.email = ["billy.reisinger@govdelivery.com"]
11
+ spec.description = %q{A small Ruby gem to connect to a WiFly RN-XV device}
12
+ spec.summary = %q{This gem can be used to talk to a WiFly RN-XV device at a specified address.}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "rspec"
24
+ end
metadata ADDED
@@ -0,0 +1,117 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: wifly
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Billy Reisinger
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-05-04 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bundler
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '1.3'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: '1.3'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rake
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: rspec
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ description: A small Ruby gem to connect to a WiFly RN-XV device
63
+ email:
64
+ - billy.reisinger@govdelivery.com
65
+ executables: []
66
+ extensions: []
67
+ extra_rdoc_files: []
68
+ files:
69
+ - .gitignore
70
+ - .rspec
71
+ - Gemfile
72
+ - LICENSE.txt
73
+ - README.md
74
+ - Rakefile
75
+ - lib/wifly.rb
76
+ - lib/wifly/connection.rb
77
+ - lib/wifly/control.rb
78
+ - lib/wifly/version.rb
79
+ - spec/connection_spec.rb
80
+ - spec/control_spec.rb
81
+ - spec/spec_helper.rb
82
+ - wifly.gemspec
83
+ homepage: ''
84
+ licenses:
85
+ - MIT
86
+ post_install_message:
87
+ rdoc_options: []
88
+ require_paths:
89
+ - lib
90
+ required_ruby_version: !ruby/object:Gem::Requirement
91
+ none: false
92
+ requirements:
93
+ - - ! '>='
94
+ - !ruby/object:Gem::Version
95
+ version: '0'
96
+ segments:
97
+ - 0
98
+ hash: 277051527078828641
99
+ required_rubygems_version: !ruby/object:Gem::Requirement
100
+ none: false
101
+ requirements:
102
+ - - ! '>='
103
+ - !ruby/object:Gem::Version
104
+ version: '0'
105
+ segments:
106
+ - 0
107
+ hash: 277051527078828641
108
+ requirements: []
109
+ rubyforge_project:
110
+ rubygems_version: 1.8.25
111
+ signing_key:
112
+ specification_version: 3
113
+ summary: This gem can be used to talk to a WiFly RN-XV device at a specified address.
114
+ test_files:
115
+ - spec/connection_spec.rb
116
+ - spec/control_spec.rb
117
+ - spec/spec_helper.rb