I'm implementing some changes to existing js code that is a plugin for freeboard. The problem is that I'm not sure how to acces received json payload.
The implementation of api that the plugin is supposed to communicate with has been changed from not-so-RESTfull to RESTfull like. And there are difficulties.
The original jscript plugin contains this code, which I think is partly a culprit of my problems described here.
$.ajax({
url : url,
dataType : "JSON",
type: "POST",
data: '{"entities":[{"type": "'+currentSettings.type+'","isPattern": false, "id" :"'+currentSettings.id+'"}]}',
beforeSend: function(xhr)
{
xhr.setRequestHeader("Content-Type", "application/json");
xhr.setRequestHeader("Accept", "application/json");
xhr.setRequestHeader("Fiware-Service", currentSettings.service);
xhr.setRequestHeader("Fiware-ServicePath", currentSettings.servicepath);
xhr.setRequestHeader("X-Auth-Token", currentSettings.xauthtoken);
},
with url of type:
127.0.0.1:1026/v1/queryContext
This whole js part should imitate query taken from example from Orion Context Broker API v1 that looks like this.
Link to query operation in Orion Context Broker API v1
As you can see it is not really RESTfull and it has been changed, however plugin has been updated 3 years ago and doesn't work with new approach.
In the new version of API implementation I can query for resources simply by
curl localhost:1026/v2/entities/Room1?type=Room -s -S -H 'Accept: application/json' | python -mjson.tool
Documentation of recent v2 of API implementation.
I changed that part of plugin code, mainly definition of URL and I suppose (but can't really check it - is there any compiler for js or something to test it? I tried Naked for python and running it via node, but there's no way of debugging it) it should work.
$.ajax({
url : url,
type: "GET",
beforeSend: function(xhr)
{
xhr.setRequestHeader("Accept", "application/json");
xhr.setRequestHeader("Fiware-Service", currentSettings.service);
xhr.setRequestHeader("Fiware-ServicePath", currentSettings.servicepath);
xhr.setRequestHeader("X-Auth-Token", currentSettings.xauthtoken);
},
I have a connection and the service using the url that I feed the code, it's tested via Insomnia. If this code is without bugs, he real problem is with following part of original code:
//Get attributes
attributes=data["contextResponses"][0]["contextElement"]["attributes"];
It gets attributes from received payload for further manipulation. In v1 api received payload looked like this:
And now it look like this:
Or if I use keyValues option like this:
where id is used in request's url and type can be too, it is known, so they don't matter. The most important things are those attributes that follows id and type.
How do I extract them?
The succes part of original code, containing mentioned getAttributes method:
Code of that plugin is mentioned in first paragraph of this question.
success : function(data)
{
//Initialize mydata
mydata={};
//if advanced setting is true do not modify received JSON
if(currentSettings.advanced){
mydata=data;
}
//if advanced setting is false reduce received JSON nesting
else{
//Get attributes
attributes=data["contextResponses"][0]["contextElement"]["attributes"];
//Get each attribute and append it to mydata
//////////////////////////////////////////////
Then there is manipulation on received data that is
similar in both cases