|
|
|
@ -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});
|
|
|
|
|
const time = currentDate.toLocaleTimeString('en-IN', { hour12: false, timeZone: 'Asia/Kolkata' });
|
|
|
|
|
|
|
|
|
|
// 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 });
|
|
|
|
|