TLDR: What is the proper way to ‘wait’ for HTTPSend function to finish its entire api call? The function's url parameter changes from a droplist widget.
Hello,
I have been testing different ways to run the httpsend function. In the script block of ‘main’ module, in a called module, and as a launched module.
Goal is to change the api link according to user preference(monthly or annual), fetch the data and plot it :
(the script runs on trigger tag every 7 seconds)
When I tried it, the function only works at application startup ( i.e. response struct object returns correct data)
But after changing api URL from monthly to annual, the whole script runs again as usual, on trigger. But a new api call should bring new data; that’s not happening; specifically the httpsend’s pResponse struct is not populating with new data even though the URL argument changes
( from https://api.eia.gov/v2/naturalgas/pri/sum/....&frequency=**monthly**&.....
to https://api.eia.gov/v2/naturalgas/pri/sum/....&frequency=**annual**&..... ).
The following code works at first run, but when changed from monthly to annual, httpResponse do not get new annual data and hold the monthly data…
Main [
If PointObj\Value == 1;
[
url = "https://api.eia.gov/v2/natural-gas/pri/sum/data?data[]=value&facets[duoarea][]=SLA&facets[process][]=PIN&api_key=N8u2ivHYBhDYWf0WEQ3Wr4GoIQYAMpCZxA9gP8JM&start=2003-01&sort[0][column]=period&sort[0][direction]=asc&frequency=annual";
\System\HTTPClient\HTTPSend( url, &httpResponse);
parsedData = JSONParse(httpResponse\Body);
{ …
code for preparing the dataarray and timearray,
then using writehistory to a given tag's historian. Then
plotting.
…
}
ScriptObj\value = CurrentTime();
]
]
Running this as a submodule:
According to docsLaunched Module is fit for this function
When running the HTTPSend function in a launched module, the debugger shows this:
Look like the whole script runs, the module runs seperately as a 'launched module'
Httpresponse is always invalid and thus Valid(parsedData) also shows false(0) in tag browser Script tag's value
Another unusual way was Using WhileLoop(!) but the app crashes due to memory overload =>
<
{============================= System\APICall Launched Module ================================}
{ A simple called module to increment and display a value }
{=============================================================================}
APICall
(
parsedDataptr;
)
[
httpResponse;
]
Main [
If 1; [
WhileLoop(
Valid(httpResponse) == FALSE;
\System\HTTPClient\HTTPSend( url, &httpResponse);
*parsedDataptr = JSONParse(httpResponse\Body);
);
ScriptObj\value = Concat("parsedData Valid ? ", Valid(parsedData), " CurrTime: ", CurrentTime());
Slay();
]
]
>
What is the proper way to ‘wait’ for HTTPSend function to finish its entire api call ?
(Also asking for tips about way to streamline coding and debugging in VTScada, need to restart VT everytime there is a change in code, and source debugger is not the... 'friendliest' tool.)
TLDR: What is the proper way to ‘wait’ for HTTPSend function to finish its entire api call? The function's url parameter changes from a droplist widget.
Hello,
I have been testing different ways to run the httpsend function. In the script block of ‘main’ module, in a called module, and as a launched module.
**Goal** is to change the api link according to user preference(monthly or annual), fetch the data and plot it :
![657fe56f1ada9](serve/attachment&path=657fe56f1ada9)
(the script runs on trigger tag every 7 seconds)
When I tried it, the function only works at application startup ( i.e. response struct object returns correct data)
But after changing api URL from monthly to annual, the whole script runs again as usual, on trigger. But a new api call should bring new data; that’s not happening; specifically the httpsend’s pResponse struct is not populating with new data even though the URL argument changes
( from https://api.eia.gov/v2/naturalgas/pri/sum/....&frequency=**monthly**&.....
to https://api.eia.gov/v2/naturalgas/pri/sum/....&frequency=**annual**&..... ).
The following code works at first run, but when changed from monthly to annual, httpResponse do not get new annual data and hold the monthly data…
````
Main [
If PointObj\Value == 1;
[
url = "https://api.eia.gov/v2/natural-gas/pri/sum/data?data[]=value&facets[duoarea][]=SLA&facets[process][]=PIN&api_key=N8u2ivHYBhDYWf0WEQ3Wr4GoIQYAMpCZxA9gP8JM&start=2003-01&sort[0][column]=period&sort[0][direction]=asc&frequency=annual";
\System\HTTPClient\HTTPSend( url, &httpResponse);
parsedData = JSONParse(httpResponse\Body);
{ …
code for preparing the dataarray and timearray,
then using writehistory to a given tag's historian. Then
plotting.
…
}
ScriptObj\value = CurrentTime();
]
]
````
**Running this as a submodule:**
According to docs[Launched Module is fit for this function](https://www.vtscada.com/help/Content/Scripting/API/LaunchedVsCalled.htm?#:~:text=function%20is%20an%20example.)
When running the HTTPSend function in a launched module, the debugger shows this:
![657fe79ce1324](serve/attachment&path=657fe79ce1324)
Look like the whole script runs, the module runs seperately as a 'launched module'
Httpresponse is always invalid and thus Valid(parsedData) also shows false(0) in tag browser Script tag's value
Another unusual way was Using WhileLoop(!) but the app crashes due to memory overload =>
````
<
{============================= System\APICall Launched Module ================================}
{ A simple called module to increment and display a value }
{=============================================================================}
APICall
(
parsedDataptr;
)
[
httpResponse;
]
Main [
If 1; [
WhileLoop(
Valid(httpResponse) == FALSE;
\System\HTTPClient\HTTPSend( url, &httpResponse);
*parsedDataptr = JSONParse(httpResponse\Body);
);
ScriptObj\value = Concat("parsedData Valid ? ", Valid(parsedData), " CurrTime: ", CurrentTime());
Slay();
]
]
>
````
**What is the proper way to ‘wait’ for HTTPSend function to finish its entire api call ?**
(Also asking for tips about way to streamline coding and debugging in VTScada, need to restart VT everytime there is a change in code, and source debugger is not the... 'friendliest' tool.)