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.



1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
# File 'lib/ruby-macrodroid.rb', line 1046

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)


1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
# File 'lib/ruby-macrodroid.rb', line 1087

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

  a = @h[:days_of_week]
  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]
  
  if $debug then
    puts 's: ' + s.inspect 
    puts 'detail: ' + detail.inspect
    puts '@h: ' + @h.inspect
  end
  
  ChronicCron.new(s, detail[:time]).to_time == detail[:time]

end

#to_sObject



1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
# File 'lib/ruby-macrodroid.rb', line 1106

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