aca-device-modules 1.0.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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 9a3208fa3bfe5c4883031c977b64704307dc5471
4
+ data.tar.gz: 2293041e12e1d83d311703f228669c9956fb904a
5
+ SHA512:
6
+ metadata.gz: 64721a3b8ffd32b26b84b6b5ced65cb9bc803587e9c4a6ad2620414cc0f4c45dec1a2f38537f06686fac618e8760fb93c73c3d0105cc69d9913512685d817f3c
7
+ data.tar.gz: 5ac2fb618e3ad1451c38aa18ad53265128d89eab9be0057dc45a1baeb6554ed0f928b2132360c54a3e3a2da5822b68c3f0ef12d21eaab2603359c82cdcc82bfa
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,3 @@
1
+ # Open Source Modules by ACA
2
+
3
+ Please feel free to contribute.
@@ -0,0 +1,21 @@
1
+ $:.push File.expand_path("../lib", __FILE__)
2
+
3
+ # Maintain your gem's version:
4
+ require "aca-device-modules/version"
5
+
6
+ # Describe your gem and declare its dependencies:
7
+ Gem::Specification.new do |s|
8
+ s.name = "aca-device-modules"
9
+ s.version = AcaDeviceModules::VERSION
10
+ s.authors = ["Stephen von Takach"]
11
+ s.email = ["steve@cotag.me"]
12
+ s.homepage = "http://cotag.me/"
13
+ s.summary = "Open Source Control Modules by ACA"
14
+ s.description = "Building automation and IoT control modules"
15
+ s.license = "LGPL3"
16
+
17
+ s.files = Dir["{modules,lib}/**/*.rb", "aca-device-modules.gemspec", "LICENSE", "README.md"]
18
+
19
+ s.add_dependency "rails"
20
+ s.add_dependency "orchestrator"
21
+ end
@@ -0,0 +1,2 @@
1
+
2
+ require 'aca-device-modules/engine'
@@ -0,0 +1,8 @@
1
+ module AcaDeviceModules
2
+ class Engine < ::Rails::Engine
3
+
4
+ config.after_initialize do |app|
5
+ app.config.orchestrator.module_paths << File.expand_path('../../../modules', __FILE__)
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,3 @@
1
+ module AcaDeviceModules
2
+ VERSION = "1.0.0"
3
+ end
@@ -0,0 +1,142 @@
1
+ module Aca; end
2
+
3
+ #
4
+ # Settings required:
5
+ # * domain (domain that we will be authenticating against)
6
+ # * username (username for authentication)
7
+ # * password (password for authentication)
8
+ #
9
+ # (built in)
10
+ # connected
11
+ #
12
+ class Aca::PcControl
13
+ include ::Orchestrator::Constants
14
+ include ::Orchestrator::Transcoder
15
+
16
+ #
17
+ # initialize will not have access to settings
18
+ #
19
+ def on_load
20
+
21
+ #
22
+ # Setup constants
23
+ #
24
+ self[:authenticated] = 0
25
+
26
+ config({
27
+ tokenize: true,
28
+ delimiter: "\x03",
29
+ indicator: "\x02"
30
+ })
31
+ end
32
+
33
+ def connected
34
+ @polling_timer = schedule.every('60s') do
35
+ logger.debug "-- Polling Computer"
36
+ do_send({:control => "app", :command => 'do_nothing'}, {wait: false})
37
+ end
38
+ end
39
+
40
+ def disconnected
41
+ self[:authenticated] = 0
42
+ @polling_timer.cancel unless @polling_timer.nil?
43
+ @polling_timer = nil
44
+ end
45
+
46
+ def launch_application(app, *args)
47
+ do_send({:control => "app", :command => app, :args => args})
48
+ end
49
+
50
+ def wake(broadcast = nil)
51
+ mac = setting(:mac_address)
52
+ if mac
53
+ # config is the database model representing this device
54
+ wake_device(mac, broadcast || '<broadcast>')
55
+ end
56
+ logger.debug "Waking computer #{mac} #{broadcast}"
57
+ nil
58
+ end
59
+
60
+ def shutdown
61
+ launch_application 'shutdown.exe', '/s', '/t', '0'
62
+ end
63
+
64
+ def logoff
65
+ launch_application 'shutdown.exe', '/l', '/t', '0'
66
+ end
67
+
68
+ def restart
69
+ launch_application 'shutdown.exe', '/r', '/t', '0'
70
+ end
71
+
72
+
73
+
74
+ #
75
+ # Camera controls
76
+ #
77
+ CAM_OPERATIONS = [:up, :down, :left, :right, :center, :zoomin, :zoomout]
78
+
79
+ #
80
+ # Automatically creates a callable function for each command
81
+ # http://blog.jayfields.com/2007/10/ruby-defining-class-methods.html
82
+ # http://blog.jayfields.com/2008/02/ruby-dynamically-define-method.html
83
+ #
84
+ CAM_OPERATIONS.each do |command|
85
+ define_method command do |*args|
86
+ # Cam control is low priority in case a camera is not plugged in
87
+ do_send({:control => "cam", :command => command.to_s, :args => []}, {:priority => 0, :retries => 0})
88
+ end
89
+ end
90
+
91
+ def zoom(val)
92
+ do_send({:control => "cam", :command => "zoom", :args => [val.to_s]})
93
+ end
94
+
95
+ def received(data, resolve, command)
96
+
97
+ #
98
+ # Convert the message into a native object
99
+ #
100
+ data = JSON.parse(data, {:symbolize_names => true})
101
+
102
+ #
103
+ # Process the response
104
+ #
105
+ if data[:command] == "authenticate"
106
+ command = {:control => "auth", :command => setting(:domain), :args => [setting(:username), setting(:password)]}
107
+ if self[:authenticated] > 0
108
+ #
109
+ # Token retry (probably always fail - at least we can see in the logs)
110
+ # We don't want to flood the network with useless commands
111
+ #
112
+ schedule.in('60s') do
113
+ do_send(command)
114
+ end
115
+ logger.info "-- Pod Computer, is refusing authentication"
116
+ else
117
+ do_send(command)
118
+ end
119
+ self[:authenticated] += 1
120
+ logger.debug "-- Pod Computer, requested authentication"
121
+ elsif data[:type] != nil
122
+ self["#{data[:device]}_#{data[:type]}"] = data # zoom, tilt, pan
123
+ return nil # This is out of order data
124
+ else
125
+ if !data[:result]
126
+ logger.warn "-- Pod Computer, request failed for command: #{command ? command[:data] : "(resp #{data})"}"
127
+ return false
128
+ end
129
+ end
130
+
131
+ return true # Command success
132
+ end
133
+
134
+
135
+ private
136
+
137
+
138
+ def do_send(command, options = {})
139
+ send("\x02#{JSON.generate(command)}\x03", options)
140
+ end
141
+ end
142
+
@@ -0,0 +1,280 @@
1
+ module Axis; end
2
+ module Axis::Camera; end
3
+
4
+
5
+ class Axis::Camera::Vapix
6
+ include ::Orchestrator::Constants
7
+ include ::Orchestrator::Transcoder
8
+
9
+ def on_load
10
+ on_update
11
+ end
12
+
13
+ def on_update
14
+ defaults({
15
+ delay: 130,
16
+ keepalive: false
17
+ })
18
+
19
+ @username = setting(:username)
20
+ unless @username.nil?
21
+ @password = setting(:password)
22
+ end
23
+
24
+ self[:pan_max] = 180.0
25
+ self[:pan_min] = -180.0
26
+ self[:pan_center] = 0.0
27
+ self[:tilt_max] = 180.0
28
+ self[:tilt_min] = -180.0
29
+ self[:tilt_center] = 0.0
30
+
31
+ self[:joy_left] = -100
32
+ self[:joy_right] = 100
33
+ self[:joy_center] = 0
34
+
35
+ self[:zoom_max] = 9999
36
+ self[:zoom_min] = 0
37
+
38
+ self[:focus_max] = 9999
39
+ self[:focus_min] = 0
40
+
41
+ self[:iris_max] = 9999
42
+ self[:iris_min] = 0
43
+
44
+ self[:power] = true
45
+ end
46
+
47
+
48
+ def connected
49
+ schedule.every('60s', method(:do_poll))
50
+ do_poll
51
+ end
52
+
53
+
54
+ # Here for cross module compatibility
55
+ def power(state = nil, &blk)
56
+ blk.call true
57
+ end
58
+
59
+ def pantilt(pan = nil, tilt = nil)
60
+ pt = {
61
+ pan: in_range(pan.to_f, self[:pan_max], self[:pan_min]),
62
+ tilt: in_range(tilt.to_f, self[:tilt_max], self[:tilt_min])
63
+ }
64
+
65
+ req(:ptz, pt, {name: :pantilt}) do |data, resolve|
66
+ val = extract(data, resolve)
67
+ if val == :success
68
+ self[:pan] = pt[:pan]
69
+ self[:tilt] = pt[:tilt]
70
+ :success
71
+ end
72
+ end
73
+ end
74
+
75
+ def joystick(pan_speed, tilt_speed)
76
+ left_max = self[:joy_left]
77
+ right_max = self[:joy_right]
78
+ pan_speed = in_range(pan_speed.to_i, right_max, left_max)
79
+ tilt_speed = in_range(tilt_speed.to_i, right_max, left_max)
80
+
81
+ is_centered = false
82
+ if pan_speed == 0 && tilt_speed == 0
83
+ is_centered = true
84
+ end
85
+
86
+ options = {}
87
+ options[:retries] = is_centered ? 1 : 0
88
+ options[:name] = :joystick
89
+
90
+ logger.debug("Sending camera: #{pan_speed}#{tilt_speed}")
91
+
92
+ req(:ptz, "continuouspantiltmove=#{pan_speed},#{tilt_speed}", options) do |data, resolve|
93
+ val = extract(data, resolve)
94
+ if val == :success
95
+ self[:joy_pan] = pan_speed
96
+ self[:joy_tilt] = tilt_speed
97
+ :success
98
+ end
99
+ end
100
+ end
101
+
102
+
103
+ def zoom(pos)
104
+ pos = in_range(pos.to_i, self[:zoom_max], self[:zoom_min])
105
+
106
+ req(:ptz, "zoom=#{pos}", {name: :zoom}) do |data, resolve|
107
+ val = extract(data, resolve)
108
+ if val == :success
109
+ self[:zoom] = pos
110
+ :success
111
+ end
112
+ end
113
+ end
114
+
115
+
116
+ def focus(pos)
117
+ pos = in_range(pos.to_i, self[:focus_max], self[:focus_min])
118
+
119
+ req(:ptz, "focus=#{pos}", {name: :focus}) do |data, resolve|
120
+ val = extract(data, resolve)
121
+ if val == :success
122
+ self[:focus] = pos
123
+ :success
124
+ end
125
+ end
126
+ end
127
+
128
+ def auto_focus(state)
129
+ state = is_affirmative?(state) ? 'on' : 'off'
130
+
131
+ req(:ptz, "autofocus=#{state}", {name: :auto_focus}) do |data, resolve|
132
+ val = extract(data, resolve)
133
+ if val == :success
134
+ self[:auto_focus] = state == 'on'
135
+ :success
136
+ end
137
+ end
138
+ end
139
+
140
+ def iris(level)
141
+ level = in_range(level.to_i, self[:iris_max], self[:iris_min])
142
+
143
+ req(:ptz, "iris=#{level}", {name: :iris}) do |data, resolve|
144
+ val = extract(data, resolve)
145
+ if val == :success
146
+ self[:iris] = level
147
+ :success
148
+ end
149
+ end
150
+ end
151
+
152
+ def auto_iris(state)
153
+ state = is_affirmative?(state) ? 'on' : 'off'
154
+
155
+ req(:ptz, "autoiris=#{state}", {name: :auto_iris}) do |data, resolve|
156
+ val = extract(data, resolve)
157
+ if val == :success
158
+ self[:auto_iris] = state == 'on'
159
+ :success
160
+ end
161
+ end
162
+ end
163
+
164
+
165
+ def query_ptz(var)
166
+ req(:ptz, "query=#{var}", {name: "query_#{var}", priority: 0}) do |data, resolve|
167
+ val = extract(data, resolve)
168
+ if val.is_a? Hash
169
+ val.each_pair do |key, value|
170
+ set_status(key, value)
171
+ end
172
+ end
173
+ :success
174
+ end
175
+ end
176
+
177
+
178
+ protected
179
+
180
+
181
+ REQUESTS = {
182
+ ptz: '/axis-cgi/com/ptz.cgi'
183
+ }
184
+
185
+
186
+ def req(type, params = nil, options = {}, &blk)
187
+ request = REQUESTS[type] || type
188
+
189
+ unless @username.nil?
190
+ options[:headers] ||= {}
191
+ options[:headers]['authorization'] = [@username, @password]
192
+ end
193
+
194
+ if params.is_a?(Hash) && !params.empty?
195
+ request += '?' # new string object
196
+ params.each do |key, value|
197
+ request << "#{key}=#{value}&"
198
+ end
199
+ request.chop!
200
+ elsif params
201
+ request += "?#{params}"
202
+ end
203
+
204
+ get(request, options, &blk)
205
+ end
206
+
207
+ def extract(data, resolv)
208
+ body = data[:body].split("\r\n")
209
+ if body.empty?
210
+ resolv.call :success
211
+ logger.debug "empty body = success"
212
+ :success
213
+ elsif body[0] == 'Error:'
214
+ cmd = data[:request]
215
+ logger.warn "Camera error response: #{body[1]} for #{cmd[:path]} #{cmd[:query]}"
216
+ resolv.call(:failed)
217
+ :failed
218
+ else
219
+ resp = {}
220
+ body.each do |line|
221
+ components = line.split('=')
222
+ resp[components[0].to_sym] = components[1]
223
+ end
224
+ logger.debug "returned #{resp}"
225
+ resp
226
+ end
227
+ end
228
+
229
+ def do_poll(*args)
230
+ if not self[:limits_configured]
231
+ query_ptz :limits
232
+ end
233
+ query_ptz :position
234
+ end
235
+
236
+ def set_status(key, value)
237
+ case key
238
+
239
+ # query limits
240
+ when :MinPan
241
+ self[:limits_configured] = true
242
+ self[:pan_min] = value.to_f
243
+ when :MaxPan
244
+ self[:pan_max] = value.to_f
245
+ when :MinTilt
246
+ self[:tilt_min] = value.to_f
247
+ when :MaxTilt
248
+ self[:tilt_max] = value.to_f
249
+ when :MinZoom
250
+ self[:zoom_min] = value.to_i
251
+ when :MaxZoom
252
+ self[:zoom_max] = value.to_i
253
+ when :MinIris
254
+ self[:iris_min] = value.to_i
255
+ when :MaxIris
256
+ self[:iris_max] = value.to_i
257
+ when :MinFocus
258
+ self[:focus_min] = value.to_i
259
+ when :MaxFocus
260
+ self[:focus_max] = value.to_i
261
+
262
+ # query position
263
+ when :pan
264
+ self[:pan] = value.to_f
265
+ when :tilt
266
+ self[:tilt] = value.to_f
267
+ when :zoom
268
+ self[:zoom] = value.to_i
269
+ when :autofocus
270
+ self[:autofocus] = value == 'on'
271
+ when :autoiris
272
+ self[:autoiris] = value == 'on'
273
+
274
+ # query speed
275
+ when :speed
276
+ self[:speed] = value.to_i
277
+ end
278
+ end
279
+ end
280
+