ashok 9 months ago
commit cf7a0a5a01

6
node_modules/.package-lock.json generated vendored

@ -9243,9 +9243,9 @@
"integrity": "sha512-eh0GgfEkpnoWDq+VY8OyvYhFEzBk6jIYbRKdIlyTiAXIVJ8PyBaKb0rp7oDtoddbdoHWhq8wwr+XZ81F1rpNdA==" "integrity": "sha512-eh0GgfEkpnoWDq+VY8OyvYhFEzBk6jIYbRKdIlyTiAXIVJ8PyBaKb0rp7oDtoddbdoHWhq8wwr+XZ81F1rpNdA=="
}, },
"node_modules/node-cron": { "node_modules/node-cron": {
"version": "3.0.2", "version": "3.0.3",
"resolved": "https://registry.npmjs.org/node-cron/-/node-cron-3.0.2.tgz", "resolved": "https://registry.npmjs.org/node-cron/-/node-cron-3.0.3.tgz",
"integrity": "sha512-iP8l0yGlNpE0e6q1o185yOApANRe47UPbLf4YxfbiNHt/RU5eBcGB/e0oudruheSf+LQeDMezqC5BVAb5wwRcQ==", "integrity": "sha512-dOal67//nohNgYWb+nWmg5dkFdIwDm8EpeGYMekPMrngV3637lqnX0lbUcCtgibHTz6SEz7DAIjKvKDFYCnO1A==",
"dependencies": { "dependencies": {
"uuid": "8.3.2" "uuid": "8.3.2"
}, },

14
package-lock.json generated

@ -55,7 +55,7 @@
"mqtt": "^5.10.1", "mqtt": "^5.10.1",
"multer": "^1.4.5-lts.1", "multer": "^1.4.5-lts.1",
"mv": "^2.1.1", "mv": "^2.1.1",
"node-cron": "^3.0.2", "node-cron": "^3.0.3",
"node-schedule": "^2.1.1", "node-schedule": "^2.1.1",
"nodemon": "^2.0.20", "nodemon": "^2.0.20",
"nunjucks": "^3.2.3", "nunjucks": "^3.2.3",
@ -9321,9 +9321,9 @@
"integrity": "sha512-eh0GgfEkpnoWDq+VY8OyvYhFEzBk6jIYbRKdIlyTiAXIVJ8PyBaKb0rp7oDtoddbdoHWhq8wwr+XZ81F1rpNdA==" "integrity": "sha512-eh0GgfEkpnoWDq+VY8OyvYhFEzBk6jIYbRKdIlyTiAXIVJ8PyBaKb0rp7oDtoddbdoHWhq8wwr+XZ81F1rpNdA=="
}, },
"node_modules/node-cron": { "node_modules/node-cron": {
"version": "3.0.2", "version": "3.0.3",
"resolved": "https://registry.npmjs.org/node-cron/-/node-cron-3.0.2.tgz", "resolved": "https://registry.npmjs.org/node-cron/-/node-cron-3.0.3.tgz",
"integrity": "sha512-iP8l0yGlNpE0e6q1o185yOApANRe47UPbLf4YxfbiNHt/RU5eBcGB/e0oudruheSf+LQeDMezqC5BVAb5wwRcQ==", "integrity": "sha512-dOal67//nohNgYWb+nWmg5dkFdIwDm8EpeGYMekPMrngV3637lqnX0lbUcCtgibHTz6SEz7DAIjKvKDFYCnO1A==",
"dependencies": { "dependencies": {
"uuid": "8.3.2" "uuid": "8.3.2"
}, },
@ -20052,9 +20052,9 @@
"integrity": "sha512-eh0GgfEkpnoWDq+VY8OyvYhFEzBk6jIYbRKdIlyTiAXIVJ8PyBaKb0rp7oDtoddbdoHWhq8wwr+XZ81F1rpNdA==" "integrity": "sha512-eh0GgfEkpnoWDq+VY8OyvYhFEzBk6jIYbRKdIlyTiAXIVJ8PyBaKb0rp7oDtoddbdoHWhq8wwr+XZ81F1rpNdA=="
}, },
"node-cron": { "node-cron": {
"version": "3.0.2", "version": "3.0.3",
"resolved": "https://registry.npmjs.org/node-cron/-/node-cron-3.0.2.tgz", "resolved": "https://registry.npmjs.org/node-cron/-/node-cron-3.0.3.tgz",
"integrity": "sha512-iP8l0yGlNpE0e6q1o185yOApANRe47UPbLf4YxfbiNHt/RU5eBcGB/e0oudruheSf+LQeDMezqC5BVAb5wwRcQ==", "integrity": "sha512-dOal67//nohNgYWb+nWmg5dkFdIwDm8EpeGYMekPMrngV3637lqnX0lbUcCtgibHTz6SEz7DAIjKvKDFYCnO1A==",
"requires": { "requires": {
"uuid": "8.3.2" "uuid": "8.3.2"
} }

@ -56,7 +56,7 @@
"mqtt": "^5.10.1", "mqtt": "^5.10.1",
"multer": "^1.4.5-lts.1", "multer": "^1.4.5-lts.1",
"mv": "^2.1.1", "mv": "^2.1.1",
"node-cron": "^3.0.2", "node-cron": "^3.0.3",
"node-schedule": "^2.1.1", "node-schedule": "^2.1.1",
"nodemon": "^2.0.20", "nodemon": "^2.0.20",
"nunjucks": "^3.2.3", "nunjucks": "^3.2.3",

@ -216,24 +216,29 @@ exports.getTank = async (req, reply) => {
const transformedDocs = docs.map((tank) => { const transformedDocs = docs.map((tank) => {
const inputConnections = tank.connections?.inputConnections || []; const inputConnections = tank.connections?.inputConnections || [];
// Count switches
const switchCount = inputConnections.reduce((count, connection) => { const switchCount = inputConnections.reduce((count, connection) => {
return count + (connection.inputismotor === true ? 1 : 0); return count + (connection.inputismotor === true ? 1 : 0);
}, 0); }, 0);
totalSwitchCount += switchCount;
totalSwitchCount += switchCount; // Accumulate the switch_count // Count sensors
// Check if the tank has need_sensor set to "yes"
if (tank.need_sensor?.toLowerCase() === "yes") { if (tank.need_sensor?.toLowerCase() === "yes") {
totalSensorCount++; totalSensorCount++;
} }
// Add the switch_count field inside connections // Determine all_motor_status
const allMotorStatus = inputConnections.some(connection => connection.motor_status === "2");
// Add switch_count and all_motor_status to the response
return { return {
...tank.toObject(), // Convert Mongoose document to plain object ...tank.toObject(), // Convert Mongoose document to plain object
connections: { connections: {
...tank.connections, ...tank.connections,
switch_count: switchCount, switch_count: switchCount,
}, },
all_motor_status: allMotorStatus, // Add all_motor_status field
}; };
}); });
@ -241,8 +246,8 @@ exports.getTank = async (req, reply) => {
status_code: 200, status_code: 200,
data: transformedDocs, data: transformedDocs,
count: transformedDocs.length, count: transformedDocs.length,
total_switch_count: totalSwitchCount, // Add the total switch count total_switch_count: totalSwitchCount,
total_sensor_count: totalSensorCount, // Add the total sensor count total_sensor_count: totalSensorCount,
}); });
}) })
.catch((err) => { .catch((err) => {
@ -2198,20 +2203,20 @@ console.log(fcmToken)
// Schedule a task to send a notification when the threshold time is reached // Schedule a task to send a notification when the threshold time is reached
motorIntervals[motorId] = setTimeout(async () => { // motorIntervals[motorId] = setTimeout(async () => {
try { // try {
// Perform threshold time logic (e.g., notification, motor stop) // // Perform threshold time logic (e.g., notification, motor stop)
console.log(`Threshold time of ${manual_threshold_time} minutes reached for motor ${motorId}`); // console.log(`Threshold time of ${manual_threshold_time} minutes reached for motor ${motorId}`);
await Tank.updateOne( // await Tank.updateOne(
{ customerId, "connections.inputConnections.motor_id": motorId }, // { customerId, "connections.inputConnections.motor_id": motorId },
{ $set: { "connections.inputConnections.$.motor_stop_status": "1" } } // { $set: { "connections.inputConnections.$.motor_stop_status": "1" } }
); // );
clearTimeout(motorIntervals[motorId]); // Clear timeout // clearTimeout(motorIntervals[motorId]); // Clear timeout
delete motorIntervals[motorId]; // delete motorIntervals[motorId];
} catch (error) { // } catch (error) {
console.error("Error in threshold time handling:", error); // console.error("Error in threshold time handling:", error);
} // }
}, thresholdTimeMs); // }, thresholdTimeMs);
const stopCriteria = const stopCriteria =
motorOnType === "time" motorOnType === "time"
? `${req.body.manual_threshold_time} minutes` ? `${req.body.manual_threshold_time} minutes`
@ -2253,27 +2258,27 @@ console.log(fcmToken)
// Schedule threshold check // Schedule threshold check
// const thresholdTimeMs = manual_threshold_time * 60 * 1000; // const thresholdTimeMs = manual_threshold_time * 60 * 1000;
motorIntervals[motorId] = setTimeout(async () => { // motorIntervals[motorId] = setTimeout(async () => {
try { // try {
// const stopMessage = `Threshold time of ${manual_threshold_time} minutes reached for motor supplying water to '${tankName}' in block '${blockName}'.`; // // const stopMessage = `Threshold time of ${manual_threshold_time} minutes reached for motor supplying water to '${tankName}' in block '${blockName}'.`;
// eventEmitter.emit('sendThresholdTimeNotification', fcmToken, stopMessage); // // eventEmitter.emit('sendThresholdTimeNotification', fcmToken, stopMessage);
await Tank.updateOne( // await Tank.updateOne(
{ customerId, "connections.inputConnections.motor_id": motorId }, // { customerId, "connections.inputConnections.motor_id": motorId },
{ // {
$set: { // $set: {
"connections.inputConnections.$.motor_stop_status": "1", // "connections.inputConnections.$.motor_stop_status": "1",
"connections.inputConnections.$.manual_threshold_time": null, // "connections.inputConnections.$.manual_threshold_time": null,
"connections.inputConnections.$.threshold_type": null, // "connections.inputConnections.$.threshold_type": null,
} // }
} // }
); // );
delete motorIntervals[motorId]; // delete motorIntervals[motorId];
} catch (error) { // } catch (error) {
console.error("Error during threshold handling:", error); // console.error("Error during threshold handling:", error);
} // }
}, thresholdTimeMs); // }, thresholdTimeMs);
reply.code(200).send({ message: "Motor started successfully." }); reply.code(200).send({ message: "Motor started successfully." });
// Schedule water level checks after motor start // Schedule water level checks after motor start
@ -2299,22 +2304,8 @@ console.log(fcmToken)
// const stopMessage = `The motor supplying water to '${tankName}' in block '${blockName}' was stopped manually at ${stopTime}.`; // const stopMessage = `The motor supplying water to '${tankName}' in block '${blockName}' was stopped manually at ${stopTime}.`;
// eventEmitter.emit("sendMotorStopNotification", fcmToken, stopMessage); // eventEmitter.emit("sendMotorStopNotification", fcmToken, stopMessage);
await Tank.updateOne(
{ customerId, "connections.inputConnections.motor_id": motorId },
{
$set: {
"connections.inputConnections.$.motor_stop_status": motorStopStatus,
"connections.inputConnections.$.motor_on_type": "manual",
"connections.inputConnections.$.stopTime": stopTime
}
}
);
// Clear intervals if any
if (motorIntervals[motorId]) {
clearInterval(motorIntervals[motorId]);
delete motorIntervals[motorId];
}
eventEmitter.emit( eventEmitter.emit(
"motorStop", "motorStop",
fcmToken, fcmToken,
@ -2326,23 +2317,23 @@ console.log(fcmToken)
motorOnType motorOnType
); );
await Tank.updateOne( // await Tank.updateOne(
{ customerId, "connections.inputConnections.motor_id": motorId }, // { customerId, "connections.inputConnections.motor_id": motorId },
{ // {
$set: { // $set: {
"connections.inputConnections.$.motor_stop_status": "1", // "connections.inputConnections.$.motor_stop_status": "1",
"connections.inputConnections.$.motor_on_type": "manual", // "connections.inputConnections.$.motor_on_type": "manual",
} // }
} // }
); // );
// const stopMessage = `Motor supplying water to '${tankName}' in block '${blockName}' stopped manually at ${stopTime}.`; // const stopMessage = `Motor supplying water to '${tankName}' in block '${blockName}' stopped manually at ${stopTime}.`;
// eventEmitter.emit('sendMotorStopNotification', fcmToken, stopMessage); // eventEmitter.emit('sendMotorStopNotification', fcmToken, stopMessage);
if (motorIntervals[motorId]) { // if (motorIntervals[motorId]) {
clearTimeout(motorIntervals[motorId]); // clearTimeout(motorIntervals[motorId]);
delete motorIntervals[motorId]; // delete motorIntervals[motorId];
} // }
} else { } else {
throw new Error("Invalid action provided."); throw new Error("Invalid action provided.");
@ -2512,8 +2503,115 @@ console.log(fcmToken)
}, 30000); // Check every minute }, 30000); // Check every minute
} }
}else if (req.body.threshold_type === "litres") {
console.log("entered litres")
const receiver_tank_info7 = await Tank.findOne({ customerId, tankName: req.body.to, tankLocation: req.body.to_type.toLowerCase() });
const supplier_tank_info7 = await Tank.findOne({ customerId, tankName: req.body.from, tankLocation: req.body.from_type.toLowerCase() });
const newMotorData = new MotorData({
customerId: customerId,
motor_id: motorId,
start_instance_id: start_instance_id,
supplierTank: req.body.from,
receiverTank: req.body.to,
supplier_type: req.body.from_type,
receiver_type: req.body.to_type,
startTime: req.body.startTime,
receiverInitialwaterlevel:parseInt(receiver_tank_info7.waterlevel, 10),
supplierInitialwaterlevel:parseInt(supplier_tank_info7.waterlevel, 10)
});
await newMotorData.save();
// If threshold type is percentage, calculate percentage threshold
const receiver_tank_info = await Tank.findOne({ customerId, tankName: req.body.to, tankLocation: req.body.to_type.toLowerCase() });
const supplier_tank_info = await Tank.findOne({ customerId, tankName: req.body.from, tankLocation: req.body.from_type.toLowerCase() });
if (!receiver_tank_info) {
throw new Error("Receiver tank not found.");
}
if (!supplier_tank_info) {
throw new Error("Supplierr tank not found.");
}
const supplier_capacity = parseInt(supplier_tank_info.capacity, 10);
const supplier_waterLevel = parseInt(supplier_tank_info.waterlevel, 10);
const capacity = parseInt(receiver_tank_info.capacity, 10);
const waterLevel = parseInt(receiver_tank_info.waterlevel, 10);
const desired_percentage = parseInt(req.body.manual_threshold_litres.replace(/,/g, ''), 10);
console.log(desired_percentage)
const threshold_water_level = waterLevel+desired_percentage;
const supplier_threshold = supplier_waterLevel-desired_percentage
console.log(supplier_threshold,"supplier_threshold")
for await (const tank of Tank.find({ "connections.inputConnections.motor_id": motorId })) {
this.publishMotorStopStatus(motorId, motorStopStatus);
for await (const tank of Tank.find({ "connections.inputConnections.motor_id": motorId })) {
const index = tank.connections.inputConnections.findIndex(connection => connection.motor_id === motorId);
if (index !== -1) {
await Tank.updateOne(
{ customerId, "connections.inputConnections.motor_id": motorId },
{ $set: { [`connections.inputConnections.${index}.manual_threshold_percentage`]: supplier_threshold.toString(), [`connections.inputConnections.${index}.startTime`]: req.body.startTime } }
);
}
}
// Update water level threshold
// Start monitoring water level based on threshold percentage
motorIntervals[motorId] = setInterval(async () => {
// Check if water level has reached the threshold percentage
const supplier_tank_info1 = await Tank.findOne({ customerId, tankName: req.body.from, tankLocation: req.body.from_type.toLowerCase() });
const current_water_level = parseInt(supplier_tank_info1.waterlevel, 10);
if (current_water_level <= supplier_threshold) {
// Stop the motor pump
await Tank.updateOne(
{ customerId, "connections.inputConnections.motor_id": motorId },
{
$set: {
"connections.inputConnections.$.motor_stop_status": "1",
"connections.inputConnections.$.threshold_type": null,
"connections.inputConnections.$.manual_threshold_time": null,
"connections.inputConnections.$.manual_threshold_percentage": null
} }
} }
);
clearInterval(motorIntervals[motorId]); // Clear interval
delete motorIntervals[motorId];
this.publishMotorStopStatus(motorId, "1");
await delay(300000);
const motorData = await MotorData.findOne({ customerId, motor_id: motorId, start_instance_id: start_instance_id });
if (motorData) {
const receiverTank = await Tank.findOne({ customerId, tankName: motorData.receiverTank, tankLocation: motorData.receiver_type.toLowerCase() });
const receiverFinalWaterLevel = parseInt(receiverTank.waterlevel, 10);
const quantityDelivered = receiverFinalWaterLevel - parseInt(motorData.receiverInitialwaterlevel, 10);
const stopTime = formatDate(new Date());
await MotorData.updateOne(
{ customerId, motor_id: motorId, start_instance_id: start_instance_id },
{
$set: {
stopTime:stopTime,
receiverfinalwaterlevel: receiverFinalWaterLevel.toString(),
quantity_delivered: quantityDelivered.toString()
}
}
);
}
}
}, 20000);
}
}
}
// Respond with success message // Respond with success message
reply.code(200).send({ message: `Motor ${action === "start" ? "started" : "stopped"} successfully.` }); reply.code(200).send({ message: `Motor ${action === "start" ? "started" : "stopped"} successfully.` });
@ -5243,3 +5341,73 @@ async function removeDuplicates () {
// Run the remove duplicates function // Run the remove duplicates function
// removeDuplicates(); // removeDuplicates();
console.log("this is for testing autopush,line located in tankscontroller") console.log("this is for testing autopush,line located in tankscontroller")
const calculateDailyConsumptionAndNotify = async () => {
try {
const today = moment().startOf("day");
const yesterday = moment(today).subtract(1, "days");
// Fetch all active users
const activeUsers = await User.find({ });
for (const user of activeUsers) {
const { customerId, fcmIds } = user;
// Fetch daily consumption for the customer
const consumptions = await TankConsumptionOriginalSchema.find({
customerId,
time: {
$gte: yesterday.format("DD-MMM-YYYY - HH:mm"),
$lt: today.format("DD-MMM-YYYY - HH:mm"),
},
});
// Calculate total consumption
const totalConsumption = consumptions.reduce((total, record) => {
return total + parseInt(record.consumption, 10);
}, 0);
// Prepare tank-wise consumption details
const tankDetails = consumptions.map((record) => ({
tankName: record.tankName,
consumption: record.consumption,
}));
// Send notification
const notificationTitle = "Daily Water Consumption Report";
const notificationBody = `
Total Consumption: ${totalConsumption} liters
Tank Details: ${tankDetails
.map((tank) => `${tank.tankName}: ${tank.consumption} liters`)
.join(", ")}
`;
if (fcmIds && fcmIds.length > 0) {
await sendNotification(fcmIds, notificationTitle, notificationBody);
}
}
console.log("Daily consumption notifications sent successfully.");
} catch (err) {
console.error("Error sending daily consumption notifications:", err);
}
};
// cron.schedule("0 11:57 * * *", async () => {
// console.log("Starting daily consumption notification task...");
// await calculateDailyConsumptionAndNotify();
// });
cron.schedule(
"0 9 * * *",
async () => {
console.log("Starting daily consumption notification task...");
await calculateDailyConsumptionAndNotify();
},
{
timezone: "Asia/Kolkata", // Specify the timezone
}
);

@ -61,6 +61,7 @@ const tanksSchema = new mongoose.Schema({
notificationSentCriticalHigh: { type: Boolean }, notificationSentCriticalHigh: { type: Boolean },
notificationSentVeryHigh: { type: Boolean }, notificationSentVeryHigh: { type: Boolean },
notificationSentHigh: { type: Boolean }, notificationSentHigh: { type: Boolean },
all_motor_status: { type: Boolean },
connections: { connections: {
source: { type: String }, source: { type: String },

Loading…
Cancel
Save