mholling-one_wire 0.1.0
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.
- data/LICENSE +20 -0
- data/README.rdoc +7 -0
- data/Rakefile +48 -0
- data/VERSION.yml +4 -0
- data/lib/one_wire.rb +9 -0
- data/lib/one_wire/config.rb +18 -0
- data/lib/one_wire/constants.rb +18 -0
- data/lib/one_wire/device.rb +30 -0
- data/lib/one_wire/directory.rb +50 -0
- data/lib/one_wire/exceptions.rb +13 -0
- data/lib/one_wire/request.rb +27 -0
- data/lib/one_wire/response.rb +15 -0
- data/lib/one_wire/retry.rb +14 -0
- data/lib/one_wire/transaction.rb +38 -0
- data/spec/one_wire_spec.rb +7 -0
- data/spec/spec_helper.rb +9 -0
- metadata +71 -0
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 Matthew Hollingworth
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
data/Rakefile
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'jeweler'
|
6
|
+
Jeweler::Tasks.new do |gem|
|
7
|
+
gem.name = "one_wire"
|
8
|
+
gem.summary = %Q{Ruby library for talking to a one-wire server (see http://owfs.org)}
|
9
|
+
gem.email = "mdholling@gmail.com"
|
10
|
+
gem.homepage = "http://github.com/mholling/one_wire"
|
11
|
+
gem.authors = ["Matthew Hollingworth"]
|
12
|
+
|
13
|
+
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
|
14
|
+
end
|
15
|
+
rescue LoadError
|
16
|
+
puts "Jeweler not available. Install it with: sudo gem install technicalpickles-jeweler -s http://gems.github.com"
|
17
|
+
end
|
18
|
+
|
19
|
+
require 'spec/rake/spectask'
|
20
|
+
Spec::Rake::SpecTask.new(:spec) do |spec|
|
21
|
+
spec.libs << 'lib' << 'spec'
|
22
|
+
spec.spec_files = FileList['spec/**/*_spec.rb']
|
23
|
+
end
|
24
|
+
|
25
|
+
Spec::Rake::SpecTask.new(:rcov) do |spec|
|
26
|
+
spec.libs << 'lib' << 'spec'
|
27
|
+
spec.pattern = 'spec/**/*_spec.rb'
|
28
|
+
spec.rcov = false
|
29
|
+
end
|
30
|
+
|
31
|
+
|
32
|
+
task :default => :spec
|
33
|
+
|
34
|
+
require 'rake/rdoctask'
|
35
|
+
Rake::RDocTask.new do |rdoc|
|
36
|
+
if File.exist?('VERSION.yml')
|
37
|
+
config = YAML.load(File.read('VERSION.yml'))
|
38
|
+
version = "#{config[:major]}.#{config[:minor]}.#{config[:patch]}"
|
39
|
+
else
|
40
|
+
version = ""
|
41
|
+
end
|
42
|
+
|
43
|
+
rdoc.rdoc_dir = 'rdoc'
|
44
|
+
rdoc.title = "one_wire #{version}"
|
45
|
+
rdoc.rdoc_files.include('README*')
|
46
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
47
|
+
end
|
48
|
+
|
data/VERSION.yml
ADDED
data/lib/one_wire.rb
ADDED
@@ -0,0 +1,9 @@
|
|
1
|
+
require 'one_wire/retry'
|
2
|
+
require 'one_wire/constants'
|
3
|
+
require 'one_wire/config'
|
4
|
+
require 'one_wire/exceptions'
|
5
|
+
require 'one_wire/request'
|
6
|
+
require 'one_wire/response'
|
7
|
+
require 'one_wire/transaction'
|
8
|
+
require 'one_wire/directory'
|
9
|
+
require 'one_wire/device'
|
@@ -0,0 +1,18 @@
|
|
1
|
+
require 'singleton'
|
2
|
+
|
3
|
+
module OneWire
|
4
|
+
Config = Struct.new(:host, :port, :units, :retries, :retry_interval, :dirall) do
|
5
|
+
include Singleton
|
6
|
+
|
7
|
+
def self.method_missing(*args)
|
8
|
+
instance.send(*args)
|
9
|
+
end
|
10
|
+
|
11
|
+
self.host = "localhost"
|
12
|
+
self.port = 4304
|
13
|
+
self.units = :celsius
|
14
|
+
self.retries = { "EINVAL" => 100, "ENOENT" => 10 }
|
15
|
+
self.retry_interval = 0.2
|
16
|
+
self.dirall = true
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
module OneWire
|
2
|
+
module Constants
|
3
|
+
module Functions
|
4
|
+
READ = 2
|
5
|
+
WRITE = 3
|
6
|
+
DIR = 4
|
7
|
+
PRESENCE = 6
|
8
|
+
DIRALL = 7
|
9
|
+
end
|
10
|
+
|
11
|
+
module Units
|
12
|
+
CELSIUS = 0x00000000
|
13
|
+
FARENHEIT = 0x00010000
|
14
|
+
KELVIN = 0x00020000
|
15
|
+
RANKINE = 0x00030000
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
module OneWire
|
2
|
+
class Device
|
3
|
+
include Retry
|
4
|
+
|
5
|
+
def initialize(address, options = {})
|
6
|
+
@options = options
|
7
|
+
@path = options[:uncached] ? "/uncached/#{address}" : "/#{address}"
|
8
|
+
@path.squeeze!("/")
|
9
|
+
end
|
10
|
+
|
11
|
+
def present?
|
12
|
+
Transaction.presence(@path, @options)
|
13
|
+
true
|
14
|
+
rescue Errno::ENOENT
|
15
|
+
false
|
16
|
+
end
|
17
|
+
|
18
|
+
def read(attribute)
|
19
|
+
with_retry { Transaction.read("#{@path}/#{attribute}").response.data || raise(BadRead) }
|
20
|
+
end
|
21
|
+
|
22
|
+
def write(attribute, value)
|
23
|
+
with_retry { Transaction.write("#{@path}/#{attribute}", value) }
|
24
|
+
end
|
25
|
+
|
26
|
+
def dir
|
27
|
+
Directory.new(@path)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,50 @@
|
|
1
|
+
module OneWire
|
2
|
+
class Directory
|
3
|
+
include Retry
|
4
|
+
# include Enumerable
|
5
|
+
|
6
|
+
def initialize(path, options = {})
|
7
|
+
@path = path
|
8
|
+
dirall = options.has_key?(:dir_all) ? options[:dirall] : Config.dirall
|
9
|
+
@entries = dirall ?
|
10
|
+
with_retry { Transaction.dirall(path) }.response.data.split(",") :
|
11
|
+
with_retry { Transaction.dir(path) }.response.map { |r| r.data }
|
12
|
+
end
|
13
|
+
|
14
|
+
def directories
|
15
|
+
@directories ||= @entries.collect do |entry|
|
16
|
+
begin
|
17
|
+
Directory.new(entry)
|
18
|
+
rescue Errno::ENOTDIR
|
19
|
+
nil
|
20
|
+
end
|
21
|
+
end.compact
|
22
|
+
end
|
23
|
+
|
24
|
+
def members
|
25
|
+
@members ||= @entries - directories.map { |directory| directory.path }
|
26
|
+
end
|
27
|
+
|
28
|
+
attr_reader :path
|
29
|
+
|
30
|
+
def ls
|
31
|
+
[ "+ " + @path.split("/").last.to_s ] +
|
32
|
+
members.map { |member| " . " + member.split("/").last } +
|
33
|
+
directories.map { |directory| " - " + directory.path.split("/").last }
|
34
|
+
end
|
35
|
+
|
36
|
+
def tree
|
37
|
+
[ "+ " + @path.split("/").last.to_s ] +
|
38
|
+
members.map { |member| " . " + member.split("/").last } +
|
39
|
+
directories.map { |directory| directory.tree.map { |line| " #{line}"} }.flatten
|
40
|
+
end
|
41
|
+
|
42
|
+
def to_s
|
43
|
+
ls.join("\n")
|
44
|
+
end
|
45
|
+
|
46
|
+
def inspect
|
47
|
+
to_s
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
module OneWire
|
2
|
+
class Request
|
3
|
+
include OneWire::Constants::Functions
|
4
|
+
def initialize(socket, type, path, options = {})
|
5
|
+
version = 0
|
6
|
+
payload = path + "\000"
|
7
|
+
case type
|
8
|
+
when :dir
|
9
|
+
request_type, data_size = DIR, 0
|
10
|
+
when :dirall
|
11
|
+
request_type, data_size = DIRALL, 0
|
12
|
+
when :read
|
13
|
+
request_type, data_size = READ, 8192
|
14
|
+
when :write
|
15
|
+
request_type, data_size = WRITE, options[:value].size
|
16
|
+
payload << options[:value] << "\000"
|
17
|
+
when :presence
|
18
|
+
request_type, data_size = PRESENCE, 0
|
19
|
+
end
|
20
|
+
flags = 0x05000000 | 0x00000002 | Constants::Units.const_get((options[:units] || Config.units).to_s.upcase)
|
21
|
+
offset = 0
|
22
|
+
header = [ version, payload.size, request_type, flags, data_size, offset ].pack("NNNNNN")
|
23
|
+
|
24
|
+
socket.write(header + payload)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
module OneWire
|
2
|
+
class Response
|
3
|
+
def initialize(socket)
|
4
|
+
header = socket.read(24)
|
5
|
+
raise ShortRead unless header && header.size == 24
|
6
|
+
version, payload_size, @return_value, @flags, data_size, offset = header.unpack('NNNNNN')
|
7
|
+
raise SystemCallError, 2**32 - @return_value, "OWServer" if @return_value > 2**31
|
8
|
+
raise BadRead if payload_size > 2**31 || data_size > 2**31
|
9
|
+
@data = socket.read(payload_size)[0...data_size] unless payload_size.zero?
|
10
|
+
# @data = payload_size.zero? ? "" : socket.read(payload_size)[0...data_size]
|
11
|
+
end
|
12
|
+
|
13
|
+
attr_reader :data, :return_value, :flags
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
module OneWire
|
2
|
+
module Retry
|
3
|
+
def with_retry
|
4
|
+
errors_counts = Config.retries.map { |err, count| { Errno::const_get(err) => count } }.inject { |hash, h| hash.merge h }
|
5
|
+
begin
|
6
|
+
yield
|
7
|
+
rescue *(errors_counts.map { |error, count| error if count > 0 }.compact) => e
|
8
|
+
errors_counts[e.class] -= 1
|
9
|
+
sleep Config.retry_interval
|
10
|
+
retry
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
require 'socket'
|
2
|
+
|
3
|
+
module OneWire
|
4
|
+
class Transaction
|
5
|
+
attr_reader :response
|
6
|
+
|
7
|
+
def initialize(type, path, options = {})
|
8
|
+
host = options[:host] || OneWire::Config.host
|
9
|
+
port = options[:port] || OneWire::Config.port
|
10
|
+
socket = Socket.new(Socket::AF_INET, Socket::SOCK_STREAM, 0)
|
11
|
+
socket.connect(Socket.pack_sockaddr_in(port, host))
|
12
|
+
OneWire::Request.new(socket, type, path, options)
|
13
|
+
if type == :dir
|
14
|
+
@response = []
|
15
|
+
while true do
|
16
|
+
response = OneWire::Response.new(socket)
|
17
|
+
response.data ? @response << response : break
|
18
|
+
end
|
19
|
+
else
|
20
|
+
@response = OneWire::Response.new(socket)
|
21
|
+
end
|
22
|
+
ensure
|
23
|
+
socket.close if socket && !socket.closed?
|
24
|
+
end
|
25
|
+
|
26
|
+
class << self
|
27
|
+
[ :dir, :dirall, :read, :presence ].each do |method|
|
28
|
+
define_method method do |path, *args|
|
29
|
+
new(method, path, *args)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
def write(path, value, options = {})
|
34
|
+
new(:write, path, options.merge(:value => value))
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,71 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: mholling-one_wire
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Matthew Hollingworth
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-09-15 00:00:00 -07:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description:
|
17
|
+
email: mdholling@gmail.com
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files:
|
23
|
+
- LICENSE
|
24
|
+
- README.rdoc
|
25
|
+
files:
|
26
|
+
- LICENSE
|
27
|
+
- README.rdoc
|
28
|
+
- Rakefile
|
29
|
+
- VERSION.yml
|
30
|
+
- lib/one_wire.rb
|
31
|
+
- lib/one_wire/config.rb
|
32
|
+
- lib/one_wire/constants.rb
|
33
|
+
- lib/one_wire/device.rb
|
34
|
+
- lib/one_wire/directory.rb
|
35
|
+
- lib/one_wire/exceptions.rb
|
36
|
+
- lib/one_wire/request.rb
|
37
|
+
- lib/one_wire/response.rb
|
38
|
+
- lib/one_wire/retry.rb
|
39
|
+
- lib/one_wire/transaction.rb
|
40
|
+
- spec/one_wire_spec.rb
|
41
|
+
- spec/spec_helper.rb
|
42
|
+
has_rdoc: false
|
43
|
+
homepage: http://github.com/mholling/one_wire
|
44
|
+
licenses:
|
45
|
+
post_install_message:
|
46
|
+
rdoc_options:
|
47
|
+
- --charset=UTF-8
|
48
|
+
require_paths:
|
49
|
+
- lib
|
50
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: "0"
|
55
|
+
version:
|
56
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
57
|
+
requirements:
|
58
|
+
- - ">="
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
version: "0"
|
61
|
+
version:
|
62
|
+
requirements: []
|
63
|
+
|
64
|
+
rubyforge_project:
|
65
|
+
rubygems_version: 1.3.5
|
66
|
+
signing_key:
|
67
|
+
specification_version: 3
|
68
|
+
summary: Ruby library for talking to a one-wire server (see http://owfs.org)
|
69
|
+
test_files:
|
70
|
+
- spec/one_wire_spec.rb
|
71
|
+
- spec/spec_helper.rb
|