i am using redis timeseries in aedes i want when ever someone publis something is topic that data should store in redis in form of object value : [{status,timestamp},{status,timestamp]
i try this code but giving error CODE =>
const { createClient } = require('redis'); const { TimeSeriesDuplicatePolicies, TimeSeriesEncoding, TimeSeriesAggregationType } = require('@redis/time-series');
const clt = createClient();
(async () => {
try {
// Connect to Redis before creating the time series
await clt.connect();
const created = await clt.ts.create('random', {
RETENTION: 86400000, // 1 day in milliseconds
ENCODING: TimeSeriesEncoding.UNCOMPRESSED, // No compression
DUPLICATE_POLICY: TimeSeriesDuplicatePolicies.BLOCK, // No duplicates
});
if (created === 'OK') {
console.log('Created timeseries.');
} else {
console.error('Error creating timeseries:', created);
}
} catch (error) {
console.error('Error connecting to Redis or creating timeseries:', error);
}
})();
aedes.on('publish', async function (packet, client) {
if (client) {
console.log(MESSAGE_PUBLISHED : MQTT Client ${(client ? client.id : 'AEDES BROKER_' + aedes.id)} has published message "${packet.payload}" on ${packet.topic} to aedes broker ${aedes.id}
);
try {
// Convert buffer payload to string
const payloadString = packet.payload.toString('utf8');
// Parse the payload string as JSON
const data = JSON.parse(payloadString);
const timestamp = Date.now();
// Extract data from the JSON payload
const deviceId = data.deviceID;
const nodeId = data.NodeID;
const status = data.status;
const value = {
status: status,
timestamp: timestamp
};
const serializedValue = JSON.stringify(value);
console.log("Data:", data);
console.log("DID:", deviceId);
console.log("NODEID:", nodeId);
console.log("Status:", status);
console.log("Value to be stored in Redis time series:", value);
// Add the data to the Redis time series
const result = await clt.ts.add(`${deviceId}:${nodeId}`, timestamp, serializedValue);
console.log("Result from Redis:", result)
console.log(`Added timestamp ${currentTimestamp}, value ${value}.`);
// console.log("Result from Redis:", result);
console.log("Data saved to Redis time series.");
} catch (error) {
console.error("Error handling publish event:", error);
}
}
});
and sending data in postman => { "deviceID": "23423432", "NodeID": "34534", "status":"1" }
ERROR => Data: { deviceID: '23423432', NodeID: '34534', status: '1' } DID: 23423432 NODEID: 34534 Status: 1 Value to be stored in Redis time series: { status: '1', timestamp: 1711579778262 } Error handling publish event: [ErrorReply: ERR TSDB: invalid value]
The value that you are trying to store is not numeric.
See TS.ADD: