one_wire 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.document +5 -0
- data/.gitignore +5 -0
- data/LICENSE +20 -0
- data/README.rdoc +7 -0
- data/Rakefile +49 -0
- data/VERSION.yml +4 -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/lib/one_wire.rb +9 -0
- data/one_wire.gemspec +58 -0
- data/spec/one_wire_spec.rb +7 -0
- data/spec/spec_helper.rb +9 -0
- metadata +75 -0
data/.document
ADDED
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,49 @@
|
|
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
|
+
Jeweler::GemcutterTasks.new
|
16
|
+
rescue LoadError
|
17
|
+
puts "Jeweler not available. Install it with: sudo gem install technicalpickles-jeweler -s http://gems.github.com"
|
18
|
+
end
|
19
|
+
|
20
|
+
require 'spec/rake/spectask'
|
21
|
+
Spec::Rake::SpecTask.new(:spec) do |spec|
|
22
|
+
spec.libs << 'lib' << 'spec'
|
23
|
+
spec.spec_files = FileList['spec/**/*_spec.rb']
|
24
|
+
end
|
25
|
+
|
26
|
+
Spec::Rake::SpecTask.new(:rcov) do |spec|
|
27
|
+
spec.libs << 'lib' << 'spec'
|
28
|
+
spec.pattern = 'spec/**/*_spec.rb'
|
29
|
+
spec.rcov = false
|
30
|
+
end
|
31
|
+
|
32
|
+
|
33
|
+
task :default => :spec
|
34
|
+
|
35
|
+
require 'rake/rdoctask'
|
36
|
+
Rake::RDocTask.new do |rdoc|
|
37
|
+
if File.exist?('VERSION.yml')
|
38
|
+
config = YAML.load(File.read('VERSION.yml'))
|
39
|
+
version = "#{config[:major]}.#{config[:minor]}.#{config[:patch]}"
|
40
|
+
else
|
41
|
+
version = ""
|
42
|
+
end
|
43
|
+
|
44
|
+
rdoc.rdoc_dir = 'rdoc'
|
45
|
+
rdoc.title = "one_wire #{version}"
|
46
|
+
rdoc.rdoc_files.include('README*')
|
47
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
48
|
+
end
|
49
|
+
|
data/VERSION.yml
ADDED
@@ -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/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'
|
data/one_wire.gemspec
ADDED
@@ -0,0 +1,58 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run `rake gemspec`
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = %q{one_wire}
|
8
|
+
s.version = "0.1.0"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["Matthew Hollingworth"]
|
12
|
+
s.date = %q{2009-10-10}
|
13
|
+
s.email = %q{mdholling@gmail.com}
|
14
|
+
s.extra_rdoc_files = [
|
15
|
+
"LICENSE",
|
16
|
+
"README.rdoc"
|
17
|
+
]
|
18
|
+
s.files = [
|
19
|
+
".document",
|
20
|
+
".gitignore",
|
21
|
+
"LICENSE",
|
22
|
+
"README.rdoc",
|
23
|
+
"Rakefile",
|
24
|
+
"VERSION.yml",
|
25
|
+
"lib/one_wire.rb",
|
26
|
+
"lib/one_wire/config.rb",
|
27
|
+
"lib/one_wire/constants.rb",
|
28
|
+
"lib/one_wire/device.rb",
|
29
|
+
"lib/one_wire/directory.rb",
|
30
|
+
"lib/one_wire/exceptions.rb",
|
31
|
+
"lib/one_wire/request.rb",
|
32
|
+
"lib/one_wire/response.rb",
|
33
|
+
"lib/one_wire/retry.rb",
|
34
|
+
"lib/one_wire/transaction.rb",
|
35
|
+
"one_wire.gemspec",
|
36
|
+
"spec/one_wire_spec.rb",
|
37
|
+
"spec/spec_helper.rb"
|
38
|
+
]
|
39
|
+
s.homepage = %q{http://github.com/mholling/one_wire}
|
40
|
+
s.rdoc_options = ["--charset=UTF-8"]
|
41
|
+
s.require_paths = ["lib"]
|
42
|
+
s.rubygems_version = %q{1.3.5}
|
43
|
+
s.summary = %q{Ruby library for talking to a one-wire server (see http://owfs.org)}
|
44
|
+
s.test_files = [
|
45
|
+
"spec/one_wire_spec.rb",
|
46
|
+
"spec/spec_helper.rb"
|
47
|
+
]
|
48
|
+
|
49
|
+
if s.respond_to? :specification_version then
|
50
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
51
|
+
s.specification_version = 3
|
52
|
+
|
53
|
+
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
54
|
+
else
|
55
|
+
end
|
56
|
+
else
|
57
|
+
end
|
58
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,75 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: 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-10-10 00:00:00 +11: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
|
+
- .document
|
27
|
+
- .gitignore
|
28
|
+
- LICENSE
|
29
|
+
- README.rdoc
|
30
|
+
- Rakefile
|
31
|
+
- VERSION.yml
|
32
|
+
- lib/one_wire.rb
|
33
|
+
- lib/one_wire/config.rb
|
34
|
+
- lib/one_wire/constants.rb
|
35
|
+
- lib/one_wire/device.rb
|
36
|
+
- lib/one_wire/directory.rb
|
37
|
+
- lib/one_wire/exceptions.rb
|
38
|
+
- lib/one_wire/request.rb
|
39
|
+
- lib/one_wire/response.rb
|
40
|
+
- lib/one_wire/retry.rb
|
41
|
+
- lib/one_wire/transaction.rb
|
42
|
+
- one_wire.gemspec
|
43
|
+
- spec/one_wire_spec.rb
|
44
|
+
- spec/spec_helper.rb
|
45
|
+
has_rdoc: true
|
46
|
+
homepage: http://github.com/mholling/one_wire
|
47
|
+
licenses: []
|
48
|
+
|
49
|
+
post_install_message:
|
50
|
+
rdoc_options:
|
51
|
+
- --charset=UTF-8
|
52
|
+
require_paths:
|
53
|
+
- lib
|
54
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
55
|
+
requirements:
|
56
|
+
- - ">="
|
57
|
+
- !ruby/object:Gem::Version
|
58
|
+
version: "0"
|
59
|
+
version:
|
60
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
61
|
+
requirements:
|
62
|
+
- - ">="
|
63
|
+
- !ruby/object:Gem::Version
|
64
|
+
version: "0"
|
65
|
+
version:
|
66
|
+
requirements: []
|
67
|
+
|
68
|
+
rubyforge_project:
|
69
|
+
rubygems_version: 1.3.5
|
70
|
+
signing_key:
|
71
|
+
specification_version: 3
|
72
|
+
summary: Ruby library for talking to a one-wire server (see http://owfs.org)
|
73
|
+
test_files:
|
74
|
+
- spec/one_wire_spec.rb
|
75
|
+
- spec/spec_helper.rb
|