@deltares/fews-ssd-requests
Version:
Library for making requests to the FEWS SSD webservice
104 lines • 4.25 kB
JavaScript
/**
* The SsdWebserviceProvider class is used to obtain
* Schematic Status Display (SSD) data and process it
*/
import { getUrlForAction } from "./requestbuilder/getUrlForAction.js";
import { FEWS_NAMESPACE } from "./response/FEWS_NAME_SPACE.js";
import { CapabilitiesParsers } from "./parser/capabilitiesParsers.js";
import { SvgElementParser } from "./parser/svgElementParser.js";
import { PiRestService, RequestOptions } from "@deltares/fews-web-oc-utils";
export 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 PiRestService(this._piUrl, options.transformRequestFn);
this.ssdWebservice = new PiRestService(this._ssdUrl, options.transformRequestFn);
}
async getSvg(url) {
const requestOptions = new RequestOptions();
requestOptions.relativeUrl = false;
const svgResponse = await this.ssdWebservice.getDataWithParser(url, requestOptions, new 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_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 = 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(excludedGroupsNames);
const result = await this.ssdWebservice.getDataWithParser("?request=GetCapabilities&format=application/json", new RequestOptions(), parser);
if (result.responseCode != 200) {
throw new Error(result.errorMessage);
}
return result.data;
}
}
//# sourceMappingURL=ssdWebserviceProvider.js.map