JSON
James Sentman
james at sentman.com
Wed Jun 16 12:25:25 EDT 2021
I’m having trouble duplicating this issue. When I run this it works fine but there may be some scripting things.
First I would use the “describe appleevent” verb instead of write log, that will show you exactly what the layout of the JSONData record is. Something like this and give it a hit:
on JSONRequest( formData, JSONData)
write log “in JSON Request”
describe appleevent JSONData
end JSONRequest
on my machine the output from that looks something like:
'misc'\'DApl'{ '----':{ 'usrf':[ 'utxt'("moisture"), 90 ] }, &'subj':null(), &'csig':65536 }
and that is what I would expect. Thats a user record with one field named moisture with a value of 90. If that output looks similar then the problem is something else in the script which may be being hidden by the onerror handler. If you remove that then the actual error will get sent to the log and we can see what it’s really saying the error is. So then try something like this:
on JSONRequest(formData, JSONData)
describe appleevent JSONData
set moist to moisture of JSONData
write log “moist is “ & moist
set value of (thisUnit) to moist
write log “set value is complete"
send reply "success" return code 200 content type "text/plain"
end JSONRequest
and post the output of that in the log or the text of any errors that popup.
> On Jun 15, 2021, at 6:00 PM, KC Hundere <khundere at gmail.com> wrote:
>
> I am trying to send JSON data from a plant monitor to Xtension using the HTTP/JSON interface and the example from machomeautomation.com. Xtension responds with success, but the data does not get to the unit. Any pointers would be appriciated. I've simplified it to the two following examples:
>
> Simple python script
> import requests
> data = {"light": 97, "temperature": 28.2, "moisture": 0, "conductivity": 0, "battery": 100}
> r = requests.post("http://10.1.1.5:21234/40787C165942/plant", json={'json_payload': data})
>
> Curl from command line
> curl --header "Content-Type: application/json" --request POST --data '{"moisture": 90}' http://10.1.1.5:21234/40787C165942/plant
>
> With either one, the data does not get to the unit. With debug on I get the following in the log:
>
> Jun 15, 2021 2:31:28 PM HTTP/JSON server v1.5.6 (HTTP Server): JSON connection from: 10.1.1.72
> Jun 15, 2021 2:31:28 PM HTTP/JSON server v1.5.6 (HTTP Server): json request (/40787C165942/plant) token=(40787C165942) address=(plant) value=()
> Jun 15, 2021 2:31:28 PM
> Jun 15, 2021 2:31:28 PM
> Jun 15, 2021 2:31:28 PM JSON data did not have an event parameter.
> Jun 15, 2021 2:31:29 PM HTTP/JSON server v1.5.6 (HTTP Server): sending reply 200 - success
> Jun 15, 2021 2:31:29 PM HTTP/JSON server v1.5.6 (HTTP Server): HTTP/1.1 200 OK
> Jun 15, 2021 2:31:29 PM Date: 2021-06-15 14-31-29
> Jun 15, 2021 2:31:29 PM Server: XTensionJSON/19
> Jun 15, 2021 2:31:29 PM Cache-Control: no-cache
> Jun 15, 2021 2:31:29 PM Connection: close
> Jun 15, 2021 2:31:29 PM Content-Length: 7
> Jun 15, 2021 2:31:29 PM Content-type: text/plain
> Jun 15, 2021 2:31:29 PM Content-Language: en
> Jun 15, 2021 2:31:29 PM HTTP/JSON server v1.5.6 (HTTP Server): SendComplete, closing the connection
>
> On script for the unit "plant"
> on JSONRequest(formData, JSONData)
> write log JSONData
> write log formData
> try
> set moist to moisture of JSONData
> set vaule of (thisUnit) to moist
> write log "moisture: " & moist
>
> on error
>
> write log "JSON data did not have an event parameter." color red
>
> end try
>
> send reply "success" return code 200 content type "text/plain"
>
> end JSONRequest
>
> --
> KC Hundere
> _______________________________________________
> XTensionList mailing list
> XTensionList at machomeautomation.com
> http://mail.machomeautomation.com/mailman/listinfo/xtensionlist
Thanks,
James
James Sentman http://www.PlanetaryGear.org http://MacHomeAutomation.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.machomeautomation.com/pipermail/xtensionlist/attachments/20210616/33c2410f/attachment.html>
More information about the XTensionList
mailing list