Class: ConcertoHardware::Player

Inherits:
ActiveRecord::Base
  • Object
show all
Extended by:
TimeAccessible
Defined in:
app/models/concerto_hardware/player.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from TimeAccessible

time_accessor

Instance Attribute Details

#always_onObject

Returns the value of attribute always_on.



12
13
14
# File 'app/models/concerto_hardware/player.rb', line 12

def always_on
  @always_on
end

#force_offObject

Returns the value of attribute force_off.



14
15
16
# File 'app/models/concerto_hardware/player.rb', line 14

def force_off
  @force_off
end

#wknd_disableObject

Returns the value of attribute wknd_disable.



13
14
15
# File 'app/models/concerto_hardware/player.rb', line 13

def wknd_disable
  @wknd_disable
end

Class Method Details

.create_time_zone_conversion_attribute?(name, column) ⇒ Boolean

Hack to get the multiparameter virtual attributes working

Returns:

  • (Boolean)


19
20
21
# File 'app/models/concerto_hardware/player.rb', line 19

def self.create_time_zone_conversion_attribute?(name, column)
  column.nil? ? true : super
end

Instance Method Details

#as_json(options) ⇒ Object



222
223
224
225
226
227
# File 'app/models/concerto_hardware/player.rb', line 222

def as_json(options)
  json = super(options)
  json["screen_on_off"] = ActiveSupport::JSON.decode(self.screen_on_off)
  json["polling_interval"] = self.polling_interval
  json
end

#default_valuesObject



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'app/models/concerto_hardware/player.rb', line 50

def default_values
  self.screen_on_off ||= [
    {
      :action => "on",
      :wkday => "12345", # M-F
      :time_after => "07:00",
      :time_before => "20:00"
    },
    {
      :action => "on",
      :wkday => "06", # Sun, Sat
      :time_after => "09:00",
      :time_before => "20:00"
    }
  ].to_json
  retrieve_screen_on_off #populate the virtual attributes.
end

#describe_screen_on_offObject

Returns an array of strings describing the screen’s behavior. Relies on retrive_screen_on_off having been called at load.



166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'app/models/concerto_hardware/player.rb', line 166

def describe_screen_on_off
  rules = []
  if self.screen_on_off.blank?
    rules << "On/off times not configured. The screen will always be on."
  elsif self.always_on
    rules << "Screen is always on. Click 'Edit Player' to configure "+
             "power-saving screen controls."
  elsif !screen_on_off_valid
    rules << "On/off rules are invalid. Edit and save the Player to fix."
  else
    rules << "Weekdays: on at "+fmt_time(wkday_on_time, "%l:%M%P")+", "+
      "off at "+fmt_time(wkday_off_time, "%l:%M%P")+"."
    if wknd_disable?
      rules << "Weekends: off."
    else
      rules << "Weekends: on at "+fmt_time(wknd_on_time, "%l:%M%P")+", "+
        "off at "+fmt_time(wknd_off_time, "%l:%M%P")+"."
    end
    if force_off
      rules << "Manual override: screen will be off until midnight tonight."
    end
  end
  rules
end

#fmt_time(timeobj, fmt = "%H:%M") ⇒ Object



191
192
193
194
195
196
197
198
# File 'app/models/concerto_hardware/player.rb', line 191

def fmt_time(timeobj, fmt = "%H:%M")
  if !timeobj.nil?
    if timeobj.is_a?(String)
      timeobj = Time.parse(timeobj)
    end
    timeobj.strftime(fmt).strip
  end
end

#polling_intervalObject



200
201
202
# File 'app/models/concerto_hardware/player.rb', line 200

def polling_interval
  return ConcertoConfig[:poll_interval].to_i
end

#process_screen_on_offObject

Take screen controls from the form and store them in a standard format that the player will understand. github.com/concerto/concerto-hardware/

wiki/Player-API#screen-onoff-times

TODO: Formatting for datetimes TODO: TIMEZONES



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
# File 'app/models/concerto_hardware/player.rb', line 74

