Browse files

Refined conditional logic. When I unlock the door, I want to turn on …

…the radio UNLESS there is already music or video playing, OR a video has been paused.
  • Loading branch information...
1 parent d3d3ee0 commit 049f6c2ad21dfc51babc70af8c8b08672ea99827 @ndbroadbent committed Mar 20, 2011
Showing with 29 additions and 10 deletions.
  1. +1 −1 octopus_sinatra.rb
  2. +28 −9 xbmc.rb
View
2 octopus_sinatra.rb
@@ -102,7 +102,7 @@ def xbmc_trigger(name)
# Send a trigger to xbmc server if user has any configured radio preferences.
# (and if the time is reasonable. AND if nothing is already playing on XBMC.)
time = hk_time
- if time.hour >= 7 and time.hour <= 22 and not xbmc_playing?
+ if time.hour >= 7 and time.hour <= 22 and not xbmc_playing? and not xbmc_video_paused?
if radio_prefs = YAML.load_file(relative("config/user_radio_prefs.yml"))
if stations = radio_prefs[name]
# Pick a random station, and play it.
View
37 xbmc.rb
@@ -20,16 +20,35 @@ def xbmc_api(method, params="{}", ignore_response=false)
end
def xbmc_playing?
+ xbmc_audio_playing? || xbmc_video_playing?
+ end
+
+ def xbmc_audio_playing?
players = eval(xbmc_api("Player.GetActivePlayers").body.gsub(":", "=>"))["result"]
- player = if players["audio"]
- "AudioPlayer"
- elsif players["video"]
- "VideoPlayer"
- else
- nil
- end
- return false unless player
- return !eval(xbmc_api("#{player}.State").body.gsub(":", "=>"))["result"]["paused"]
+ players["audio"] && !eval(xbmc_api("AudioPlayer.State").body.gsub(":", "=>"))["result"]["paused"]
+ end
+
+ # The following two methods are not the inverse of each other, since they are
+ # dealing with the boolean representation of three separate states.
+ # ----------------------------------------------------------------------------
+ # playing, !paused, !stopped => xbmc_video_playing?
+ # !playing, paused, stopped => !xbmc_video_playing?
+ # !playing, paused, !stopped => xbmc_video_paused?
+ # playing, !paused, stopped => !xbmc_video_paused?
+ #
+ # !playing, !paused, stopped => !xbmc_video_playing && !xbmc_video_paused
+ # ----------------------------------------------------------------------------
+ # => nothing playing, and video not paused => !xbmc_playing? && !xbmc_video_paused?
+
+ def xbmc_video_playing?
+ players = eval(xbmc_api("Player.GetActivePlayers").body.gsub(":", "=>"))["result"]
+ players["video"] && !eval(xbmc_api("VideoPlayer.State").body.gsub(":", "=>"))["result"]["paused"]
end
+ def xbmc_video_paused?
+ players = eval(xbmc_api("Player.GetActivePlayers").body.gsub(":", "=>"))["result"]
+ return false unless players["video"]
+ return eval(xbmc_api("VideoPlayer.State").body.gsub(":", "=>"))["result"]["paused"]
+ end
+
end

0 comments on commit 049f6c2

Please sign in to comment.