diff --git a/src/controllers/tankersController.js b/src/controllers/tankersController.js index 28e73284..621483bc 100644 --- a/src/controllers/tankersController.js +++ b/src/controllers/tankersController.js @@ -186,13 +186,21 @@ exports.tankerBooking = async (req, reply) => { const month = (date.getMonth() + 1).toString().padStart(2, '0') const day = date.getDate().toString().padStart(2, '0') const bookingId = `ARM${year}${month}${day}${b_id}` + // const customer = await User.findOne({customerId:customerId}) + // const customer_mobile = customer.phone + // const customer_name = customer.username + // const supplier = await Supplier.findOne({supplierId:req.body.supplierId}) + // console.log(supplier, "11") + // const supplier_name = supplier.suppliername + // const supplier_mobile = supplier.phone + // console.log(supplier_mobile, "==========") + const customer = await User.findOne({customerId:customerId}) - const customer_mobile = customer.phoneNumber - const customer_name = customer.username + const customerPhone = customer.phone + const customerName = customer.username const supplier = await Supplier.findOne({supplierId:req.body.supplierId}) - console.log(supplier) - const supplier_name = supplier.suppliername - const supplier_mobile = supplier.phoneNumber + const supplierName = supplier.suppliername + const supplierPhone = supplier.phone var booking_id = await Tankerbooking.findOne({ bookingid: bookingId}) if(booking_id){ b_id= await generateBookingId() @@ -215,10 +223,10 @@ exports.tankerBooking = async (req, reply) => { longitude: req.body.longitude, //date: req.body.date, // time: req.body.time, - customerPhone : customer_mobile, - supplierPhone : supplier_mobile, - customerName : customer_name, - supplierName : supplier_name, + customerPhone : customerPhone, + supplierPhone : supplierPhone, + customerName : customerName, + supplierName : supplierName, }; @@ -241,10 +249,10 @@ exports.tankerBooking = async (req, reply) => { tankersBookingData.tankLocation = usertobeInserted.tankLocation; //tankersBookingData.date = usertobeInserted.date; //tankersBookingData.time = usertobeInserted.time; - tankersBookingData.customerPhone = customer_mobile; - tankersBookingData.supplierPhone = supplier_mobile; - tankersBookingData.customerName = customer_name; - tankersBookingData.supplierName = supplier_name; + tankersBookingData.customerPhone = customerPhone; + tankersBookingData.supplierPhone = supplierPhone; + tankersBookingData.customerName = customerName; + tankersBookingData.supplierName = supplierName; tankersBookingData.latitude =usertobeInserted.latitude tankersBookingData.longitude=usertobeInserted.longitude diff --git a/src/controllers/tanksController.js b/src/controllers/tanksController.js index 91fb7be9..9600a005 100644 --- a/src/controllers/tanksController.js +++ b/src/controllers/tanksController.js @@ -721,6 +721,58 @@ exports.checkStatusofIot = async (req, reply) => { } }; +exports.totalwaterLevelSum = async (request, reply) => { + const { tankLocation, typeOfWater } = request.query; + + const waterlevelSum = await Tank.aggregate([ + { + $match: { tankLocation, typeOfWater } + }, + { + $group: { + _id: null, + totalWaterlevel: { $sum: { $toInt: '$waterlevel' } } + } + } + ]); + + const result = waterlevelSum[0]?.totalWaterlevel ?? 0; + + reply.send({ waterlevelSum: result }); +} + +exports.startUpdateLoop = async (request, reply) => { + const updateInterval = 5000; + + setInterval(async () => { + try { + const iotTank = await IotData.findOne({ hardwareId: request.body.hardwareId }); + if (!iotTank) { + console.log(`IOTtank not found for hardwareId ${request.body.hardwareId}`); + return; + } + + const currentWaterlevel = Number(iotTank.tankHeight) * 200; + const tank = await Tank.findOne({ hardwareId: iotTank.hardwareId }); + + let combinedWaterlevel; + if (tank) { + combinedWaterlevel = currentWaterlevel + Number(tank.waterlevel); + } else { + combinedWaterlevel = currentWaterlevel; + } + + await Tank.updateOne({ hardwareId: iotTank.hardwareId }, { $set: { waterlevel: combinedWaterlevel } }); + + console.log(`Waterlevel updated successfully for hardwareId ${iotTank.hardwareId}`); + console.log(`Previous waterlevel: ${tank ? tank.waterlevel : 0}`); + console.log(`Current waterlevel: ${currentWaterlevel}`); + console.log(`Combined waterlevel: ${combinedWaterlevel}`); + } catch (err) { + console.error(err); + } + }, updateInterval); +}; diff --git a/src/models/User.js b/src/models/User.js index 338a7a17..fc6f6c06 100644 --- a/src/models/User.js +++ b/src/models/User.js @@ -91,8 +91,8 @@ const userSchema = new mongoose.Schema( coordinates: { type: [Number], default: [0, 0] }, }, - longitude: { type : Number}, - latitude: {type: Number}, + longitude: { type : Number,default: 0.0}, + latitude: {type: Number,default: 0.0}, isActive: Boolean, tenantId: ObjectId, fcmId: { type: String, default: null }, diff --git a/src/models/supplier.js b/src/models/supplier.js index a5099f0b..06dd303c 100644 --- a/src/models/supplier.js +++ b/src/models/supplier.js @@ -63,8 +63,8 @@ const supplierSchema = new mongoose.Schema( // type: { type: String }, // coordinates: [Number] // }, - longitude: { type : Number}, - latitude: {type: Number}, + longitude: { type : Number,default: 0.0}, + latitude: {type: Number,default: 0.0}, isActive: Boolean, tenantId: ObjectId, fcmId: { type: String, default: null }, @@ -126,8 +126,8 @@ const supplierSchema = new mongoose.Schema( zip: { type: String, default: null }, timestamp: { type: Date, default: Date.now }, status: { type: String, default: "Inactive" }, - longitude: { type : Number}, - latitude: {type: Number}, + longitude: { type : Number,default: 0.0}, + latitude: {type: Number,default: 0.0}, fcmId: { type: String, default: null }, }); diff --git a/src/models/tankers.js b/src/models/tankers.js index 770061b6..30599e9b 100644 --- a/src/models/tankers.js +++ b/src/models/tankers.js @@ -63,8 +63,8 @@ const tankersbookingSchema = new mongoose.Schema({ customerName : { type: String, default: null }, supplierName : { type: String, default: null }, tankerRunningStatus: { type: String, default: "0" }, - longitude: { type : Number}, - latitude: {type: Number}, + longitude: { type : Number,default: 0.0}, + latitude: {type: Number,default: 0.0}, }); diff --git a/src/routes/supplierOrdersRoutes.js b/src/routes/supplierOrdersRoutes.js index 76e59002..f7a9ff1d 100644 --- a/src/routes/supplierOrdersRoutes.js +++ b/src/routes/supplierOrdersRoutes.js @@ -86,8 +86,8 @@ module.exports = function (fastify, opts, next) { state: { type: "string", default: null }, zip: { type: "string", default: null }, status: { type: "string" }, - latitude: { type: 'number' }, - longitude: { type: 'number'}, + latitude: { type: 'number', default: 0.0 }, + longitude: { type: 'number', default: 0.0}, fcmId: { type: "string", default: null }, }, diff --git a/src/routes/supplierRoute.js b/src/routes/supplierRoute.js index 9e19caf4..80720233 100644 --- a/src/routes/supplierRoute.js +++ b/src/routes/supplierRoute.js @@ -278,8 +278,8 @@ module.exports = function (fastify, opts, next) { state: { type: "string", default: null }, zip: { type: "string", default: null }, country: { type: "string", default: null }, - latitude: { type: 'number' }, - longitude: { type: 'number'}, + latitude: { type: 'number', default: 0.0}, + longitude: { type: 'number', default: 0.0}, fcmId: { type: "string", default: null }, }, }, diff --git a/src/routes/tankersRoute.js b/src/routes/tankersRoute.js index d74e8fca..80b1bcc1 100644 --- a/src/routes/tankersRoute.js +++ b/src/routes/tankersRoute.js @@ -208,8 +208,8 @@ module.exports = function (fastify, opts, next) { dateOfOrder: { type: "string"}, price: { type: "string"}, supplierId: { type: "string"}, - latitude: { type: 'number' }, - longitude: { type: 'number'} + latitude: { type: 'number' , default: 0.0}, + longitude: { type: 'number', default: 0.0} }, diff --git a/src/routes/tanksRoute.js b/src/routes/tanksRoute.js index 0bd6aef7..0eb7e1e4 100644 --- a/src/routes/tanksRoute.js +++ b/src/routes/tanksRoute.js @@ -1,5 +1,6 @@ const fastify = require("fastify"); const tanksController = require("../controllers/tanksController"); +const { Tank } = require('../models/tanks') module.exports = function (fastify, opts, next) { @@ -431,6 +432,78 @@ module.exports = function (fastify, opts, next) { handler: tanksController.checkStatusofIot, }); + fastify.route({ + method: 'GET', + url: '/waterlevel-sum', + + schema: { + tags: ["Tank"], + description: "This is for water level sum", + summary: "This is forwater level sum.", + querystring: { + type: 'object', + properties: { + tankLocation: { type: 'string' }, + typeOfWater: { type: 'string' } + }, + required: ['tankLocation', 'typeOfWater'] + }, + response: { + 200: { + type: 'object', + properties: { + waterlevelSum: { type: 'integer' } + } + } + } + }, + handler: tanksController.totalwaterLevelSum + }); + + + fastify.route({ + method: 'POST', + url: '/update-waterlevel', + schema: { + tags: ['Tank'], + description: 'This is for updating waterlevel of a tank based on its IOTtank document', + summary: 'This is for updating waterlevel of a tank', + body: { + type: 'object', + properties: { + hardwareId: { type: 'string' }, + }, + required: ['hardwareId'], + }, + response: { + 200: { + type: 'object', + properties: { + message: { type: 'string' }, + }, + }, + 404: { + type: 'object', + properties: { + message: { type: 'string' }, + }, + }, + 500: { + type: 'object', + properties: { + message: { type: 'string' }, + }, + }, + }, + security: [ + { + basicAuth: [], + }, + ], + }, + handler: tanksController.startUpdateLoop, + }); + next(); diff --git a/src/routes/usersRoute.js b/src/routes/usersRoute.js index b5a33830..a4778684 100644 --- a/src/routes/usersRoute.js +++ b/src/routes/usersRoute.js @@ -155,8 +155,8 @@ module.exports = function (fastify, opts, next) { zip: { type: "string", default: null }, country: { type: "string", default: null }, notes: { type: "string", default: null }, - latitude: { type: 'number' }, - longitude: { type: 'number'}, + latitude: { type: 'number', default: 0.0 }, + longitude: { type: 'number', default: 0.0}, fcmId: { type: "string", default: null }, }, },