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

@ -6381,29 +6381,44 @@ 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;
}
// ✅ 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 blockName = motorTank.blockName || "N/A";
const tankName = motorTank.tankName;
const allowNotifications = (await User.findOne({ customerId }))?.manualStartAndStopNotify ?? true;
// ✅ Motor Start Notification // ✅ Motor Start Notification
if (allowNotifications && inputConnection.motor_stop_status === "1" && status === 2 && inputConnection.motor_on_type !== "forced_manual") { 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_stop_status = "2";
inputConnection.motor_on_type = "forced_manual"; inputConnection.motor_on_type = "forced_manual";
inputConnection.startTime = currentTime; inputConnection.startTime = new Date().toISOString();
console.log(`🚀 Motor started in FORCED_MANUAL mode.`); console.log(`🚀 Motor started in FORCED_MANUAL mode.`);
@ -6412,13 +6427,13 @@ client.on('message', async (topic, message) => {
eventEmitter.emit( eventEmitter.emit(
"sendMotorStartNotification", "sendMotorStartNotification",
hw_Id, // Motor ID hw_Id,
customerId, // Customer ID customerId, // ✅ Pass customerId here
fcmTokens, // Valid tokens fcmTokens,
inputConnection.water_level || 0, inputConnection.water_level || 0,
blockName, blockName,
tankName, tankName,
"forced_manual", // Forced manual start "forced_manual",
stopCriteria, stopCriteria,
inputConnection.typeOfWater, inputConnection.typeOfWater,
inputConnection.manual_threshold_time inputConnection.manual_threshold_time
@ -6427,26 +6442,27 @@ client.on('message', async (topic, message) => {
// 🛑 Motor Stop Notification // 🛑 Motor Stop Notification
if (allowNotifications && 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.motor_stop_status = "1";
inputConnection.stopTime = currentTime; inputConnection.stopTime = new Date().toISOString();
console.log(`🛑 Motor stopped manually.`); console.log(`🛑 Motor stopped manually.`);
eventEmitter.emit( eventEmitter.emit(
"sendMotorStopNotification", "sendMotorStopNotification",
hw_Id, // Motor ID hw_Id,
customerId, // Customer ID customerId, // ✅ Ensure customerId is passed
fcmTokens, // Valid tokens fcmTokens,
inputConnection.water_level || 0, inputConnection.water_level || 0,
blockName, blockName,
tankName, tankName,
"forced_manual", // Forced manual stop "forced_manual",
inputConnection.typeOfWater inputConnection.typeOfWater
); );
} }
await motorTank.save(); await motorTank.save();
} }
console.log(`✅ Data processed successfully for hardwareId: ${hw_Id}`); console.log(`✅ Data processed successfully for hardwareId: ${hw_Id}`);

Loading…
Cancel
Save