class SamsungWamApi::Device

Public Class Methods

new(ip:, port: '55001', endpoint: 'UIC', logger: Logger.new(STDOUT), timeout_seconds: 3) click to toggle source
# File lib/samsung_wam_api/device.rb, line 17
def initialize(ip:, port: '55001', endpoint: 'UIC', logger: Logger.new(STDOUT), timeout_seconds: 3)
  @ip = ip
  @port = port
  @endpoint = endpoint
  @logger = logger
  @timeout_seconds = timeout_seconds
end

Public Instance Methods

audio_info() click to toggle source
# File lib/samsung_wam_api/device.rb, line 99
def audio_info
  info = cloud_provider_info
  info['audioinfo']
end
cloud_provider_info() click to toggle source
# File lib/samsung_wam_api/device.rb, line 95
def cloud_provider_info
  command!('<name>GetCpInfo</name>', 'CPM')
end
cloud_username() click to toggle source
# File lib/samsung_wam_api/device.rb, line 109
def cloud_username
  info = cloud_provider_info
  info['username']
end
command!(cmd, endpoint = nil) click to toggle source
# File lib/samsung_wam_api/device.rb, line 114
def command!(cmd, endpoint = nil)
  endpoint ||= @endpoint
  query = "http://#{@ip}:#{@port}/#{endpoint}?cmd=#{URI.encode(cmd)}"
  @logger.debug { "Firing query '#{URI.decode(query)}'" }

  # if we e.g. request to power on device which is already on, we never receive a response, therefore we use configurable timeout
  raw_response = nil
  Timeout::timeout(@timeout_seconds) {
    raw_response = Net::HTTP.get(URI(query))
  }
  parsed_response = Hash.from_xml(raw_response)
  @logger.debug { "Got response:\n #{raw_response.inspect} \nparsed to:\n#{parsed_response}" }
  parsed_response[endpoint]['response']
rescue Timeout::Error
  @logger.warn "Timeout and #{@timeout_seconds} seconds, you most likely did invalid operation"
end
decrease_volume(step = 1) click to toggle source
# File lib/samsung_wam_api/device.rb, line 58
def decrease_volume(step = 1)
  set_volume(volume - step.to_i)
end
increase_volume(step = 1) click to toggle source
# File lib/samsung_wam_api/device.rb, line 54
def increase_volume(step = 1)
  set_volume(volume + step.to_i)
end
input() click to toggle source
# File lib/samsung_wam_api/device.rb, line 82
def input
  command!('<name>GetFunc</name>')['function']
end
mute!() click to toggle source
# File lib/samsung_wam_api/device.rb, line 70
def mute!
  command!('<name>SetMute</name><p type="str" name="mute" val="on"/>')
end
mute_status() click to toggle source
# File lib/samsung_wam_api/device.rb, line 62
def mute_status
  command!('<name>GetMute</name>')['mute']
end
muted?() click to toggle source
# File lib/samsung_wam_api/device.rb, line 66
def muted?
  mute_status == 'on'
end
off!() click to toggle source
# File lib/samsung_wam_api/device.rb, line 42
def off!
  command!('<name>SetPowerStatus</name><p type="dec" name="powerstatus" val="0"/>')
end
off?() click to toggle source
# File lib/samsung_wam_api/device.rb, line 34
def off?
  power_status == 0
end
on!() click to toggle source
# File lib/samsung_wam_api/device.rb, line 38
def on!
  command!('<name>SetPowerStatus</name><p type="dec" name="powerstatus" val="1"/>')
end
on?() click to toggle source
# File lib/samsung_wam_api/device.rb, line 30
def on?
  power_status == 1
end
play_info() click to toggle source
# File lib/samsung_wam_api/device.rb, line 104
def play_info
  info = cloud_provider_info
  info['playstatus']
end
power_status() click to toggle source

returns '1' (on) or '0' (off)

# File lib/samsung_wam_api/device.rb, line 26
def power_status
  command!('<name>GetPowerStatus</name>')['powerStatus'].to_i
end
set_input!(input) click to toggle source
# File lib/samsung_wam_api/device.rb, line 86
def set_input!(input)
  unless INPUTS.keys.include?(input)
    @logger.error "Unsupported input #{input}, ignoring and failing"
    return false
  end

  command!('<name>SetFunc</name><p type="str" name="function" val="' + input + '"/>')
end
set_volume(vol) click to toggle source
# File lib/samsung_wam_api/device.rb, line 50
def set_volume(vol)
  command!('<name>SetVolume</name><p type="dec" name="Volume" val="' + vol.to_i.to_s + '"/>')
end
toggle_mute!() click to toggle source
# File lib/samsung_wam_api/device.rb, line 78
def toggle_mute!
  muted? ? unmute! : mute!
end
unmute!() click to toggle source
# File lib/samsung_wam_api/device.rb, line 74
def unmute!
  command!('<name>SetMute</name><p type="str" name="mute" val="off"/>')
end
volume() click to toggle source
# File lib/samsung_wam_api/device.rb, line 46
def volume
  command!('<name>GetVolume</name>')['volume'].to_i
end