This is probably very obvious to more experienced node red developers but here I go.
I have a node red flow. In it I have a function node. Inside the function node I have a javascript class. My javascript class in only exposing static members, extract:
class MeasurementsList {
static INTERNAL_LIST_CONTEXT_NAME = "INTERNAL_LIST_CONTEXT_NAME";
static get availableMeasurements() { //this is an array
const tempMeasurementsInternalList = context.get(INTERNAL_LIST_CONTEXT_NAME);
const measurementsInternalList = tempMeasurementsInternalList?.length ? tempMeasurementsInternalList : new Array();
return measurementsInternalList;
}
...
}
To make it very short, I want to be able to operate on the array, example: MeasurementsList .availableMeasurements.push(newMeasurementObject);
. My problem is that I do not know how to make the availableMeasurements
array "remain"/persistent between different msg.payload
's.
This is what I want to achieve:
MeasurementsList.availableMeasurements.push(msg.payload1.newMeasurementObject);
...
MeasurementsList.availableMeasurements.push(msg.payload999.newMeasurementObject);
MeasurementsList.availableMeasurements.push(msg.payload1000.newMeasurementObject);
...
Is this possible to do in node-red with javascript? How should I deal with the context variable?
Edit 1:
I refactored my former static member class to an instantiable dito, residing on the [On Start
]-tab:
// Code added here will be run once
// whenever the node is started.
class Measurement {
constructor(time, device, temperatureC, maxAgeMs) {
this.time = time;
this.device = device;
this.temperatureC = temperatureC;
this.maxAgeMs = maxAgeMs;
}
}
class MeasurementsList {
constructor() {
this.#measurementsList = new Array();
}
/**
* @param {Measurement} measurement
*/
refreshAndAdd(measurement) {
this.#removeOutdatedValuesAndSortByTemperaturesAscending();
this.#measurementsList.push(measurement);
return {
"maxMeasurement": this.#maxMeasurement(),
"meanTemperatureC": this.#meanTemperatureC(),
"minMeasurement": this.#minMeasurement()
};
}
get length() {
return this.#measurementsList.length;
}
#maxMeasurement() {
const maxIndex = this.#measurementsList.length - 1;
return this.#measurementsList[maxIndex];
}
#meanTemperatureC() {
let sum = 0;
this.#measurementsList
.forEach((m) => {
sum += m.temperatureC;
});
const mean = sum / this.#measurementsList.length;
return mean;
}
#minMeasurement() {
const minIndex = 0;
return this.#measurementsList[minIndex];
}
#removeOutdatedValuesAndSortByTemperaturesAscending() {
const currentTime = Date.now();
this.#measurementsList = this.#measurementsList
.filter((m) => {
return (currentTime - m.time) < m.maxAgeMs;
})
.sort((m1, m2) => {
return m1.temperatureC - m2.temperatureC
});
}
}
let measurementsList = new MeasurementsList();
Unfortunately there are things that I don't understand about scope? I declared and instatiated my MeasurementsList
class on the [On Start
]-tab to measurementsList
. But the measurementsList
is not accessible on the [On Message
]-tab, neither is the Message
-class? Did I missunderstood your answer mr @hardillb?
Question: How should I do to access my
measurementsList
on the [On Message
]-tab?