Class: SamsungWamApi::Device
- Inherits:
-
Object
- Object
- SamsungWamApi::Device
- Defined in:
- lib/samsung_wam_api/device.rb
Instance Method Summary collapse
- #audio_info ⇒ Object
- #cloud_provider_info ⇒ Object
- #cloud_username ⇒ Object
- #command!(cmd, endpoint = nil) ⇒ Object
- #decrease_volume(step = 1) ⇒ Object
- #increase_volume(step = 1) ⇒ Object
-
#initialize(ip:, port: '55001', endpoint: 'UIC', logger: Logger.new(STDOUT), timeout_seconds: 3) ⇒ Device
constructor
A new instance of Device.
- #input ⇒ Object
- #mute! ⇒ Object
- #mute_status ⇒ Object
- #muted? ⇒ Boolean
- #off! ⇒ Object
- #off? ⇒ Boolean
- #on! ⇒ Object
- #on? ⇒ Boolean
- #play_info ⇒ Object
-
#power_status ⇒ Object
returns ‘1’ (on) or ‘0’ (off).
- #set_input!(input) ⇒ Object
- #set_volume(vol) ⇒ Object
- #toggle_mute! ⇒ Object
- #unmute! ⇒ Object
- #volume ⇒ Object
Constructor Details
#initialize(ip:, port: '55001', endpoint: 'UIC', logger: Logger.new(STDOUT), timeout_seconds: 3) ⇒ Device
Returns a new instance of Device.
18 19 20 21 22 23 24 |
# File 'lib/samsung_wam_api/device.rb', line 18 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 |
Instance Method Details
#audio_info ⇒ Object
100 101 102 103 |
# File 'lib/samsung_wam_api/device.rb', line 100 def audio_info info = cloud_provider_info info['audioinfo'] end |
#cloud_provider_info ⇒ Object
96 97 98 |
# File 'lib/samsung_wam_api/device.rb', line 96 def cloud_provider_info command!('<name>GetCpInfo</name>', 'CPM') end |
#cloud_username ⇒ Object
110 111 112 113 |
# File 'lib/samsung_wam_api/device.rb', line 110 def cloud_username info = cloud_provider_info info['username'] end |
#command!(cmd, endpoint = nil) ⇒ Object
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/samsung_wam_api/device.rb', line 115 def command!(cmd, endpoint = nil) endpoint ||= @endpoint query = "http://#{@ip}:#{@port}/#{endpoint}?cmd=#{URI::Parser.new.escape(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) ⇒ Object
59 60 61 |
# File 'lib/samsung_wam_api/device.rb', line 59 def decrease_volume(step = 1) set_volume(volume - step.to_i) end |
#increase_volume(step = 1) ⇒ Object
55 56 57 |
# File 'lib/samsung_wam_api/device.rb', line 55 def increase_volume(step = 1) set_volume(volume + step.to_i) end |
#input ⇒ Object
83 84 85 |
# File 'lib/samsung_wam_api/device.rb', line 83 def input command!('<name>GetFunc</name>')['function'] end |
#mute! ⇒ Object
71 72 73 |
# File 'lib/samsung_wam_api/device.rb', line 71 def mute! command!('<name>SetMute</name><p type="str" name="mute" val="on"/>') end |
#mute_status ⇒ Object
63 64 65 |
# File 'lib/samsung_wam_api/device.rb', line 63 def mute_status command!('<name>GetMute</name>')['mute'] end |
#muted? ⇒ Boolean
67 68 69 |
# File 'lib/samsung_wam_api/device.rb', line 67 def muted? mute_status == 'on' end |
#off! ⇒ Object
43 44 45 |
# File 'lib/samsung_wam_api/device.rb', line 43 def off! command!('<name>SetPowerStatus</name><p type="dec" name="powerstatus" val="0"/>') end |
#off? ⇒ Boolean
35 36 37 |
# File 'lib/samsung_wam_api/device.rb', line 35 def off? power_status == 0 end |
#on! ⇒ Object
39 40 41 |
# File 'lib/samsung_wam_api/device.rb', line 39 def on! command!('<name>SetPowerStatus</name><p type="dec" name="powerstatus" val="1"/>') end |
#on? ⇒ Boolean
31 32 33 |
# File 'lib/samsung_wam_api/device.rb', line 31 def on? power_status == 1 end |
#play_info ⇒ Object
105 106 107 108 |
# File 'lib/samsung_wam_api/device.rb', line 105 def play_info info = cloud_provider_info info['playstatus'] end |
#power_status ⇒ Object
returns ‘1’ (on) or ‘0’ (off)
27 28 29 |
# File 'lib/samsung_wam_api/device.rb', line 27 def power_status command!('<name>GetPowerStatus</name>')['powerStatus'].to_i end |
#set_input!(input) ⇒ Object
87 88 89 90 91 92 93 94 |
# File 'lib/samsung_wam_api/device.rb', line 87 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) ⇒ Object
51 52 53 |
# File 'lib/samsung_wam_api/device.rb', line 51 def set_volume(vol) command!('<name>SetVolume</name><p type="dec" name="Volume" val="' + vol.to_i.to_s + '"/>') end |
#toggle_mute! ⇒ Object
79 80 81 |
# File 'lib/samsung_wam_api/device.rb', line 79 def toggle_mute! muted? ? unmute! : mute! end |
#unmute! ⇒ Object
75 76 77 |
# File 'lib/samsung_wam_api/device.rb', line 75 def unmute! command!('<name>SetMute</name><p type="str" name="mute" val="off"/>') end |
#volume ⇒ Object
47 48 49 |
# File 'lib/samsung_wam_api/device.rb', line 47 def volume command!('<name>GetVolume</name>')['volume'].to_i end |