class TimerTrigger

Category: Date/Time

Public Class Methods

new(h={}) click to toggle source
Calls superclass method Trigger::new
# File lib/ruby-macrodroid/triggers.rb, line 674
def initialize(h={})

  puts 'TimerTrigger h: ' + h.inspect if $debug
  
  if h[:days] then
    
    days = [false] * 7
    
    h[:days].split(/, */).each do |x|

      r = Date::DAYNAMES.grep /#{x}/i
      i = Date::DAYNAMES.index(r.first)
      days[i-1] = true

    end      
    
    h[:days_of_week] = days
    
  end
  
  if h[:time] then
    
    t = Time.parse(h[:time])
    h[:hour], h[:minute] = t.hour, t.min
    
  end
  
  #puts ('h: ' + h.inspect).debug

  @options = {
    alarm_id: uuid(),
    days_of_week: [false, false, false, false, false, false, false],
    minute: 10,
    hour: 7,
    use_alarm: false
  }
          
  #super(options.merge filter(options, h))
  super(@options.merge h)

end

Public Instance Methods

match?(detail={time: $env[:time]}, model=nil) click to toggle source
# File lib/ruby-macrodroid/triggers.rb, line 716
def match?(detail={time: $env[:time]}, model=nil)
  
 time() == detail[:time]

end
set_env() click to toggle source

sets the environmental conditions for this trigger to fire

# File lib/ruby-macrodroid/triggers.rb, line 724
def set_env()
  $env[:time] = time()
end
to_pc() click to toggle source
# File lib/ruby-macrodroid/triggers.rb, line 728
def to_pc()    
  "time.is? '%s'" % self.to_s.gsub(',', ' or')
end
to_s(colour: false) click to toggle source
# File lib/ruby-macrodroid/triggers.rb, line 732
def to_s(colour: false)
  
  dow = @h[:days_of_week]        

  wd = Date::ABBR_DAYNAMES    
  a = (wd[1..-1] << wd.first)
  
  a2 = dow.map.with_index.to_a
  start = a2.find {|x,i| x}.last
  r = a2[start..-1].take_while {|x,i| x == true}
  r2 = a2[start..-1].select {|x,i| x}
  
  days = if r == r2 then
  
    x1, x2 = a2[start].last, a2[r.length-1].last
    
    if (x2 - x1) >= 2 then
      "%s-%s" % [a[x1],a[x2]]
    else
      a.zip(dow).select {|_,b| b}.map(&:first).join(', ')
    end
  else  
    a.zip(dow).select {|_,b| b}.map(&:first).join(', ')
  end
  
  time = Time.parse("%s:%s" % [@h[:hour], @h[:minute]]).strftime("%H:%M")    
  
  "%s %s" % [time, days]
end
Also aliased as: to_summary
to_summary(colour: false)
Alias for: to_s

Private Instance Methods

time() click to toggle source
# File lib/ruby-macrodroid/triggers.rb, line 766
def time()
  
  a = @h[:days_of_week].clone
  a.unshift a.pop

  dow = a.map.with_index {|x, i| x ? i : nil }.compact.join(',')
  s = "%s %s * * %s" % [@h[:minute], @h[:hour], dow]        
  recent_time = ($env && $env[:time]) ? $env[:time] : Time.now
  ChronicCron.new(s, recent_time).to_time
  
end