Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added xbmc hooks for octopus / web triggers

  • Loading branch information...
commit a297951664a82a57b236d9026169d6efd5045191 1 parent 89d4cca
@ndbroadbent authored
Showing with 44 additions and 16 deletions.
  1. +9 −16 octopus_sinatra.rb
  2. +35 −0 xbmc.rb
View
25 octopus_sinatra.rb
@@ -6,8 +6,10 @@ def relative(filename)
require 'rubygems'
require 'rubyk8055'
-require relative('dsp420.rb')
+require relative('dsp420')
include USB
+require relative('xbmc')
+include XBMC
require 'sinatra'
require 'yaml'
@@ -98,26 +100,17 @@ def hk_time_fmt
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 the time is reasonable. AND if nothing is already playing on XBMC.)
time = hk_time
- if time.hour >= 7 and time.hour <= 22
+ if time.hour >= 7 and time.hour <= 22 and not xbmc_playing?
if radio_prefs = YAML.load_file(relative("config/user_radio_prefs.yml"))
if stations = radio_prefs[name]
# Pick a random station, and play it.
station = stations[rand(stations.size)]
- url = URI.parse($config["xbmc_url"])
- req = Net::HTTP::Post.new(url.path)
- req.basic_auth $config["xbmc_username"], $config["xbmc_password"]
- req.add_field 'Content-Type', 'application/json'
- req.body = '{"method":"XBMC.Play","params":{"file":"#{station}"},"id":1,"jsonrpc":"2.0"}'
- begin
- res = Net::HTTP.new(url.host, url.port).start do |http|
- # Don't need to stick around for the response.
- http.read_timeout = 2
- http.request(req)
- end
- rescue
- end
+ # Set volume to 45
+ xbmc_api("XBMC.SetVolume", '"number":"45"')
+ # Play the station.
+ xbmc_api("XBMC.Play", %Q{"file":"#{station.gsub(" ", "%20")}"}, :ignore)
end
end
end
View
35 xbmc.rb
@@ -0,0 +1,35 @@
+module XBMC
+ def xbmc_api(method, params, ignore_response=false)
+ url = URI.parse($config["xbmc_url"])
+ req = Net::HTTP::Post.new(url.path)
+ req.basic_auth $config["xbmc_username"], $config["xbmc_password"]
+ req.add_field 'Content-Type', 'application/json'
+ req.body = %Q[{"method":"#{method}","params":{#{params}},"id":1,"jsonrpc":"2.0"}]
+ begin
+ # fetch ip from system nslookup. ruby seems to hang a lot on DNS lookups.
+ if ip_addr = `nslookup #{url.host}`[Regexp.new('Address 1: (\d+\.\d+\.\d+\.\d+) ' << url.host), 1]
+ res = Net::HTTP.new(ip_addr, url.port).start do |http|
+ # Don't need to stick around for the response.
+ http.read_timeout = 2 if ignore_response
+ http.request(req)
+ end
+ end
+ rescue Timeout::Error
+ end
+ return ignore_response ? true : res
+ end
+
+ def xbmc_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"]
+ end
+end
+
Please sign in to comment.
Something went wrong with that request. Please try again.