So basically I have these 2 curl commands that will trigger an android app on my Sony Bravia tv in order to see my front camera when someone press the ringer and than revert to another input after 10 sec…
The commands works well in shell but I couldn’t figure how to translate that appropriately in lua… I tried both with osexecute and http.request
I think there is too many bracket or something
Any idea ?
Now I am trying with another method in order to get the response to a command I send… maybe you can help me ?
I’m looking to see if my tv is on or not…
I can do this with a curl command as such : curl -v -XPOST http://XXX.XXX.XXX.XXX/sony/system -H 'X-Auth-PSK:ZZZ' -d '{"method":"getPowerStatus","params":[],"id":50, "version":"1.0"}'
Doing this curl command from a shell will return the following :
Ok… I made a step forward by reading the response_body table…
Is there any easy way to make a variable that would only have “active” or “standby” as value ?
added :
function dump(o)
if type(o) == 'table' then
local s = '{ '
for k,v in pairs(o) do
if type(k) ~= 'number' then k = '"'..k..'"' end
s = s .. '['..k..'] = ' .. dump(v) .. ','
end
return s .. '} '
else
return tostring(o)
end
end
print("Result:", dump(response_body))
I have to say that your dump() function is quite the worst piece of code I have seen for a while. Where did you get it? It does nothing for you. Throw it away.
The response body is simply an array of strings, and in your case this seems to be in JSON syntax. Use a proper decoder to get your result.
local json = require "dkjson"
local reply = json.decode (table.concat(response_body))
The reply variable is now a Lua table, from which you can directly access your required result.
print(pretty(reply))
will show you the structure (assuming you’re using AltUI.)
PS: in this case, the answer you need is in reply.result[1].status
local dst = "xxx.xxx.xxx.xxx"
local url = 'http://'.. dst ..'/sony/system'
local headername = 'X-Auth-PSK'
local headervalue = 'xxxxxxx'
local jsonbodygetstatus = '{\\"id\\":2,\\"method\\":\\"getPowerStatus\\",\\"version\\":\\"1.0\\",\\"params\\":[]}'
local runcommand = 'curl -v -H \"Content-Type:application/json\" -H \"' .. headername .. ':' .. headervalue .. '\" -d \"' .. jsonbodygetstatus .. '\" ' .. url .. ''
ping_success = os.execute('ping -c1 '.. dst)
if ping_success <= 10 then
print('Device responded')
local h=io.popen(runcommand)
local response=h:read("*a")
h:close()
if string.find(response, '{"status":"active"}') then
print('TV is active')
elseif string.find(response, '{"status":"standby"}') then
print('TV is in standby')
else
print('TV is in an unknowned state')
end
else
print ("Device didn't respond")
end
Yes, glad that works. Looks a bit convoluted, though.
Since you discovered how to do POST requests with the http module, you could probably replace your curl commands with their http equivalents and ditch the delay functions, which are a bit of a kludge.
Still, you have something which works for you, and that’s the thing.
Best Home Automation shopping experience. Shop at Ezlo!