Class: TimerTrigger

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

Overview

Category: Date/Time

Instance Attribute Summary

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.



1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
# File 'lib/ruby-macrodroid.rb', line 1491

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)


1532
1533
1534
1535
1536
# File 'lib/ruby-macrodroid.rb', line 1532

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

end

#set_env ⇒ Object

sets the environmental conditions for this trigger to fire



1540
1541
1542
# File 'lib/ruby-macrodroid.rb', line 1540

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

#to_pc ⇒ Object



1544
1545
1546
# File 'lib/ruby-macrodroid.rb', line 1544

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

#to_s ⇒ Object



1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
# File 'lib/ruby-macrodroid.rb', line 1548

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