Skip to content
Snippets Groups Projects
sensor-things.js 2.92 KiB
Newer Older
  • Learn to ignore specific revisions
  • import config from 'ui/config';
    
    
    Briggs, Aaron Shane's avatar
    Briggs, Aaron Shane committed
    /**
    
     * Returns the sensor things datastream URL used to fetch the datastream. Add the filter for parameter code if not the null string
    
    Briggs, Aaron Shane's avatar
    Briggs, Aaron Shane committed
     * @param {String} agencySiteNumberCode - combines the Agency Code with the Site Number for example USGS-01646500
    
     * @param {String} parameterCode - five digit code that uniquely identifies the observed property (parameter)
    
     * @return {String} Sensor Things datastream URL for the site with parameter code.
    
    Briggs, Aaron Shane's avatar
    Briggs, Aaron Shane committed
     */
    
    const getSiteDatastreamURL = function(agencySiteNumberCode, parameterCode) {
        const parameterQuery = parameterCode ? `ParameterCode eq '${parameterCode}'` : '';
        const queryString = parameterQuery ? `?$filter=properties/${encodeURIComponent(parameterQuery)}` : '';
        return `${config.SENSOR_THINGS_ENDPOINT}Things('${agencySiteNumberCode}')/Datastreams${queryString}`;
    
    Briggs, Aaron Shane's avatar
    Briggs, Aaron Shane committed
    /**
    
     * Does the work of contacting the Sensor Things API and returning the datastream
    
    Briggs, Aaron Shane's avatar
    Briggs, Aaron Shane committed
     * @param {String} agencySiteNumberCode - combines the Agency Code with the Site Number for example USGS-01646500
    
     * @param {String} parameterCode - five digit code that uniquely identifies the observed property
    
    Briggs, Aaron Shane's avatar
    Briggs, Aaron Shane committed
     * @return {Promise} resolves to an empty object or one containing threshold information (and other secondary information)
     */
    
    export const fetchSiteDatastream = async function(agencySiteNumberCode, parameterCode) {
        const url = getSiteDatastreamURL(agencySiteNumberCode, parameterCode);
        try {
            const response = await fetch(url, {
                method: 'GET'
            });
            if (response.status === 200) {
                return await response.json();
    
            } else {
                console.error(`Received bad status, ${response.status} at ${url}`);
                return {};
            }
        } catch (error) {
            console.error(`Failed fetch for ${url}`);
            return {};
        }
    };
    
    /**
     * Fetch the sites that are within the polygon and return the geojson.
     * @param {String} - Well Known Text Polygon string, for example 'Polygon((-92 44, -89 44, -89 43, -92 43, -92 44))'
     * @returns {Promise} - resolves to a json object. If the call fails or returns an unexpected status the json object
     *      will be empty
     */
    export const fetchSitesGeoJson = async function(wktPolygon) {
        const expandQuery = encodeURIComponent('Locations($select=id,location)');
        const locationQuery = encodeURIComponent(`st_within(Location/location, geography'${wktPolygon}')`);
        const url =
    
            `${config.SENSOR_THINGS_ENDPOINT}/Things?$expand=${expandQuery}&$filter=${locationQuery}&$resultFormat=GeoJSON&$top=1000`;
    
        try {
            const response = await fetch(url, {
                method: 'GET'
    
            if (response.status === 200) {
                return await response.json();
    
            } else {
                console.error(`Received bad status, ${response.status} at ${url}`);
                return {};
            }
    
        } catch (error) {
    
            console.error(`Failed fetch for ${url}`);
            return {};
        }