feat: add support for later forecasts and implement force update functionality for rules
This commit is contained in:
@@ -13,6 +13,7 @@ const writeApi = client.getWriteApi(org, bucket, 'ms', {
|
||||
batchSize: 50,
|
||||
});
|
||||
|
||||
// Mapping legacy per sensor_data (logs telemetry)
|
||||
const fieldMap = {
|
||||
t: 'temperature',
|
||||
h: 'humidity',
|
||||
@@ -24,6 +25,10 @@ const fieldMap = {
|
||||
lon: 'longitude',
|
||||
};
|
||||
|
||||
/**
|
||||
* Scrive dati telemetria sensore (logs) con mapping campi abbreviati.
|
||||
* Measurement: sensor_data
|
||||
*/
|
||||
function writeSensorData(fields, sensor, session, timestamp) {
|
||||
const point = new Point('sensor_data')
|
||||
.tag('sensor', sensor)
|
||||
@@ -39,6 +44,46 @@ function writeSensorData(fields, sensor, session, timestamp) {
|
||||
writeApi.writePoint(point);
|
||||
}
|
||||
|
||||
/**
|
||||
* Scrive dati generici (weather, forecast, ecc.) senza mapping.
|
||||
* I campi vengono scritti con il nome originale (ref da Open-Meteo).
|
||||
* @param {string} measurement - nome della measurement InfluxDB (es. 'weather_current', 'weather_forecast')
|
||||
* @param {Object} fields - campi { ref: value }
|
||||
* @param {string} sensor - nome del sensore
|
||||
* @param {string} session - id sessione
|
||||
* @param {number} timestamp - timestamp unix ms
|
||||
*/
|
||||
function writeGenericData(measurement, fields, sensor, session, timestamp) {
|
||||
const point = new Point(measurement)
|
||||
.tag('sensor', sensor)
|
||||
.tag('session', session)
|
||||
.timestamp(timestamp);
|
||||
|
||||
for (const [key, value] of Object.entries(fields)) {
|
||||
if (value === null || value === undefined) continue;
|
||||
if (typeof value === 'number') {
|
||||
point.floatField(key, value);
|
||||
} else if (typeof value === 'string') {
|
||||
point.stringField(key, value);
|
||||
}
|
||||
}
|
||||
|
||||
writeApi.writePoint(point);
|
||||
}
|
||||
|
||||
/**
|
||||
* Scrive un batch di punti forecast (previsioni orarie).
|
||||
* Ogni punto ha il proprio timestamp.
|
||||
* @param {Array} points - array di [timestamp_ms, { ref: value, ... }]
|
||||
* @param {string} sensor - nome del sensore
|
||||
* @param {string} session - id sessione
|
||||
*/
|
||||
function writeForecastBatch(points, sensor, session) {
|
||||
for (const [ts, fields] of points) {
|
||||
writeGenericData('weather_forecast', fields, sensor, session, ts);
|
||||
}
|
||||
}
|
||||
|
||||
async function queryHistory(sensor, session, since) {
|
||||
const queryApi = client.getQueryApi(org);
|
||||
const query = `
|
||||
@@ -62,4 +107,4 @@ async function queryHistory(sensor, session, since) {
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = { writeSensorData, queryHistory };
|
||||
module.exports = { writeSensorData, writeGenericData, writeForecastBatch, queryHistory };
|
||||
|
||||
Reference in New Issue
Block a user