|
|
|
@ -4281,6 +4281,8 @@ const motorActionAuto = async (req, reply) => {
|
|
|
|
|
|
|
|
|
|
let motorStopStatus;
|
|
|
|
|
const tank = await Tank.findOne({ customerId, "connections.inputConnections.motor_id": motorId });
|
|
|
|
|
const user = await User.findOne({ customerId });
|
|
|
|
|
const allowNotifications = user?.automaticStartAndStopNotify ?? true; // Default to true if not set
|
|
|
|
|
|
|
|
|
|
if (!tank) {
|
|
|
|
|
throw new Error("Tank not found for the provided motor ID.");
|
|
|
|
@ -4302,16 +4304,18 @@ const motorActionAuto = async (req, reply) => {
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
eventEmitter.emit(
|
|
|
|
|
"motorStartAutomatic",
|
|
|
|
|
fcmTokens,
|
|
|
|
|
tankName,
|
|
|
|
|
blockName,
|
|
|
|
|
startTime,
|
|
|
|
|
"Automatic",
|
|
|
|
|
typeOfWater,
|
|
|
|
|
threshold
|
|
|
|
|
);
|
|
|
|
|
if (allowNotifications) {
|
|
|
|
|
eventEmitter.emit(
|
|
|
|
|
"motorStartAutomatic",
|
|
|
|
|
fcmTokens,
|
|
|
|
|
tankName,
|
|
|
|
|
blockName,
|
|
|
|
|
startTime,
|
|
|
|
|
"Automatic",
|
|
|
|
|
typeOfWater,
|
|
|
|
|
threshold
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -4331,16 +4335,17 @@ const motorActionAuto = async (req, reply) => {
|
|
|
|
|
const formattedDate = currentDateTime.toLocaleDateString();
|
|
|
|
|
const formattedTime = currentDateTime.toLocaleTimeString();
|
|
|
|
|
|
|
|
|
|
const stopMessage =
|
|
|
|
|
`🚰 Motor Name: ${tankName}-${blockName}-${typeOfWater}\n` +
|
|
|
|
|
`🛢️ Tank Name: '${tankName}'\n` +
|
|
|
|
|
`🏢 Block Name: '${blockName}'\n` +
|
|
|
|
|
`🕒 Pump stopped at: ${stopTime}\n` +
|
|
|
|
|
`⏳ Operation Duration: ${threshold} `;
|
|
|
|
|
|
|
|
|
|
// Send stop notification
|
|
|
|
|
await sendNotification(fcmTokens, "Arminta Water Management", stopMessage);
|
|
|
|
|
if (allowNotifications) {
|
|
|
|
|
const stopMessage =
|
|
|
|
|
`🚰 Motor Name: ${tankName}-${blockName}-${typeOfWater}\n` +
|
|
|
|
|
`🛢️ Tank Name: '${tankName}'\n` +
|
|
|
|
|
`🏢 Block Name: '${blockName}'\n` +
|
|
|
|
|
`🕒 Pump stopped at: ${stopTime}\n` +
|
|
|
|
|
`⏳ Operation Duration: ${threshold} `;
|
|
|
|
|
|
|
|
|
|
// Send stop notification
|
|
|
|
|
await sendNotification(fcmTokens, "Arminta Water Management", stopMessage);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -7009,6 +7014,25 @@ exports.sendUserManualStartAndStop = async (request, reply) => {
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
exports.sendUserAutomaticStartAndStop = async (request, reply) => {
|
|
|
|
|
const { customerId, automaticStartAndStopNotify } = request.body;
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
const user = await User.findOneAndUpdate(
|
|
|
|
|
{ customerId },
|
|
|
|
|
{ automaticStartAndStopNotify},
|
|
|
|
|
{ new: true, upsert: true } // Create user if not exists
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
console.log(`User ${customerId} updated: Allowed - ${automaticStartAndStopNotify}`);
|
|
|
|
|
|
|
|
|
|
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();
|
|
|
|
|