def process_screen_on_off
  ruleset = []
  if wknd_disable?
    # Special case: we can ignore invlaid wknd times if we're off
    # on the weekend (avoids a rough edge on form submission).
    self.wknd_on_time = "09:00" if wknd_on_time.nil?
    self.wknd_off_time = "23:00" if wknd_off_time.nil?
  end
  unless self.wkday_on_time.nil? or self.wkday_off_time.nil?
    ruleset << {
      :action => "on",
      :wkday => "12345", # M-F
      :time_after => fmt_time(wkday_on_time),   # "07:00"
      :time_before => fmt_time(wkday_off_time), # "23:00"
    }
  end
  unless self.wknd_on_time.nil? or self.wknd_off_time.nil?
    ruleset << {
      :action => self.wknd_disable? ? "off" : "on",
      :wkday => "06", # Sun, Sat
      :time_after => fmt_time(wknd_on_time),  # "07:00"
      :time_before => fmt_time(wknd_off_time), # "23:00"
    }
  end
  if self.force_off == "1"
    ruleset << {
      :action => "off",
      :date => Time.now.strftime("%Y-%m-%d")
    }
  end
  if self.always_on == "1"
    # Note: supersedes everything else.
    ruleset << {
      :action => "force_on"
    }
  end
  if ruleset.empty? && self.screen_on_off.blank?
    ruleset << {
      :action => "on"
    }
  end

  self.screen_on_off = ruleset.to_json unless ruleset.empty?
end

#retrieve_screen_on_offObject

This is a very limited parsing of the on/off rules Pretty much it will only work on rulesets created by the methods in this model. The format is very flexible, but this model only supports 3 simple rules.



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'app/models/concerto_hardware/player.rb', line 123

def retrieve_screen_on_off
  return nil if screen_on_off.blank?
  self.always_on = false # default unless rule exists

  ruleset = ActiveSupport::JSON.decode(self.screen_on_off)
  ruleset.each do |rule|
    if rule.has_key? 'action'
      if rule.has_key? 'wkday' and rule['wkday']=='12345' and 
        rule['action']='on'
        if rule.has_key? 'time_after' and rule.has_key? 'time_before'
          self.wkday_on_time = Time.parse(rule['time_after'])
          self.wkday_off_time = Time.parse(rule['time_before'])
        end
      end # weekday
      if rule.has_key? 'wkday' and rule['wkday']=='06' 
        if rule.has_key? 'time_after' and rule.has_key? 'time_before'
          self.wknd_on_time = Time.parse(rule['time_after'])
          self.wknd_off_time = Time.parse(rule['time_before'])
        end
        self.wknd_disable = (rule['action'] != 'on')
      end # weekend
      if rule.has_key? 'date'
        if rule['date'] == Time.now.strftime("%Y-%m-%d")
          self.force_off = (rule['action'] != 'on')
        end
      end # force off rules
      if rule['action'] == 'force_on'
        self.always_on = true
      end # always on
    end # has an action
  end # each rule
end

#screen_on_off_validObject

Relies on retrive_screen_on_off having been called at load.



157
158
159
160
161
162
# File 'app/models/concerto_hardware/player.rb', line 157

def screen_on_off_valid
  !(
    wkday_on_time.nil? or  wkday_off_time.nil? or
    wknd_on_time.nil? or wknd_off_time.nil? or wknd_disable.nil?
  )
end

#time_zoneObject

Get the timezone that has been configured for this screen, using in the standard region/city format. Example: “America/New York” This is used in the Player API for Bandshell.



208
209
210
211
212
213
214
215
216
217
218
219
220
# File 'app/models/concerto_hardware/player.rb', line 208

def time_zone
  # The concerto config and screen config are stored in
  # ActiveSupport::TimeZone's "friendly" time zone format:
  #   "Eastern Time (US & Canada)"
  if screen.nil? or screen.time_zone.nil?
    pretty_time_zone = ConcertoConfig[:system_time_zone]
  else
    pretty_time_zone = screen.time_zone
  end
  # Now just backtrack to the canonical name which will
  # be useful to the players.
  ActiveSupport::TimeZone::MAPPING[pretty_time_zone]
end

#wknd_disable?Boolean

Evaluates truthiness of the virtual attribute.

Returns:

  • (Boolean)


44
45
46
47
48
# File 'app/models/concerto_hardware/player.rb', line 44

def wknd_disable?
  return true if self.wknd_disable == "1"
  return true if self.wknd_disable == true
  return false
end