onewire 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,17 @@
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
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in onewire.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Alexey Noskov
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,61 @@
1
+ # Onewire
2
+
3
+ It's simple Ruby client for OWFS [owserver](http://owfs.org/index.php?page=owserver_protocol)
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'onewire'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install onewire
18
+
19
+ ## Basic usage
20
+
21
+ Create client like:
22
+
23
+ require 'onewire'
24
+ client = Onewire.client
25
+
26
+ You may specify optional host/port like this:
27
+
28
+ Onewire.client 'localhost', 4304
29
+
30
+ Then, you may list OWFS directories:
31
+
32
+ client.dir('/')
33
+ => ["/10.67C6697351FF", "/05.4AEC29CDBAAB", "/bus.0", "/uncached", "/settings", "/system", "/statistics", "/structure", "/simultaneous", "/alarm"]
34
+
35
+ And read values:
36
+
37
+ client.read('/10.67C6697351FF/temperature')
38
+ => 24.2887
39
+
40
+ Or even write them:
41
+
42
+ client.write('/10.67C6697351FF/alias','somealias')
43
+ => nil
44
+ c.read('/10.67C6697351FF/alias')
45
+ => "somealias"
46
+
47
+ ## Scopes
48
+
49
+ To simplify some operations _scopes_ are available. Scopes provide client-like interface, but where all path arguments are relative to some node. For example:
50
+
51
+ scope = client.scope('/10.67C6697351FF')
52
+ scope.read('temperature')
53
+ => 24.2887
54
+
55
+ ## Contributing
56
+
57
+ 1. Fork it
58
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
59
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
60
+ 4. Push to the branch (`git push origin my-new-feature`)
61
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/lib/onewire.rb ADDED
@@ -0,0 +1,10 @@
1
+ require 'onewire/version'
2
+ require 'onewire/client'
3
+
4
+ module Onewire
5
+
6
+ def self.client(*args)
7
+ Client.new(*args)
8
+ end
9
+
10
+ end
@@ -0,0 +1,74 @@
1
+ require 'onewire/connection'
2
+ require 'onewire/scope'
3
+
4
+ module Onewire
5
+ class Client
6
+
7
+ class Error < StandardError; end
8
+
9
+ ERROR = 0
10
+ NOP = 1
11
+ READ = 2
12
+ WRITE = 3
13
+ DIR = 4
14
+ SIZE = 5
15
+ PRESENCE = 6
16
+
17
+ def initialize(host = 'localhost', port = 4304)
18
+ @host, @port = host, port
19
+ end
20
+
21
+ def dir(path = '')
22
+ interact do |c|
23
+ c.write DIR, "#{normalize path}\0", 8192
24
+
25
+ res = []
26
+
27
+ while dir = c.read
28
+ res << dir
29
+ end
30
+
31
+ res
32
+ end
33
+ end
34
+
35
+ def read(path)
36
+ interact do |c|
37
+ c.write READ, "#{normalize path}\0", 8192 # Default value size to read
38
+ typecast c.read
39
+ end
40
+ end
41
+
42
+ def write(path, value)
43
+ interact do |c|
44
+ c.write WRITE, "#{normalize path}\0#{value}\0", value.size
45
+ c.read
46
+ end
47
+ end
48
+
49
+ def scope(path = '')
50
+ Onewire::Scope.new self, path
51
+ end
52
+
53
+ private
54
+
55
+ def normalize(path)
56
+ path.gsub(/\A(?!\/)|\/{2,}/,'/')
57
+ end
58
+
59
+ def typecast(val)
60
+ Float(val) rescue val
61
+ end
62
+
63
+ def interact
64
+ conn = Connection.new(@host, @port)
65
+
66
+ begin
67
+ yield conn
68
+ ensure
69
+ conn.close
70
+ end
71
+ end
72
+
73
+ end
74
+ end
@@ -0,0 +1,50 @@
1
+ require 'socket'
2
+
3
+ module Onewire
4
+ class Connection
5
+
6
+ PING = 4294967295 # -1 as unsigned int
7
+
8
+ def initialize(host = 'localhost', port = 4304)
9
+ @socket = TCPSocket.new host, port
10
+ end
11
+
12
+ def close
13
+ @socket.close
14
+ end
15
+
16
+ def write(type, payload, size = 0, offset = 0)
17
+ request_fields = [version, payload.size, type, flags, size, offset, payload] # Prepare request structure (see http://owfs.org/index.php?page=owserver-protocol)
18
+ request_data = request_fields.pack('NNNNNNA*') # Pack it - first six parts are integers, payload is string
19
+
20
+ @socket.write request_data # Send it
21
+ end
22
+
23
+ def read
24
+ _, payload_size, _, _, size, offset = read_header
25
+
26
+ if payload_size > 0
27
+ @socket.read(payload_size)[offset..offset+size-1] # Read response payload
28
+ else
29
+ nil
30
+ end
31
+ end
32
+
33
+ def read_header
34
+ loop do
35
+ header = @socket.read(24).unpack('NNNNNN') # Read and unpack response header (see http://owfs.org/index.php?page=owserver-protocol)
36
+ return header if header[1] != PING # Return when non-ping response read
37
+ end
38
+ end
39
+
40
+ def flags
41
+ 258 # TODO
42
+ end
43
+
44
+ def version
45
+ 0
46
+ end
47
+
48
+ end
49
+
50
+ end
@@ -0,0 +1,27 @@
1
+ require 'onewire/connection'
2
+
3
+ module Onewire
4
+ class Scope
5
+
6
+ DELEGATED_METHODS = %q{dir read write scope}
7
+
8
+ def initialize(client, path)
9
+ @client, @path = client, path
10
+ end
11
+
12
+ def method_missing(name, *args, &block)
13
+ if DELEGATED_METHODS.include? name.to_s
14
+ path = "#{@path}/#{args.shift}" # Update path
15
+
16
+ @client.send name, path, *args, &block
17
+ else
18
+ super
19
+ end
20
+ end
21
+
22
+ def respond_to_missing?(name, include_private = false)
23
+ DELEGATED_METHODS.include? name.to_s
24
+ end
25
+
26
+ end
27
+ end
@@ -0,0 +1,3 @@
1
+ module Onewire
2
+ VERSION = "0.0.1"
3
+ end
data/onewire.gemspec ADDED
@@ -0,0 +1,23 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'onewire/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "onewire"
8
+ spec.version = Onewire::VERSION
9
+ spec.authors = ["Alexey Noskov"]
10
+ spec.email = ["alexey.noskov@gmail.com"]
11
+ spec.description = %q{Simple client for owserver}
12
+ spec.summary = %q{Simple client for owserver}
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
+ end
metadata ADDED
@@ -0,0 +1,95 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: onewire
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Alexey Noskov
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-03-24 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
+ description: Simple client for owserver
47
+ email:
48
+ - alexey.noskov@gmail.com
49
+ executables: []
50
+ extensions: []
51
+ extra_rdoc_files: []
52
+ files:
53
+ - .gitignore
54
+ - Gemfile
55
+ - LICENSE.txt
56
+ - README.md
57
+ - Rakefile
58
+ - lib/onewire.rb
59
+ - lib/onewire/client.rb
60
+ - lib/onewire/connection.rb
61
+ - lib/onewire/scope.rb
62
+ - lib/onewire/version.rb
63
+ - onewire.gemspec
64
+ homepage: ''
65
+ licenses:
66
+ - MIT
67
+ post_install_message:
68
+ rdoc_options: []
69
+ require_paths:
70
+ - lib
71
+ required_ruby_version: !ruby/object:Gem::Requirement
72
+ none: false
73
+ requirements:
74
+ - - ! '>='
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ segments:
78
+ - 0
79
+ hash: -2165025040075908644
80
+ required_rubygems_version: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ segments:
87
+ - 0
88
+ hash: -2165025040075908644
89
+ requirements: []
90
+ rubyforge_project:
91
+ rubygems_version: 1.8.23
92
+ signing_key:
93
+ specification_version: 3
94
+ summary: Simple client for owserver
95
+ test_files: []