Skip to content

Commit 33182c0

Browse files
author
Aaron Gotwalt
committed
Working. Covers its tracks
1 parent c2a8d6b commit 33182c0

File tree

4 files changed

+40
-19
lines changed

4 files changed

+40
-19
lines changed

Gemfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
source "http://rubygems.org"
22

33
ruby '2.0.0'
4-
54
gem 'sonos'
65
gem 'rack'
6+
gem 'addressable'
77
gem 'celluloid'
88
gem 'timers'
99
gem 'thor'

Gemfile.lock

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
GEM
22
remote: http://rubygems.org/
33
specs:
4+
addressable (2.3.5)
45
akami (1.2.2)
56
gyoku (>= 0.4.0)
67
nokogiri
78
builder (3.2.2)
8-
celluloid (0.15.2)
9-
timers (~> 1.1.0)
9+
celluloid (0.14.1)
10+
timers (>= 1.0.0)
1011
gyoku (1.1.1)
1112
builder (>= 2.1.2)
13+
hitimes (1.2.1)
1214
httpclient (2.3.4.1)
1315
httpi (2.1.0)
1416
rack
@@ -38,7 +40,8 @@ GEM
3840
thor
3941
systemu (2.6.4)
4042
thor (0.19.1)
41-
timers (1.1.0)
43+
timers (2.0.0)
44+
hitimes
4245
uuid (2.3.7)
4346
macaddr (~> 1.0)
4447
wasabi (3.3.0)
@@ -50,7 +53,9 @@ PLATFORMS
5053
ruby
5154

5255
DEPENDENCIES
56+
addressable
5357
celluloid
5458
rack
5559
sonos
5660
thor
61+
timers

lib/ghosty/cli.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,13 @@ def start
1616
web.async.start
1717

1818
# Wait for the web to start
19-
sleep 2
19+
sleep 1
2020

2121
player = Ghosty::Player.new(base_uri, assets_directory)
2222
player.async.perform
2323

2424
# Final sleep is to keep things alive
25-
sleep
25+
sleep 20
2626
end
2727

2828
end

lib/ghosty/player.rb

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,31 +17,40 @@ def perform
1717
speaker = random_speaker
1818
file = ghost_url
1919

20+
is_paused = ['PAUSED_PLAYBACK', 'STOPPED'].include?(speaker.get_player_state[:state])
21+
22+
speaker.pause unless is_paused
23+
2024
previous = speaker.now_playing
21-
previous_uri = Addressable::URI.parse(previous[:uri])
22-
previous_uri.query = nil
2325

2426
puts "Playing #{file} on #{speaker.name}"
25-
speaker.play(file)
26-
track_info = speaker.now_playing
27+
speaker.play file
28+
2729
speaker.play
2830

29-
# Determine the track length, add a few extra seconds for padding
30-
duration = Time.parse(track_info[:track_duration]).sec + 2
31-
puts "Sleeping for #{duration}s"
32-
sleep 0 # duration
31+
until ['PAUSED_PLAYBACK', 'STOPPED'].include?(speaker.get_player_state[:state])
32+
sleep 0.1
33+
end
3334

34-
puts "Resetting speaker source to #{previous_uri}"
35-
# reset the current track
36-
speaker.play(previous_uri.to_s)
37-
speaker.play
35+
puts "Resetting speaker source"
36+
37+
# the sonos app does this. I think it tells the player to think of the master queue as active again
38+
speaker.play speaker.uid.gsub('uuid', 'x-rincon-queue') + '#0'
39+
40+
if previous
41+
speaker.select_track previous[:queue_position]
42+
speaker.seek Time.parse("1/1/1970 #{previous[:current_position]} -0000" ).sec
43+
end
44+
45+
speaker.play unless is_paused
3846
end
3947

4048
private
4149

50+
4251
def random_speaker
4352
@system.speakers.map do |speaker|
44-
speaker #speaker.get_player_state[:state] == 'PAUSED_PLAYBACK'
53+
speaker if speaker.name == 'Office' #speaker.get_player_state[:state] == 'PAUSED_PLAYBACK'
4554
end.compact.sample
4655
end
4756

@@ -58,3 +67,10 @@ def ghost_url
5867

5968
end
6069
end
70+
71+
module Sonos::Endpoint::AVTransport
72+
def select_track(index)
73+
parse_response send_transport_message('Seek', "<Unit>TRACK_NR</Unit><Target>#{index}</Target>")
74+
end
75+
end
76+

0 commit comments

Comments
 (0)