Page 1 of 1
JSON API addition
Posted: Wednesday 14 October 2020 15:07
by lost
Hello,
I'm quite sure I already done this suggestion a few years ago, but looks there was no addition in the wiki about this:
From HTTP/JSON API, we can change a switch... but we can't retrieve it's current on/of/level position:
In the past, I already had to deal with this and today again. A few years ago, this ended with a device script that copies switch current status to a user var... and as getuservariables is implemented (not only set/command side, as for switches) I was able to get switch status from external python services needing this data.
For API coherence and avoiding such dirty hacks, would be nice to add an API for this: A "switchget" on top of "switchcmd" for instance.
If there is already a way to get this easily (at least easier than current hack), please let me know...
Re: JSON API addition
Posted: Wednesday 14 October 2020 15:15
by boum
Have you tried:
Code: Select all
http://DZ/json.htm?type=devices&rid=IDX
?
Re: JSON API addition
Posted: Wednesday 14 October 2020 15:31
by lost
boum wrote: ↑Wednesday 14 October 2020 15:15
Have you tried:
Code: Select all
http://DZ/json.htm?type=devices&rid=IDX
?
I just did: This may do the job, must agree, but needs quite heavy JSON parsing as this returns full switch data (mostly linked to Domoticz internals) and even more (you also get sunset/sunrise... as well).
This does not really compare with user variables counterpart that just returns useful data:
Code: Select all
/json.htm?type=command¶m=getuservariable&idx=IDX
If there is no other solution that may not have made it's way in the wiki, I'll use your suggestion indeed. Just hope JSON response will not change in the future. This may be made easier IMO but at least removes external dirty dependencies difficult to maintain over time!
Re: JSON API addition
Posted: Wednesday 14 October 2020 16:34
by lost
Looks this works, added a get switch to a module that interacts with Domoticz:
Code: Select all
dmtJsonGetSwitch = {"type":"devices", "rid":999}
# Get domoticz switch status, returns sys.maxint if error
def dmtGetSwitch(url, idx, name, logger):
"""
Get Domoticz switch status, returns -1 if idx/name mismatch, -2 if connection fails...
"""
ret = -1
dmtJsonGetSwitch['rid'] = idx
try:
# Connect to Domoticz via JSON API and send data
dmtRget = requests.get(url, params=dmtJsonGetSwitch)
except requests.exceptions.RequestException as dmtErr:
logger.log(logging.ERROR, "Unable to connect with URL=%s \nGet requests error %s" % (url, dmtErr))
ret = -2
return ret
#logger.log(logging.DEBUG, "Sent data: [%s]" % (dmtRget.url))
#logger.log(logging.DEBUG, "dmtGetSwitch(idx=%d <=> %s) server response :\n%s" % (idx, name, dmtRget.text))
# Check var/idx coherence to validate domoticz response
jsonRget = json.loads(dmtRget.text)
if jsonRget["result"][0]["Name"] != name:
logger.log(logging.INFO, "dmtGetSwitch(idx=%d <=> %s != %s) MISMATCH" % (idx, name, jsonRget["result"][0]["Name"]))
return sys.maxint
ret = jsonRget["result"][0]["Data"]
#logger.log(logging.DEBUG, "dmtJsonGetSwitch(%s) = %s" % (name, ret))
# Looks OK, return domoticz switch data...
return ret
With a few imports (logging/json/requests...) this may be called like this from some code that already setup logger after setting right IDX/SWITCH_NAME (coherency checked by get):
Code: Select all
domoticzJurl='http://127.0.0.1:8080/json.htm?'
print "switch IDX/SWITCH_NAME status:"
status = dmtGetSwitch(domoticzJurl, IDX, 'SWITCH_NAME', logger)
print status
Thanks for the suggestion, I did not noticed this one when I 1st checked for something appropriate!
Re: JSON API addition
Posted: Wednesday 14 October 2020 21:55
by kiddigital
Don't forget to check the 'status' field in the JSON payload. It should state 'OK' to confirm that everything went ok. (So field '["status"]' in your code)
Maybe also even check that you received a valid HTTP response 200 code and not something else.
These checks should be done to see if the request/response went well. Than you can check for '["result"][0]["Name"]'

Re: JSON API addition
Posted: Thursday 15 October 2020 8:00
by lost
kiddigital wrote: ↑Wednesday 14 October 2020 21:55
Don't forget to check the 'status' field in the JSON payload. It should state 'OK' to confirm that everything went ok. (So field '["status"]' in your code)
Maybe also even check that you received a valid HTTP response 200 code and not something else.
These checks should be done to see if the request/response went well. Than you can check for '["result"][0]["Name"]'
OK, I'll add these checks after pure connection issue errors and before name/idx coherence check (that was more targeted for a dummy read at services using these functions init, to check for obvious errors in the yaml configuration file)... In this function and others using the JSON API as well. Some verify security status, read/write user variables linked to alarm system... Better being reliable!
For now, when connection was OK (domoticz http server sometimes hanged in the past with domoticz core still running so no systemd auto restart: This can last for long) I did not noticed issues with HTTP API or external signs of misbehaving. But future may show others issues.
Re: JSON API addition
Posted: Monday 14 December 2020 10:21
by Hirthe
Hi, someone can tell me how can i get a JSON from a subreddit?
I made it, its working with /top.json
But can someone tell me, how can i use it with like all.json or something?
i used &limit=100, but this is the maximum. i want more than 100
mygroundbiz