raj 2 years ago
commit 73df149ac9

@ -956,19 +956,19 @@ exports.calculateCapacity = async (req, reply) => {
exports.IotDevice = async (req, reply) => { exports.IotDevice = async (req, reply) => {
try { try {
const { hardwareId, tankHeight, maxLevel, minLevel, mode } = req.body; const { hardwareId, tankHeight, maxLevel, minLevel, mode,tankhardwareId } = req.body;
// create a new tank document with the current date and time // create a new tank document with the current date and time
const currentDate = new Date(); const currentDate = new Date();
const date = currentDate.toISOString(); // save the date as an ISO string const date = currentDate.toISOString(); // save the date as an ISO string
const time = currentDate.toLocaleTimeString('en-IN', {hour12:false, timeZone: 'Asia/Kolkata' }); const time = currentDate.toLocaleTimeString('en-IN', {hour12:false, timeZone: 'Asia/Kolkata' });
const tank = new IotData({ hardwareId, tankHeight, maxLevel, minLevel, mode, date, time }); const tank = new IotData({ hardwareId, tankHeight, maxLevel, minLevel, mode, date, time, tankhardwareId});
// save the document to MongoDB // save the document to MongoDB
await tank.save(); await tank.save();
// get all the tank documents for the current hardwareId sorted in descending order of date and time // get all the tank documents for the current hardwareId sorted in descending order of date and time
const tanks = await IotData.find({ hardwareId }).sort({ date: -1, time: -1 }); const tanks = await IotData.find({ hardwareId,tankhardwareId }).sort({ date: -1, time: -1 });
// if the number of documents for the current hardwareId is greater than three, remove the oldest documents until there are only three left // 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) { if (tanks.length > 3) {
@ -979,7 +979,7 @@ exports.IotDevice = async (req, reply) => {
} }
// get the latest three tank documents for the current hardwareId sorted in descending order of date and time // get the latest three tank documents for the current hardwareId sorted in descending order of date and time
const latestTanks = await IotData.find({ hardwareId }).sort({ date: -1, time: -1 }).limit(3); const latestTanks = await IotData.find({ hardwareId,tankhardwareId }).sort({ date: -1, time: -1 }).limit(3);
// send the latest three documents in descending order of date and time // send the latest three documents in descending order of date and time
reply.code(200).send({ latestTanks: latestTanks.reverse() }); reply.code(200).send({ latestTanks: latestTanks.reverse() });

@ -343,7 +343,7 @@ module.exports = function (fastify, opts, next) {
tankHeight: { type: "string"}, tankHeight: { type: "string"},
maxLevel: { type: "string" }, maxLevel: { type: "string" },
minLevel: { type: "string" }, minLevel: { type: "string" },
tankhardwareId:{ type: "string" },
mode: { type: "string" }, mode: { type: "string" },
}, },
}, },

Loading…
Cancel
Save