|
|
|
@ -6339,19 +6339,45 @@ client.on('message', async (topic, message) => {
|
|
|
|
|
|
|
|
|
|
// Find the inputConnection for the motor and update motor status
|
|
|
|
|
const inputConnection = motorTank.connections.inputConnections.find(conn => conn.motor_id === hw_Id); // Updated variable name
|
|
|
|
|
const user = await User.findOne({ customerId: motorTank.customerId }); // Fetch user by customerId
|
|
|
|
|
const allowNotifications = user?.manualStartAndStopNotify ?? true; // Default to true if not set
|
|
|
|
|
|
|
|
|
|
if (inputConnection) {
|
|
|
|
|
inputConnection.motor_status = status; // Update motor status
|
|
|
|
|
if (inputConnection.motor_stop_status === "1" && status === 2 && inputConnection.motor_on_type !== "forced_manual") {
|
|
|
|
|
const tankName = motorTank.tankName;
|
|
|
|
|
if (allowNotifications && inputConnection.motor_stop_status === "1" && status === 2 && inputConnection.motor_on_type !== "forced_manual") {
|
|
|
|
|
const currentTime = moment().tz('Asia/Kolkata').format('DD-MMM-YYYY - HH:mm');
|
|
|
|
|
inputConnection.motor_stop_status = "2";
|
|
|
|
|
inputConnection.motor_on_type = "forced_manual";
|
|
|
|
|
inputConnection.startTime = currentTime;
|
|
|
|
|
|
|
|
|
|
eventEmitter.emit(
|
|
|
|
|
"sendMotorStartNotification",
|
|
|
|
|
fcmToken, // FCM tokens
|
|
|
|
|
hw_Id, // Motor ID
|
|
|
|
|
inputConnection.water_level || 0, // Water level
|
|
|
|
|
motorTank.blockName || "N/A", // Block name
|
|
|
|
|
tankName, // Tank name
|
|
|
|
|
inputConnection.motor_on_type, // Motor on type
|
|
|
|
|
"threshold", // Stop criteria
|
|
|
|
|
manual_threshold_time // Threshold time in mins
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (inputConnection.motor_stop_status === "2" && status === 1) {
|
|
|
|
|
if (allowNotifications && inputConnection.motor_stop_status === "2" && status === 1) {
|
|
|
|
|
const currentTime = moment().tz('Asia/Kolkata').format('DD-MMM-YYYY - HH:mm');
|
|
|
|
|
inputConnection.motor_stop_status = "1";
|
|
|
|
|
inputConnection.stopTime = currentTime;
|
|
|
|
|
|
|
|
|
|
eventEmitter.emit(
|
|
|
|
|
"sendMotorStopNotification",
|
|
|
|
|
fcmToken, // FCM tokens
|
|
|
|
|
hw_Id, // Motor ID
|
|
|
|
|
inputConnection.water_level || 0, // Water level
|
|
|
|
|
motorTank.blockName || "N/A", // Block name
|
|
|
|
|
tankName, // Tank name
|
|
|
|
|
inputConnection.motor_on_type // Motor on type
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
await motorTank.save(); // Save the updated tank
|
|
|
|
@ -6964,6 +6990,25 @@ exports.sendUserSetCriticallyLowWaterNotificationsSwitch = async (request, reply
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
exports.sendUserManualStartAndStop = async (request, reply) => {
|
|
|
|
|
const { customerId, manualStartAndStopNotify } = request.body;
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
const user = await User.findOneAndUpdate(
|
|
|
|
|
{ customerId },
|
|
|
|
|
{ manualStartAndStopNotify},
|
|
|
|
|
{ new: true, upsert: true } // Create user if not exists
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
console.log(`User ${customerId} updated: Allowed - ${manualStartAndStopNotify}`);
|
|
|
|
|
|
|
|
|
|
return reply.send({ success: true, user });
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error("Error setting notification time:", error);
|
|
|
|
|
return reply.status(500).send({ success: false, message: "Internal server error" });
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// const calculateWaterLevelAndNotify = async () => {
|
|
|
|
|
// try {
|
|
|
|
|
// const now = moment();
|
|
|
|
@ -7229,7 +7274,7 @@ cron.schedule('* * * * *', async () => {
|
|
|
|
|
timezone: "Asia/Kolkata",
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//run the every one hour
|
|
|
|
|
cron.schedule('0 * * * *', async () => {
|
|
|
|
|
console.log("Checking for user notification times...");
|
|
|
|
|
await calculateLowWaterLevelAndNotify();
|
|
|
|
|