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.



18
19
20
21
22
# 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

Instance Attribute Details

#last_updateObject

Returns the value of attribute last_update.



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

def last_update
  @last_update
end

#on_off_rulesObject

Returns the value of attribute on_off_rules.



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

def on_off_rules
  @on_off_rules
end

#screen_nameObject

Returns the value of attribute screen_name.



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

def screen_name
  @screen_name
end

#shelf_lifeObject

Returns the value of attribute shelf_life.



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

def shelf_life
  @shelf_life
end

#timezoneObject

String representation of TZ Location



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

def timezone
  @timezone
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.

Returns:

  • (Boolean)


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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# 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) ⇒ Object

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



127
128
129
# File 'lib/bandshell/player_info.rb', line 127

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.



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# 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_staleObject

Returns false on failure.



25
26
27
28
29
30
31
# File 'lib/bandshell/player_info.rb', line 25

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