Facility Control

 View Only
  • 1.  API GET Commands

    Posted 06-11-2024 13:03

    Hi, 

    I'm having trouble with API Get commands, Id like to use the response from the command to replace text in order to display status of external equipment within my custom touchpanel, I am able to het the commands to work from postman, but having no success within dashboard. 

    Thanks

     



    ------------------------------
    Dave Kennedy
    ------------------------------


  • 2.  RE: API GET Commands

    Posted 06-11-2024 15:57

    Hi Dave,

    I've attached an example panel snippet below making use of ogScript to send a GET request and receive the response. 

    To read the response, a callback function needs to be added as an argument to the HTTP ASync Message block:


    The callback function is defined here:
    From the callback, the response text is parsed into JSON. From the JSON object, you can then read the desired value.

    Example Panel:

    <?xml version="1.0" encoding="UTF-8"?><abs contexttype="opengear" gridsize="20" id="_top" keepalive="false">
       <meta>
          <params>
             <param access="1" maxlength="0" name="ResponseText" oid="responseText" stateless="true" type="STRING" value="" widget="multiline-text"/>
          </params>
       </meta>
       <button buttontype="push" height="140" left="20" name="Send HTTP GET Request" top="40" width="180">
          <task tasktype="ogscript">function myCallback(responseText) {
    	/*-arg responseText: { "type": "STRING",  "widget": "text",  "value": ""} -*/
    
    
    
    	var parsedResponse = "";
    	var requestedValue = "";
    
    	/*! block id=1010,1007,1013,1014,1016,1015,1016 !*/
    	if (	responseText  !=  null) { 	parsedResponse = JSON.parse(responseText);
    requestedValue = parsedResponse.origin; 	ogscript.debug(	requestedValue); 	params.setValue('responseText', 0, requestedValue) } else {  }
    	/*!!
     	&lt;block id="1010" type="if" x="283" y="10" w="268" INPUT1="ID:1007" OPERATION="does not equal" INPUT2="null" TRUE="ID:1013" FALSE="" IGNORE="" /&gt;
    	&lt;block id="1007" type="responseText_String_variable" x="10" y="10" w="243" color="#404040" SET="" /&gt;
    	&lt;block id="1013" type="customcode" x="854" y="60" w="343" CODE="parsedResponse = JSON.parse(responseText);&amp;amp;#10;requestedValue = parsedResponse.origin;"  next="ID:1014" /&gt;
    	&lt;block id="1014" type="ogscript_debug" x="854" y="204" w="243" MESSAGE="ID:1016"  next="ID:1015" /&gt;
    	&lt;block id="1016" type="requestedValue_String_variable" x="581" y="204" w="243" color="#ee862a" SET="" /&gt;
    	&lt;block id="1015" type="param_setvalue" x="854" y="276" w="318" PARAM="[root, Params for Community%20Help.grid, Not In Menu, ResponseText (responseText)]" PARAM_DATA="_top&amp;amp;responseText[0]" VALUE="ID:1016" /&gt;
    	&lt;block id="1016" type="requestedValue_String_variable" x="581" y="204" w="243" color="#ee862a" SET="" /&gt;
    	!!*/
    }
    
    
    
    
    /*! block id=1006 !*/
    ogscript.asyncHTTP("https://httpbin.org/get", "GET", "application/json", null, myCallback);
    /*!!
     &lt;block id="1006" type="ogscript_async" x="176" y="108" w="318" URL="https://httpbin.org/get" METHOD="GET" TYPE="application/json" DATA="null" CALLBACK="myCallback" /&gt;
    !!*/
    /*!!&lt;checksum&gt;07399059f99438e00e39975a24cdb66a&lt;/checksum&gt;!!*/</task>
       </button>
       <param editable="false" expand="true" height="240" left="20" oid="responseText" showlabel="false" top="260" width="320"/>
       <label height="40" left="20" name="Response:" style="txt-align:west;" top="220" width="180"/>
    </abs>

    Hopefully this helps, please let me know if you have any further questions!



    ------------------------------
    Michael Quach
    ------------------------------