rzwaveway 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 @@
1
+ *.gem
data/LICENSE ADDED
@@ -0,0 +1,165 @@
1
+ GNU LESSER GENERAL PUBLIC LICENSE
2
+ Version 3, 29 June 2007
3
+
4
+ Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>
5
+ Everyone is permitted to copy and distribute verbatim copies
6
+ of this license document, but changing it is not allowed.
7
+
8
+
9
+ This version of the GNU Lesser General Public License incorporates
10
+ the terms and conditions of version 3 of the GNU General Public
11
+ License, supplemented by the additional permissions listed below.
12
+
13
+ 0. Additional Definitions.
14
+
15
+ As used herein, "this License" refers to version 3 of the GNU Lesser
16
+ General Public License, and the "GNU GPL" refers to version 3 of the GNU
17
+ General Public License.
18
+
19
+ "The Library" refers to a covered work governed by this License,
20
+ other than an Application or a Combined Work as defined below.
21
+
22
+ An "Application" is any work that makes use of an interface provided
23
+ by the Library, but which is not otherwise based on the Library.
24
+ Defining a subclass of a class defined by the Library is deemed a mode
25
+ of using an interface provided by the Library.
26
+
27
+ A "Combined Work" is a work produced by combining or linking an
28
+ Application with the Library. The particular version of the Library
29
+ with which the Combined Work was made is also called the "Linked
30
+ Version".
31
+
32
+ The "Minimal Corresponding Source" for a Combined Work means the
33
+ Corresponding Source for the Combined Work, excluding any source code
34
+ for portions of the Combined Work that, considered in isolation, are
35
+ based on the Application, and not on the Linked Version.
36
+
37
+ The "Corresponding Application Code" for a Combined Work means the
38
+ object code and/or source code for the Application, including any data
39
+ and utility programs needed for reproducing the Combined Work from the
40
+ Application, but excluding the System Libraries of the Combined Work.
41
+
42
+ 1. Exception to Section 3 of the GNU GPL.
43
+
44
+ You may convey a covered work under sections 3 and 4 of this License
45
+ without being bound by section 3 of the GNU GPL.
46
+
47
+ 2. Conveying Modified Versions.
48
+
49
+ If you modify a copy of the Library, and, in your modifications, a
50
+ facility refers to a function or data to be supplied by an Application
51
+ that uses the facility (other than as an argument passed when the
52
+ facility is invoked), then you may convey a copy of the modified
53
+ version:
54
+
55
+ a) under this License, provided that you make a good faith effort to
56
+ ensure that, in the event an Application does not supply the
57
+ function or data, the facility still operates, and performs
58
+ whatever part of its purpose remains meaningful, or
59
+
60
+ b) under the GNU GPL, with none of the additional permissions of
61
+ this License applicable to that copy.
62
+
63
+ 3. Object Code Incorporating Material from Library Header Files.
64
+
65
+ The object code form of an Application may incorporate material from
66
+ a header file that is part of the Library. You may convey such object
67
+ code under terms of your choice, provided that, if the incorporated
68
+ material is not limited to numerical parameters, data structure
69
+ layouts and accessors, or small macros, inline functions and templates
70
+ (ten or fewer lines in length), you do both of the following:
71
+
72
+ a) Give prominent notice with each copy of the object code that the
73
+ Library is used in it and that the Library and its use are
74
+ covered by this License.
75
+
76
+ b) Accompany the object code with a copy of the GNU GPL and this license
77
+ document.
78
+
79
+ 4. Combined Works.
80
+
81
+ You may convey a Combined Work under terms of your choice that,
82
+ taken together, effectively do not restrict modification of the
83
+ portions of the Library contained in the Combined Work and reverse
84
+ engineering for debugging such modifications, if you also do each of
85
+ the following:
86
+
87
+ a) Give prominent notice with each copy of the Combined Work that
88
+ the Library is used in it and that the Library and its use are
89
+ covered by this License.
90
+
91
+ b) Accompany the Combined Work with a copy of the GNU GPL and this license
92
+ document.
93
+
94
+ c) For a Combined Work that displays copyright notices during
95
+ execution, include the copyright notice for the Library among
96
+ these notices, as well as a reference directing the user to the
97
+ copies of the GNU GPL and this license document.
98
+
99
+ d) Do one of the following:
100
+
101
+ 0) Convey the Minimal Corresponding Source under the terms of this
102
+ License, and the Corresponding Application Code in a form
103
+ suitable for, and under terms that permit, the user to
104
+ recombine or relink the Application with a modified version of
105
+ the Linked Version to produce a modified Combined Work, in the
106
+ manner specified by section 6 of the GNU GPL for conveying
107
+ Corresponding Source.
108
+
109
+ 1) Use a suitable shared library mechanism for linking with the
110
+ Library. A suitable mechanism is one that (a) uses at run time
111
+ a copy of the Library already present on the user's computer
112
+ system, and (b) will operate properly with a modified version
113
+ of the Library that is interface-compatible with the Linked
114
+ Version.
115
+
116
+ e) Provide Installation Information, but only if you would otherwise
117
+ be required to provide such information under section 6 of the
118
+ GNU GPL, and only to the extent that such information is
119
+ necessary to install and execute a modified version of the
120
+ Combined Work produced by recombining or relinking the
121
+ Application with a modified version of the Linked Version. (If
122
+ you use option 4d0, the Installation Information must accompany
123
+ the Minimal Corresponding Source and Corresponding Application
124
+ Code. If you use option 4d1, you must provide the Installation
125
+ Information in the manner specified by section 6 of the GNU GPL
126
+ for conveying Corresponding Source.)
127
+
128
+ 5. Combined Libraries.
129
+
130
+ You may place library facilities that are a work based on the
131
+ Library side by side in a single library together with other library
132
+ facilities that are not Applications and are not covered by this
133
+ License, and convey such a combined library under terms of your
134
+ choice, if you do both of the following:
135
+
136
+ a) Accompany the combined library with a copy of the same work based
137
+ on the Library, uncombined with any other library facilities,
138
+ conveyed under the terms of this License.
139
+
140
+ b) Give prominent notice with the combined library that part of it
141
+ is a work based on the Library, and explaining where to find the
142
+ accompanying uncombined form of the same work.
143
+
144
+ 6. Revised Versions of the GNU Lesser General Public License.
145
+
146
+ The Free Software Foundation may publish revised and/or new versions
147
+ of the GNU Lesser General Public License from time to time. Such new
148
+ versions will be similar in spirit to the present version, but may
149
+ differ in detail to address new problems or concerns.
150
+
151
+ Each version is given a distinguishing version number. If the
152
+ Library as you received it specifies that a certain numbered version
153
+ of the GNU Lesser General Public License "or any later version"
154
+ applies to it, you have the option of following the terms and
155
+ conditions either of that published version or of any later version
156
+ published by the Free Software Foundation. If the Library as you
157
+ received it does not specify a version number of the GNU Lesser
158
+ General Public License, you may choose any version of the GNU Lesser
159
+ General Public License ever published by the Free Software Foundation.
160
+
161
+ If the Library as you received it specifies that a proxy can decide
162
+ whether future versions of the GNU Lesser General Public License shall
163
+ apply, that proxy's public statement of acceptance of any version is
164
+ permanent authorization for you to choose that version for the
165
+ Library.
data/README.md ADDED
@@ -0,0 +1,22 @@
1
+ rzwaveway
2
+ =========
3
+
4
+ A Ruby library for communicating with the ZWave protocol stack from ZWay, running on the Raspberry Pi "razberry" add-on card (see http://razberry.z-wave.me/).
5
+
6
+ ## Example
7
+ ```
8
+ require 'rzwaveway'
9
+
10
+ z_way = RZWaveWay::ZWay.new('192.168.1.123)
11
+ devices = z_way.get_devices
12
+ devices.values.each do | device |
13
+ puts device.build_json
14
+ end
15
+
16
+ z_way.on_event(RZWaveWay::AliveEvent) {|event| puts "A device woke up" }
17
+ z_way.on_event(RZWaveWay::LevelEvent) {|event| puts "A device got triggered" }
18
+ while true do
19
+ sleep 5
20
+ z_way.process_events
21
+ end
22
+ ```
@@ -0,0 +1,51 @@
1
+ require 'pp'
2
+ require_relative 'events'
3
+
4
+ module RZWaveWay
5
+ module CommandClasses
6
+ SENSOR_BINARY = 48
7
+ MULTI_LEVEL_SENSOR = 49
8
+ CONFIGURATION = 112
9
+ BATTERY = 128
10
+ WAKEUP = 132
11
+ ALARM_SENSOR = 156
12
+ end
13
+
14
+ class CommandClass
15
+ include CommandClasses
16
+ attr_reader :id
17
+ @data
18
+
19
+ def initialize(id, data)
20
+ @id = id
21
+ @data = data
22
+ end
23
+
24
+ def get_data name
25
+ parts = name.split '.'
26
+ result = @data
27
+ parts.each do | part |
28
+ raise "Could not find part '#{part}' in '#{name}'" unless result.has_key? part
29
+ result = result[part]
30
+ end
31
+ result
32
+ end
33
+
34
+ def process updates, device_id
35
+ event = nil
36
+ names = updates.collect { |x| x[0]}
37
+ case id
38
+ when WAKEUP
39
+ if names.include?("data.lastWakeup") &&
40
+ names.include?("data.lastSleep")
41
+ event = AliveEvent.new(device_id, updates[1][1]["value"])
42
+ end
43
+ when SENSOR_BINARY
44
+ if names.include?("data.1")
45
+ event = LevelEvent.new(device_id,updates[0][1]["level"]["updateTime"], updates[0][1]["level"]["value"])
46
+ end
47
+ end
48
+ event
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,38 @@
1
+ module RZWaveWay
2
+ class AliveEvent
3
+ attr_reader :device_id
4
+ attr_reader :time
5
+
6
+ def initialize device_id, time
7
+ @device_id = device_id
8
+ @time = time
9
+ end
10
+ end
11
+
12
+ class LevelEvent
13
+ attr_reader :device_id
14
+ attr_reader :time
15
+ attr_reader :level
16
+
17
+ def initialize device_id, time, level
18
+ @device_id = device_id
19
+ @time = time
20
+ @level = level
21
+ end
22
+ end
23
+
24
+ class SmokeEvent
25
+ attr_reader :device_id
26
+ attr_reader :time
27
+ end
28
+
29
+ class HighTemperatureEvent
30
+ attr_reader :device_id
31
+ attr_reader :time
32
+ end
33
+
34
+ class TamperingEvent
35
+ attr_reader :device_id
36
+ attr_reader :time
37
+ end
38
+ end
@@ -0,0 +1,81 @@
1
+ require 'json'
2
+
3
+ require_relative 'command_classes'
4
+
5
+ module RZWaveWay
6
+ class ZWaveDevice
7
+ include CommandClasses
8
+
9
+ attr_reader :id
10
+
11
+ def initialize(id, data)
12
+ @id = id
13
+ @command_classes = create_commandclasses_from data
14
+ puts "Created ZWaveDevice with id='#{id}'"
15
+ end
16
+
17
+ def create_commandclasses_from data
18
+ cc_classes = {}
19
+ data['instances']['0']['commandClasses'].each do |cc_id, sub_tree|
20
+ cc_classes[cc_id.to_i] = CommandClass.new(cc_id.to_i, sub_tree)
21
+ end
22
+ cc_classes
23
+ end
24
+
25
+ def build_json
26
+ properties = {'deviceId' => @id}
27
+ if(support_commandclass? SENSOR_BINARY)
28
+ properties.merge!( {
29
+ 'level' => @command_classes[SENSOR_BINARY].get_data('data.1.level.value'),
30
+ 'lastLevelChangeTime' => @command_classes[SENSOR_BINARY].get_data('data.1.level.updateTime')
31
+ })
32
+ end
33
+ if(support_commandclass? WAKEUP)
34
+ properties.merge!( {
35
+ 'wakeUpInterval' => @command_classes[WAKEUP].get_data('data.interval.value'),
36
+ 'lastSleepTime' => @command_classes[WAKEUP].get_data('data.lastSleep.value'),
37
+ 'lastWakeUpTime' => @command_classes[WAKEUP].get_data('data.lastWakeup.value')
38
+ })
39
+ end
40
+ if(support_commandclass? BATTERY)
41
+ properties.merge!( {
42
+ 'batteryLevel' => @command_classes[BATTERY].get_data('data.last.value')
43
+ })
44
+ end
45
+ properties.to_json
46
+ end
47
+
48
+ def support_commandclass? command_class
49
+ @command_classes.has_key? command_class
50
+ end
51
+
52
+ def process updates
53
+ events = []
54
+ updates_per_commandclass = group_per_commandclass updates
55
+ updates_per_commandclass.each do |cc, values|
56
+ if @command_classes.has_key? cc
57
+ events << (@command_classes[cc].process(values, @id))
58
+ else
59
+ puts "Could not find command class: '#{cc}'"
60
+ end
61
+ events
62
+ end
63
+
64
+ def group_per_commandclass updates
65
+ updates_per_commandclass = Hash.new { [] }
66
+ updates.each do | key, value |
67
+ match_data = key.match(/\Ainstances.0.commandClasses.(\d+)./)
68
+ if match_data
69
+ command_class = match_data[1].to_i
70
+ cc_updates = updates_per_commandclass[command_class]
71
+ cc_updates << [match_data.post_match, value]
72
+ updates_per_commandclass[command_class] = cc_updates
73
+ else
74
+ puts "? #{key}" unless key.match(/\Adata./)
75
+ end
76
+ end
77
+ updates_per_commandclass
78
+ end
79
+
80
+ end
81
+ end
@@ -0,0 +1,94 @@
1
+ require 'uri'
2
+ require 'net/http'
3
+ require 'json'
4
+
5
+ module RZWaveWay
6
+ class ZWay
7
+ def initialize hostname
8
+ @devices = {}
9
+ @update_time = "0"
10
+ @uri = URI::HTTP.build({:host => hostname, :port => 8083})
11
+ @event_handlers = {}
12
+ end
13
+
14
+ def get_devices
15
+ @uri.path = "/ZWaveAPI/Data/#{@update_time}"
16
+ response = http_post_request
17
+ if response.code == "200"
18
+ results = JSON.parse response.body
19
+ @update_time = results["updateTime"]
20
+ results["devices"].each do |device |
21
+ device_id = device[0].to_i
22
+ @devices[device_id] = ZWaveDevice.new(device_id, device[1]) if device_id > 1
23
+ end
24
+ @devices
25
+ else
26
+ pp response.code
27
+ return {}
28
+ end
29
+ end
30
+
31
+ def process_events
32
+ events = []
33
+ updates = get_updates
34
+ updates_per_device = group_per_device updates
35
+ updates_per_device.each do | id, updates |
36
+ if @devices[id]
37
+ events.concat (@devices[id].process updates)
38
+ else
39
+ puts "Could not find device with id '#{id}'"
40
+ end
41
+ end
42
+ events.each do |event|
43
+ handler = @event_handlers[event.class]
44
+ if handler
45
+ handler.call(event)
46
+ else
47
+ puts "no handler for #{event.class}"
48
+ end
49
+ end
50
+ events
51
+ end
52
+
53
+ def get_updates
54
+ @uri.path = "/ZWaveAPI/Data/#{@update_time}"
55
+ response = http_post_request
56
+ if response.is_a?(Net::HTTPSuccess)
57
+ results = JSON.parse response.body
58
+ @update_time = results.delete("updateTime")
59
+ return results
60
+ else
61
+ pp response.code
62
+ return {}
63
+ end
64
+ end
65
+
66
+ def group_per_device updates
67
+ updates_per_device = Hash.new { [] }
68
+ updates.each do | key, value |
69
+ match_data = key.match(/\Adevices\.(\d+)\./)
70
+ if match_data
71
+ device_id = match_data[1].to_i
72
+ device_updates = updates_per_device[device_id]
73
+ device_updates << [match_data.post_match, value]
74
+ updates_per_device[device_id] = device_updates
75
+ else
76
+ puts "? #{key}"
77
+ end
78
+ end
79
+ updates_per_device
80
+ end
81
+
82
+ def http_post_request
83
+ http = Net::HTTP.new(@uri.host, @uri.port)
84
+ http.use_ssl = false
85
+
86
+ request = Net::HTTP::Post.new(@uri.path)
87
+ http.request(request)
88
+ end
89
+
90
+ def on_event (event, &listener)
91
+ @event_handlers[event] = listener
92
+ end
93
+ end
94
+ end
data/lib/rzwaveway.rb ADDED
@@ -0,0 +1,4 @@
1
+ require 'rzwaveway/command_classes'
2
+ require 'rzwaveway/events'
3
+ require 'rzwaveway/zwave_device'
4
+ require 'rzwaveway/zway'
data/rzwaveway.gemspec ADDED
@@ -0,0 +1,12 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = "rzwaveway"
3
+ s.version = "0.0.1"
4
+ s.authors = ["Vincent Touchard"]
5
+ s.date = %q{2014-02-08}
6
+ s.summary = 'ZWave API for ZWay'
7
+ s.description = 'A Ruby API to use the Razberry ZWave ZWay interface'
8
+ s.email = 'vincentoo@yahoo.com'
9
+ s.homepage = 'https://github.com/rzwaveway'
10
+ s.files = `git ls-files`.split("\n")
11
+ s.has_rdoc = false
12
+ end
metadata ADDED
@@ -0,0 +1,53 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rzwaveway
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Vincent Touchard
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2014-02-08 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: A Ruby API to use the Razberry ZWave ZWay interface
15
+ email: vincentoo@yahoo.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - .gitignore
21
+ - LICENSE
22
+ - README.md
23
+ - lib/rzwaveway.rb
24
+ - lib/rzwaveway/command_classes.rb
25
+ - lib/rzwaveway/events.rb
26
+ - lib/rzwaveway/zwave_device.rb
27
+ - lib/rzwaveway/zway.rb
28
+ - rzwaveway.gemspec
29
+ homepage: https://github.com/rzwaveway
30
+ licenses: []
31
+ post_install_message:
32
+ rdoc_options: []
33
+ require_paths:
34
+ - lib
35
+ required_ruby_version: !ruby/object:Gem::Requirement
36
+ none: false
37
+ requirements:
38
+ - - ! '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ required_rubygems_version: !ruby/object:Gem::Requirement
42
+ none: false
43
+ requirements:
44
+ - - ! '>='
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
47
+ requirements: []
48
+ rubyforge_project:
49
+ rubygems_version: 1.8.23
50
+ signing_key:
51
+ specification_version: 3
52
+ summary: ZWave API for ZWay
53
+ test_files: []