chnages on notification

master
Bhaskar 12 months ago
parent c12635d76f
commit 5c3658fec6

@ -1342,31 +1342,33 @@ admin.initializeApp({
// }); // });
// Handle motor start event with timestamp // Handle motor start event with timestamp
eventEmitter.on('motorStart', async (fcmTokens, timestamp) => { eventEmitter.on('motorStart', async (fcmTokens, timestamp, motorId, waterLevel) => {
await sendNotification(fcmTokens, 'Motor Started', `The motor has been started successfully at ${timestamp}.`); await sendNotification(fcmTokens, 'Motor Started', `Motor ID: ${motorId} started successfully at ${timestamp}. Current Water Level: ${waterLevel} Ltrs`);
}); });
// Handle motor stop event with timestamp // Emit motor stop event with motorId
eventEmitter.on('motorStop', async (fcmTokens, timestamp) => { eventEmitter.on('motorStop', async (fcmTokens, timestamp, motorId, waterLevel) => {
await sendNotification(fcmTokens, 'Motor Stopped', `The motor has been stopped successfully at ${timestamp}.`); await sendNotification(fcmTokens, 'Motor Stopped', `Motor ID: ${motorId} stopped successfully at ${timestamp}.Current Water Level: ${waterLevel} Ltrs`);
}); });
// Handle low water level event with timestamp // Emit low water level event with motorId
eventEmitter.on('lowWaterLevel', async (fcmTokens, timestamp) => { eventEmitter.on('lowWaterLevel', async (fcmTokens, timestamp, motorId, waterLevel) => {
await sendNotification(fcmTokens, 'Low Water Level', `The water level dropped below 20% at ${timestamp}.`); await sendNotification(fcmTokens, 'Low Water Level', `Motor ID: ${motorId}, water level dropped below 20% at ${timestamp}. Current Water Level: ${waterLevel} Ltrs`);
}); });
// Handle high water level event with timestamp // Emit high water level event with motorId
eventEmitter.on('highWaterLevel', async (fcmTokens, timestamp) => { eventEmitter.on('highWaterLevel', async (fcmTokens, timestamp, motorId, waterLevel) => {
await sendNotification(fcmTokens, 'High Water Level', `The water level reached above 90% at ${timestamp}.`); await sendNotification(fcmTokens, 'High Water Level', `Motor ID: ${motorId}, water level reached above 90% at ${timestamp}. Current Water Level: ${waterLevel} Ltrs`);
}); });
// Function to emit events with timestamps // Function to emit events with timestamps
const emitWithTimestamp = (eventName, fcmTokens) => { const emitWithTimestamp = (eventName, fcmTokens, motorId, waterLevel) => {
const timestamp = moment().format('YYYY-MM-DD HH:mm:ss'); const timestamp = moment().format('HH:mm:ss YYYY-MM-DD ');
eventEmitter.emit(eventName, fcmTokens, timestamp); eventEmitter.emit(eventName, fcmTokens, timestamp, motorId, waterLevel);
}; };
const sendNotification = async (fcmTokens, title, body) => { const sendNotification = async (fcmTokens, title, body) => {
if (!Array.isArray(fcmTokens) || fcmTokens.length === 0) { if (!Array.isArray(fcmTokens) || fcmTokens.length === 0) {
console.error('No FCM tokens provided.'); console.error('No FCM tokens provided.');
@ -1742,14 +1744,18 @@ exports.motorAction = async (req, reply) => {
const users = await User.find({ customerId }); const users = await User.find({ customerId });
const fcmToken = users.map(user => user.fcmId).filter(fcmId => fcmId); const fcmToken = users.map(user => user.fcmId).filter(fcmId => fcmId);
const receiverTank = await Tank.findOne({ customerId, tankName: req.body.to, tankLocation: req.body.to_type.toLowerCase() });
console.log(receiverTank)
const currentWaterLevel = parseInt(receiverTank.waterlevel, 10);
// Determine the motor stop status based on the action // Determine the motor stop status based on the action
let motorStopStatus; let motorStopStatus;
if (action === "start") { if (action === "start") {
motorStopStatus = "2"; // If action is start, set stop status to "2" motorStopStatus = "2"; // If action is start, set stop status to "2"
emitWithTimestamp('motorStart', fcmToken); // Emit motor start notification with timestamp emitWithTimestamp('motorStart', fcmToken, motorId, currentWaterLevel);
} else if (action === "stop") { } else if (action === "stop") {
motorStopStatus = "1"; // If action is stop, set stop status to "1" motorStopStatus = "1"; // If action is stop, set stop status to "1"
emitWithTimestamp('motorStop', fcmToken); // Emit motor stop notification with timestamp emitWithTimestamp('motorStop', fcmToken, motorId, currentWaterLevel);
} else { } else {
throw new Error("Invalid action provided."); throw new Error("Invalid action provided.");
} }
@ -4270,7 +4276,7 @@ exports.getBlockData = async (req, reply) => {
const mqtt = require('mqtt'); const mqtt = require('mqtt');
const client = mqtt.connect('mqtt://35.207.198.4:1883'); // Connect to MQTT broker const client = mqtt.connect('mqtt://35.207.198.4:1884'); // Connect to MQTT broker
client.on('connect', () => { client.on('connect', () => {
console.log('Connected to MQTT broker'); console.log('Connected to MQTT broker');

Loading…
Cancel
Save