sonos 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in control.gemspec
4
+ gemspec
5
+
6
+ gem 'rake'
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Sam Soffes
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1 @@
1
+ require 'bundler/gem_tasks'
@@ -0,0 +1,62 @@
1
+ # Sonos
2
+
3
+ Control Sonos speakers with Ruby.
4
+
5
+ Huge thanks to [Rahim Sonawalla](https://github.com/rahims) for making [SoCo](https://github.com/rahims/SoCo). Control would not be possible without his work.
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ gem 'sonos'
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install sonos
20
+
21
+ ## Usage
22
+
23
+ I'm working on a CLI client. For now, we'll use IRB. You will need the IP address of a speaker (auto-detection is on my list too). To get the IP of a speaker, one of your Sonos controllers and go to "About My Sonos System".
24
+
25
+ ``` shell
26
+ $ gem install sonos
27
+ $ irb
28
+ ```
29
+
30
+ ``` ruby
31
+ > require 'rubygems'
32
+ > require 'sonos'
33
+ > speaker = Sonos::Speaker('10.0.1.10') # or whatever the IP is
34
+ ```
35
+
36
+ Now that we have a reference to the speaker, we can do all kinds of stuff.
37
+
38
+ ``` ruby
39
+ > speaker.pause # Pause whatever is playing
40
+ > speaker.play # Resumes the playlist
41
+ > speaker.play 'http://assets.samsoff.es/music/Airports.mp3' # Stream!
42
+ > speaker.now_playing
43
+ > speaker.volume
44
+ > speaker.volume = 70
45
+ > speaker.volume -= 10
46
+ ```
47
+
48
+ ## Todo
49
+
50
+ * Fix album art in `now_playing`
51
+ * Handle line-in in `now_playing`
52
+ * Auto-discovery
53
+ * Better support for stero pairs
54
+ * CLI client
55
+
56
+ ## Contributing
57
+
58
+ 1. Fork it
59
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
60
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
61
+ 4. Push to the branch (`git push origin my-new-feature`)
62
+ 5. Create new Pull Request
@@ -0,0 +1,11 @@
1
+ require 'sonos/version'
2
+ require 'sonos/speaker'
3
+
4
+ module Sonos
5
+ PORT = 1400.freeze
6
+ NAMESPACE = 'http://www.sonos.com/Services/1.1'.freeze
7
+
8
+ def self.Speaker(ip)
9
+ Speaker.new(ip)
10
+ end
11
+ end
@@ -0,0 +1,124 @@
1
+ require 'savon'
2
+
3
+ module Sonos
4
+ class Speaker
5
+ TRANSPORT_ENDPOINT = '/MediaRenderer/AVTransport/Control'.freeze
6
+ RENDERING_ENDPOINT = '/MediaRenderer/RenderingControl/Control'.freeze
7
+ DEVICE_ENDPOINT = '/DeviceProperties/Control'.freeze
8
+
9
+ attr_accessor :zone_name, :zone_icon, :uid, :serial_number, :software_version, :hardware_version, :mac_address
10
+
11
+ def initialize(ip)
12
+ @ip = ip
13
+
14
+ # Get meta data
15
+ self.get_speaker_info
16
+ end
17
+
18
+ #
19
+ # Get information about the currently playing track.
20
+ #
21
+ def get_position_info
22
+ action = 'urn:schemas-upnp-org:service:AVTransport:1#GetPositionInfo'
23
+ message = '<u:GetPositionInfo xmlns:u="urn:schemas-upnp-org:service:AVTransport:1"><InstanceID>0</InstanceID><Channel>Master</Channel></u:GetPositionInfo>'
24
+ response = transport_client.call(:get_position_info, soap_action: action, message: message)
25
+ body = response.body[:get_position_info_response]
26
+ doc = Nokogiri::XML(body[:track_meta_data])
27
+
28
+ {
29
+ title: doc.xpath('//dc:title').inner_text,
30
+ artist: doc.xpath('//dc:creator').inner_text,
31
+ album: doc.xpath('//upnp:album').inner_text,
32
+ playlist_position: body[:track],
33
+ track_duration: body[:track_duration],
34
+ current_position: body[:rel_time],
35
+ uri: body[:track_uri],
36
+ album_art: "http://#{@ip}:#{PORT}#{doc.xpath('//upnp:albumArtURI').inner_text}"
37
+ }
38
+ end
39
+ alias_method :now_playing, :get_position_info
40
+
41
+ #
42
+ # Pause the currently playing track.
43
+ #
44
+ def pause
45
+ action = 'urn:schemas-upnp-org:service:AVTransport:1#Pause'
46
+ message = '<u:Pause xmlns:u="urn:schemas-upnp-org:service:AVTransport:1"><InstanceID>0</InstanceID><Speed>1</Speed></u:Pause>'
47
+ transport_client.call(:play, soap_action: action, message: message)
48
+ end
49
+
50
+ #
51
+ # Play the currently selected track or play a stream.
52
+ #
53
+ def play(uri = nil)
54
+ # Play a song from the uri
55
+ if uri
56
+ self.set_av_transport_uri(uri)
57
+ return
58
+ end
59
+
60
+ # Play the currently selected track
61
+ action = 'urn:schemas-upnp-org:service:AVTransport:1#Play'
62
+ message = '<u:Play xmlns:u="urn:schemas-upnp-org:service:AVTransport:1"><InstanceID>0</InstanceID><Speed>1</Speed></u:Play>'
63
+ transport_client.call(:play, soap_action: action, message: message)
64
+ end
65
+
66
+ #
67
+ # Play a stream.
68
+ #
69
+ def set_av_transport_uri(uri)
70
+ action = 'urn:schemas-upnp-org:service:AVTransport:1#SetAVTransportURI'
71
+ message = %Q{<u:SetAVTransportURI xmlns:u="urn:schemas-upnp-org:service:AVTransport:1"><InstanceID>0</InstanceID><CurrentURI>#{uri}</CurrentURI><CurrentURIMetaData></CurrentURIMetaData></u:SetAVTransportURI>}
72
+ transport_client.call(:set_av_transport_uri, soap_action: action, message: message)
73
+ self.play
74
+ end
75
+ alias_method :play_stream, :set_av_transport_uri
76
+
77
+ #
78
+ # Get information about the Sonos speaker.
79
+ #
80
+ def get_speaker_info
81
+ doc = Nokogiri::XML(open("http://#{@ip}:1400/status/zp"))
82
+
83
+ self.zone_name = doc.xpath('.//ZoneName').inner_text
84
+ self.zone_icon = doc.xpath('.//ZoneIcon').inner_text
85
+ self.uid = doc.xpath('.//LocalUID').inner_text
86
+ self.serial_number = doc.xpath('.//SerialNumber').inner_text
87
+ self.software_version = doc.xpath('.//SoftwareVersion').inner_text
88
+ self.hardware_version = doc.xpath('.//HardwareVersion').inner_text
89
+ self.mac_address = doc.xpath('.//MACAddress').inner_text
90
+ self
91
+ end
92
+
93
+ #
94
+ # Get the current volume.
95
+ #
96
+ def get_volume
97
+ action = 'urn:schemas-upnp-org:service:RenderingControl:1#GetVolume'
98
+ message = '<u:GetVolume xmlns:u="urn:schemas-upnp-org:service:RenderingControl:1"><InstanceID>0</InstanceID><Channel>Master</Channel></u:GetVolume>'
99
+ response = rendering_client.call(:get_volume, soap_action: action, message: message)
100
+ response.body[:get_volume_response][:current_volume].to_i
101
+ end
102
+ alias_method :volume, :get_volume
103
+
104
+ #
105
+ # Set the volume from 0 to 100.
106
+ #
107
+ def set_volume(level)
108
+ action = 'urn:schemas-upnp-org:service:RenderingControl:1#SetVolume'
109
+ message = %Q{<u:SetVolume xmlns:u="urn:schemas-upnp-org:service:RenderingControl:1"><InstanceID>0</InstanceID><Channel>Master</Channel><DesiredVolume>#{level}</DesiredVolume></u:SetVolume>}
110
+ rendering_client.call(:set_volume, soap_action: action, message: message)
111
+ end
112
+ alias_method :volume=, :set_volume
113
+
114
+ private
115
+
116
+ def transport_client
117
+ @transport_client ||= Savon.client endpoint: "http://#{@ip}:#{PORT}#{TRANSPORT_ENDPOINT}", namespace: NAMESPACE
118
+ end
119
+
120
+ def rendering_client
121
+ @rendering_client ||= Savon.client endpoint: "http://#{@ip}:#{PORT}#{RENDERING_ENDPOINT}", namespace: NAMESPACE
122
+ end
123
+ end
124
+ end
@@ -0,0 +1,3 @@
1
+ module Sonos
2
+ VERSION = '0.0.1'
3
+ end
@@ -0,0 +1,21 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'sonos/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = 'sonos'
8
+ gem.version = Sonos::VERSION
9
+ gem.authors = ['Sam Soffes']
10
+ gem.email = ['sam@soff.es']
11
+ gem.description = 'Sonos Controller'
12
+ gem.summary = 'Control Sonos speakers with Ruby'
13
+ gem.homepage = 'https://github.com/soffes/sonos'
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ['lib']
19
+
20
+ gem.add_dependency 'savon', '~> 2.0.2'
21
+ end
metadata ADDED
@@ -0,0 +1,76 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sonos
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Sam Soffes
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-12-23 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: savon
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: 2.0.2
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: 2.0.2
30
+ description: Sonos Controller
31
+ email:
32
+ - sam@soff.es
33
+ executables: []
34
+ extensions: []
35
+ extra_rdoc_files: []
36
+ files:
37
+ - .gitignore
38
+ - Gemfile
39
+ - LICENSE
40
+ - Rakefile
41
+ - Readme.markdown
42
+ - lib/sonos.rb
43
+ - lib/sonos/speaker.rb
44
+ - lib/sonos/version.rb
45
+ - sonos.gemspec
46
+ homepage: https://github.com/soffes/sonos
47
+ licenses: []
48
+ post_install_message:
49
+ rdoc_options: []
50
+ require_paths:
51
+ - lib
52
+ required_ruby_version: !ruby/object:Gem::Requirement
53
+ none: false
54
+ requirements:
55
+ - - ! '>='
56
+ - !ruby/object:Gem::Version
57
+ version: '0'
58
+ segments:
59
+ - 0
60
+ hash: -970140811595000206
61
+ required_rubygems_version: !ruby/object:Gem::Requirement
62
+ none: false
63
+ requirements:
64
+ - - ! '>='
65
+ - !ruby/object:Gem::Version
66
+ version: '0'
67
+ segments:
68
+ - 0
69
+ hash: -970140811595000206
70
+ requirements: []
71
+ rubyforge_project:
72
+ rubygems_version: 1.8.23
73
+ signing_key:
74
+ specification_version: 3
75
+ summary: Control Sonos speakers with Ruby
76
+ test_files: []