Please Note This forum exists for community support for the Mango product family and the Radix IoT Platform. Although Radix IoT employees participate in this forum from time to time, there is no guarantee of a response to anything posted here, nor can Radix IoT, LLC guarantee the accuracy of any information expressed or conveyed. Specific project questions from customers with active support contracts are asked to send requests to support@radixiot.com.

Radix IoT Website Mango 3 Documentation Website Mango 4 Documentation Website

Is there a way to add a token to HTTP JSON Retriever DS?


  • Just curious if it's possible to add a token to the ds?


  • Hi psysak, welcome back! So soon! :P

    It is not currently available, but it is a really good idea to allow some static headers to be configured, as the publisher does.

    You could achieve this using the HttpBuilder from a scripting data source, though. Perhaps as in this thread: https://forum.infiniteautomation.com/topic/3031/pulling-multiple-parameters-from-a-single-json-result


  • This post is deleted!

  • OK laaast question :)

    I've created this in a scripting source

    function getRequest(_id, serial) {
        // This portion is the part which actually retreives the data from api
        HttpBuilder.get("https://thisisadomain/api/v2/sensors/" + _id + "/analyses/timeseries?dateRequest=dtd&units=l&resolution=h", headers, parameters).err(function(status, headers, content) {
          throw "Script failed with HTTP status: " + status;
        }).resp(function(status, headers, content) { 
           // Needs to be 200 status or you can set the accepted statuses
           // If all is well then call the parsing function and pass the JSON to it
           //print(content);
           match = RegExp(regex).exec(content);
           //print(match[1]);
           serial.set(match[1]);
        }).execute();
    }
    

    Ignoring that there is clearly other parts missing which I didn't paste here, my question revolves around the line

    serial.set();
    

    I should have known that doesn't work because serial is not a variable name, but I want to someone use serial to point to the correct variable in the data source. Basically, I am passing a serial and id number to this function which calls an api passing the id to it which in turn returns a value, I regex that value and want to set a variable with the "Variable Name" of serial. I have created these variables in the DS prior to this. How do I get that reference to the point so I can run set on it?


  • My suggestion would be to chain it.
    have it return that value and use that as the context point to fire the next part in the function. return alphanumeric as a json string and parse with new part of your script. take the parsed values to get your datapoint and set accordingly.
    EDIT, you may have to do this with a meta point source...
    Oh I see.... you're losing the value in the callback. I wonder if you can use bind().
    set your callback as a separate function and bind serial to it... I've not got a lot of time to test and help right now,,,
    EDIT 2:

    OK i've gone back and had a tinker. Think I will need more info Psysak,,,

     function httpReq(url,method,callback,headers,params,serial)
    {//Second format, full request
        if(headers===undefined){headers={};}
        if(params===undefined){params={};}
       // callback.bind(serial);
        if(method=="get")
        {
        HttpBuilder.get(url,headers,params)
    	.err(function(status, headers, content) { //setErrorCallback for linguistic completion
    		 
    	}).resp( 
    	    
    	// callback
    	function(status, headers, content) { //setResponseCallback
    			print(content);//
    		
    	}  ).excp(function(exception) { //setExceptionCallback
    		throw exception.getMessage();
    	}).execute();
        }
        else
        {
             HttpBuilder.post(url,headers,params)
    	.err(function(status, headers, content) { //setErrorCallback for linguistic completion
    		  //setErrorCallback for linguistic completion
                	print(status);	print(content);
    	}).resp( 
    	 //callback
    	function(status, headers, content) { //setResponseCallback
    			print(content);//	print(status);
    			print(serial);
    	}  ).excp(function(exception) { //setExceptionCallback
    		throw exception.getMessage();
    	}).execute();
        }
    }
    

    I can't replicate your issue. and am still able to print the serial. however, serial is a context point (different name). that' i've injected as a parameter,. Shouldn't make a difference though.


  • Hey @MattFox thanks for the response.

    Hmm. So what I'm doing is trying to substitute serial with a string from the api call, and that string is supposed to be the same name as the "Variable Name" of a point I created in that scripting DS. I noticed that if I hard code a value of serial so for example

    serial = 123456789;
    

    Where 123456789 is an existing data point with that Variable Name, if I print serial it is an object. So I assume the issue is that I get a string back from the API and then I'm trying to substitute that for serial and call .set() on it. This is not a reference to the data point right?


  • So I dug around in the forum a bit and for now I think I'm going to do a pointquery for the xid and then set from there :)


  • It's not clear to me what serial was because it's only an argument in a function in the code you presented, which could be anything. If it was a variable storing a string that was the variable name of something in context, you could do

    this[serial]
    

    to access the context point.


  • Sorry @phildunlap you're right I missed details. Yes it was just a string which stores the variable name of a context object.
    So can you set on a this[serial]? I'll try it later


  • You should be able to. this[serial] would be a reference to a context point wrapper, so it would function like the context point (AbstractPointWrapper object) when referred to directly. CONTEXT_POINTS[serial] would then be the DataPointRT object.


  • Works like a charm.