changes regarding iot data

master
varun 2 years ago
parent eaaf9f1216
commit cfe1351d63

@ -956,43 +956,71 @@ exports.calculateCapacity = async (req, reply) => {
} }
}; };
exports.IotDevice = async (req, reply) => { exports.IotDevice = async (req, reply) => {
try { try {
const { hardwareId, mode, tanks } = req.body; const { hardwareId, mode, tanks } = req.body;
// create a new tank document with the current date and time // Find the existing IOttank document with the provided hardwareId
let ottank = await IotData.findOne({ hardwareId });
// If no existing document is found, create a new one
if (!ottank) {
ottank = new IotData({ hardwareId, mode, tanks: [], date: '', time: '' });
}
// Update the date and time of the IOttank document
const currentDate = new Date(); const currentDate = new Date();
const date = currentDate.toISOString(); // save the date as an ISO string ottank.date = currentDate.toISOString();
const time = currentDate.toLocaleTimeString('en-IN', { hour12: false, timeZone: 'Asia/Kolkata' }); ottank.time = currentDate.toLocaleTimeString('en-IN', { hour12: false, timeZone: 'Asia/Kolkata' });
// Create an array of tank documents // Remove excess tank records for each tankhardwareId
const tankDocuments = tanks.map(tank => ({ for (const tank of ottank.tanks) {
tankhardwareId: tank.tankhardwareId, if (tank.tankhardwareId === tank.tankhardwareId) {
tankHeight: tank.tankHeight, const tankRecords = ottank.tanks.filter(t => t.tankhardwareId === tank.tankhardwareId);
maxLevel: tank.maxLevel, if (tankRecords.length > 3) {
minLevel: tank.minLevel tankRecords.sort((a, b) => new Date(b.date) - new Date(a.date));
const excessRecords = tankRecords.slice(3);
})); ottank.tanks = ottank.tanks.filter(t => !excessRecords.includes(t));
}
}
}
// create a new IOttank document with the provided data // Iterate through the tanks received in the request
const ottank = new IotData({ hardwareId, mode, tanks: tankDocuments, date, time }); for (const tank of tanks) {
// Find the existing tank document with the provided tankhardwareId
const existingTank = ottank.tanks.find(t => t.tankhardwareId === tank.tankhardwareId);
if (existingTank) {
// Update the existing tank document
existingTank.tankHeight = tank.tankHeight;
existingTank.maxLevel = tank.maxLevel;
existingTank.minLevel = tank.minLevel;
existingTank.date = ottank.date;
existingTank.time = ottank.time;
} else {
// Create a new tank document and add it to the IOttank document
ottank.tanks.push({
tankhardwareId: tank.tankhardwareId,
tankHeight: tank.tankHeight,
maxLevel: tank.maxLevel,
minLevel: tank.minLevel,
date: ottank.date,
time: ottank.time
});
}
}
// save the document to MongoDB // Save the updated or new IOttank document to MongoDB
await ottank.save(); await ottank.save();
// get the latest document sorted in descending order of date and time // Send the updated or new IOttank document
const latestOttank = await IotData.findOne({ hardwareId }).sort({ date: -1, time: -1 }); reply.code(200).send({ latestOttank: ottank });
// send the latest document
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 });
} }
}; };
exports.getIotD = async(req, reply) => { exports.getIotD = async(req, reply) => {
try { try {
await IotData.find({hardwareId: req.query.hardwareId}) await IotData.find({hardwareId: req.query.hardwareId})

@ -85,7 +85,9 @@ const tankSchema = new mongoose.Schema({
tankhardwareId: { type: String }, tankhardwareId: { type: String },
tankHeight: { type: String, required: true }, tankHeight: { type: String, required: true },
maxLevel: { type: String, required: true }, maxLevel: { type: String, required: true },
minLevel: { type: String, required: true } minLevel: { type: String, required: true },
date: { type: String, required: true },
time: { type: String, required: true }
}); });
const IOttankSchema = new mongoose.Schema({ const IOttankSchema = new mongoose.Schema({

Loading…
Cancel
Save