reverted notofications

master
Varun 1 year ago
parent 9967ff4ac1
commit 29b86221c4

@ -1366,25 +1366,26 @@ exports.motorAction = async (req, reply) => {
const customerId = req.params.customerId;
const action = req.body.action;
const motorId = req.body.motor_id;
// const fcmToken = req.body.fcmToken; // Ensure this is provided in the request
const start_instance_id = req.body.start_instance_id;
const start_instance_id = req.body.start_instance_id
console.log(req.body.startTime)
// Ensure motor_id is provided
if (!motorId) {
throw new Error("Motor ID is required.");
}
const users = await User.find({ customerId: customerId });
const fcmToken = users.map(user => user.fcmId).filter(fcmId => fcmId);
console.log(fcmToken)
// Determine the motor stop status based on the action
let motorStopStatus;
if (action === "start") {
motorStopStatus = "2";
motorStopStatus = "2"; // If action is start, set stop status to "2"
} else if (action === "stop") {
motorStopStatus = "1";
motorStopStatus = "1"; // If action is stop, set stop status to "1"
} else {
throw new Error("Invalid action provided.");
}
// Update the motor stop status immediately if action is stop
if (action === "stop") {
// Update the motor stop status and other fields
await Tank.updateOne(
{ customerId, "connections.inputConnections.motor_id": motorId },
{
@ -1397,22 +1398,15 @@ exports.motorAction = async (req, reply) => {
}
}
);
if (intervals[motorId]) {
clearInterval(stat_stop_intervals[motorId]);
delete stat_stop_intervals[motorId];
console.log("interval cleared")
}
// Send immediate response to the client
reply.code(200).send({ message: "Motor stopped successfully." });
// Send stop notification
if (fcmToken) {
await sendNotification(fcmToken, 'Motor Stopped', `Motor ${motorId} has been stopped.`);
}
// Perform stop operations in the background
(async () => {
await delay(300000);
// Update the existing motor data entry with stop details
const motorData = await MotorData.findOne({ customerId, motor_id: motorId, start_instance_id: start_instance_id });
if (motorData) {
const receiverTank = await Tank.findOne({ customerId, tankName: motorData.receiverTank, tankLocation: motorData.receiver_type.toLowerCase() });
@ -1435,31 +1429,27 @@ exports.motorAction = async (req, reply) => {
}
}
);
// Send high water level notification
if (receiverFinalWaterLevel / parseInt(receiverTank.capacity, 10) * 100 >= 90) {
if (fcmToken) {
await sendNotification(fcmToken, 'High Water Level Alert', `Water level in tank ${motorData.receiverTank} has reached 90%.`);
}
}
}
})();
// Return here to ensure the rest of the code is not executed for the stop action
return;
} else {
// Update the motor stop status to "2" for start action
await Tank.updateOne(
{ customerId, "connections.inputConnections.motor_id": motorId },
{ $set: { "connections.inputConnections.$.motor_stop_status": "2" } }
);
// Send start notification
if (fcmToken) {
await sendNotification(fcmToken, 'Motor Started', `Motor ${motorId} has been started.`);
}
}
// Check threshold settings if action is start
if (action === "start") {
if (req.body.threshold_type === "time") {
// If threshold type is time, update threshold time
// await Tank.updateOne(
// { customerId, "connections.inputConnections.motor_id": motorId },
// { $set: { "connections.inputConnections.$.manual_threshold_time": req.body.manual_threshold_time,startTime:req.body.startTime } }
// );
const receiver_tank_info7 = await Tank.findOne({ customerId, tankName: req.body.to, tankLocation: req.body.to_type.toLowerCase() });
const newMotorData = new MotorData({
@ -1485,16 +1475,21 @@ exports.motorAction = async (req, reply) => {
}
}
// Start monitoring water level based on threshold time
const thresholdTime = moment().add(req.body.manual_threshold_time, 'minutes').toDate();
stat_stop_intervals[motorId] = setInterval(async () => {
console.log(stat_stop_intervals)
const intervalId = setInterval(async () => {
const splr_tank_info3 = await Tank.findOne({ customerId, tankName: req.body.from, tankLocation: req.body.from_type.toLowerCase() });
const splr_tank_info3_waterlevel = parseInt(splr_tank_info3.waterlevel, 10);
//console.log(splr_tank_info3_waterlevel,"splr_tank_info3_waterlevel")
const splr_tank_info3_capacity = parseInt(splr_tank_info3.capacity.replace(/,/g, ''), 10);
// const splr_tank_info3_capacity = parseInt(splr_tank_info3.capacity, 10);
// console.log(splr_tank_info3.capacity,splr_tank_info3_capacity,"splr_tank_info3_capacity")
const splr_tank_info3_percentage = (splr_tank_info3_waterlevel / splr_tank_info3_capacity) * 100;
// console.log(splr_tank_info3_percentage, "percentage for less than 20");
if (new Date() >= thresholdTime || splr_tank_info3_percentage <= 20) {
console.log(splr_tank_info3_percentage,)
await Tank.updateOne(
{ customerId, "connections.inputConnections.motor_id": motorId },
{
@ -1506,14 +1501,7 @@ exports.motorAction = async (req, reply) => {
}
}
);
console.log(stat_stop_intervals[motorId])
clearInterval(stat_stop_intervals[motorId]);
delete stat_stop_intervals[motorId];
// Send notification after motor stops due to time threshold
if (fcmToken) {
await sendNotification(fcmToken, 'Motor Stopped After Time Threshold', `Motor ${motorId} has been stopped after reaching the set time threshold.`);
}
clearInterval(intervalId);
await delay(300000);
@ -1523,11 +1511,8 @@ exports.motorAction = async (req, reply) => {
const receiverFinalWaterLevel = parseInt(receiverTank.waterlevel, 10);
const quantityDelivered = receiverFinalWaterLevel - parseInt(motorData.receiverInitialwaterlevel, 10);
const water_pumped_till_now = parseInt(receiverTank.total_water_added_from_midnight, 10);
const totalwaterpumped = quantityDelivered + water_pumped_till_now;
await Tank.findOneAndUpdate(
{ customerId, tankName: motorData.receiverTank, tankLocation: motorData.receiver_type.toLowerCase() },
{ $set: { total_water_added_from_midnight: totalwaterpumped } }
);
const totalwaterpumped = quantityDelivered + water_pumped_till_now
await Tank.findOneAndUpdate({customerId, tankName: motorData.receiverTank, tankLocation: motorData.receiver_type.toLowerCase()}, { $set: { total_water_added_from_midnight: totalwaterpumped } })
const stopTime = formatDate(new Date());
@ -1535,18 +1520,12 @@ exports.motorAction = async (req, reply) => {
{ customerId, motor_id: motorId, start_instance_id: start_instance_id },
{
$set: {
stopTime: stopTime,
stopTime:stopTime,
receiverfinalwaterlevel: receiverFinalWaterLevel.toString(),
quantity_delivered: quantityDelivered.toString()
}
}
);
// Send low water level notification
if (receiverFinalWaterLevel / parseInt(receiverTank.capacity, 10) * 100 <= 20) {
if (fcmToken) {
await sendNotification(fcmToken, 'Low Water Level Alert', `Water level in tank ${motorData.receiverTank} has dropped below 20%.`);
}
}
}
}
}, 60000);
@ -1598,25 +1577,31 @@ exports.motorAction = async (req, reply) => {
}
}
// Start monitoring water level based on litres
stat_stop_intervals[motorId] = setInterval(async () => {
// Update water level threshold
// Start monitoring water level based on threshold percentage
const intervalId = setInterval(async () => {
// Check if water level has reached the threshold percentage
const supplier_tank_info1 = await Tank.findOne({ customerId, tankName: req.body.from, tankLocation: req.body.from_type.toLowerCase() });
const current_water_level = parseInt(supplier_tank_info1.waterlevel, 10);
if (current_water_level <= supplier_threshold) {
// Stop the motor pump
await Tank.updateOne(
{ customerId, "connections.inputConnections.motor_id": motorId },
{
$set: {
"connections.inputConnections.$.motor_stop_status": "1",
"connections.inputConnections.$.threshold_type": null,
"connections.inputConnections.$.manual_threshold_time": null,
"connections.inputConnections.$.manual_threshold_percentage": null
}
}
);
clearInterval(stat_stop_intervals[motorId]);
delete stat_stop_intervals[motorId];
clearInterval(intervalId); // Stop monitoring water level
await delay(300000);
const motorData = await MotorData.findOne({ customerId, motor_id: motorId, start_instance_id: start_instance_id });
@ -1625,33 +1610,29 @@ exports.motorAction = async (req, reply) => {
const receiverFinalWaterLevel = parseInt(receiverTank.waterlevel, 10);
const quantityDelivered = receiverFinalWaterLevel - parseInt(motorData.receiverInitialwaterlevel, 10);
const stopTime = formatDate(new Date());
await MotorData.updateOne(
{ customerId, motor_id: motorId, start_instance_id: start_instance_id },
{
$set: {
stopTime: stopTime,
stopTime:stopTime,
receiverfinalwaterlevel: receiverFinalWaterLevel.toString(),
quantity_delivered: quantityDelivered.toString()
}
}
);
// Send low water level notification
if (receiverFinalWaterLevel / parseInt(receiverTank.capacity, 10) * 100 <= 20) {
if (fcmToken) {
await sendNotification(fcmToken, 'Low Water Level Alert', `Water level in tank ${motorData.receiverTank} has dropped below 20%.`);
}
}
}
}
}, 20000);
}, 20000); // Check water level every minute
}
}
// Respond with success message
reply.code(200).send({ message: `Motor ${action === "start" ? "started" : "stopped"} successfully.` });
} catch (err) {
// Handle errors
throw boom.boomify(err);
}
};

Loading…
Cancel
Save