class Bandshell::PlayerInfo
Attributes
last_update[RW]
on_off_rules[RW]
screen_name[RW]
shelf_life[RW]
timezone[RW]
Public Class Methods
new()
click to toggle source
# File lib/bandshell/player_info.rb, line 18 def initialize @last_update = Time.new(0) @shelf_life = 60 @on_off_rules = [{"action"=>"on"}] # default to always-on end
Public Instance Methods
screen_scheduled_on?()
click to toggle source
Returns true if the screen should be turned on right now, according to the latest data recieved from concerto-hardware. Assumes on_off_rules
is either nil or a valid ruleset.
# File lib/bandshell/player_info.rb, line 73 def screen_scheduled_on? return true if on_off_rules.nil? results = [] begin tz = TZInfo::Timezone.get(timezone) unless timezone.nil? rescue TZInfo::InvalidTimezoneIdentifier => e puts('screen_scheduled_on?: Invalid Time Zone. Defaulting to NY.') end tz = TZInfo::Timezone.get('America/New_York') if tz.nil? t = tz.now on_off_rules.each do |rule| rule_active = true rule.each do |key, value| case key when "wkday" rule_active = false unless value.include? t.wday.to_s when "time_after" rule_secs = seconds_since_midnight(Time.parse(value)) curr_secs = seconds_since_midnight(t) rule_active = false unless curr_secs > rule_secs when "time_before" rule_secs = seconds_since_midnight(Time.parse(value)) curr_secs = seconds_since_midnight(t) rule_active = false unless curr_secs < rule_secs when "date" day = Time.parse(value) rule_active = false unless t.year==day.year and t.yday==day.yday when "action" # Do nothing. else # Do nothing. # Err on the side of being on too long. end # case key end if rule_active and rule.has_key? "action" results << rule["action"] end end # each rule if results.include? "force_on" return true elsif results.include? "off" return false elsif results.include? "on" return true else # All rules failed return false end end
seconds_since_midnight(time)
click to toggle source
For a given time object, gives a numeric representation of the time of day on the day it represents.
# File lib/bandshell/player_info.rb, line 127 def seconds_since_midnight(time) time.sec + (time.min * 60) + (time.hour * 3600) end
update()
click to toggle source
Fetches the latest player settings from Concerto TODO: Store settings in BandshellConfig (and update whenever they have changed) so that configs are immediately available at boot. Returns true on success, false on failure.
# File lib/bandshell/player_info.rb, line 37 def update data = Bandshell::HardwareApi::get_player_info if data.nil? puts "update_player_info: Recieved null data from get_player_info!" elsif data == :stat_serverr puts "update_player_info: Server error while retrieving player info." elsif data == :stat_badauth puts "update_player_info: Auth error while retrieving player info." else unless data['screen'].nil? or data['screen']['name'].nil? @screen_name = data['screen']['name'] end tz_string = data['time_zone'] unless tz_string.nil? or tz_string.empty? begin new_tz = TZInfo::Timezone.get(tz_string) @timezone = tz_string rescue TZInfo::InvalidTimezoneIdentifier => e puts "update_player_info: Invalid timezone received." end end # TZ identifier present new_rules = data['screen_on_off'] if new_rules.nil? or !new_rules.is_a? Array puts "update_player_info: Invalid screen on/off rules received." else @on_off_rules = new_rules @last_update = Time.now return true end end return false end
update_if_stale()
click to toggle source
Returns false on failure.
# File lib/bandshell/player_info.rb, line 25 def update_if_stale if (@last_update < Time.now - @shelf_life) update else true end end