Page 1 of 1

Using domoticz.openURL()

Posted: Wednesday 09 August 2017 18:06
by Olgmo
Hi
I am trying to figure out how to use the openurl() command. How is this suppose to work?

I want to fetch a status for a wifi device by this command (http://10.0.0.99/json?cmd=status) This is not the Domoticz server.
When doing this in a browser i get:
"{"successful": true, "name": "430x", "status": {"status": 2, "stopped": false, "duration": 592, "mode": 0, "battery": 55, "hours": 773}, "timer": {"status": 1}, "wlan": {"signal": -70}}"

To debug i´ve tried this:

Code: Select all

        
        local url = 'http://10.0.0.99/json?cmd=status'
        local urlReply = domoticz.openURL(url)
        domoticz.log("Status URL" .. urlReply)
But I get this errors.
"attempt to concatenate local 'urlReply' (a nil value)"
"Error opening url: http://10.0.0.99/xml?cmd=status"

Am I trying the impossible here?
(running on Raspi3 and latest beta)

Re: Using domoticz.openURL()

Posted: Wednesday 09 August 2017 23:12
by mivo
Hi,

dzVents is probably using OpenURL via LUA commandArray, so it returns no data. Found this on Wiki:
Using 'OpenURL' it is possible to trigger a url/api based on an event. The url will not retrieve/parse any data, use it for one way communication.
https://www.domoticz.com/wiki/Events

Re: Using domoticz.openURL()

Posted: Thursday 10 August 2017 0:23
by randytsuch
When I used openURL, seems like it queues it up, and the command is executed after exiting from the dzvents script.
So no way it could work.

Wonder if you tried something like this?
os.execute('curl "http:..." ')

I think this is executed in the script, but I haven't tried to get the results back from the http call. I've just used it to send commands.

Re: Using domoticz.openURL()

Posted: Thursday 10 August 2017 9:02
by Olgmo
Okay,
I have s normal LUA scripts that work with curl, but wanted to to try out this new script

Re: Using domoticz.openURL()

Posted: Thursday 10 August 2017 9:29
by zicht
You can do this .... but different with curl and io.popen ( i am not yet on dzevents)

first create a function :

Code: Select all

function os.capture(cmd, raw)				-- os.command uitvoeren en resultaat daarvan lezen in string
  local f = assert(io.popen(cmd, 'r'))
  local s = assert(f:read('*a'))
  f:close()
  if raw then return s end
  s = string.gsub(s, '^%s+', '')
  s = string.gsub(s, '%s+$', '')
  s = string.gsub(s, '[\n\r]+', ' ')
  --
	if s~='{"ok":true,"result":[]}' and string.find(s,'"first_name":"msledgem_bot"')==nil then 
		--WriteToFile(cmd,"capture") 
		--WriteToFile("Resp: "..s,"capture") 
    end
  return s
end
then you can call this like (=just an example i use in my script)

Code: Select all

url = 'http://gadgets.buienradar.nl/data/raintext/ -F lat='..lat..' -F lon='..lon  
content = os.capture("Curl "..url)
"content" will hold the result you are seeking for.

Re: Using domoticz.openURL()

Posted: Thursday 10 August 2017 13:02
by BakSeeDaa
Just be careful so you don't stall the Domoticz eventsystem while waiting for that http response... There might be all kind of delays on the Internet and a few seconds waiting is a long time for the event system.

domoticz.openURL(url) won't wait for a response and I believe that's the preferred way to do it. But there is obviously no response code.