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) => {
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
const currentDate = new Date();
const date = currentDate.toISOString(); // save the date as an ISO string
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
await tank.save();
// Create an array of tank documents
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
const tanks = await IotData.find({ hardwareId,tankhardwareId }).sort({ date: -1, time: -1 });
// create a new IOttank document with the provided data
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
if (tanks.length > 3) {
const oldestDocuments = tanks.slice(3);
for (const doc of oldestDocuments) {
await IotData.deleteOne({ _id: doc._id });
}
}
// save the document to MongoDB
await ottank.save();
// get the latest three tank documents for the current hardwareId sorted in descending order of date and time
const latestTanks = await IotData.find({ hardwareId,tankhardwareId }).sort({ date: -1, time: -1 }).limit(3);
// get the latest document sorted in descending order of date and time
const latestOttank = await IotData.findOne({ hardwareId }).sort({ date: -1, time: -1 });
// send the latest three documents in descending order of date and time
reply.code(200).send({ latestTanks: latestTanks.reverse() });
// send the latest document
reply.code(200).send({ latestOttank });
} catch (err) {
// send an error response
reply.code(500).send({ error: err.message });

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

Loading…
Cancel
Save