Class: WinkScheduler::Schedule

Inherits:
Object
  • Object
show all
Defined in:
lib/wink_scheduler/schedule.rb

Instance Method Summary collapse

Constructor Details

#initialize(context, opts) ⇒ Schedule

Returns a new instance of Schedule.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/wink_scheduler/schedule.rb', line 9

def initialize(context, opts)
  @last_time = nil
  @logger = context[:logger]
  devices = context[:devices]
  groups = context[:groups]

  if opts["device"]
    @object = devices.select { |d| next if d == nil; d.name == opts["device"] }[0]
    raise "device \"#{opts["device"]}\" not found" unless @object
    @type = :device
  elsif opts["group"]
    @object = groups.select { |g| next if g == nil; g.name == opts["group"] }[0]
    raise "group \"#{opts["group"]}\" not found" unless @object
    @type = :group
  else
    raise "Schedule must define either a 'device' or a 'group' option"
  end

  if opts["days"]
    @days = get_days(opts["days"])
  else
    raise "Schedule must define 'days' option"
  end

  if opts["method"]
    @method = opts["method"]
    raise "#{@method} is not a supported function of #{@object.name}" unless @object.respond_to?(@method)
  else
    raise "Schedule must define 'method' option"
  end

  @method_args = []
  if opts["method_args"]
    arity = @object.method(@method).arity
    if arity == 0
      raise "#{@method} does not take arguments. 'method_args' must be nil"
    elsif arity > 0
      if opts["method_args"].length == arity
        @method_args = opts["method_args"]
      else
        raise "#{@method} requires #{arity} argument(s). Invalid 'method_args' option"
      end
    elsif arity < 0
      if opts["method_args"].length >= arity.abs
        @method_args = opts["method_args"]
      else
        raise "#{@method} requires at least #{arity} argument(s). Invalid 'method_args' option"
      end
    end
  end

  if opts["time"]
    @time = opts["time"]
  else
    raise "Schedule must define 'time' option"
  end

  if opts["offset"]
    @offset = opts["offset"] * 60
  else
    @offset = 0
  end

  @window = opts["window"]

  if (@time == :sunrise || @time == :sunset)
    if opts["location"]
      @woeid = get_woeid(opts["location"])
    else
      raise "Schedule must define 'location' option if time used is sunrise or sunset"
    end
  end
end

Instance Method Details

#runObject



83
84
85
86
87
# File 'lib/wink_scheduler/schedule.rb', line 83

def run
  while true
    execute
  end
end