Class: Bandshell::PlayerInfo

Inherits:
Object
  • Object
show all
Defined in:
lib/bandshell/player_info.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializePlayerInfo

Returns a new instance of PlayerInfo.



15
16
17
18
19
# File 'lib/bandshell/player_info.rb', line 15

def initialize
  @last_update  = Time.new(0)
  @shelf_life   = 60
  @on_off_rules = [{"action"=>"on"}] # default to always-on
end

Instance Attribute Details

#last_updateObject

Returns the value of attribute last_update.



11
12
13
# File 'lib/bandshell/player_info.rb', line 11

def last_update
  @last_update
end

#on_off_rulesObject

Returns the value of attribute on_off_rules.



12
13
14
# File 'lib/bandshell/player_info.rb', line 12

def on_off_rules
  @on_off_rules
end

#shelf_lifeObject

Returns the value of attribute shelf_life.



13
14
15
# File 'lib/bandshell/player_info.rb', line 13

def shelf_life
  @shelf_life
end

Instance Method Details

#screen_scheduled_on?Boolean

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. TODO: Evaluate effects of timezones

Returns:

  • (Boolean)


59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/bandshell/player_info.rb', line 59

def screen_scheduled_on?
  return true if on_off_rules.nil?

  results = []
  t = Time.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) ⇒ Object

For a given time object, gives a numeric representation of the time of day on the day it represents.



106
107
108
# File 'lib/bandshell/player_info.rb', line 106

def seconds_since_midnight(time)
  time.sec + (time.min * 60) + (time.hour * 3600)
end

#updateObject

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.



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/bandshell/player_info.rb', line 34

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
    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_staleObject

Returns false on failure.



22
23
24
25
26
27
28
# File 'lib/bandshell/player_info.rb', line 22

def update_if_stale
  if (@last_update < Time.now - @shelf_life)
    update
  else
    true
  end
end