lib/radiodan/player.rb in radiodan-0.0.1 vs lib/radiodan/player.rb in radiodan-0.0.2
- old
+ new
@@ -1,53 +1,67 @@
-require 'radiodan/event_binding'
+require 'logging'
+require 'event_binding'
+require 'playlist_sync'
class Radiodan
class Player
include Logging
include EventBinding
- attr_reader :adapter, :state
+ attr_reader :adapter, :playlist
- def initialize
- @state = State.new(:playback => 'stopped')
- end
-
def adapter=(adapter)
@adapter = adapter
@adapter.player = self
end
def adapter?
!adapter.nil?
end
- def state=(new_state)
- @state = new_state
- trigger_event(:state, @state)
+ def playlist=(new_playlist)
+ @playlist = new_playlist
+ trigger_event(:playlist, @playlist)
+ # run sync to explicitly conform to new playlist?
- @state
+ @playlist
end
=begin
Sync checks the current status of the player.
Is it paused? Playing? What is it playing?
It compares the expected to actual statuses and
makes changes required to keep them the same.
=end
def sync
- current_state = adapter.state
- expected_state = state
+ return false unless adapter?
- # playlist
- unless expected_state.content.files.include?(current_state.file)
- logger.debug "Expected: #{expected_state.content.files.first} Got: #{current_state.file}"
- trigger_event :playlist, expected_state.content
- end
+ current = adapter.playlist
+ expected = playlist
- # playback state
- unless expected_state.playback == current_state.state
- logger.debug "Expected: #{expected_state.playback} Got: #{current_state.state}"
- trigger_event expected_state.playback
+ state = Radiodan::PlaylistSync.new expected, current
+
+ if state.sync?
+ true
+ else
+ # playback state
+ if state.errors.include? :state
+ logger.debug "Expected: #{expected.state} Got: #{current.state}"
+ trigger_event :play_state, expected.state
+ end
+
+ if state.errors.include? :mode
+ logger.debug "Expected: #{expected.mode} Got: #{current.mode}"
+ trigger_event :play_mode, expected.mode
+ end
+
+ # playlist
+ if state.errors.include? :playlist
+ logger.debug "Expected: #{expected.current} Got: #{current.current}"
+ trigger_event :playlist, expected
+ end
+
+ false
end
end
end
end