UNPKG

@deltares/fews-ssd-requests

Version:

Library for making requests to the FEWS SSD webservice

108 lines 4.6 kB
"use strict"; /** * The SsdWebserviceProvider class is used to obtain * Schematic Status Display (SSD) data and process it */ Object.defineProperty(exports, "__esModule", { value: true }); exports.SsdWebserviceProvider = void 0; const getUrlForAction_js_1 = require("./requestbuilder/getUrlForAction.js"); const FEWS_NAME_SPACE_js_1 = require("./response/FEWS_NAME_SPACE.js"); const capabilitiesParsers_js_1 = require("./parser/capabilitiesParsers.js"); const svgElementParser_js_1 = require("./parser/svgElementParser.js"); const fews_web_oc_utils_1 = require("@deltares/fews-web-oc-utils"); class SsdWebserviceProvider { _ssdUrl; _piUrl; SSD_ENDPOINT = 'ssd'; PI_ENDPOINT = ''; piWebservice; ssdWebservice; /** * Constructor for SsdWebserviceProvider * * @param url the base url where the SSD servive is available * @param {Object} [options] Optional constructor options * @param {TransformRequestFunction} [options.transformRequestFn] A function that can be used to modify the Request * before it is sent to the server. This function takes a Request as a parameter and returns the modified Request. * If this option is not specified, the Request will be sent as is. */ constructor(url, options = {}) { if (!url.endsWith('/')) { url += '/'; } this._ssdUrl = url + this.SSD_ENDPOINT; this._piUrl = url + this.PI_ENDPOINT; this.piWebservice = new fews_web_oc_utils_1.PiRestService(this._piUrl, options.transformRequestFn); this.ssdWebservice = new fews_web_oc_utils_1.PiRestService(this._ssdUrl, options.transformRequestFn); } async getSvg(url) { const requestOptions = new fews_web_oc_utils_1.RequestOptions(); requestOptions.relativeUrl = false; const svgResponse = await this.ssdWebservice.getDataWithParser(url, requestOptions, new svgElementParser_js_1.SvgElementParser()); if (svgResponse.responseCode != 200) { throw new Error(svgResponse.errorMessage); } return svgResponse.data; } /** * Retrieve the SSD actions for a specific SVG element on a specific panel * Raises an error if the element is not part of the FEWS namespace */ async getActionFromElement(element, actionRequest) { const objectId = element.getAttributeNS(FEWS_NAME_SPACE_js_1.FEWS_NAMESPACE, "id"); if (objectId == null) { throw new Error(`No element with 'fews:id=${objectId}] present`); } actionRequest.objectId = objectId; const promise = this.getAction(actionRequest); return promise.then((action) => { return { id: objectId, action: action }; }); } /** * Retrieve a PI timeseries using the request path supplied in a action */ async fetchPiRequest(request) { request = request.startsWith("/") ? request : "/" + request; const result = await this.piWebservice.getData(request); if (result.responseCode != 200) { throw new Error(result.errorMessage); } return result.data; } /** * Retrieve the SSD actions for a specific object id on a specific panel */ async getAction(actionRequest) { const url = (0, getUrlForAction_js_1.getUrlForAction)(actionRequest); const result = await this.ssdWebservice.getData(url); if (result.responseCode != 200) { throw new Error(result.errorMessage); } return result.data; } /** * Get the url to retrieve an SSD panel */ urlForPanel(panelName, date) { const dateString = date.toISOString().split('.')[0] + 'Z'; const request = '?request=GetDisplay&ssd=' + panelName + '&time=' + dateString; return encodeURI(this._ssdUrl + request); } /** * Retrieve the SSD capabilities */ async getCapabilities(excludeGroups = { displayGroups: [] }) { const excludedGroupsNames = excludeGroups.displayGroups.map((group) => { return group.name; }); const parser = new capabilitiesParsers_js_1.CapabilitiesParsers(excludedGroupsNames); const result = await this.ssdWebservice.getDataWithParser("?request=GetCapabilities&format=application/json", new fews_web_oc_utils_1.RequestOptions(), parser); if (result.responseCode != 200) { throw new Error(result.errorMessage); } return result.data; } } exports.SsdWebserviceProvider = SsdWebserviceProvider; //# sourceMappingURL=ssdWebserviceProvider.js.map