diff --git a/node_modules/.package-lock.json b/node_modules/.package-lock.json index 2b55da50..7487ed7d 100644 --- a/node_modules/.package-lock.json +++ b/node_modules/.package-lock.json @@ -10230,6 +10230,8 @@ "node": ">=0.10.0" } }, +<<<<<<< HEAD +======= "node_modules/strtok3": { "version": "7.0.0", "resolved": "https://registry.npmjs.org/strtok3/-/strtok3-7.0.0.tgz", @@ -10246,6 +10248,7 @@ "url": "https://github.com/sponsors/Borewit" } }, +>>>>>>> 127251e2ebfc5c15d56a359b7f12754359034ee7 "node_modules/stubs": { "version": "3.0.0", "resolved": "https://registry.npmjs.org/stubs/-/stubs-3.0.0.tgz", diff --git a/package-lock.json b/package-lock.json index d5a963be..1552631e 100644 --- a/package-lock.json +++ b/package-lock.json @@ -10306,6 +10306,8 @@ "node": ">=0.10.0" } }, +<<<<<<< HEAD +======= "node_modules/strtok3": { "version": "7.0.0", "resolved": "https://registry.npmjs.org/strtok3/-/strtok3-7.0.0.tgz", @@ -10322,6 +10324,7 @@ "url": "https://github.com/sponsors/Borewit" } }, +>>>>>>> 127251e2ebfc5c15d56a359b7f12754359034ee7 "node_modules/stubs": { "version": "3.0.0", "resolved": "https://registry.npmjs.org/stubs/-/stubs-3.0.0.tgz", @@ -19371,6 +19374,8 @@ "is-utf8": "^0.2.0" } }, +<<<<<<< HEAD +======= "strtok3": { "version": "7.0.0", "resolved": "https://registry.npmjs.org/strtok3/-/strtok3-7.0.0.tgz", @@ -19380,6 +19385,7 @@ "peek-readable": "^5.0.0" } }, +>>>>>>> 127251e2ebfc5c15d56a359b7f12754359034ee7 "stubs": { "version": "3.0.0", "resolved": "https://registry.npmjs.org/stubs/-/stubs-3.0.0.tgz", diff --git a/src/controllers/tanksController.js b/src/controllers/tanksController.js index 83dcebc7..0aefd7cb 100644 --- a/src/controllers/tanksController.js +++ b/src/controllers/tanksController.js @@ -159,6 +159,23 @@ exports.getTank = async (req, reply) => { // } //}; +exports.getTankmotordata = async (req, reply) => { + try { + await MotorData.find({customerId: req.query.customerId}) + .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); + } +}; + + exports.updateTanklevels = async (req, reply) => { try { const customerId = req.params.customerId; @@ -639,7 +656,8 @@ exports.motorAction = async (req, reply) => { console.log(sump_final_water_level,"2") sump_water_levels = sump_water_levels.filter(tank => tank.supplier_tank !== supplier_tank); - const quantity_delivered = sump_water_level1-sump_final_water_level + const quantity_delivered = Math.abs(sump_water_level1 - sump_final_water_level); + if (supplier_tank_type === "sump") { @@ -738,16 +756,18 @@ exports.motorAction = async (req, reply) => { exports.consumption = async (req, reply) => { try { const customerId = req.params.customerId; - const tanks = await Tank.find({ customerId }); + const tanks = await Tank.find({ customerId,tankLocation:"overhead"}); const tankData = []; for (const tank of tanks) { const tankId = tank._id; - let capacity = parseInt(tank.capacity.replace(/,/g, ''), 10); - let waterLevel = capacity*2+1700; // initial water level + const waterlevel_at_midnight = parseInt(tank.waterlevel_at_midnight.replace(/,/g, ''), 10); + const total_water_added_from_midnight = parseInt(tank.total_water_added_from_midnight.replace(/,/g, ''), 10); + const waterlevel = parseInt(tank.waterlevel.replace(/,/g, ''), 10); + const tankname = tank.tankName - + const consumption = (waterlevel_at_midnight+total_water_added_from_midnight)-waterlevel - const newWaterLevel = Math.floor(waterLevel); + const newWaterLevel = Math.floor(consumption); tankData.push({ tankname, waterLevel: newWaterLevel }); @@ -939,33 +959,33 @@ exports.calculateCapacity = async (req, reply) => { exports.IotDevice = async (req, reply) => { try { - const { hardwareId, tankHeight, maxLevel, minLevel, mode } = req.body; + const { hardwareId, mode, tanks } = req.body; // create a new tank document with the current date and time const currentDate = new Date(); - const date = currentDate.toISOString(); // save the date as an ISO string - const time = currentDate.toLocaleTimeString('en-IN', {hour12:false, timeZone: 'Asia/Kolkata' }); - const tank = new IotData({ hardwareId, tankHeight, maxLevel, minLevel, mode, date, time }); - - // save the document to MongoDB - await tank.save(); + const date = currentDate.toISOString(); // save the date as an ISO string + const time = currentDate.toLocaleTimeString('en-IN', { hour12: false, timeZone: 'Asia/Kolkata' }); + + // Create an array of tank documents + const tankDocuments = tanks.map(tank => ({ + tankhardwareId: tank.tankhardwareId, + tankHeight: tank.tankHeight, + maxLevel: tank.maxLevel, + minLevel: tank.minLevel + + })); - // get all the tank documents for the current hardwareId sorted in descending order of date and time - const tanks = await IotData.find({ hardwareId }).sort({ date: -1, time: -1 }); + // create a new IOttank document with the provided data + const ottank = new IotData({ hardwareId, mode, tanks: tankDocuments, date, time }); - // if the number of documents for the current hardwareId is greater than three, remove the oldest documents until there are only three left - if (tanks.length > 3) { - const oldestDocuments = tanks.slice(3); - for (const doc of oldestDocuments) { - await IotData.deleteOne({ _id: doc._id }); - } - } + // save the document to MongoDB + await ottank.save(); - // get the latest three tank documents for the current hardwareId sorted in descending order of date and time - const latestTanks = await IotData.find({ hardwareId }).sort({ date: -1, time: -1 }).limit(3); + // get the latest document sorted in descending order of date and time + const latestOttank = await IotData.findOne({ hardwareId }).sort({ date: -1, time: -1 }); - // send the latest three documents in descending order of date and time - reply.code(200).send({ latestTanks: latestTanks.reverse() }); + // send the latest document + reply.code(200).send({ latestOttank }); } catch (err) { // send an error response reply.code(500).send({ error: err.message }); diff --git a/src/handlers/commanHelper.js b/src/handlers/commanHelper.js index a43fcebb..c961e071 100644 --- a/src/handlers/commanHelper.js +++ b/src/handlers/commanHelper.js @@ -14,4 +14,5 @@ async function decodeToken(request) { }); } + module.exports = fp(decodeToken, { fastify: ">=1.0.0" }); diff --git a/src/handlers/friendRequestHandler.js b/src/handlers/friendRequestHandler.js index 07a781b2..f035f54b 100644 --- a/src/handlers/friendRequestHandler.js +++ b/src/handlers/friendRequestHandler.js @@ -14,6 +14,7 @@ const schedule = require('node-schedule'); + // Handle friend request creation exports.friendRequest = async (request, reply) => { diff --git a/src/handlers/supplierHandler.js b/src/handlers/supplierHandler.js index f77dda36..14b7ed5f 100644 --- a/src/handlers/supplierHandler.js +++ b/src/handlers/supplierHandler.js @@ -915,6 +915,7 @@ exports.getCurrentSupplier = async (req, reply) => { throw boom.boomify(err); } }; + // Get all suppliers // exports.getSuppliers = async (req, reply) => { // const limit = parseInt(req.query.limit) || 100; diff --git a/src/handlers/userHandler.js b/src/handlers/userHandler.js index 4282213c..97e82db7 100644 --- a/src/handlers/userHandler.js +++ b/src/handlers/userHandler.js @@ -108,6 +108,7 @@ exports.verifyUser = async (req, reply) => { // check if user exists in the system. If user exists , display message that // phone number is not available + userExists = await User.findOne({ phone: phone }); if (userExists) { // return user exists message diff --git a/src/models/tanks.js b/src/models/tanks.js index 19cb4c05..2b279362 100644 --- a/src/models/tanks.js +++ b/src/models/tanks.js @@ -81,15 +81,20 @@ const motordataSchema = new mongoose.Schema({ +const tankSchema = new mongoose.Schema({ + tankhardwareId: { type: String }, + tankHeight: { type: String, required: true }, + maxLevel: { type: String, required: true }, + minLevel: { type: String, required: true } +}); + const IOttankSchema = new mongoose.Schema({ - hardwareId: { type: String}, - tankHeight: { type: String, required: true }, - maxLevel: { type: String, required: true }, - minLevel: { type: String, required: true }, - date: { type: String, required: true }, - time: { type: String, required: true }, - mode: { type: Number, required: true } - }); + hardwareId: { type: String }, + mode: { type: Number, required: true }, + tanks: [tankSchema], + date: { type: String, required: true }, + time: { type: String, required: true } +}); @@ -102,3 +107,4 @@ const IotData = mongoose.model("IotData", IOttankSchema); module.exports = { Tank, MotorData,IotData } + diff --git a/src/routes/tanksRoute.js b/src/routes/tanksRoute.js index 194e0b91..f3a84719 100644 --- a/src/routes/tanksRoute.js +++ b/src/routes/tanksRoute.js @@ -327,43 +327,46 @@ module.exports = function (fastify, opts, next) { }); + - fastify.route({ - method: "POST", - url: "/api/APIWrite", - schema: { - tags: ["Tank"], - description: "This is for cretae IOT Device", - summary: "This is for Create IOT Device.", - - body: { - type: "object", - properties: { - hardwareId: { type: "string" }, - tankHeight: { type: "string"}, - maxLevel: { type: "string" }, - minLevel: { type: "string" }, - - mode: { type: "string" }, + fastify.route({ + method: "POST", + url: "/api/APIWrite", + schema: { + tags: ["Tank"], + description: "This is for creating an IOT Device", + summary: "this is to Create IOT Device", + body: { + type: "object", + properties: { + hardwareId: { type: "string" }, + mode: { type: "string" }, + tanks: { + type: "array", + items: { + type: "object", + properties: { + tankhardwareId: { type: "string" }, + tankHeight: { type: "string" }, + maxLevel: { type: "string" }, + minLevel: { type: "string" } + }, + required: ["tankhardwareId", "tankHeight", "maxLevel", "minLevel"] + } + } + }, + required: ["hardwareId", "mode", "tanks"] }, + security: [ + { + basicAuth: [] + } + ] }, - security: [ - { - basicAuth: [], - }, - ], - }, - // preHandler: [ - // fastify.auth([fastify.operatorAuthenticate]), - // validationHandler.validatePhoneFormat, - // ], - - - + handler: tanksController.IotDevice + }); + - handler: tanksController.IotDevice, - - }); fastify.get("/api/APIRead", { schema: { @@ -543,6 +546,25 @@ module.exports = function (fastify, opts, next) { preHandler: fastify.auth([fastify.authenticate]), handler: tanksController.deletemotordatarecordsbefore7days, }); + + fastify.get("/api/getTankmotordata", { + schema: { + tags: ["Tank"], + description: "This is for Get Tank Motor Data", + summary: "This is for to Get Tank Motor Data", + querystring: { + customerId: {type: 'string'} + }, + security: [ + { + basicAuth: [], + }, + ], + }, + preHandler: fastify.auth([fastify.authenticate]), + handler: tanksController.getTankmotordata, + }); + next(); }