wink_scheduler 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 801d0b306b4c65b601fc2aeced74759dc63f14c3
4
- data.tar.gz: 020cb77aa62adbb059bceadaeb282cad0819b458
3
+ metadata.gz: 257aa680bf39c29694d5f954c88670d252edb130
4
+ data.tar.gz: 1e29b204166b0dbcb921d2958340ccce33d00782
5
5
  SHA512:
6
- metadata.gz: ad58b6216cb945341f08618a854e4b603880eedce3f4a76d062fc4950f14fc2589af2489b0fe2aba26f3b195bd43aecb985fd728caf815dd7a398320f34d044d
7
- data.tar.gz: 1b9fab5079419db65764ccce18b6693c5bca1999fae5f95fe1f5dcbd773f57ac6dbafc990f541220b9a7c6efafe0c267bd7dee6d583ee90e3f83acdbada3fab3
6
+ metadata.gz: 14ee3c440801ac710d835e2a942ffaab1f3e198aed87fa2e34463c10ab4557c1c4679f4a082c3937c0baa82d6cb1e3d49356d431df93089d1fd074b0f5e7de96
7
+ data.tar.gz: b40f52c72e2fb94390d186f5a1560ab75230fc1279d870e7a083f5b3c587a7d9ef1a715a4147f57c47e9ecb8005b7e466dddc5913b0972191b91717154fb04cf
data/bin/wink_scheduler CHANGED
@@ -3,9 +3,17 @@
3
3
  require 'wink_scheduler'
4
4
 
5
5
  if ARGV[0]
6
- conf = YAML.load_file(ARGV[0])
7
- w = WinkScheduler::Client.new(conf)
8
- w.run
6
+ begin
7
+ conf = YAML.load_file(ARGV[0])
8
+ w = WinkScheduler::Client.new(conf)
9
+ w.run
10
+ rescue Psych::SyntaxError => e1
11
+ $stderr.puts "ERROR -- invalid configuration file #{e1.message}"
12
+ exit 1
13
+ rescue => e2
14
+ $stderr.puts "ERROR -- #{e2.message}"
15
+ exit 1
16
+ end
9
17
  else
10
18
  $stderr.puts "ERROR: must provide configuration file"
11
19
  end
@@ -23,9 +23,11 @@ module WinkScheduler
23
23
 
24
24
  def run
25
25
  threads = []
26
+ devices = Winker.devices
27
+ groups = Winker.groups
26
28
  @schedules.each do |s|
27
29
  threads << Thread.new do
28
- w = WinkScheduler::Schedule.new(s)
30
+ w = WinkScheduler::Schedule.new(s, devices, groups)
29
31
  w.run
30
32
  end
31
33
  end
@@ -5,21 +5,53 @@ require 'time'
5
5
 
6
6
  module WinkScheduler
7
7
  class Schedule
8
- def initialize(opts)
8
+ def initialize(opts, devices, groups)
9
9
  if opts["device"]
10
- @object = Winker.devices.select { |d| next if d == nil; d.name == opts["device"] }[0]
10
+ @object = devices.select { |d| next if d == nil; d.name == opts["device"] }[0]
11
+ raise "device \"#{opts["device"]}\" not found" unless @object
11
12
  @type = :device
12
13
  elsif opts["group"]
13
- @object = Winker.groups.select { |g| next if g == nil; g.name == opts["group"] }[0]
14
+ @object = groups.select { |g| next if g == nil; g.name == opts["group"] }[0]
15
+ raise "group \"#{opts["group"]}\" not found" unless @object
14
16
  @type = :group
15
17
  else
16
- raise "device or group not defined"
18
+ raise "Schedule must define either a 'device' or a 'group' option"
17
19
  end
18
- @days = get_days(opts["days"])
19
- @method = opts["method"]
20
- @time = opts["time"]
20
+
21
+ if opts["days"]
22
+ @days = get_days(opts["days"])
23
+ else
24
+ raise "Schedule must define 'days' option"
25
+ end
26
+
27
+ if opts["method"]
28
+ @method = opts["method"]
29
+ raise "#{@method} is not a supported function of #{@object.name}" unless @object.respond_to?(@method)
30
+ else
31
+ raise "Schedule must define 'method' option"
32
+ end
33
+
34
+ if opts["time"]
35
+ @time = opts["time"]
36
+ else
37
+ raise "Schedule must define 'time' option"
38
+ end
39
+
40
+ if opts["offset"]
41
+ @offset = opts["offset"] * 60
42
+ else
43
+ @offset = 0
44
+ end
45
+
21
46
  @window = opts["window"]
