|
|
|
@ -6381,72 +6381,88 @@ client.on('message', async (topic, message) => {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 🔹 Motor Status Processing
|
|
|
|
|
const status = Motor_status;
|
|
|
|
|
const motorTank = await Tank.findOne({ "connections.inputConnections.motor_id": hw_Id });
|
|
|
|
|
// 🔹 Motor Status Processing
|
|
|
|
|
const status = Motor_status;
|
|
|
|
|
const motorTank = await Tank.findOne({ "connections.inputConnections.motor_id": hw_Id });
|
|
|
|
|
|
|
|
|
|
if (!motorTank) {
|
|
|
|
|
console.log(`⚠️ Motor not found for motor_id: ${hw_Id}`);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (!motorTank) {
|
|
|
|
|
console.log(`⚠️ Motor not found for motor_id: ${hw_Id}`);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const inputConnection = motorTank.connections.inputConnections.find(conn => conn.motor_id === hw_Id);
|
|
|
|
|
const user = await User.findOne({ customerId: motorTank.customerId });
|
|
|
|
|
const allowNotifications = user?.manualStartAndStopNotify ?? true;
|
|
|
|
|
// ✅ Extract customerId from motorTank
|
|
|
|
|
const customerId = motorTank.customerId;
|
|
|
|
|
|
|
|
|
|
if (inputConnection) {
|
|
|
|
|
inputConnection.motor_status = status;
|
|
|
|
|
const tankName = motorTank.tankName;
|
|
|
|
|
const blockName = motorTank.blockName || "N/A";
|
|
|
|
|
if (!customerId) {
|
|
|
|
|
console.error(`❌ Error: customerId is missing for motor_id: ${hw_Id}`);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ✅ Motor Start Notification
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
|
|
console.log(`🚀 Motor started in FORCED_MANUAL mode.`);
|
|
|
|
|
|
|
|
|
|
// Determine stop criteria
|
|
|
|
|
const stopCriteria = inputConnection.motor_stop_status === "1" ? "manual" : "threshold";
|
|
|
|
|
|
|
|
|
|
eventEmitter.emit(
|
|
|
|
|
"sendMotorStartNotification",
|
|
|
|
|
hw_Id, // Motor ID
|
|
|
|
|
customerId, // Customer ID
|
|
|
|
|
fcmTokens, // Valid tokens
|
|
|
|
|
inputConnection.water_level || 0,
|
|
|
|
|
blockName,
|
|
|
|
|
tankName,
|
|
|
|
|
"forced_manual", // Forced manual start
|
|
|
|
|
stopCriteria,
|
|
|
|
|
inputConnection.typeOfWater,
|
|
|
|
|
inputConnection.manual_threshold_time
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
// ✅ Find users linked to this customer and extract valid FCM tokens
|
|
|
|
|
const users = await User.find({ customerId }).select("fcmIds");
|
|
|
|
|
const fcmTokens = users.flatMap(user => user.fcmIds).filter(token => token);
|
|
|
|
|
|
|
|
|
|
if (!fcmTokens.length) {
|
|
|
|
|
console.log(`⚠️ No valid FCM tokens found for Customer ID: ${customerId}`);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ✅ Extract motor input connection details
|
|
|
|
|
const inputConnection = motorTank.connections.inputConnections.find(conn => conn.motor_id === hw_Id);
|
|
|
|
|
|
|
|
|
|
if (inputConnection) {
|
|
|
|
|
const blockName = motorTank.blockName || "N/A";
|
|
|
|
|
const tankName = motorTank.tankName;
|
|
|
|
|
const allowNotifications = (await User.findOne({ customerId }))?.manualStartAndStopNotify ?? true;
|
|
|
|
|
|
|
|
|
|
// ✅ Motor Start Notification
|
|
|
|
|
if (allowNotifications && inputConnection.motor_stop_status === "1" && status === 2 && inputConnection.motor_on_type !== "forced_manual") {
|
|
|
|
|
inputConnection.motor_stop_status = "2";
|
|
|
|
|
inputConnection.motor_on_type = "forced_manual";
|
|
|
|
|
inputConnection.startTime = new Date().toISOString();
|
|
|
|
|
|
|
|
|
|
console.log(`🚀 Motor started in FORCED_MANUAL mode.`);
|
|
|
|
|
|
|
|
|
|
// Determine stop criteria
|
|
|
|
|
const stopCriteria = inputConnection.motor_stop_status === "1" ? "manual" : "threshold";
|
|
|
|
|
|
|
|
|
|
eventEmitter.emit(
|
|
|
|
|
"sendMotorStartNotification",
|
|
|
|
|
hw_Id,
|
|
|
|
|
customerId, // ✅ Pass customerId here
|
|
|
|
|
fcmTokens,
|
|
|
|
|
inputConnection.water_level || 0,
|
|
|
|
|
blockName,
|
|
|
|
|
tankName,
|
|
|
|
|
"forced_manual",
|
|
|
|
|
stopCriteria,
|
|
|
|
|
inputConnection.typeOfWater,
|
|
|
|
|
inputConnection.manual_threshold_time
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 🛑 Motor Stop Notification
|
|
|
|
|
if (allowNotifications && inputConnection.motor_stop_status === "2" && status === 1) {
|
|
|
|
|
inputConnection.motor_stop_status = "1";
|
|
|
|
|
inputConnection.stopTime = new Date().toISOString();
|
|
|
|
|
|
|
|
|
|
console.log(`🛑 Motor stopped manually.`);
|
|
|
|
|
|
|
|
|
|
eventEmitter.emit(
|
|
|
|
|
"sendMotorStopNotification",
|
|
|
|
|
hw_Id,
|
|
|
|
|
customerId, // ✅ Ensure customerId is passed
|
|
|
|
|
fcmTokens,
|
|
|
|
|
inputConnection.water_level || 0,
|
|
|
|
|
blockName,
|
|
|
|
|
tankName,
|
|
|
|
|
"forced_manual",
|
|
|
|
|
inputConnection.typeOfWater
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
await motorTank.save();
|
|
|
|
|
|
|
|
|
|
// 🛑 Motor Stop Notification
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
|
|
console.log(`🛑 Motor stopped manually.`);
|
|
|
|
|
|
|
|
|
|
eventEmitter.emit(
|
|
|
|
|
"sendMotorStopNotification",
|
|
|
|
|
hw_Id, // Motor ID
|
|
|
|
|
customerId, // Customer ID
|
|
|
|
|
fcmTokens, // Valid tokens
|
|
|
|
|
inputConnection.water_level || 0,
|
|
|
|
|
blockName,
|
|
|
|
|
tankName,
|
|
|
|
|
"forced_manual", // Forced manual stop
|
|
|
|
|
inputConnection.typeOfWater
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
await motorTank.save();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
console.log(`✅ Data processed successfully for hardwareId: ${hw_Id}`);
|
|
|
|
|