30.06.2020, 11:54
Hi,
This is the most cleaned methode to do what you need:
BR,
Erwin
This is the most cleaned methode to do what you need:
Code:
-- Function to execute Sonos requests
function upnpavcmd(host, port, cmd, param)
local client, soap, reqs, service, res, err
require('socket')
client = socket.tcp()
client:settimeout(3)
-- try connecting to upnp endpoint
res, err = client:connect(host, port)
if not res then
return nil, err
end
-- guess service name based on command
if cmd == 'SetVolume' or
cmd == 'GetVolume' or
cmd == 'SetMute' or
cmd == 'GetMute' or
cmd == 'SetBass' or
cmd == 'GetBass' or
cmd == 'SetTreble' or
cmd == 'GetTreble' or
cmd == 'SetLoudness' or
cmd == 'GetLoudness' then
base = 'MediaRenderer'
service = 'RenderingControl'
elseif cmd == 'Browse' then
base = 'MediaServer'
service = 'ContentDirectory'
else
base = 'MediaRenderer'
service = 'AVTransport'
end
-- soap envelope
soap = '<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">' ..
'<s:Body>' ..
'<u:' .. cmd .. ' xmlns:u="urn:schemas-upnp-org:service:' .. service .. ':1">' ..
'<InstanceID>0</InstanceID>' ..
(param or '') ..
'</u:' .. cmd .. '>' ..
'</s:Body>' ..
'</s:Envelope>'
-- http request
reqs = 'POST /' .. base .. '/' .. service .. '/Control HTTP/1.1\r\n' ..
'CONNECTION: close\r\n' ..
'HOST: ' .. host .. ':' .. port .. '\r\n' ..
'CONTENT-LENGTH: ' .. soap:len() .. '\r\n' ..
'CONTENT-TYPE: text/xml; charset="utf-8"\r\n' ..
'SOAPACTION: "urn:schemas-upnp-org:service:' .. service .. ':1#' .. cmd .. '"\r\n' ..
'\r\n' .. soap
-- send http request
res, err = client:send(reqs)
if not res then
return nil, err
end
-- get reply and close connection
res, err = client:receive('*a')
client:close()
return res, err
end
if event.getvalue() == true then
upnpavcmd('192.168.10.181', 1400, 'SetVolume', '<Channel>Master</Channel><DesiredVolume>30</DesiredVolume>')
upnpavcmd('192.168.10.181', 1400, 'SetAVTransportURI', '<CurrentURI>x-rincon-mp3radio://http://192.168.0.10/user/deurbel.mp3</CurrentURI><CurrentURIMetaData/>')
upnpavcmd('192.168.10.181', 1400, 'Play', '<Speed>1</Speed>')
else
upnpavcmd('192.168.10.181', 1400, 'Pause')
end
Erwin