Varun 9 months ago
commit c0ea44e04b

@ -1759,6 +1759,43 @@ eventEmitter.on(
}
);
eventEmitter.on(
'motorStartAutomatic',
async (fcmTokens, tankName, blockName, startTime, motorOnType, manual_threshold_time, typeOfWater,threshold) => {
try {
// Retrieve the user information
const users = await User.find({ fcmIds: { $in: fcmTokens } });
console.log("users", users);
const userNames = users.map(user => user.username).join(', ');
console.log("userNames", userNames);
const startMethod = motorOnType === "Automatic" ? "Automatic" : "Manual";
// Generate motor name dynamically based on tank name, block name, and type of water
const motorName = `${tankName}-${blockName}-${typeOfWater}`;
// Get current date and time for the motor start time
const currentDateTime = new Date();
const formattedDate = currentDateTime.toLocaleDateString(); // Customize this format as needed
const formattedTime = currentDateTime.toLocaleTimeString(); // Customize this format as needed
// Prepare the message
const message =
`🚰 Motor Name: ${motorName}\n` +
`🚰 Tank Name: '${tankName}'\n` +
`🚰 Block Name: '${blockName}'\n` +
`👤 Started by: '${startMethod}' \n` +
`📱 Mode: "Atomatic System" \n` +
`🕒 Pump started at: ${startTime} (Time: ${formattedTime} on ${formattedDate})\n` +
`Will stop after: '${threshold}' `;
// Send the notification
await sendNotification(fcmTokens, 'Arminta Water Management', message);
} catch (error) {
console.error('Error in motorStart event:', error);
}
}
);
// eventEmitter.on('motorStop', async (fcmTokens, tankName,stopTime, motorOnType) => {
@ -3693,12 +3730,23 @@ const motorActionAuto = async (req, reply) => {
const action = req.body.action;
const motorId = req.body.motor_id;
const motor_on_type = req.body.motor_on_type
const startTime = req.body.startTime;
const stopTime = req.body.stopTime;
const threshold = req.body.threshold || "unknown";
if (!motorId) {
throw new Error("Motor ID is required.");
}
let motorStopStatus;
const tank = await Tank.findOne({ customerId, "connections.inputConnections.motor_id": motorId });
if (!tank) {
throw new Error("Tank not found for the provided motor ID.");
}
const { tankName, blockName, typeOfWater, fcmTokens } = tank; // Extracting necessary details
if (action === "start") {
await Tank.updateOne(
@ -3713,7 +3761,17 @@ const motorActionAuto = async (req, reply) => {
}
);
;
eventEmitter.emit(
"motorStartAutomatic",
fcmTokens,
tankName,
blockName,
startTime,
"Automatic",
typeOfWater,
threshold
);
}
if (action === "stop") {
@ -3727,6 +3785,21 @@ const motorActionAuto = async (req, reply) => {
}
}
);
const currentDateTime = new Date();
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} (Time: ${formattedTime} on ${formattedDate})\n` +
`⏳ Operation Duration: ${threshold} `;
// Send stop notification
await sendNotification(fcmTokens, "Arminta Water Management", stopMessage);
}

@ -374,8 +374,8 @@ module.exports = function (fastify, opts, next) {
type: "object",
required: ["city", "zone"],
properties: {
city: { type: "string" },
zone: { type: "string" },
city: { type: "string" },
},
},
},

Loading…
Cancel
Save