Class: Sonos::System

Inherits:
Object
  • Object
show all
Defined in:
lib/sonos/system.rb

Overview

The Sonos system. The root object to manage the collection of groups and devices. This is intended to be a singleton accessed from ‘Sonos.system`.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(topology = Discovery.new.topology) ⇒ System

Initialize the system

Parameters:

  • the (Array)

    system topology. If this is nil, it will autodiscover.



11
12
13
# File 'lib/sonos/system.rb', line 11

def initialize(topology = Discovery.new.topology)
  rescan topology
end

Instance Attribute Details

#devicesObject (readonly)

Returns the value of attribute devices.



7
8
9
# File 'lib/sonos/system.rb', line 7

def devices
  @devices
end

#groupsObject (readonly)

Returns the value of attribute groups.



6
7
8
# File 'lib/sonos/system.rb', line 6

def groups
  @groups
end

#topologyObject (readonly)

Returns the value of attribute topology.



5
6
7
# File 'lib/sonos/system.rb', line 5

def topology
  @topology
end

Instance Method Details

#find_party_masterObject



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/sonos/system.rb', line 48

def find_party_master
  # 1: If there are any pre-existing groups playing something, use
  # the lowest-numbered group's master. But ensure to only check a
  # master_speaker that is actually a speaker, and not an Accessory.
  groups.each do |group|
    if group.master_speaker.speaker? and group.master_speaker.has_music?
      return group.master_speaker
    end
  end

  # 2: Lowest-number speaker that's playing something
  speakers.each do |speaker|
    return speaker if speaker.has_music?
  end

  # 3: lowest-numbered speaker
  speakers[0]
end

#party_mode(new_master = nil) ⇒ Object

Party Mode! Join all speakers into a single group.



35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/sonos/system.rb', line 35

def party_mode new_master = nil
  return nil unless speakers.length > 1

  new_master = find_party_master if new_master.nil?

  party_over
  speakers.each do |slave|
    next if slave.uid == new_master.uid
    slave.join new_master
  end
  rescan @topology
end

#party_overObject

Party’s over :(



68
69
70
71
# File 'lib/sonos/system.rb', line 68

def party_over
  groups.each { |g| g.disband }
  rescan @topology
end

#pause_allObject

Pause all speakers



21
22
23
24
25
# File 'lib/sonos/system.rb', line 21

def pause_all
  speakers.each do |speaker|
    speaker.pause if speaker.has_music?
  end
end

#play_allObject

Play all speakers



28
29
30
31
32
# File 'lib/sonos/system.rb', line 28

def play_all
  speakers.each do |speaker|
    speaker.play if speaker.has_music?
  end
end

#rescan(topology = Discovery.new.topology) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/sonos/system.rb', line 73

def rescan(topology = Discovery.new.topology)
  @topology = topology
  @groups = []
  @devices = @topology.collect(&:device)

  construct_groups

  speakers.each do |speaker|
    speaker.group_master = speaker
    @groups.each do |group|
      if group.master_speaker.uid == speaker.uid
        speaker.group_master = speaker
        group.master_speaker = speaker
      end
      group.slave_speakers.each do |slave|
        speaker.group_master = group.master_speaker if slave.uid == speaker.uid
      end
    end
  end
end

#speakersObject

Returns all speakers



16
17
18
# File 'lib/sonos/system.rb', line 16

def speakers
  @devices.select(&:speaker?)
end