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.



1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
# File 'lib/ruby-macrodroid.rb', line 1423

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)


1464
1465
1466
1467
1468
# File 'lib/ruby-macrodroid.rb', line 1464

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

end

#set_envObject

sets the environmental conditions for this trigger to fire



1472
1473
1474
# File 'lib/ruby-macrodroid.rb', line 1472

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

#to_pcObject



1476
1477
1478
# File 'lib/ruby-macrodroid.rb', line 1476

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

#to_sObject



1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
# File 'lib/ruby-macrodroid.rb', line 1480

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