Skip to content

Instantly share code, notes, and snippets.

@KenYN
Last active December 24, 2015 23:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save KenYN/23a33ba61e91279ba9c4 to your computer and use it in GitHub Desktop.
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.
# 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
Copy link
Author

KenYN commented Oct 8, 2013

For use with Huginn.

@0xdevalias
Copy link

@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