|
|
|
@ -1,6 +1,8 @@
|
|
|
|
|
|
|
|
|
|
//const Supplier = require("../models/supplier");
|
|
|
|
|
const { Supplier, generateSupplierId, FriendRequest,DeliveryBoy} = require("../models/supplier")
|
|
|
|
|
const { Tank } = require('../models/tanks')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const boom = require("boom");
|
|
|
|
|
const fastify = require("fastify")({
|
|
|
|
@ -181,3 +183,117 @@ exports.getactivedeliveryboysofsupplier = async (req, reply) => {
|
|
|
|
|
throw boom.boomify(err);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const intervals = {};
|
|
|
|
|
exports.deliveryboystartandstop = async (req, reply) => {
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
//let start_time,stop_time
|
|
|
|
|
const bookingId = req.params.bookingId
|
|
|
|
|
const bookingdata = await Tankerbooking.findOne({bookingid: bookingId})
|
|
|
|
|
// console.log(bookingdata,"0")
|
|
|
|
|
// console.log(bookingdata.supplierId,"6")
|
|
|
|
|
const customerId = bookingdata.customerId;
|
|
|
|
|
const action = req.body.action
|
|
|
|
|
const receiver_tank = bookingdata.tankName
|
|
|
|
|
// console.log(receiver_tank,"5")
|
|
|
|
|
const receiver_tank_info = await Tank.findOne({ customerId ,tankName:receiver_tank,tankLocation:"sump"});
|
|
|
|
|
// console.log(receiver_tank_info,"1")
|
|
|
|
|
|
|
|
|
|
const receiver_capacity = parseInt((receiver_tank_info.capacity).replace(/,/g, ''), 10)
|
|
|
|
|
// console.log(receiver_capacity,"2")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if(action === "start"){
|
|
|
|
|
// console.log("started")
|
|
|
|
|
start_time = new Date().toLocaleString('en-US', {timeZone: 'Asia/Kolkata'})
|
|
|
|
|
const initial_receiver_waterlevel = parseInt(receiver_tank_info.waterlevel.replace(/,/g, ''), 10)
|
|
|
|
|
|
|
|
|
|
await Tankerbooking.findOneAndUpdate({bookingid:bookingId}, { $set: { initial_water_level: initial_receiver_waterlevel ,start_time:start_time} });
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let receiver_waterlevel = parseInt(receiver_tank_info.waterlevel.replace(/,/g, ''), 10)
|
|
|
|
|
// console.log(receiver_waterlevel,"1")
|
|
|
|
|
const intervalId = setInterval(async function () {
|
|
|
|
|
// Calculate new water levels
|
|
|
|
|
|
|
|
|
|
const newWaterLevel = receiver_waterlevel+450;
|
|
|
|
|
//console.log(newWaterLevel,"2",receiver_tank_info.tankName)
|
|
|
|
|
// Check if updating should stop
|
|
|
|
|
if ((newWaterLevel/receiver_capacity)*100 >= 97 ) {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
clearInterval(intervals[receiver_tank]); // Clear the interval for this tank
|
|
|
|
|
|
|
|
|
|
delete intervals[receiver_tank];
|
|
|
|
|
stop_time = new Date().toLocaleString('en-US', {timeZone: 'Asia/Kolkata'})
|
|
|
|
|
|
|
|
|
|
const final_tank_info = await Tank.findOne({ customerId ,tankName:receiver_tank,tankLocation:"sump"});
|
|
|
|
|
const final_receiver_waterlevel = parseInt(final_tank_info.waterlevel.replace(/,/g, ''), 10)
|
|
|
|
|
// console.log(final_receiver_waterlevel)
|
|
|
|
|
// clearInterval(intervalId);
|
|
|
|
|
await Tankerbooking.findOneAndUpdate({bookingid:bookingId}, { $set: { final_water_level: final_receiver_waterlevel ,stop_time:stop_time} });
|
|
|
|
|
// console.log("end for" + receiver_tank);
|
|
|
|
|
} else {
|
|
|
|
|
// Update water levels in database
|
|
|
|
|
|
|
|
|
|
receiver_waterlevel = newWaterLevel;
|
|
|
|
|
// console.log(receiver_tank+""+newWaterLevel+""+"bore to sump")
|
|
|
|
|
//console.log((newWaterLevel/receiver_capacity)*100,"4",receiver_tank_info.tankName)
|
|
|
|
|
await Tank.findOneAndUpdate({customerId, tankName: receiver_tank,tankLocation:"sump"}, { $set: { waterlevel: newWaterLevel } })
|
|
|
|
|
// console.log(intervals,"check1")
|
|
|
|
|
}
|
|
|
|
|
}, 2000);
|
|
|
|
|
intervals[receiver_tank] = intervalId;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// console.log(customerId,req.body.from,req.body.from_type,receiver_tank,req.body.to_type,)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
reply.send({ status_code: 200, "start time": start_time,});
|
|
|
|
|
console.log(start_time)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (action === "stop") {
|
|
|
|
|
const booking_data = await Tankerbooking.findOne({bookingid: bookingId})
|
|
|
|
|
console.log(intervals,"hii3")
|
|
|
|
|
try {
|
|
|
|
|
clearInterval(intervals[receiver_tank]); // Clear the interval for this tank
|
|
|
|
|
delete intervals[receiver_tank]; // Delete the interval ID for this tank from the intervals object
|
|
|
|
|
} catch (err) {
|
|
|
|
|
console.log(err);
|
|
|
|
|
}
|
|
|
|
|
stop_time = new Date().toLocaleString('en-US', {timeZone: 'Asia/Kolkata'})
|
|
|
|
|
const final_tank_info = await Tank.findOne({ customerId ,tankName:receiver_tank,tankLocation:"sump"});
|
|
|
|
|
// console.log(final_tank_info,"hii")
|
|
|
|
|
const final_receiver_waterlevel = parseInt(final_tank_info.waterlevel.replace(/,/g, ''), 10)
|
|
|
|
|
|
|
|
|
|
const start_water_level = parseInt(booking_data.initial_water_level.replace(/,/g, ''), 10)
|
|
|
|
|
const quantity_delivered = final_receiver_waterlevel-start_water_level
|
|
|
|
|
|
|
|
|
|
await Tankerbooking.findOneAndUpdate({bookingid:bookingId}, { $set: { final_water_level: final_receiver_waterlevel ,stop_time:stop_time,quantityDelivered:quantity_delivered} });
|
|
|
|
|
reply.send({ status_code: 200, "stop time": stop_time});
|
|
|
|
|
} else {
|
|
|
|
|
throw new Error("Invalid action");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return { message: 'Water level updates started' };
|
|
|
|
|
|
|
|
|
|
} catch (err) {
|
|
|
|
|
throw new Error(`Failed to start/stop water level updates: ${err.message}`);
|
|
|
|
|
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|