Hi,
I already upgraded the script so you can have it here…
/**
* Javascript module for reading weather data from FMI API - HARMONIE.
*
* Copyright (c) 2022 Markus Sipilä, Updated for HARMONIE Rainer Keto
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
* REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
* AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
* INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
* LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
* OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
* PERFORMANCE OF THIS SOFTWARE.
*/
/**
* Exports.
*/
module.exports = {
getForecast: getForecast,
};
/**
* Reads the weather forecase from FMI API.
*
* @param string place
* Place recoginzed by FMI API.
*
* @return string
* XML response from FMI API.
*/
function getForecast(place) {
const xml = makeApiCall(place);
const points = preparePoints(xml);
return points;
}
/**
* Makes an API call to the Finnish Meteorology Institute.
*
* @param string place
* Place recoginzed by FMI API.
*
* @return string
* XML response from FMI API.
*/
function makeApiCall(place) {
const http = Java.type("org.openhab.core.model.script.actions.HTTP");
console.log('fmi.js: Making an API call to FMI API...');
const url = 'http://opendata.fmi.fi/wfs?service=WFS&version=2.0.0&request=getFeature&storedquery_id=fmi::forecast::harmonie::surface::point::simple&place=' + place + '¶meters=temperature';
const xml = http.sendHttpGetRequest(url, 10000);
return xml;
}
/**
* Parses the forecasted temperatures from the XML response.
*
* @param string xml
* FMI response in XML format.
*
* @return array
* Array of point objects.
*/
function preparePoints(xml) {
console.log('fmi.js: transforming XML to JSON and parsing temperatures...');
const transformation = Java.type("org.openhab.core.transform.actions.Transformation");
let tempPoints = [];
// Early exit in case XML is null.
if (xml == null) {
console.error('fmi.js: XML empty, parsing aborted.')
return tempPoints;
}
try {
const jsObject = JSON.parse(transformation.transform('XSLT', 'xml2json.xsl', xml));
const members = jsObject['wfs:FeatureCollection']['wfs:member'];
for (let i = 0; i < members.length; i++) {
let point = {
datetime: members[i]['BsWfs:BsWfsElement']['BsWfs:Time'],
value: members[i]['BsWfs:BsWfsElement']['BsWfs:ParameterValue']
};
tempPoints.push(point);
}
console.log('fmi.js: Temperatures parsed!');
}
catch (exception) {
console.error('fmi.js: Exception parsing temperatures: ' + exception.message);
}
return tempPoints;
}