Class: SamsungWamApi::Device

Inherits:
Object
  • Object
show all
Defined in:
lib/samsung_wam_api/device.rb

Instance Method Summary collapse

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_infoObject



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_infoObject



96
97
98
# File 'lib/samsung_wam_api/device.rb', line 96

def cloud_provider_info
  command!('<name>GetCpInfo</name>', 'CPM')
end

#cloud_usernameObject



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

#inputObject



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_statusObject



63
64
65
# File 'lib/samsung_wam_api/device.rb', line 63

def mute_status
  command!('<name>GetMute</name>')['mute']
end

#muted?Boolean

Returns:

  • (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

Returns:

  • (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

Returns:

  • (Boolean)


31
32
33
# File 'lib/samsung_wam_api/device.rb', line 31

def on?
  power_status == 1
end

#play_infoObject



105
106
107
108
# File 'lib/samsung_wam_api/device.rb', line 105

def play_info
  info = cloud_provider_info
  info['playstatus']
end

#power_statusObject

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

#volumeObject



47
48
49
# File 'lib/samsung_wam_api/device.rb', line 47

def volume
  command!('<name>GetVolume</name>')['volume'].to_i
end