changes regarding iot data

master
varun 2 years ago
parent b2d4fd48b9
commit cb7695b84d

@ -956,71 +956,44 @@ exports.calculateCapacity = async (req, reply) => {
}
};
exports.IotDevice = async (req, reply) => {
try {
const { hardwareId, mode, tanks } = req.body;
// 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
// create a new tank document with the current date and time
const currentDate = new Date();
ottank.date = currentDate.toISOString();
ottank.time = currentDate.toLocaleTimeString('en-IN', { hour12: false, timeZone: 'Asia/Kolkata' });
// Remove excess tank records for each tankhardwareId
for (const tank of ottank.tanks) {
if (tank.tankhardwareId === tank.tankhardwareId) {
const tankRecords = ottank.tanks.filter(t => t.tankhardwareId === tank.tankhardwareId);
if (tankRecords.length > 3) {
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));
}
}
}
// Iterate through the tanks received in the request
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 updated or new IOttank document to MongoDB
const date = currentDate.toISOString(); // save the date as an ISO string
const time = currentDate.toLocaleTimeString('en-IN', { hour12: false, timeZone: 'Asia/Kolkata' });
// Create an array of tank documents
const tankDocuments = tanks.map(tank => ({
tankhardwareId: tank.tankhardwareId,
tankHeight: tank.tankHeight,
maxLevel: tank.maxLevel,
minLevel: tank.minLevel,
date:date,
time:time
}));
// create a new IOttank document with the provided data
const ottank = new IotData({ hardwareId, mode, tanks: tankDocuments, date, time });
// save the document to MongoDB
await ottank.save();
// Send the updated or new IOttank document
reply.code(200).send({ latestOttank: ottank });
// 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 document
reply.code(200).send({ latestOttank });
} catch (err) {
// Send an error response
// send an error response
reply.code(500).send({ error: err.message });
}
};
exports.getIotD = async(req, reply) => {
try {
await IotData.find({hardwareId: req.query.hardwareId})

Loading…
Cancel
Save