Class: Squeezer::Models::Player

Inherits:
Model
  • Object
show all
Defined in:
lib/squeezer/models/player.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Model

extract_records

Methods included from Connection

close_connection, exec, #exit, open_connection, retrieve_connection

Constructor Details

#initialize(id, options = {}) ⇒ Player

Returns a new instance of Player.



9
10
11
12
# File 'lib/squeezer/models/player.rb', line 9

def initialize(id, options={})
  super options
  @id = id
end

Instance Attribute Details

#idObject (readonly)

most of these attributes are cached for the lifetime of the object, since they never change anyway



7
8
9
# File 'lib/squeezer/models/player.rb', line 7

def id
  @id
end

Class Method Details

.allObject



182
183
184
185
186
187
188
189
190
# File 'lib/squeezer/models/player.rb', line 182

def self.all
  result = Array.new
  count = Connection.exec("player count ?").to_i
  count.to_i.times do |index|
    id = Connection.exec("player id #{index} ?")
    result << Models::Player.new(id)
  end
  result
end

.find(values) ⇒ Object



205
206
207
208
209
210
211
212
213
214
# File 'lib/squeezer/models/player.rb', line 205

def self.find(values)
  all.each do |player|
    match = true
    values.each do |property,value|
      match = false unless player.send(property.to_sym) == value
    end
    return player if match == true
  end
  return nil
end

.find_by_id(id) ⇒ Object



201
202
203
# File 'lib/squeezer/models/player.rb', line 201

def self.find_by_id(id)
  find(:id => id)
end

.find_by_ip(ip) ⇒ Object



197
198
199
# File 'lib/squeezer/models/player.rb', line 197

def self.find_by_ip(ip)
  find(:ip => ip)
end

.find_by_name(name) ⇒ Object

handle duplicates in these find methods, specs expect only one result



193
194
195
# File 'lib/squeezer/models/player.rb', line 193

def self.find_by_name(name)
  find(:name => name)
end

Instance Method Details

#<=>(target) ⇒ Object



172
173
174
# File 'lib/squeezer/models/player.rb', line 172

def <=>(target)
  self.name <=> target.name
end

#alert(message, options = {}) ⇒ Object



156
157
158
159
# File 'lib/squeezer/models/player.rb', line 156

def alert(message, options={})
  options = {:line2 => URI.escape(message), :centered => true, :duration => 3, :font => :huge}.merge(options)
  show(options)
end

#bassObject



65
66
67
# File 'lib/squeezer/models/player.rb', line 65

def bass
  mixer(:bass)
end

#bass=(value) ⇒ Object



60
61
62
63
# File 'lib/squeezer/models/player.rb', line 60

def bass=(value)
  raise "command is not supported on this player's model: #{model}" unless %w{slimp3 squeezebox}.include?(model)
  mixer(:bass, value)
end


161
162
163
164
165
166
# File 'lib/squeezer/models/player.rb', line 161

def blink(message, times, duration=1, options={})
  times.times do
    alert(message, :duration => duration)
    sleep duration
  end
end

#can_power_offObject



36
37
38
# File 'lib/squeezer/models/player.rb', line 36

def can_power_off
  @canpoweroff ||= cmd("player canpoweroff #{id} ?").to_boolean
end

#connected?Boolean

Returns:

  • (Boolean)


48
49
50
# File 'lib/squeezer/models/player.rb', line 48

def connected?
  cmd("#{id} connected ?").to_boolean
end

#display_typeObject



32
33
34
# File 'lib/squeezer/models/player.rb', line 32

def display_type
  @display_type ||= cmd "player displaytype #{id} ?"
end

#ipObject



18
19
20
# File 'lib/squeezer/models/player.rb', line 18

def ip
  @ip ||= URI.unescape(cmd("player ip #{id} ?")).split(":").first
end

#is_a?(value) ⇒ Boolean

“transporter”, “squeezebox2”, “squeezebox”, “slimp3”, “softsqueeze”, or “http”

Returns:

  • (Boolean)


27
28
29
30
# File 'lib/squeezer/models/player.rb', line 27

def is_a?(value)
  value = value.to_s if value.is_a?(Symbol)
  model == value
end

#mixer(attribute, value = "?") ⇒ Object



87
88
89
90
91
92
# File 'lib/squeezer/models/player.rb', line 87

def mixer(attribute, value="?")
  raise "unknown attribute" unless %w{volume bass treble pitch}.include?(attribute.to_s)
  modifier = "+" if value.is_a?(String) and value.include?("+")
  result = cmd("#{id} mixer #{attribute.to_s} #{modifier}#{value == "?" ? "?" : value.to_i}")
  result.respond_to?(:to_i) ? result.to_i : result
