diff --git a/src/controllers/tanksController.js b/src/controllers/tanksController.js index a0aee61e..fc75521b 100644 --- a/src/controllers/tanksController.js +++ b/src/controllers/tanksController.js @@ -153,8 +153,6 @@ exports.updateTanklevels = async (req, reply) => { const tankName = tank.tankName; let capacity = parseInt(tank.capacity.replace(/,/g, ''), 10); - - let waterLevel = capacity - 100; // initial water level const intervalId = setInterval(async function () { const newWaterLevel = Math.floor(waterLevel / 1.5); @@ -209,8 +207,135 @@ exports.getTanklevels = async (req, reply) => { } }; +const changingfrom_tankwaterlevel = async (customerId,initial_update,supplier_tank_info) => { + //console.log(customerId,"0") + //console.log(update_level,"1") + //console.log(fromSump,"2") + + var result = await Tank.findOneAndUpdate( + {customerId:customerId, tankName: supplier_tank_info.tankName}, { $set: { waterlevel: initial_update } } + ); + + return result.waterlevel; +}; + + + + +exports.motorAction1 = async (req, reply) => { + try { + const action = req.body.action + let from, to_capacity, sump_capacity, update_level, intervalId; + + if (action === "start") { + const customerId = req.params.customerId; + const to = req.body.to + const from_type = req.body.from_type + + if (from_type === "sump") { + from = req.body.from + const [tankInfo, fromSump] = await Promise.all([ + Tank.findOne({customerId, tankName: to}), + Tank.findOne({customerId, tankName: from}) + ]); + + to_capacity = parseInt(tankInfo.capacity.replace(/,/g, ''), 10); + sump_capacity = parseInt(fromSump.capacity.replace(/,/g, ''), 10); + update_level = sump_capacity - 200 + console.log(to_capacity,sump_capacity,update_level) + var b_id = await changingfrom_tankwaterlevel(customerId,update_level,fromSump) + //await Tank.findOneAndUpdate({customerId, tankName: from}, { $set: { waterlevel: update_level } }) + + intervalId = setInterval(async function () { + let sumpWaterLevel = fromSump.waterlevel + let toWaterLevel = tankInfo.waterlevel + console.log( Math.floor(sumpWaterLevel * 0.10),"0") + console.log(toWaterLevel,"1") + + //const [sumpWaterLevel, toWaterLevel] = await Promise.all([ + + // tankInfo.waterlevel + //]); + + const newWaterLevel =parseInt(toWaterLevel.replace(/,/g, ''), 10) + Math.floor(sumpWaterLevel * 0.1); + const newSumpWaterLevel = sumpWaterLevel - Math.floor(sumpWaterLevel * 0.1) + console.log(newWaterLevel,"3") + console.log(newSumpWaterLevel,"4") + + await Promise.all([ + Tank.findOneAndUpdate({customerId, tankName: to}, { $set: { waterlevel: newWaterLevel } }), + Tank.findOneAndUpdate({customerId, tankName: from}, { $set: { waterlevel: newSumpWaterLevel } }) + ]); + }, 2000); + } + } else if (action === "stop") { + clearInterval(intervalId); + } 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}`); + + }; +}; + + + +exports.motorAction = async (req, reply) => { + try { + const customerId = req.params.customerId; + const action = req.body.action + const receiver_tank = req.body.to + const receiver_tank_info = await Tank.findOne({ customerId ,tankName:receiver_tank}); + const receiver_capacity = receiver_tank_info.capacity + + if(action === "start"){ + supplier_tank = req.body.from + supplier_tank_type = req.body.from_type + + if(supplier_tank_type==="sump"){ + const supplier_tank_info = await Tank.findOne({ customerId ,tankName:supplier_tank}); + initial_update = parseInt(supplier_tank_info.capacity.replace(/,/g, ''), 10)-200; + await changingfrom_tankwaterlevel(customerId,initial_update,supplier_tank_info); + let supplier_waterlevel = parseInt(supplier_tank_info.waterlevel.replace(/,/g, ''), 10) + let receiver_waterlevel = parseInt(receiver_tank_info.waterlevel.replace(/,/g, ''), 10) + intervalId = setInterval(async function () { + // Calculate new water levels + const newWaterLevel = receiver_waterlevel + Math.floor(supplier_waterlevel * 0.1); + const newSupplierWaterLevel = supplier_waterlevel - Math.floor(supplier_waterlevel * 0.1); + + // Check if updating should stop + if (newSupplierWaterLevel <= 0 || newWaterLevel >= receiver_capacity) { + clearInterval(intervalId); + } else { + // Update water levels in database + supplier_waterlevel = newSupplierWaterLevel; + receiver_waterlevel = newWaterLevel; + await Promise.all([ + Tank.findOneAndUpdate({customerId, tankName: receiver_tank}, { $set: { waterlevel: newWaterLevel } }), + Tank.findOneAndUpdate({customerId, tankName: supplier_tank}, { $set: { waterlevel: newSupplierWaterLevel } }) + ]); + } + }, 2000); + + } + + } else if (action === "stop") { + clearInterval(intervalId); + } 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}`); + + }; +}; \ No newline at end of file diff --git a/src/models/tanks.js b/src/models/tanks.js index eab91661..93065055 100644 --- a/src/models/tanks.js +++ b/src/models/tanks.js @@ -14,7 +14,7 @@ const tanksSchema = new mongoose.Schema({ blockName: { type: String, default: null }, capacity: { type: String, default: "0" }, typeOfWater: { type: String, default: null }, - waterlevel: { type: String, default: null }, + waterlevel: { type: String, default: "0" }, tankLocation: { type: String, default: null }, connections: { source: { type: String}, diff --git a/src/routes/tanksRoute.js b/src/routes/tanksRoute.js index a9aa097f..cef09249 100644 --- a/src/routes/tanksRoute.js +++ b/src/routes/tanksRoute.js @@ -157,7 +157,7 @@ module.exports = function (fastify, opts, next) { description: "customerId", }, }, - + }, // querystring: { // tankName: {type: 'string'} @@ -202,6 +202,51 @@ module.exports = function (fastify, opts, next) { handler: tanksController.getTanklevels, }); + + fastify.route({ + method: "PUT", + url: "/api/motorAction/:customerId", + schema: { + tags: ["Tank"], + summary: "This is for start and stop", + params: { + required: ["customerId"], + type: "object", + properties: { + customerId: { + type: "string", + description: "customerId", + }, + }, + }, + + + body: { + type: "object", + // required: ['phone'], + properties: { + + to: { type: "string", default: null }, + from: { type: "string", default: null }, + from_type: { type: "string" }, + action: { type: "string" }, + + }, + }, + security: [ + { + basicAuth: [], + }, + ], + }, + // preHandler: [ + // fastify.auth([fastify.operatorAuthenticate]), + // validationHandler.validatePhoneFormat, + // ], + preHandler: fastify.auth([fastify.authenticate]), + handler: tanksController.motorAction, + }); + next();