iot devices for multiple

master
varun 2 years ago
parent 702edb712a
commit d006a23e65

@ -959,33 +959,32 @@ exports.calculateCapacity = async (req, reply) => {
exports.IotDevice = async (req, reply) => { exports.IotDevice = async (req, reply) => {
try { try {
const { hardwareId, tankHeight, maxLevel, minLevel, mode,tankhardwareId } = req.body; const { hardwareId, mode, tanks } = req.body;
// create a new tank document with the current date and time // create a new tank document with the current date and time
const currentDate = new Date(); const currentDate = new Date();
const date = currentDate.toISOString(); // save the date as an ISO string const date = currentDate.toISOString(); // save the date as an ISO string
const time = currentDate.toLocaleTimeString('en-IN', {hour12:false, timeZone: 'Asia/Kolkata' }); const time = currentDate.toLocaleTimeString('en-IN', { hour12: false, timeZone: 'Asia/Kolkata' });
const tank = new IotData({ hardwareId, tankHeight, maxLevel, minLevel, mode, date, time, tankhardwareId});
// save the document to MongoDB // Create an array of tank documents
await tank.save(); const tankDocuments = tanks.map(tank => ({
tankhardwareId: tank.tankhardwareId,
tankHeight: tank.tankHeight,
maxLevel: tank.maxLevel,
minLevel: tank.minLevel
}));
// get all the tank documents for the current hardwareId sorted in descending order of date and time // create a new IOttank document with the provided data
const tanks = await IotData.find({ hardwareId,tankhardwareId }).sort({ date: -1, time: -1 }); const ottank = new IotData({ hardwareId, mode, tanks: tankDocuments, date, time });
// if the number of documents for the current hardwareId is greater than three, remove the oldest documents until there are only three left // save the document to MongoDB
if (tanks.length > 3) { await ottank.save();
const oldestDocuments = tanks.slice(3);
for (const doc of oldestDocuments) {
await IotData.deleteOne({ _id: doc._id });
}
}
// get the latest three tank documents for the current hardwareId sorted in descending order of date and time // get the latest document sorted in descending order of date and time
const latestTanks = await IotData.find({ hardwareId,tankhardwareId }).sort({ date: -1, time: -1 }).limit(3); const latestOttank = await IotData.findOne({ hardwareId }).sort({ date: -1, time: -1 });
// send the latest three documents in descending order of date and time // send the latest document
reply.code(200).send({ latestTanks: latestTanks.reverse() }); reply.code(200).send({ latestOttank });
} catch (err) { } catch (err) {
// send an error response // send an error response
reply.code(500).send({ error: err.message }); reply.code(500).send({ error: err.message });

@ -81,15 +81,20 @@ const motordataSchema = new mongoose.Schema({
const tankSchema = new mongoose.Schema({
tankhardwareId: { type: String },
tankHeight: { type: String, required: true },
maxLevel: { type: String, required: true },
minLevel: { type: String, required: true }
});
const IOttankSchema = new mongoose.Schema({ const IOttankSchema = new mongoose.Schema({
hardwareId: { type: String}, hardwareId: { type: String },
tankHeight: { type: String, required: true }, mode: { type: Number, required: true },
maxLevel: { type: String, required: true }, tanks: [tankSchema],
minLevel: { type: String, required: true }, date: { type: String, required: true },
date: { type: String, required: true }, time: { type: String, required: true }
time: { type: String, required: true }, });
mode: { type: Number, required: true }
});

Loading…
Cancel
Save