HttpBuilder not defined
-
@jared-wiltshire Hey, of course! sorry about that. Let me see if updating it helps.
@phildunlap hmmm... what version did that functionality appear in? The Mango in question that I will be loading it into is still a v2.8.
-
The release notes suggest 3.2.2: https://github.com/infiniteautomation/ma-core-public/blob/main/Core/RELEASE-NOTES
-
Here's the script, this is part of a scripting data source.
var headers = {} var parameters = {} var regex = '{\"time\":(\\d*),.*?\"temperature\":(-?\\d*\\.?\\d*).*?\"daily\".*?{.*?\[.*?\"time\":(\\d*),.*?\"temperatureHigh\":(-?\\d*\\.?\\d*).*?\"temperatureHigh\":(-?\\d*\\.?\\d*).*?\"temperatureHigh\":(-?\\d*\\.?\\d*).*?\"temperatureHigh\":(-?\\d*\\.?\\d*).*?\"temperatureHigh\":(-?\\d*\\.?\\d*).*?\"temperatureHigh\":(-?\\d*\\.?\\d*).*?\"temperatureHigh\":(-?\\d*\\.?\\d*).*?\"temperatureHigh\":(-?\\d*\\.?\\d*).*?]'; var match; // This is the function which parses the returned data from DarkSky // The passed variable "data" is the actual JSON contents returned function handleTemperatureData(data) { // The var match is an array which results from parsing the data through the regex // Each element of the array is an individual value where // match[1] returns current time // match[2] returns the current temperature // match[3] is a time value for the first forecast value // match[4-11] are the daily forecast high temperatures match = RegExp(regex).exec(data); var k; // This is required because the way that DarkSky returns data sometimes // the first "forecast" data point is actually still a forecast temperature // for today. If this is the case then we have to skip this value otherwise // the forecast for tomorrow will actually be the forecast for today etc if(match[3] < match[1]) { // This means that the first reading is for today, skip it and use the // next value as a starting point k = 5; } else { k = 4; } if(match.length >= 11) { CurrentTemp.set(match[2]); OneDayForecastTemp.set(match[k]); TwoDayForecastTemp.set(match[k+1]); ThreeDayForecastTemp.set(match[k+2]); FourDayForecastTemp.set(match[k+3]); FiveDayForecastTemp.set(match[k+4]); SixDayForecastTemp.set(match[k+5]); SevenDayForecastTemp.set(match[k+6]); } } // End of function // This portion is the part which actually retreives the data from DarkSky HttpBuilder.get("https://api.darksky.net/forecast/token/45.321488,%20-75.671071?units=si&exclude=minutely,hourly", 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 handleTemperatureData(content); }).execute();
Is there any way to duplicate this functionality on a V2 Mango ES?
-
No trivial way.
If you know the headers you need in that GET (no dynamic request construction), you could put the whole GET HTTP request into the Query command of an alphanumeric TCP/IP point (don't forget the trailing
\n\n
in HTTP, and a\n
between header rows)So probably a query command like (you may need to experiment with removing the Host header, adding Content-Type, whatever. The easiest would be making the request in the browser with the developer tools open, then you can see a working request's [probably excessive] headers),
GET /forecast/token/45.321488,%20-75.671071?units=si&exclude=minutely,hourly HTTP/1.1\nHost: api.darksky.net\n\n
Then from your script you can do
RuntimeManager.refreshDataPoint("DP_TCPIP_HTTP_Magic"); var response = tcpIpHttpMagicContextPoint.value;
-
It's all good, I'll see if about upgrading that unit and it not simplifying something down for this particular instance.
As always thank you!
-
Most welcome!
Could always publish this to it from the updated Mango. The Persistent TCP publisher or HTTP publisher would probably work.
MangoES's do have 2-->3 upgrade pricing available but not through the store.
The TCP/IP data point solution may not be so bad.
-
@phildunlap to publish I would need to be able to connect into the V2 mango right? That ES is on a customer site and I don't know how willing they would be to let me open up ports sadly :(
-
Correct. There may still be options like a reverse SSH tunnel to leave connection initiation in the hands of the client-housed device. Was just a thought!
It looks like if there was a regex solution to the first forecast changing the group number you're starting at, then the HTTP retriever data source would be an option.
-
This post is deleted! -
OK I think I came up with something else. I use the HTTP Retriever, pull the entire string into an alpha point and then simply pass that alpha point into my original script and let regex do it's thing. I think that works!
-
Good solution!