Setting up JSON data source and point
-
Hi all,
I am trying to set up a JSON receiver data source but cant seem to get it working. I have a GSM router with a modbus gateway that can poll modbus devices and send the data by HTTP/JSON. I have fairly limited experience with this so could anyone help with the setting up of the JSON receiver?
Here is the router setup.
Any help would be greatly appreciated.Thanks,
Rob -
Hi Rob,
Can you post what a JSON message from it would look like, and what you're trying to parse out of it?
From what I see in your screenshot, it a point may look something like,
This thread may be helpful: https://forum.infiniteautomation.com/topic/4057/how-to-set-a-value-using-http/9
-
Hi Phil, thanks for the reply.
I have received the JSON data using a test application and it looks like this:
POST / HTTP/1.1
Host: 192.168.1.113:1883
Accept: /
Content-Length: 64
Content-Type: application/x-www-form-urlencoded[{"ID":"2", "TS":"1566412285","ST":"71","VR":"[66,71,183,224]"}]POST / HTTP/1.1
Host: 192.168.1.113:1883
Accept: /
Content-Length: 63
Content-Type: application/x-www-form-urlencoded[{"ID":"2", "TS":"1566412286","ST":"1","VR":"[67,110,14,168]"}]
The data i want are the 2 numerical values - 66,71,183,224 and 67,110,14,168 - Frequency and Voltage values.
It also took me a while to figure out the data format. Its a 32Bit float but each 8 bits is in decimal. I have gotten the correct values by converting each 8 bits to binary and inputting the result into a IEE754 converter. Can this be done with the data in mango?As i said I have the device sending the data to the test app, running on the same pc as mango, but when i change the url to 192.168.1.113/httpds and use the listen function on the data source setup menu I don't get anything back.
Thanks again Phil.
-
Do you have the ability to set the Content-Type header to
application/json
? Otherwise you'll need to do something like catch the whole message with an HttpReceiver and parse the JSON / do the locating of points from within scripts. Only POSTs with content types containingapplication/json
are passed to the JSON receiver data sources.Edit: This may not be so bad, if you'll need to do bit-banging to get the numeric value anyway (which it sounds like would be needed by that number encoding).
It also took me a while to figure out the data format. Its a 32Bit float but each 8 bits is in decimal. I have gotten the correct values by converting each 8 bits to binary and inputting the result into a IEE754 converter. Can this be done with the data in mango?
You can use the parseInt() function after you split the string of decimal values and pair it down. After that you'd need to bitshift and decode. Probably going to have to go through java.lang.Long (instantiate one with the lowest order byte, then bitshift and or the others into it) and java.lang.Double.longBitsToDouble() (pass this function the long from step 1)
Maybe something like...
//untested var message = JSON.parse(incoming.value); var regexValues = /.*(\d+),(\d+),(\d+),(\d+).*/.exec(message.VR); var longBits = new java.lang.Long(parseInt(regexValues[1])); longBits |= parseInt(regexValues[2]) << 8; longBits |= parseInt(regexValues[3]) << 16; longBits |= parseInt(regexValues[4]) << 24; CONTEXT_POINTS[message.ID].set( java.lang.Double.longBitsToDouble( longBits ));
-
Thanks Phil,
Sounds like its a bit out of my realm of knowledge but ill give it a go. Would sending the data by MQTT protocol be any better? -
Just judging from the screenshot it looks like it would still pass the numeric value in the same format, so no it wouldn't simplify the bit-banging in a script part of things. It may simplify receiving the messages, though, since without parameter names the HTTP Receiver may have other issues to get past. It seems like that content type header is just wrong, it is not
application/x-www-form-urlencoded
data.