|
|
@ -159,6 +159,23 @@ exports.getTank = async (req, reply) => {
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
//};
|
|
|
|
//};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
exports.getTankmotordata = async (req, reply) => {
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
await MotorData.find({customerId: req.query.customerId})
|
|
|
|
|
|
|
|
.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.updateTanklevels = async (req, reply) => {
|
|
|
|
exports.updateTanklevels = async (req, reply) => {
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
const customerId = req.params.customerId;
|
|
|
|
const customerId = req.params.customerId;
|
|
|
@ -639,7 +656,8 @@ exports.motorAction = async (req, reply) => {
|
|
|
|
console.log(sump_final_water_level,"2")
|
|
|
|
console.log(sump_final_water_level,"2")
|
|
|
|
sump_water_levels = sump_water_levels.filter(tank => tank.supplier_tank !== supplier_tank);
|
|
|
|
sump_water_levels = sump_water_levels.filter(tank => tank.supplier_tank !== supplier_tank);
|
|
|
|
|
|
|
|
|
|
|
|
const quantity_delivered = sump_water_level1-sump_final_water_level
|
|
|
|
const quantity_delivered = Math.abs(sump_water_level1 - sump_final_water_level);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (supplier_tank_type === "sump") {
|
|
|
|
if (supplier_tank_type === "sump") {
|
|
|
|
|
|
|
|
|
|
|
@ -738,16 +756,18 @@ exports.motorAction = async (req, reply) => {
|
|
|
|
exports.consumption = async (req, reply) => {
|
|
|
|
exports.consumption = async (req, reply) => {
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
const customerId = req.params.customerId;
|
|
|
|
const customerId = req.params.customerId;
|
|
|
|
const tanks = await Tank.find({ customerId });
|
|
|
|
const tanks = await Tank.find({ customerId,tankLocation:"overhead"});
|
|
|
|
const tankData = [];
|
|
|
|
const tankData = [];
|
|
|
|
for (const tank of tanks) {
|
|
|
|
for (const tank of tanks) {
|
|
|
|
const tankId = tank._id;
|
|
|
|
const tankId = tank._id;
|
|
|
|
let capacity = parseInt(tank.capacity.replace(/,/g, ''), 10);
|
|
|
|
const waterlevel_at_midnight = parseInt(tank.waterlevel_at_midnight.replace(/,/g, ''), 10);
|
|
|
|
let waterLevel = capacity*2+1700; // initial water level
|
|
|
|
const total_water_added_from_midnight = parseInt(tank.total_water_added_from_midnight.replace(/,/g, ''), 10);
|
|
|
|
const tankname = tank.tankName
|
|
|
|
const waterlevel = parseInt(tank.waterlevel.replace(/,/g, ''), 10);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const tankname = tank.tankName
|
|
|
|
|
|
|
|
const consumption = (waterlevel_at_midnight+total_water_added_from_midnight)-waterlevel
|
|
|
|
|
|
|
|
|
|
|
|
const newWaterLevel = Math.floor(waterLevel);
|
|
|
|
const newWaterLevel = Math.floor(consumption);
|
|
|
|
tankData.push({ tankname, waterLevel: newWaterLevel });
|
|
|
|
tankData.push({ tankname, waterLevel: newWaterLevel });
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -939,33 +959,33 @@ 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, mode, tanks } = 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 });
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// save the document to MongoDB
|
|
|
|
// Create an array of tank documents
|
|
|
|
await tank.save();
|
|
|
|
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 }).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
|
|
|
|
// create a new IOttank document with the provided data
|
|
|
|
if (tanks.length > 3) {
|
|
|
|
const ottank = new IotData({ hardwareId, mode, tanks: tankDocuments, date, time });
|
|
|
|
const oldestDocuments = tanks.slice(3);
|
|
|
|
|
|
|
|
for (const doc of oldestDocuments) {
|
|
|
|
// save the document to MongoDB
|
|
|
|
await IotData.deleteOne({ _id: doc._id });
|
|
|
|
await ottank.save();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// get the latest three tank documents for the current hardwareId sorted in descending order of date and time
|
|
|
|
// get the latest document sorted in descending order of date and time
|
|
|
|
const latestTanks = await IotData.find({ hardwareId }).sort({ date: -1, time: -1 }).limit(3);
|
|
|
|
const latestOttank = await IotData.findOne({ hardwareId }).sort({ date: -1, time: -1 });
|
|
|
|
|
|
|
|
|
|
|
|
// send the latest three documents in descending order of date and time
|
|
|
|
// send the latest document
|
|
|
|
reply.code(200).send({ latestTanks: latestTanks.reverse() });
|
|
|
|
reply.code(200).send({ latestOttank });
|
|
|
|
} catch (err) {
|
|
|
|
} catch (err) {
|
|
|
|
// send an error response
|
|
|
|
// send an error response
|
|
|
|
reply.code(500).send({ error: err.message });
|
|
|
|
reply.code(500).send({ error: err.message });
|
|
|
|