26.06.2016, 21:08
(This post was last modified: 26.06.2016, 22:19 by Erwin van der Zwart.)
Hi David,
I had some spare time and added it to the function:
BR,
Erwin van der Zwart
I had some spare time and added it to the function:
Code:
-- Set Sonos IP address
sonos_ipaddress = '192.168.10.31'
-- Function to send commands to Sonos
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
media = 'MediaRenderer'
service = 'RenderingControl'
elseif cmd == 'Browse' then
media = 'MediaServer'
service = 'ContentDirectory'
else
media = '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 /' .. media .. '/' .. 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
-- Get what's in the queue
function getQueueContent(ip, port)
BrowseConfig = '<ObjectID>Q:0</ObjectID>' ..
'<BrowseFlag>BrowseDirectChildren</BrowseFlag>' ..
'<Filter>*</Filter>' ..
'<StartingIndex>0</StartingIndex>' ..
'<RequestedCount>0</RequestedCount>' ..
'<SortCriteria>*</SortCriteria>'
result,err = upnpavcmd(ip, port, 'Browse', BrowseConfig)
return result,err
end
-- Command to get queue content and count (<NumberReturned>)
queuecontent = getQueueContent(sonos_ipaddress, 1400)
for i in string.gmatch(queuecontent, '<NumberReturned.-</NumberReturned>') do
if i ~= nil then
queuecount = i:match([[<NumberReturned>(.-)</NumberReturned>]])
if queuecount then
queuecount = tonumber(queuecount)
end
end
end
-- Do action(s) based on number of items inside queue
if queuecount > 0 then
-- Put here your normal play action
else
-- Put here your URI play action
end
BR,
Erwin van der Zwart