master^2
Bhaskar 7 months ago
parent 0709c0a56c
commit 1af5427f25

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

Loading…
Cancel
Save