motor on and off status functionality

master
varun 3 years ago
parent 3b6f227fbf
commit 4d22f4605b

@ -270,9 +270,12 @@ exports.motorAction = async (req, reply) => {
supplier_tank = req.body.from
supplier_tank_type = req.body.from_type
receiver_type = req.body.to_type
console.log(supplier_tank)
await Tank.findOneAndUpdate({customerId, tankName: receiver_tank}, { $set: { motor_status: 1 } });
if(supplier_tank_type==="sump" && receiver_type === "overhead"){
const supplier_tank_info1 = await Tank.findOne({ customerId ,tankName:supplier_tank});
console.log(supplier_tank_info1)
initial_update = parseInt(supplier_tank_info1.capacity.replace(/,/g, ''), 10)-200;
await Tank.findOneAndUpdate({customerId, tankName: supplier_tank}, { $set: { waterlevel: initial_update } });
const supplier_tank_info = await Tank.findOne({ customerId ,tankName:supplier_tank});
@ -289,6 +292,8 @@ exports.motorAction = async (req, reply) => {
// Check if updating should stop
if ((newSupplierWaterLevel/supplier_capacity)*100 <= 5 || (newWaterLevel/receiver_capacity)*100 >= 95 || (newWaterLevel/receiver_capacity)*100 >= desired_water_percentage) {
clearInterval(intervalId)
await Tank.findOneAndUpdate({customerId, tankName: receiver_tank}, { $set: { motor_status: 0 } });
console.log("end");
} else {
// Update water levels in database
@ -307,6 +312,8 @@ exports.motorAction = async (req, reply) => {
if(supplier_tank_type==="bore" && receiver_type === "sump"){
const receiver_capacity = receiver_tank_info.capacity
await Tank.findOneAndUpdate({customerId, tankName: receiver_tank}, { $set: { motor_status: 1 } });
let receiver_waterlevel = parseInt(receiver_tank_info.waterlevel.replace(/,/g, ''), 10)
intervalId = setInterval(async function () {
// Calculate new water levels
@ -314,6 +321,8 @@ exports.motorAction = async (req, reply) => {
// Check if updating should stop
if ((newWaterLevel/receiver_capacity)*100 >= 97 || (newWaterLevel/receiver_capacity)*100 >= desired_water_percentage ) {
clearInterval(intervalId)
await Tank.findOneAndUpdate({customerId, tankName: receiver_tank}, { $set: { motor_status: 0 } });
console.log("end");
} else {
// Update water levels in database
@ -404,6 +413,8 @@ exports.motorAction = async (req, reply) => {
// stop_time = new Date().toLocaleString('en-US', {timeZone: 'Asia/Kolkata'})
// console.log(stop_time)
clearInterval(intervalId);
await Tank.findOneAndUpdate({customerId, tankName: receiver_tank}, { $set: { motor_status: 0 } });
// reply.send({ status_code: 200, "stop time": stop_time});
} else {
throw new Error("Invalid action");

@ -16,6 +16,7 @@ const tanksSchema = new mongoose.Schema({
typeOfWater: { type: String, default: null },
waterlevel: { type: String, default: "0" },
tankLocation: { type: String, default: null },
motor_status: { type: String, default: 0 },
connections: {
source: { type: String},
inputConnections: [{ inputConnections: String,input_type:String }],

Loading…
Cancel
Save