|
|
|
@ -956,43 +956,71 @@ exports.calculateCapacity = async (req, reply) => {
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
exports.IotDevice = async (req, reply) => {
|
|
|
|
|
try {
|
|
|
|
|
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 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
|
|
|
|
|
|
|
|
|
|
}));
|
|
|
|
|
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));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// create a new IOttank document with the provided data
|
|
|
|
|
const ottank = new IotData({ hardwareId, mode, tanks: tankDocuments, date, time });
|
|
|
|
|
// 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 document to MongoDB
|
|
|
|
|
// Save the updated or new IOttank document to MongoDB
|
|
|
|
|
await ottank.save();
|
|
|
|
|
|
|
|
|
|
// 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 });
|
|
|
|
|
// Send the updated or new IOttank document
|
|
|
|
|
reply.code(200).send({ latestOttank: ottank });
|
|
|
|
|
} 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})
|
|
|
|
|