made changes in iot create data

master
varun 2 years ago
parent cf70001a5e
commit c23bcb9495

@ -960,7 +960,7 @@ exports.calculateCapacity = async (req, reply) => {
exports.IotDevice1 = async (req, reply) => { exports.IotDevice = async (req, reply) => {
try { try {
const { hardwareId, mode, tanks } = req.body; const { hardwareId, mode, tanks } = req.body;
@ -1010,21 +1010,35 @@ exports.IotDevice1 = async (req, reply) => {
exports.getIotD = async(req, reply) => { // exports.getIotD = async(req, reply) => {
// try {
// await IotData.find({hardwareId: req.query.hardwareId})
// .exec()
// .then((docs) => {
// reply.send({ status_code: 200, data: docs, count: docs.length });
// })
// .catch((err) => {
// console.log(err);
// reply.send({ error: err });
// });
// } catch (err) {
// throw boom.boomify(err);
// }
// }
exports.getIotD = async (req, reply) => {
try { try {
await IotData.find({hardwareId: req.query.hardwareId}) const latestRecords = await IotData.find({ hardwareId: req.query.hardwareId })
.exec() .sort({ date: -1, time: -1 }) // Sort by date and time in descending order
.then((docs) => { .limit(3) // Limit the result to 3 records
reply.send({ status_code: 200, data: docs, count: docs.length }); .exec();
})
.catch((err) => { reply.send({ status_code: 200, data: latestRecords, count: latestRecords.length });
console.log(err);
reply.send({ error: err });
});
} catch (err) { } catch (err) {
console.error(err);
throw boom.boomify(err); throw boom.boomify(err);
} }
} };
exports.getLatestData = async (req, reply) => { exports.getLatestData = async (req, reply) => {
@ -1209,32 +1223,3 @@ exports.deletemotordatarecordsbefore7days = async (req, reply) => {
} }
}; };
exports.IotDevice = async (req, reply) => {
try {
// Schedule the task to run every day at 10 seconds past the minute
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' });
// 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
}));
reply.send({ status_code: 200,hardwareId:hardwareId, mode:mode, tanks:tanks,date: date,
time: time });
} catch (err) {
throw boom.boomify(err);
}
};

@ -383,7 +383,7 @@ module.exports = function (fastify, opts, next) {
}, },
], ],
}, },
preHandler: fastify.auth([fastify.authenticate]), //preHandler: fastify.auth([fastify.authenticate]),
handler: tanksController.getIotD, handler: tanksController.getIotD,
}); });

Loading…
Cancel
Save