-
-
Save KenYN/23a33ba61e91279ba9c4 to your computer and use it in GitHub Desktop.
A simple agent for Huginn that passes through events only between given times.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Pass through an event if within a time setting range | |
# A more sophisticated version of this would handle weekdays/weekends, etc | |
require 'active_support' | |
module Agents | |
class TimeGatewayAgent < Agent | |
cannot_be_scheduled! | |
description <<-MD | |
The TimeGatewayAgent passes through an event if it falls within the time ranges specified. | |
The ranges are combined with OR. | |
MD | |
event_description <<-MD | |
Events are a copy of the input parameters | |
MD | |
# Currently we only handle the 24 hour clock | |
def default_options | |
{ | |
:times => [{ | |
:from => "23:00", | |
:to => "23:59", | |
}, | |
{ | |
:from => "0:00", | |
:to => "8:00", | |
}] | |
} | |
end | |
def working? | |
true | |
end | |
def validate_options | |
errors.add(:base, "At least one time range is required") unless options[:times].present? && options[:times][0][:from].present? && options[:times][0][:to].present? | |
end | |
def receive(incoming_events) | |
incoming_events.each do |event| | |
# Get current time as seconds since midnight | |
secs_since_midnight = Time.new.seconds_since_midnight | |
options[:times].each do |time| | |
# if current time in between times | |
if Time.parse(time[:from]).seconds_since_midnight <= secs_since_midnight && secs_since_midnight <= Time.parse(time[:to]).seconds_since_midnight | |
create_event :payload => event.payload | |
# OK, skip to next event | |
next | |
end | |
end | |
end | |
end | |
end | |
end |
@KenYN Did you ever create a pull request for getting this merged? Could be useful?
Linked from: huginn/huginn#67
Also see: huginn/huginn#351
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
For use with Huginn.