module Bandshell::HardwareApi

Attributes

screen_id[R]
screen_url[R]

Public Class Methods

attempt_to_get_screen_data!() click to toggle source

Can return:

:stat_badauth
:stat_err
:stat_serverr on connection or sever failure
:stat_badauth on an invalid permanent token
:stat_success when screen data retrieved.
# File lib/bandshell/hardware_api.rb, line 46
def attempt_to_get_screen_data!
  unless have_temp_token? or have_auth_token?
    request_temp_token!
  end

  unless have_auth_token?
    tt_status = check_temp_token!
    return tt_status unless tt_status == :stat_success
  end

  if have_auth_token?
    status = fetch_screen_data
    if status == :stat_badauth
      ConfigStore.write_config('auth_token','')
      request_temp_token!
    end
  elsif have_temp_token?
    status = :stat_temponly
  else
    status = :stat_err
  end
  status
end
auth_token() click to toggle source
# File lib/bandshell/hardware_api.rb, line 12
def auth_token
  Bandshell::ConfigStore.read_config('auth_token')
end
concerto_url() click to toggle source
# File lib/bandshell/hardware_api.rb, line 16
def concerto_url
  # Trailing slash required for proper URI Join behavior.
  # Double slashes not harmful.
  Bandshell::ConfigStore.read_config('concerto_url', '')+"/"
end
frontend_api_uri() click to toggle source
# File lib/bandshell/hardware_api.rb, line 26
def frontend_api_uri
  URI::join(concerto_url,'frontend.json')
end
frontend_uri() click to toggle source
# File lib/bandshell/hardware_api.rb, line 22
def frontend_uri
  URI::join(concerto_url,'frontend')
end
get_player_info() click to toggle source

Fetch player settings from concerto-hardware TODO: clean up errors/ return values

# File lib/bandshell/hardware_api.rb, line 204
def get_player_info
  return nil if auth_token.empty?

  # Try to do this in one GET.
  player_info_uri = URI::join(concerto_url,'hardware/',
                             'players/','current.json')

  response = get_with_auth(player_info_uri, 'screen', auth_token)
  if response.nil?
    return :stat_serverr
  end
    
  if response.code != "200"
    return :stat_serverr
  end
  
  begin
    data = JSON.parse(response.body)
    if data.has_key? 'screen_on_off'
      # We actually got some data
      return data
    else
      return :stat_badauth
    end
  rescue
    return :stat_serverr
  end
end
have_auth_token?() click to toggle source
# File lib/bandshell/hardware_api.rb, line 34
def have_auth_token?
  !auth_token.empty?
end
have_temp_token?() click to toggle source
# File lib/bandshell/hardware_api.rb, line 30
def have_temp_token?
  !temp_token.empty?
end
temp_token() click to toggle source
# File lib/bandshell/hardware_api.rb, line 8
def temp_token
  Bandshell::ConfigStore.read_config('auth_temp_token')
end

Private Class Methods

check_temp_token!() click to toggle source

If the temp token has been accepted, convert it into an auth token, which is saved in the config store. Returns success of the action of checking:

stat_err on generic or unknown errors
stat_serverr if the server is inaccessible or erroring
stat_success if the acceptedness was reliably determined
# File lib/bandshell/hardware_api.rb, line 169
def check_temp_token!
  return :stat_err if temp_token.empty? #should not happen

  query = URI.join(frontend_api_uri,"?screen_temp_token="+temp_token)
  begin   
    response = get_https_response(query)

    if response.code != "200"
      puts "check_temp_token: Unsuccessful request, HTTP "+response.code+"."
      return :stat_serverr
    end
    
    data=JSON.parse(response.body)
    if data.has_key? 'screen_auth_token'
      ConfigStore.write_config('auth_token',data['screen_auth_token'])
      ConfigStore.write_config('auth_temp_token','')
      return :stat_success
    elsif data.has_key? 'screen_temp_token'
      # Indicates the API was accessed successfuly but the temp token
      # has not been entered yet.
      return :stat_success
    end
    return :stat_err

  rescue StandardError => ex
    puts "check_temp_token: Failed to access concerto server:\n"+
         "   "+ex.message.chomp
    return :stat_serverr
  end     
end
clear_screen_data() click to toggle source
# File lib/bandshell/hardware_api.rb, line 72
def clear_screen_data
  @status = nil
  @screen_url = nil
  @screen_id =nil
end
fetch_screen_data() click to toggle source

Get array of data about the screen from the server This can only succeed once we have obtained a valid auth token. Returns:

:stat_serverr on connection or sever failure
:stat_badauth on an invalid permanent token
:stat_success when screen data retrieved.

TODO: save screen data in configs???

# File lib/bandshell/hardware_api.rb, line 95
def fetch_screen_data
  return nil if auth_token.empty?

  response = get_with_auth(frontend_api_uri, 'screen', auth_token)
  if response.nil?
    clear_screen_data
    return :stat_serverr
  end
    
  if response.code != "200"
    clear_screen_data
    return :stat_serverr
  end
  
  begin
    data = JSON.parse(response.body)
    if data.has_key? 'screen_id'
      @screend_id = data['screen_id']
      @screen_url = data['frontend_url']
      return :stat_success
    else
      clear_screen_data
      return :stat_badauth
    end
  rescue
    clear_screen_data
    return :stat_serverr
  end
end
get_https_response(uri, options={}) click to toggle source
# File lib/bandshell/hardware_api.rb, line 78
def get_https_response(uri, options={})
  Net::HTTP.start(uri.host, uri.port, :use_ssl => uri.scheme == 'https') do |http|
    request = Net::HTTP::Get.new uri.request_uri
    unless options[:user].nil?  && options[:pass].nil?
      request.basic_auth options[:user], options[:pass]
    end
    response = http.request request
  end
end
get_with_auth(uri, user, pass) click to toggle source
# File lib/bandshell/hardware_api.rb, line 125
def get_with_auth(uri, user, pass)
  begin       
    response = get_https_response(uri, {:user => user, :pass => pass})         
  rescue StandardError => ex
    puts "get_with_auth: Failed to access concerto server:\n"+
         "   "+ex.message.chomp
    response = nil
  end
  response
end
request_temp_token!() click to toggle source
# File lib/bandshell/hardware_api.rb, line 136
def request_temp_token!
  begin
    response = get_https_response(frontend_api_uri)
    
    if response.code != "200"
      puts "request_temp_token: Unsuccessful request, HTTP "+response.code+"."
      return false
    end

    data=JSON.parse(response.body)
    if data.has_key? 'screen_temp_token'
      # We modify the token by appending an "s".
      # Concerto allows this and concerto-hardware will use it to
      # recognize that the user is setting up a managed player in
      # addition to a simple screen.
      token = data['screen_temp_token'] + 's'
      ConfigStore.write_config('auth_temp_token',token)
      return true
    end
    return false   
  rescue StandardError => ex
    puts "request_temp_token: Failed to access concerto server:\n"+
         "   "+ex.message.chomp
    return false
  end
end