Commit f2727b3f authored by Giannis Kepas's avatar Giannis Kepas
Browse files

fix temporal queries

parent d80c0459
Loading
Loading
Loading
Loading
+12 −11
Original line number Diff line number Diff line
const axios = require("axios");
const qs = require("qs");
const NGSITranslator = require("../translator/NGSITranslator");
const typeMaps = require("../typeMaps/typeMaps");

@@ -55,14 +56,14 @@ const fetchEntities = async (type, attrs, fetcher, additionalParams, res) => {
        }

        const apiResponse = await fetcher(type, measurementTypes, additionalParams);
        if (!apiResponse || apiResponse.data.length === 0) {
        if (!apiResponse || !apiResponse.data || apiResponse.data.length === 0) {
            return res.status(500).json({ error: "Failed to retrieve data from API. See http-bridge logs for info." });
        }

        const ngsiData = NGSITranslator.toNGSI(apiResponse.data, type);
        res.status(200).json(ngsiData);
    } catch (error) {
        console.error("Error in fetchEntities:", error.message);
        console.error(`error in fetchEntities\n${error.stack}\n`);
        res.status(500).json({ error: error.message || "Failed to fetch entities." });
    }
};
@@ -82,14 +83,14 @@ const fetchTemporalData = async (type, measurementTypes, params) => {
                const response = await axios.get(`${process.env.API_URL}/device_measurement`, {
                    params: {
                        measurement_type: `in.(${measurementTypes.join(",")})`,
                        ts: params.ts, // Pass as an array to avoid encoding issues
                        limit: params.limit || 100,
                    }
                        ts: params.ts,
                    },
                    paramsSerializer: (params) => qs.stringify(params, { encode: false }) // Prevent double encoding
                });

                return response;
            } catch (error) {
                console.error(`Error fetching temporal data: ${JSON.stringify(error.response.data)}\npath: ${error.request.path}`);
                console.error(`error fetching temporal data\n${error.stack}`);
                return { data: [] };
            }

@@ -113,13 +114,13 @@ const fetchRealTimeData = async (type, measurementTypes, params) => {
                    params: {
                        measurement_type: `in.(${measurementTypes.join(",")})`,
                        order: "ts.desc",
                        limit: params.limit || 100, // Default to 100 if limit is not provided. (MEETING: Remove this?)
                        limit: params.limit || 100, // Default to 100 if limit is not provided. (MEETING: Remove this? It will tank performance...)
                    }
                });

                return response;
            } catch (error) {
                console.error(`Error fetching temporal data: ${JSON.stringify(error.response.data)}\npath: ${error.request.path}`);
                console.error(`error fetching real-time data\n${error.stack}`);
                return { data: [] };
            }

@@ -140,7 +141,7 @@ const fetchRealTimeData = async (type, measurementTypes, params) => {
 */
const getTemporalEntities = async (req, res) => {
    console.log("Incoming temporal request:", req.query);
    let { type, timerel, time, endTime, attrs, limit } = req.query;
    let { type, timerel, time, endTime, attrs } = req.query;

    let tsParams = [];

@@ -165,8 +166,8 @@ const getTemporalEntities = async (req, res) => {
            return res.status(400).json({ error: "Invalid timerel value" });
    }

    // console.log("Final timestamp:", finalTimestamp);
    await fetchEntities(type, attrs, fetchTemporalData, { ts: tsParams, limit }, res);
    // console.log("Final timestamp:", tsParams.join(", "));
    await fetchEntities(type, attrs, fetchTemporalData, { ts: tsParams }, res);
};

/**
+302 −106

File changed.

Preview size limit exceeded, changes collapsed.

+3 −2
Original line number Diff line number Diff line
{
  "name": "wrappertest",
  "name": "uowm-http-bridge",
  "version": "1.0.0",
  "main": "index.js",
  "scripts": {
@@ -13,6 +13,7 @@
    "axios": "^1.7.8",
    "dotenv": "^16.4.5",
    "express": "^4.21.1",
    "morgan": "^1.10.0"
    "morgan": "^1.10.0",
    "qs": "^6.14.0"
  }
}
+1 −0
Original line number Diff line number Diff line
@@ -69,6 +69,7 @@ class NGSITranslator {
                    value: value,
                    // Extra data (optional)
                    metadata: {
                        type: "Property",
                        unitCode: { type: "Property", value: unit },
                        healthRange: { type: "Property", value: { min, max } },
                        slug: { type: "Property", value: slug }
+0 −19
Original line number Diff line number Diff line
const weatherObservedTypeMap = {
    1: { key: "ambient_light", unit: "LUX", min: 0, max: 12000, slug: "light" },
    2: { key: "loudness", unit: "DEC", min: 34.6, max: 85, slug: "noise" },
    3: { key: "rainfall", unit: "MMT", min: 0, max: 8, slug: "rain" },
    4: { key: "ambient_temperature", unit: "CEL", min: -10, max: 50, slug: "temperature" },
    5: { key: "voc", unit: "PPB", min: 0, max: 600, slug: "voc" },
    6: { key: "sulphurDioxide", unit: "PPB", min: 0, max: 7.09, slug: "so2" },
    7: { key: "ozone", unit: "PPB", min: 0, max: 47.3, slug: "o3" },
    8: { key: "nitrogenDioxide", unit: "PPB", min: 0, max: 19.7, slug: "no2" },
    9: { key: "nitrogenOxide", unit: "PPB", min: 0, max: 25000, slug: "no" },
    10: { key: "dust", unit: "MCG", min: 0, max: 50, slug: "pm10" },
    11: { key: "ambient_humidity", unit: "P1", min: 0, max: 100, slug: "humidity" },
    12: { key: "carbonDioxide", unit: "PPM", min: 0, max: 430, slug: "co2" },
    13: { key: "atmosphericPressure", unit: "HPA", min: 750, max: 1050, slug: "atmospheric-pressure" },
    14: { key: "windSpeed", unit: "KMT", min: 0, max: 50, slug: "wind-speed" },
    15: { key: "windDirection", unit: "DEG", min: 0, max: 359.9, slug: "wind-direction" },
};

module.exports = weatherObservedTypeMap;
 No newline at end of file