Class: TimerTrigger

Inherits:
Trigger show all
Defined in:
lib/ruby-macrodroid.rb

Overview

Category: Date/Time

Instance Attribute Summary

Attributes inherited from Trigger

#constraints

Attributes inherited from MacroObject

#options, #type

Instance Method Summary collapse

Methods inherited from MacroObject

#to_h

Constructor Details

#initialize(h = {}) ⇒ TimerTrigger

Returns a new instance of TimerTrigger.



1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
# File 'lib/ruby-macrodroid.rb', line 1611

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))

end

Instance Method Details

#match?(detail = {time: $env[:time]}, model = nil) ⇒ Boolean

Returns:

  • (Boolean)


1652
1653
1654
1655
1656
# File 'lib/ruby-macrodroid.rb', line 1652

def match?(detail={time: $env[:time]}, model=nil)
  
 time() == detail[:time]

end

#set_envObject

sets the environmental conditions for this trigger to fire



1660
1661
1662
# File 'lib/ruby-macrodroid.rb', line 1660

def set_env()
  $env[:time] = time()
end

#to_pcObject



1664
1665
1666
# File 'lib/ruby-macrodroid.rb', line 1664

def to_pc()    
  "time.is? '%s'" % self.to_s.gsub(',', ' or')
end

#to_sObject



1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
# File 'lib/ruby-macrodroid.rb', line 1668

def to_s()
  
  dow = @h[:days_of_week]

  a = Date::ABBR_DAYNAMES

  time = Time.parse("%s:%s" % [@h[:hour], @h[:minute]]).strftime("%-H:%M%P")
  days = (a[1..-1] << a.first).zip(dow).select {|_,b| b}.map(&:first)
  
  "at %s on %s" % [time, days.join(', ')]
end