end

#modeObject



148
149
150
# File 'lib/squeezer/models/player.rb', line 148

def mode
  cmd("#{id} mode ?").to_sym
end

#modelObject



22
23
24
# File 'lib/squeezer/models/player.rb', line 22

def model
  @model ||= cmd "player model #{id} ?"
end

#nameObject



14
15
16
# File 'lib/squeezer/models/player.rb', line 14

def name
  @name ||= cmd "player name #{id} ?"
end

#off!Object



119
120
121
# File 'lib/squeezer/models/player.rb', line 119

def off!
  power(:off)
end

#off?Boolean

Returns:

  • (Boolean)


111
112
113
# File 'lib/squeezer/models/player.rb', line 111

def off?
  :off == power?
end

#on!Object



115
116
117
# File 'lib/squeezer/models/player.rb', line 115

def on!
  power(:on)
end

#on?Boolean

Returns:

  • (Boolean)


107
108
109
# File 'lib/squeezer/models/player.rb', line 107

def on?
  :on == power?
end

#pauseObject



132
133
134
# File 'lib/squeezer/models/player.rb', line 132

def pause
  cmd("#{id} pause")
end

#paused?Boolean

Returns:

  • (Boolean)


144
145
146
# File 'lib/squeezer/models/player.rb', line 144

def paused?
  mode == :pause
end

#pitchObject



83
84
85
# File 'lib/squeezer/models/player.rb', line 83

def pitch
  mixer(:pitch)
end

#pitch=(value) ⇒ Object



78
79
80
81
# File 'lib/squeezer/models/player.rb', line 78

def pitch=(value)
  raise "command is not supported on this player's model: #{model}" unless is_a?("squeezebox")
  mixer(:pitch, value)
end

#play(fade = 0) ⇒ Object

fade in seconds



124
125
126
# File 'lib/squeezer/models/player.rb', line 124

def play(fade=0)
  cmd("#{id} play #{fade}")
end

#playersObject

beware of offline players, they still show up on the list test if those players are connected with Player#connected?



178
179
180
# File 'lib/squeezer/models/player.rb', line 178

def players
  Player.all
end

#playing?Boolean

Returns:

  • (Boolean)


136
137
138
# File 'lib/squeezer/models/player.rb', line 136

def playing?
  mode == :play
end

#playlistObject



168
169
170
# File 'lib/squeezer/models/player.rb', line 168

def playlist
  Models::Playlist.new(id)
end

#power(state) ⇒ Object



94
95
96
97
98
99
# File 'lib/squeezer/models/player.rb', line 94

def power(state)
  state_map = {:on => "1", :off => "0"}
  raise "unknown power state" unless state_map.keys.include?(state)
  result = cmd "#{id} power #{state_map[state]}"
  result      
end

#power?Boolean

Returns:

  • (Boolean)


101
102
103
104
105
# File 'lib/squeezer/models/player.rb', line 101

def power?
  on = cmd("#{id} power ?").to_boolean
  return :on if on
  return :off if not on
end

#show(options) ⇒ Object



152
153
154
# File 'lib/squeezer/models/player.rb', line 152

def show(options)
  cmd("#{id} show font:#{options[:font]} line2:#{options[:line2]} centered:#{options[:centered]} duration:#{options[:duration]}")
end

#signal_strengthObject



40
41
42
# File 'lib/squeezer/models/player.rb', line 40

def signal_strength
  cmd("#{id} signalstrength ?").to_i
end

#stopObject



128
129
130
# File 'lib/squeezer/models/player.rb', line 128

def stop
  cmd("#{id} stop")
end

#stopped?Boolean

Returns:

  • (Boolean)


140
141
142
# File 'lib/squeezer/models/player.rb', line 140

def stopped?
  mode == :stop
end

#trebleObject



74
75
76
# File 'lib/squeezer/models/player.rb', line 74

def treble
  mixer(:treble)
end

#treble=(value) ⇒ Object



69
70
71
72
# File 'lib/squeezer/models/player.rb', line 69

def treble=(value)
  raise "command is not supported on this player's model: #{model}" unless %w{slimp3 squeezebox}.include?(model)
  mixer(:treble, value)
end

#volumeObject



56
57
58
# File 'lib/squeezer/models/player.rb', line 56

def volume
  mixer(:volume)
end

#volume=(value) ⇒ Object



52
53
54
# File 'lib/squeezer/models/player.rb', line 52

def volume=(value)
  mixer(:volume, value)
end

#wireless?Boolean

Returns:

  • (Boolean)


44
45
46
# File 'lib/squeezer/models/player.rb', line 44

def wireless?
  @wireless ||= (signal_strength != 0)
end