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, #siguid, #type

Instance Method Summary collapse

Methods inherited from MacroObject

#to_h

Constructor Details

#initialize(h = {}) ⇒ TimerTrigger

Returns a new instance of TimerTrigger.



1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
# File 'lib/ruby-macrodroid.rb', line 1659

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)


1700
1701
1702
1703
1704
# File 'lib/ruby-macrodroid.rb', line 1700

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

end

#set_envObject

sets the environmental conditions for this trigger to fire



1708
1709
1710
# File 'lib/ruby-macrodroid.rb', line 1708

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

#to_pcObject



1712
1713
1714
# File 'lib/ruby-macrodroid.rb', line 1712

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

#to_sObject



1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
# File 'lib/ruby-macrodroid.rb', line 1716

def to_s()
  
  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%P")    
  
  "%s %s" % [time, days]
end