diff --git a/src/config/config.js b/src/config/config.js deleted file mode 100644 index 2c55bc89..00000000 --- a/src/config/config.js +++ /dev/null @@ -1,78 +0,0 @@ -// This file establishes conenction to the mongodb. Connection is used by the applicaiton and initialized -// when it loads during starup . This is registered in the root level index.js - -// require mongoose module -const fp = require("fastify-plugin"); -const db = require("mongoose"); -db.set("useFindAndModify", false); -// require clak module to give colors to console text. -var chalk = require("chalk"); - -const boom = require("boom"); - - -// Define colors for db messages on cosole. - -var connected = chalk.bold.cyan; -var error = chalk.bold.yellow; -var disconnected = chalk.bold.red; -var termination = chalk.bold.magenta; - -// TODO -// Need to read database url from a environment variable. -// const databaseURL = "mongodb://armintatankdbuser:armintatank1@35.207.198.4:27017/arminta-tank-db; -const databaseURLNew = "mongodb://35.207.198.4:27017/arminta-tank-db"; -//const databaseURLNew = "mongodb://35.200.129.165:27017/health-care-db"; - -// const databaseURLNew = "mongodb://127.0.0.1:27017/arminta-tank-db"; -// Next line not used , or no need to pass default db in the mongo connection url. -const defaultDatabase = "arminta-tank-db"; - -//export this function and imported by server.js - - -async function dbConnection() { - try { - //db.connect(databaseURLNew,{ useNewUrlParser: true }) - db.connect(databaseURLNew, { user: "armintatankdbuser" , pass: "armintatank1" , useUnifiedTopology: true, useNewUrlParser: true, }); -// db.connect('mongodb://armintatankdbuser01:password@35.207.198.4:27017/arminta-tank', { useNewUrlParser: true }); - // db.connect(databaseURLNew, { user: "healthcaredbuser01", pass: "healthcare01" , useUnifiedTopology: true, }); - - // db.connect(databaseURLNew) - db.connection.on("connected", function () { - console.log( - connected("Mongoose default connection is open to ", databaseURLNew) - ); - - - - }); - - db.connection.on("error", function (err) { - console.log( - error("Mongoose default connection has occured " + err + " error") - ); - }); - - db.connection.on("disconnected", function () { - console.log(disconnected("Mongoose default connection is disconnected")); - }); - - process.on("SIGINT", function () { - db.connection.close(function () { - console.log( - termination( - "Mongoose default connection is disconnected due to application termination" - ) - ); - process.exit(0); - }); - }); - } catch (err) { - throw boom.boomify(err); - } -} - - - -module.exports = fp(dbConnection, { fastify: ">=1.0.0" }); diff --git a/src/controllers/tanksController.js b/src/controllers/tanksController.js index 0110fa94..c122ddfb 100644 --- a/src/controllers/tanksController.js +++ b/src/controllers/tanksController.js @@ -1175,6 +1175,10 @@ exports.calculateCapacity = async (req, reply) => { // reply.code(500).send({ error: err.message }); // } // }; + + + + exports.IotDevice = async (req, reply) => { try { const { hardwareId, mode, tanks } = req.body; @@ -1222,7 +1226,8 @@ exports.IotDevice = async (req, reply) => { const tank_height = parseInt(existingTank.height.replace(/,/g, ''), 10) * 30.48; const water_level_height = tank_height - tankHeight; const waterCapacityPerCm = parseInt(existingTank.waterCapacityPerCm.replace(/,/g, ''), 10); - const water_level = water_level_height * waterCapacityPerCm; + let water_level = water_level_height * waterCapacityPerCm; + water_level = Math.round(water_level); // Round to nearest whole number existingTank.waterlevel = water_level.toString(); // Convert to string as per schema definition // Save the updated tank document @@ -1259,6 +1264,8 @@ exports.IotDevice = async (req, reply) => { } }; + + // exports.getIotD = async(req, reply) => { // try { // await IotData.find({hardwareId: req.query.hardwareId})