webrelay_scheduler 0.6.2 → 0.7.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.
data/bin/webrelay CHANGED
@@ -11,8 +11,9 @@ Escort::App.create do |app|
11
11
  app.config_file ".webrelay_scheduler", :autocreate => true
12
12
  app.options do |opts|
13
13
  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
- opts.opt :power, "Sets the power on the relays. Accepts either ON or OFF", :short => '-p', :long => '--power', :type => :string, :default => "ON"
14
+ opts.opt :power, "Sets the power on the relays. Accepts either ON or OFF", :long => '--power', :type => :string, :default => "ON"
15
15
  opts.opt :delay, "The delay between sending commands to each relay", :short => '-d', :long => '--delay', :type => :integer, :default => 5
16
+ opts.opt :password, "The password to use when authenticating with the webrelay", :short => '-p', :long => '--password', :type => :string
16
17
  opts.opt :timeout, "The maximum timeout (in seconds) when sending a command to a relay", :short => '-t', :long => '--timeout', :type => :integer, :default => 5
17
18
  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
18
19
  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
@@ -4,7 +4,7 @@ module WebrelayScheduler
4
4
  class Command < ::Escort::ActionCommand::Base
5
5
  def execute
6
6
  o = command_options
7
- relays = o[:ip].collect{ |ip| Relay.new(ip, o[:timeout]) }
7
+ relays = o[:ip].collect{ |ip| Relay.new(ip, o[:timeout], o[:password]) }
8
8
 
9
9
  Holidays.load_all
10
10
  if o[:skip_if_today_is_holiday] and Date.today.holiday?(o[:locale_code])
@@ -1,24 +1,48 @@
1
- require 'httpclient'
2
- require 'crack'
1
+ require 'httparty'
3
2
  module WebrelayScheduler
3
+
4
+ # Exception raised when there was an HTTP error
5
+ # while communicating with the webrelay
6
+ class HTTPError < StandardError
7
+ def initialize(http_response)
8
+ super("HTTP #{http_response.code} #{http_response.message}")
9
+ end
10
+ end
11
+
12
+ # Exception raised when the return value
13
+ class ValueNotChangedError < StandardError
14
+ def initialize(msg = "Value does not match intended state")
15
+ super
16
+ end
17
+ end
18
+
4
19
  class Relay
5
20
  attr_accessor :ip_address
21
+ include HTTParty
22
+
23
+ def initialize(ip_address, timeout=5, password=nil)
24
+ self.class.basic_auth 'user', password unless password.nil?
25
+ self.class.default_timeout timeout
26
+ self.class.base_uri ip_address
6
27
 
7
- def initialize(ip_address, timeout=5)
8
28
  @ip_address = ip_address
9
- @client = HTTPClient.new
10
- @client.receive_timeout = timeout * 1000
11
29
  end
12
30
 
13
31
  # Sets the specified relay to a value
14
32
  # Returns true if successful, or false if the value was not changed correctly
15
33
  def set(relay_identifier, value)
16
- url = "http://#{ip_address}/state.xml?#{relay_identifier}State=#{value}"
17
- response = @client.get url
18
- parsed_response = Crack::XML.parse response.body
34
+ url = "/stateFull.xml?#{relay_identifier}State=#{value}"
35
+ request = self.class.get url
36
+ response = request.parsed_response
19
37
 
20
38
  # Check the response shows to see if the value successfully changed
21
- parsed_response["datavalues"]["#{relay_identifier}state"] == value.to_s
39
+ if !request.success?
40
+ raise HTTPError, request
41
+ elsif response.fetch("datavalues",{}).fetch("#{relay_identifier}state") != value.to_s
42
+ raise ValueNotChangedError
43
+ end
44
+
45
+ true
22
46
  end
23
47
 
24
48
  # Close the relay
@@ -1,3 +1,3 @@
1
1
  module WebrelayScheduler
2
- VERSION = "0.6.2"
2
+ VERSION = "0.7.0"
3
3
  end
@@ -21,8 +21,7 @@ Gem::Specification.new do |spec|
21
21
 
22
22
  spec.add_dependency "holidays"
23
23
  spec.add_dependency "escort"
24
- spec.add_dependency "crack"
25
- spec.add_dependency "httpclient"
24
+ spec.add_dependency "httparty"
26
25
  spec.add_development_dependency "bundler", "~> 1.3"
27
26
  spec.add_development_dependency "rake"
28
27
  end
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.6.2
4
+ version: 0.7.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-24 00:00:00.000000000 Z
13
+ date: 2013-05-28 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: holidays
@@ -45,23 +45,7 @@ dependencies:
45
45
  - !ruby/object:Gem::Version
46
46
  version: '0'
47
47
  - !ruby/object:Gem::Dependency
48
- name: crack
49
- requirement: !ruby/object:Gem::Requirement
50
- none: false
51
- requirements:
52
- - - ! '>='
53
- - !ruby/object:Gem::Version
54
- version: '0'
55
- type: :runtime
56
- prerelease: false
57
- version_requirements: !ruby/object:Gem::Requirement
58
- none: false
59
- requirements:
60
- - - ! '>='
61
- - !ruby/object:Gem::Version
62
- version: '0'
63
- - !ruby/object:Gem::Dependency
64
- name: httpclient
48
+ name: httparty
65
49
  requirement: !ruby/object:Gem::Requirement
66
50
  none: false
67
51
  requirements:
@@ -142,7 +126,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
142
126
  version: '0'
143
127
  segments:
144
128
  - 0
145
- hash: 4354468138948314233
129
+ hash: 4225319025533001504
146
130
  required_rubygems_version: !ruby/object:Gem::Requirement
147
131
  none: false
148
132
  requirements:
@@ -151,7 +135,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
151
135
  version: '0'
152
136
  segments:
153
137
  - 0
154
- hash: 4354468138948314233
138
+ hash: 4225319025533001504
155
139
  requirements: []
156
140
  rubyforge_project:
157
141
  rubygems_version: 1.8.23