class DaFunk::EventHandler
Attributes
option[R]
timer[RW]
type[R]
Public Class Methods
new(type, option, &block)
click to toggle source
# File lib/da_funk/event_handler.rb, line 6 def initialize(type, option, &block) @type = type @option = option @perform_block = block register end
Public Instance Methods
execute?()
click to toggle source
# File lib/da_funk/event_handler.rb, line 73 def execute? schedule_timer unless self.timer if self.timer self.timer < Time.now else ! self.timer end end
perform(*parameter)
click to toggle source
# File lib/da_funk/event_handler.rb, line 18 def perform(*parameter) if execute? schedule_timer @perform_block.call(*parameter) end end
register()
click to toggle source
# File lib/da_funk/event_handler.rb, line 13 def register schedule_timer EventListener.add_handler(self) end
schedule_timer()
click to toggle source
# File lib/da_funk/event_handler.rb, line 25 def schedule_timer if option.is_a?(Hash) if option.include?(:hours) && option.include?(:slot) self.timer = Time.at(seconds_from_file) elsif option.include?(:minutes) self.timer = Time.now + (option[:minutes].to_i * 60) elsif option.include?(:seconds) self.timer = Time.now + option[:seconds] end end rescue ArgumentError File.delete("main/schedule.dat") Device::System.reboot end
seconds_from_file()
click to toggle source
# File lib/da_funk/event_handler.rb, line 40 def seconds_from_file unless option[:slot] && option[:hours] raise "slot or hours missing on EventHandler creation" end db = FileDb.new("main/schedule.dat") string = db[option[:slot]] config = parse_slot(string) unless config # configure from scrath config = {"timestamp" => nil, "interval" => stringify_hours(option)} end unless config["timestamp"] config["timestamp"] = hours2seconds(option[:hours]) else if config["interval"]["hours"].to_s != option[:hours].to_s config["timestamp"] = hours2seconds(option[:hours]) config["interval"]["hours"] = option[:hours].to_s else if self.timer && self.timer.to_i <= Time.now.to_i config["timestamp"] = hours2seconds(option[:hours]) config["interval"]["hours"] = option[:hours].to_s end end end db[option[:slot]] = config.to_json config["timestamp"].to_i end
Private Instance Methods
hours2seconds(interval)
click to toggle source
# File lib/da_funk/event_handler.rb, line 87 def hours2seconds(interval) hours = 60 * 60 * interval.to_i hours = 99_999 if hours == 0 (Time.now.to_i + hours) end
parse_slot(string)
click to toggle source
# File lib/da_funk/event_handler.rb, line 93 def parse_slot(string) unless string.to_s.empty? JSON.parse(string) end rescue # old format slot=<fixnum> nil end
stringify_hours(options)
click to toggle source
# File lib/da_funk/event_handler.rb, line 83 def stringify_hours(options) {"hours" => options[:hours]} end