webrelay_scheduler 0.5.1 → 0.6.0
Sign up to get free protection for your applications and to get access to all the features.
- data/bin/webrelay +5 -5
- data/lib/webrelay_scheduler/command.rb +28 -23
- data/lib/webrelay_scheduler/relay.rb +12 -14
- data/lib/webrelay_scheduler/version.rb +1 -1
- data/webrelay_scheduler.gemspec +3 -3
- metadata +9 -7
data/bin/webrelay
CHANGED
@@ -5,18 +5,18 @@ require "escort"
|
|
5
5
|
|
6
6
|
Escort::App.create do |app|
|
7
7
|
app.version WebrelayScheduler::VERSION
|
8
|
-
app.description "A tool to power on/off
|
9
|
-
"This tool is usually called by a task scheduler, such as cron or Windows Task Scheduler
|
10
|
-
"By default, the tool will not power down webrelays if the current day or following day is a public holiday\n"
|
8
|
+
app.description "A tool to power on/off devices connected to ControlByWeb webrelays. "+
|
9
|
+
"This tool is usually called by a task scheduler, such as cron or Windows Task Scheduler."
|
11
10
|
|
12
11
|
app.options do |opts|
|
13
12
|
opts.opt :ip, "The ip address of the relay to control. This argument can be used multiple times to control more than one relay at a time, eg. '... --ip 10.0.0.1 --ip 10.0.0.2'", :short => '-i', :long => '--ip', :type => :string, :multi => true
|
14
13
|
opts.opt :power, "Sets the power on the relays. Accepts either ON or OFF", :short => '-p', :long => '--power', :type => :string, :default => "ON"
|
15
14
|
opts.opt :delay, "The delay between sending commands to each relay", :short => '-d', :long => '--delay', :type => :integer, :default => 5
|
16
15
|
opts.opt :timeout, "The maximum timeout (in seconds) when sending a command to a relay", :short => '-t', :long => '--timeout', :type => :integer, :default => 5
|
17
|
-
opts.opt :
|
16
|
+
opts.opt :skip_if_today_is_holiday, "Do not run the action if today is a holiday in the current locale", :short => :none, :long => '--skip-if-today-is-holiday', :type => :flag, :default => false
|
17
|
+
opts.opt :skip_if_tomorrow_is_holiday, "Do not run the action if tomorrow is a holiday in the current locale", :short => :none, :long => '--skip-if-tomorrow-is-holiday', :type => :flag, :default => false
|
18
18
|
opts.opt :locale_code, "The country/locale code to use when determining if today's date is currently a holiday", :short => '-l', :long => '--locale-code', :type => :string, :default => "au_nsw"
|
19
|
-
opts.opt :
|
19
|
+
opts.opt :relay_identifier, "The identifier of the relay on each webrelay. This will be 'relay' for single channel relays, or 'relay1', 'relay2', ... , 'relay<N>' for multi-channel relays", :short => '-r', :long => '--relay-identifier', :type => :string, :default => "relay"
|
20
20
|
end
|
21
21
|
|
22
22
|
app.action do |options, arguments|
|
@@ -1,48 +1,53 @@
|
|
1
1
|
require "escort"
|
2
2
|
require "holidays"
|
3
|
-
|
4
3
|
module WebrelayScheduler
|
5
4
|
class Command < ::Escort::ActionCommand::Base
|
6
5
|
def execute
|
7
6
|
o = command_options
|
8
|
-
|
9
7
|
relays = o[:ip].collect{ |ip| Relay.new(ip, o[:timeout]) }
|
10
|
-
delay = o[:delay]
|
11
8
|
|
12
|
-
|
13
|
-
|
9
|
+
Holidays.load_all
|
10
|
+
if o[:skip_if_today_is_holiday] and Date.today.holiday?(o[:locale_code])
|
11
|
+
Escort::Logger.output.warn "Action was skipped for today's holiday: #{holiday_names(Date.today,o[:locale_code])}"
|
12
|
+
elsif o[:skip_if_tomorrow_is_holiday] and (Date.today+1).holiday?(o[:locale_code])
|
13
|
+
Escort::Logger.output.warn "Action was skipped for tomorrow's holiday: #{holiday_names(Date.today+1,o[:locale_code])}"
|
14
|
+
elsif o[:power] == "ON"
|
15
|
+
switch_on(relays,o[:relay_identifier],o[:delay])
|
14
16
|
elsif o[:power] == "OFF"
|
15
|
-
|
16
|
-
switch_off(relays,o[:relay_name],delay)
|
17
|
-
end
|
17
|
+
switch_off(relays,o[:relay_identifier],o[:delay])
|
18
18
|
end
|
19
19
|
end
|
20
20
|
|
21
|
-
|
22
|
-
|
23
|
-
today = Date.today
|
24
|
-
tomorrow = today + 1
|
25
|
-
Holidays.load_all
|
26
|
-
today.holiday?(locale_code.to_sym) or tomorrow.holiday?(locale_code.to_sym)
|
21
|
+
def holiday_names(date, locale_code)
|
22
|
+
date.holidays(locale_code).collect{|h|h[:name]}.join(",")
|
27
23
|
end
|
28
24
|
|
29
|
-
def switch_on(relays,
|
30
|
-
|
25
|
+
def switch_on(relays,relay_identifier,delay)
|
26
|
+
send_to_each(relays) do |relay|
|
27
|
+
relay.close relay_identifier
|
28
|
+
sleep delay
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
def switch_off(relays,relay_identifier,delay)
|
33
|
+
send_to_each(relays) do |relay|
|
34
|
+
relay.open relay_identifier
|
35
|
+
sleep delay
|
36
|
+
end
|
37
|
+
end
|
31
38
|
|
32
39
|
# Safely executes a method on each relay
|
33
|
-
def send_to_each(relays,
|
40
|
+
def send_to_each(relays, &block)
|
34
41
|
relays.each do |relay|
|
35
42
|
begin
|
36
|
-
|
37
|
-
|
38
|
-
Escort::Logger.output.info "#{relay.to_s} - OK"
|
43
|
+
if yield(relay)
|
44
|
+
Escort::Logger.output.info "#{relay.ip_address} - OK"
|
39
45
|
else
|
40
|
-
Escort::Logger.output.error "#{relay.
|
46
|
+
Escort::Logger.output.error "#{relay.ip_address} - FAILED"
|
41
47
|
end
|
42
48
|
rescue Exception => e
|
43
|
-
Escort::Logger.output.error "#{relay.
|
49
|
+
Escort::Logger.output.error "#{relay.ip_address} - FAILED (#{e.message})"
|
44
50
|
end
|
45
|
-
sleep delay
|
46
51
|
end
|
47
52
|
end
|
48
53
|
end
|
@@ -10,27 +10,25 @@ module WebrelayScheduler
|
|
10
10
|
@client.receive_timeout = timeout * 1000
|
11
11
|
end
|
12
12
|
|
13
|
-
|
14
|
-
|
13
|
+
# Sets the specified relay to a value
|
14
|
+
# Returns true if successful, or false if the value was not changed correctly
|
15
|
+
def set(relay_identifier, value)
|
16
|
+
url = "http://#{ip_address}/state.xml?#{relay_identifier}State=#{value}"
|
15
17
|
response = @client.get url
|
16
18
|
parsed_response = Crack::XML.parse response.body
|
17
19
|
|
18
|
-
|
20
|
+
# Check the response shows to see if the value successfully changed
|
21
|
+
parsed_response["datavalues"]["#{relay_identifier}state"] == value.to_s
|
19
22
|
end
|
20
23
|
|
21
|
-
#
|
22
|
-
def
|
23
|
-
set
|
24
|
+
# Close the relay
|
25
|
+
def close(relay_identifier="relay")
|
26
|
+
set relay_identifier, "0"
|
24
27
|
end
|
25
28
|
|
26
|
-
#
|
27
|
-
def
|
28
|
-
set
|
29
|
+
# Opens the relay
|
30
|
+
def open(relay_identifier="relay")
|
31
|
+
set relay_identifier, "1"
|
29
32
|
end
|
30
|
-
|
31
|
-
def to_s
|
32
|
-
"Relay #{ip_address}"
|
33
|
-
end
|
34
|
-
|
35
33
|
end
|
36
34
|
end
|
data/webrelay_scheduler.gemspec
CHANGED
@@ -8,9 +8,9 @@ Gem::Specification.new do |spec|
|
|
8
8
|
spec.version = WebrelayScheduler::VERSION
|
9
9
|
spec.authors = ["Ian Yamey","Jobin Varughese"]
|
10
10
|
spec.email = ["ian.yamey@parkassist.com"]
|
11
|
-
spec.description = %q{
|
12
|
-
spec.summary = %q{}
|
13
|
-
spec.homepage = "
|
11
|
+
spec.description = %q{Set the relay state of ControlByWeb webrelays}
|
12
|
+
spec.summary = %q{This gem is used to control (open/close) relays. It is used to schedule the shutdown / powerup of hardware devices using Cron, Windows Task Manager or any other automated scheduler.}
|
13
|
+
spec.homepage = "https://github.com/parkassist/webrelay_tool"
|
14
14
|
spec.license = "MIT"
|
15
15
|
|
16
16
|
spec.files = `git ls-files`.split($/)
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: webrelay_scheduler
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.6.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -10,7 +10,7 @@ authors:
|
|
10
10
|
autorequire:
|
11
11
|
bindir: bin
|
12
12
|
cert_chain: []
|
13
|
-
date: 2013-05-
|
13
|
+
date: 2013-05-24 00:00:00.000000000 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: holidays
|
@@ -108,7 +108,7 @@ dependencies:
|
|
108
108
|
- - ! '>='
|
109
109
|
- !ruby/object:Gem::Version
|
110
110
|
version: '0'
|
111
|
-
description:
|
111
|
+
description: Set the relay state of ControlByWeb webrelays
|
112
112
|
email:
|
113
113
|
- ian.yamey@parkassist.com
|
114
114
|
executables:
|
@@ -127,7 +127,7 @@ files:
|
|
127
127
|
- lib/webrelay_scheduler/relay.rb
|
128
128
|
- lib/webrelay_scheduler/version.rb
|
129
129
|
- webrelay_scheduler.gemspec
|
130
|
-
homepage:
|
130
|
+
homepage: https://github.com/parkassist/webrelay_tool
|
131
131
|
licenses:
|
132
132
|
- MIT
|
133
133
|
post_install_message:
|
@@ -142,7 +142,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
142
142
|
version: '0'
|
143
143
|
segments:
|
144
144
|
- 0
|
145
|
-
hash:
|
145
|
+
hash: -1726181485213364509
|
146
146
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
147
147
|
none: false
|
148
148
|
requirements:
|
@@ -151,11 +151,13 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
151
151
|
version: '0'
|
152
152
|
segments:
|
153
153
|
- 0
|
154
|
-
hash:
|
154
|
+
hash: -1726181485213364509
|
155
155
|
requirements: []
|
156
156
|
rubyforge_project:
|
157
157
|
rubygems_version: 1.8.23
|
158
158
|
signing_key:
|
159
159
|
specification_version: 3
|
160
|
-
summary:
|
160
|
+
summary: This gem is used to control (open/close) relays. It is used to schedule the
|
161
|
+
shutdown / powerup of hardware devices using Cron, Windows Task Manager or any other
|
162
|
+
automated scheduler.
|
161
163
|
test_files: []
|