diff --git a/src/controllers/tankersController.js b/src/controllers/tankersController.js index 4f63e0fe..7a33bb23 100644 --- a/src/controllers/tankersController.js +++ b/src/controllers/tankersController.js @@ -178,6 +178,24 @@ exports.getTanker = async (req, reply) => { } }; +exports.getallTanker = async (req, reply) => { + try { + await Tanker.find() + .exec() + .then((docs) => { + reply.send({ status_code: 200, data: docs, count: docs.length }); + }) + .catch((err) => { + console.log(err); + reply.send({ error: err }); + }); + } catch (err) { + throw boom.boomify(err); + } +}; + + + diff --git a/src/controllers/tanksController.js b/src/controllers/tanksController.js index 0d32f301..0f55c36f 100644 --- a/src/controllers/tanksController.js +++ b/src/controllers/tanksController.js @@ -121,6 +121,8 @@ exports.getTank = async (req, reply) => { throw boom.boomify(err); } }; + + //exports.getTanklevels = async (req, reply) => { // try { // const customerId = req.params.customerId; @@ -150,7 +152,7 @@ exports.updateTanklevels = async (req, reply) => { for (const tank of tanks) { const tankId = tank._id; let capacity = parseInt(tank.capacity.replace(/,/g, ''), 10); - let waterLevel = capacity - 100; // initial water level + let waterLevel = parseInt(tank.waterlevel.replace(/,/g, ''), 10); const intervalId = setInterval(async function () { const newWaterLevel = Math.floor(waterLevel - 200); @@ -197,17 +199,17 @@ exports.updateTanklevels1 = async (req, reply) => { const tank_type = tank.tankLocation let capacity = parseInt(tank.capacity.replace(/,/g, ''), 10); - let waterLevel = capacity - 100; // initial water level + const waterLevel = parseInt(tank.waterLevel.replace(/,/g, ''), 10); + //capacity - 100; // initial water level const intervalId = setInterval(async function () { - const newWaterLevel = Math.floor(waterLevel - 200); + const motor_status = tank.motor_status + if(motor_status === "0"){ + const newWaterLevel = Math.floor(waterLevel - 200); console.log(tank.tankName,newWaterLevel) const result = await Tank.findOneAndUpdate( { customerId, tankName, }, { $set: { waterlevel: newWaterLevel } } ); - - // console.log(result); - if (newWaterLevel === 0) { clearInterval(intervals[tankName]); console.log(`Stopped updating ${tankName}`); @@ -215,6 +217,31 @@ exports.updateTanklevels1 = async (req, reply) => { } waterLevel = newWaterLevel; + + } + else{ + if(tank.tankLocation==="overhead",motor_status==="1"){ + tank_waterlevel = math.floor(waterLevel + 250) + const supplier_water= await findOne({customerId:req.params.customerId,tankName:req.body.from,tankLocation:req.body.from_type}) + const supplier_waterlevel = parseInt(supplier_water.waterlevel .replace(/,/g, ''), 10); + const newSupplierWaterLevel = math.floor(supplier_waterlevel - 250) + + await Tank.findOneAndUpdate({customerId:req.params.customerId,tankName:req.body.from,tankLocation:req.body.from_type}, { $set: { waterlevel: newSupplierWaterLevel } }) + await tank.save() + } + if(tank.tankLocation==="sump",motor_status==="1"){ + const sumpwaterlevel = math.floor(waterLevel + 250) + await tank.save() + } + + + } + + + // console.log(result); + + + }, 2000); intervals[tankName] = intervalId; @@ -247,8 +274,6 @@ exports.getTanklevels = async (req, reply) => { - - exports.motorAction = async (req, reply) => { try { //let start_time,stop_time @@ -258,14 +283,15 @@ exports.motorAction = async (req, reply) => { const receiver_tank_info = await Tank.findOne({ customerId ,tankName:receiver_tank,tankLocation:(req.body.to_type).toLowerCase()}); const receiver_capacity = parseInt((receiver_tank_info.capacity).replace(/,/g, ''), 10) const desired_water_percentage = parseInt((req.body.percentage).replace(/,/g, ''), 10) + const intervals = {}; if(action === "start"){ - // start_time = new Date().toLocaleString('en-US', {timeZone: 'Asia/Kolkata'}) + start_time = new Date().toLocaleString('en-US', {timeZone: 'Asia/Kolkata'}) - supplier_tank = req.body.from - supplier_tank_type = (req.body.from_type).toLowerCase() - receiver_type = (req.body.to_type).toLowerCase() + const supplier_tank = req.body.from + const supplier_tank_type = (req.body.from_type).toLowerCase() + const receiver_type = (req.body.to_type).toLowerCase() console.log(supplier_tank) @@ -276,14 +302,15 @@ exports.motorAction = async (req, reply) => { const supplier_tank_info1 = await Tank.findOne({ customerId ,tankName:supplier_tank,tankLocation:supplier_tank_type}); console.log(supplier_tank_info1) - initial_update = parseInt(supplier_tank_info1.capacity.replace(/,/g, ''), 10)-200; - await Tank.findOneAndUpdate({customerId, tankName: supplier_tank,tankLocation:supplier_tank_type}, { $set: { waterlevel: initial_update } }); + //const initial_update = parseInt(supplier_tank_info1.waterlevel.replace(/,/g, ''), 10)-200; + // await Tank.findOneAndUpdate({customerId, tankName: supplier_tank,tankLocation:supplier_tank_type}, { $set: { waterlevel: initial_update } }); const supplier_tank_info = await Tank.findOne({ customerId ,tankName:supplier_tank,tankLocation:supplier_tank_type}); // await changingfrom_tankwaterlevel(customerId,initial_update,supplier_tank_info); let supplier_waterlevel = parseInt(supplier_tank_info.waterlevel.replace(/,/g, ''), 10) + console.log(supplier_waterlevel) let receiver_waterlevel = parseInt(receiver_tank_info.waterlevel.replace(/,/g, ''), 10) - intervalId = setInterval(async function () { + intervals[receiver_tank] = setInterval(async function () { // Calculate new water levels const rcvr_info = await Tank.findOne({ customerId ,tankName:receiver_tank,tankLocation:receiver_type}); const newWaterLevel = receiver_waterlevel + 350//Math.floor(supplier_waterlevel * 0.1); @@ -292,10 +319,11 @@ 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 || rcvr_info.motor_status === "0") { - clearInterval(intervalId) + clearInterval(intervals[receiver_tank]); // Clear the interval for this tank + delete intervals[receiver_tank]; await Tank.findOneAndUpdate({customerId, tankName: receiver_tank,tankLocation:receiver_type}, { $set: { motor_status: "0" } }); - console.log("end"); + console.log("end for"+receiver_tank); } else { // Update water levels in database supplier_waterlevel = newSupplierWaterLevel; @@ -361,29 +389,31 @@ exports.motorAction = async (req, reply) => { if(supplier_tank_type==="bore" && receiver_type === "sump"){ const receiver_capacity = parseInt(receiver_tank_info.capacity.replace(/,/g, ''), 10) - console.log(receiver_capacity,"0") + console.log(receiver_capacity,"0",receiver_tank_info.tankName) await Tank.findOneAndUpdate({customerId, tankName: receiver_tank,tankLocation:receiver_type}, { $set: { motor_status: "1" } }); let receiver_waterlevel = parseInt(receiver_tank_info.waterlevel.replace(/,/g, ''), 10) console.log(receiver_waterlevel,"1") - intervalId = setInterval(async function () { + intervals[receiver_tank] = setInterval(async function () { // Calculate new water levels const rcvr_info = await Tank.findOne({ customerId ,tankName:receiver_tank,tankLocation:receiver_type}); + console.log(rcvr_info) console.log(rcvr_info.motor_status) const newWaterLevel = receiver_waterlevel+450; - console.log(newWaterLevel,"2") + console.log(newWaterLevel,"2",receiver_tank_info.tankName) // Check if updating should stop if ((newWaterLevel/receiver_capacity)*100 >= 97 || (newWaterLevel/receiver_capacity)*100 >= desired_water_percentage || rcvr_info.motor_status === "0" ) { await Tank.findOneAndUpdate({customerId, tankName: receiver_tank,tankLocation:receiver_type}, { $set: { motor_status: "0" } }); - clearInterval(intervalId) - console.log("end"); + clearInterval(intervals[receiver_tank]); // Clear the interval for this tank + delete intervals[receiver_tank]; + console.log("end for" + receiver_tank); } else { // Update water levels in database receiver_waterlevel = newWaterLevel; - console.log((newWaterLevel/receiver_capacity)*100,"4") + console.log((newWaterLevel/receiver_capacity)*100,"4",receiver_tank_info.tankName) await Promise.all([ Tank.findOneAndUpdate({customerId, tankName: receiver_tank,tankLocation:receiver_type}, { $set: { waterlevel: newWaterLevel } }), ]); @@ -422,24 +452,25 @@ exports.motorAction = async (req, reply) => { } const motor_data = await motorData.save(); - reply.send({ status_code: 200, data: motor_data }); - return motor_data - + // reply.send({ status_code: 200, data: motor_data }); + - // reply.send({ status_code: 200, "start time": start_time}); - //console.log(start_time) + reply.send({ status_code: 200, "start time": start_time, data: motor_data}); + console.log(start_time) + return motor_data + } else if (action === "stop") { - // stop_time = new Date().toLocaleString('en-US', {timeZone: 'Asia/Kolkata'}) + stop_time = new Date().toLocaleString('en-US', {timeZone: 'Asia/Kolkata'}) // console.log(stop_time) // clearInterval(intervalId); await Tank.findOneAndUpdate({customerId, tankName: receiver_tank,tankLocation:(req.body.to_type).toLowerCase()}, { $set: { motor_status: "0" } }); - // reply.send({ status_code: 200, "stop time": stop_time}); + reply.send({ status_code: 200, "stop time": stop_time}); } else { throw new Error("Invalid action"); } @@ -452,7 +483,6 @@ exports.motorAction = async (req, reply) => { }; }; - exports.consumption = async (req, reply) => { try { diff --git a/src/controllers/userController.js b/src/controllers/userController.js index df0fd65d..870d2bd4 100644 --- a/src/controllers/userController.js +++ b/src/controllers/userController.js @@ -342,7 +342,7 @@ exports.delPhoneUser = async (req, reply) => { exports.uploadProfilePicture = async (req, reply) => { try { const customerId = req.params.customerId; - const picture = new Buffer.from(req.body.picture, 'base64'); + const picture = req.body.picture; let profilePicture = await ProfilePicture.findOne({ customerId }); @@ -388,6 +388,7 @@ exports.logout = async (request, reply) => { + // controller.js const http = require('https'); diff --git a/src/models/User.js b/src/models/User.js index b59a853e..ea410169 100644 --- a/src/models/User.js +++ b/src/models/User.js @@ -117,11 +117,12 @@ const profilePictureSchema = new Schema({ required: true }, picture: { - type: Buffer, + type: String, required: true } }); + const ProfilePicture = mongoose.model('ProfilePicture', profilePictureSchema); const Counter = mongoose.model('Counter', CounterSchema); const User = mongoose.model("User", userSchema); diff --git a/src/routes/tankersRoute.js b/src/routes/tankersRoute.js index 1d9407ce..95fbbe7d 100644 --- a/src/routes/tankersRoute.js +++ b/src/routes/tankersRoute.js @@ -232,6 +232,22 @@ module.exports = function (fastify, opts, next) { handler: tankersController.getTanker, }); + fastify.get("/api/getallTankers", { + schema: { + tags: ["Supplier"], + description: "This is for Get all Tanker Data", + summary: "This is for to Get all Tanker Data", + + security: [ + { + basicAuth: [], + }, + ], + }, + preHandler: fastify.auth([fastify.authenticate]), + handler: tankersController.getallTanker, + }); + fastify.route({ method: "POST", url: "/api/addBores/:customerId",