22
- @location = opts["location"]
47
+
48
+ if (@time == :sunrise || @time == :sunset)
49
+ if opts["location"]
50
+ @location = opts["location"]
51
+ else
52
+ raise "Schedule must define 'location' option if time used is sunrise or sunset"
53
+ end
54
+ end
23
55
  end
24
56
 
25
57
  def run
@@ -34,20 +66,28 @@ module WinkScheduler
34
66
  map = { "Sunday" => 0, "Monday" => 1, "Tuesday" => 2, "Wednesday" => 3, "Thursday" => 4, "Friday" => 5, "Saturday" => 6,
35
67
  "sunday" => 0, "monday" => 1, "tuesday" => 2, "wednesday" => 3, "thursday" => 4, "friday" => 5, "saturday" => 6,
36
68
  :sunday => 0, :monday => 1, :tuesday => 2, :wednesday => 3, :thursday => 4, :friday => 5, :saturday => 6 }
37
- return days.map { |d| map[d] }.sort
69
+ if (days - map.keys).size > 0 || days.size == 0
70
+ raise "Schedule 'days' option is invalid"
71
+ else
72
+ days.map { |d| map[d] }.uniq.sort
73
+ end
38
74
  elsif days == :daily
39
75
  return (0..6).to_a
76
+ else
77
+ raise "Schedule 'days' option is invalid"
40
78
  end
41
79
  end
42
80
 
43
81
  def run_once
44
82
  cur_time = Time.now
45
- next_time = get_next_time
46
- randomness = rand(@window*60) - @window*60/2
47
- puts "Scheduled \"#{@method}\" on #{@type} \"#{@object.name}\" at #{next_time + randomness}."
48
- sleep next_time - cur_time + randomness
83
+ randomness = @window ? rand(@window*60) - @window*60/2 : 0
84
+ next_time = get_next_time + randomness
85
+ puts "Scheduled \"#{@method}\" on #{@type} \"#{@object.name}\" at #{next_time}."
86
+ while Time.now < next_time
87
+ sleep 1
88
+ end
49
89
  @object.send(@method)
50
- puts "Executed \"#{@method}\" on #{@type} \"#{@object.name}\" at #{next_time + randomness}."
90
+ puts "Executed \"#{@method}\" on #{@type} \"#{@object.name}\" at #{next_time}."
51
91
  end
52
92
 
53
93
  def get_next_time
@@ -59,6 +99,7 @@ module WinkScheduler
59
99
  else
60
100
  Time.parse(@time)
61
101
  end
102
+ time += @offset
62
103
  possible_next_times = 0.upto(6).collect{ |i| time + 3600*24*i }
63
104
  cur_time = Time.now
64
105
  possible_next_times.select{ |t| t > cur_time && @days.include?(t.wday) }[0]
@@ -1,3 +1,3 @@
1
1
  module WinkScheduler
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
@@ -19,7 +19,7 @@ Gem::Specification.new do |spec|
19
19
  spec.require_paths = ["lib"]
20
20
 
21
21
  spec.add_dependency "weather-api", "~> 1.2"
22
- spec.add_dependency "winker", "~> 0.0.14"
22
+ spec.add_dependency "winker", "~> 0.0", ">= 0.0.15"
23
23
 
24
24
  spec.add_development_dependency "bundler", "~> 1.7"
25
25
  spec.add_development_dependency "rake", "~> 10.0"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: wink_scheduler
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Joe Lanford
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-07-08 00:00:00.000000000 Z
11
+ date: 2015-07-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: weather-api
@@ -30,14 +30,20 @@ dependencies:
30
30
  requirements:
31
31
  - - "~>"
32
32
  - !ruby/object:Gem::Version
33
- version: 0.0.14
33
+ version: '0.0'
34
+ - - ">="
35
+ - !ruby/object:Gem::Version
36
+ version: 0.0.15
34
37
  type: :runtime
35
38
  prerelease: false
36
39
  version_requirements: !ruby/object:Gem::Requirement
37
40
  requirements:
38
41
  - - "~>"
39
42
  - !ruby/object:Gem::Version
40
- version: 0.0.14
43
+ version: '0.0'
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: 0.0.15
41
47
  - !ruby/object:Gem::Dependency
42
48
  name: bundler
43
49
  requirement: !ruby/object:Gem::Requirement