diff --git a/src/config/config.js b/src/config/config.js index 0b1f4ff5..5afdd88a 100644 --- a/src/config/config.js +++ b/src/config/config.js @@ -21,16 +21,21 @@ 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.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, { user: "armintatankdbuser", pass: "armintatank1" , useUnifiedTopology: true, }); + // db.connect(databaseURLNew, { user: "armintatankdbuser", pass: "armintatank1" , useUnifiedTopology: true, }); + db.connect(databaseURLNew, { user: "healthcaredbuser01", pass: "healthcare01" , useUnifiedTopology: true, }); + // db.connect(databaseURLNew) db.connection.on("connected", function () { console.log( diff --git a/src/controllers/tanksController.js b/src/controllers/tanksController.js index c07bfa30..ec253f4c 100644 --- a/src/controllers/tanksController.js +++ b/src/controllers/tanksController.js @@ -1346,7 +1346,7 @@ exports.totalwaterLevelSum = async (request, reply) => { } ]); - const result = waterlevelSum[0]?.totalWaterlevel ?? 0; + const result = waterlevelSum[0]?totalWaterlevel : 0; reply.send({ waterlevelSum: result }); } @@ -1445,48 +1445,28 @@ exports.deletemotordatarecordsbefore7days = async (req, reply) => { -exports.startmotoriot = async (req, reply) => { - try { - var motor_id = req.body.motor_id; - - - - - const result = await Tank.findOneAndUpdate( - { motor_id: motor_id }, - { $set: { motor_status: "2" } } - ); - //return update; - console.log(result) - reply.send({ status_code: 200}); - } - catch (err) { - throw boom.boomify(err); - } -}; -exports.stopmotoriot = async (req, reply) => { +exports.motorstatus = async (req, reply) => { try { - var motor_id = req.body.motor_id; - + const motor_id = req.params.motor_id; + console.log(motor_id) - + const motorInfo = await Tank.findOne({ motor_id: motor_id }); + + console.log(motorInfo) - const result = await Tank.findOneAndUpdate( - { motor_id: motor_id }, - { $set: { motor_status: "1" } } - ); + //return update; - console.log(result) - reply.send({ status_code: 200}); + + reply.send({ status_code: 200,status:motorInfo.motor_status}); @@ -1497,51 +1477,70 @@ exports.stopmotoriot = async (req, reply) => { }; +exports.readMotorStatus = async (req, reply) => { + try { + const motor_id = req.query.motor_id; -exports.motorstatus = async (req, reply) => { + // Perform any necessary logic based on action (1: Start, 2: Stop) - try { - const motor_id = req.params.motor_id; - console.log(motor_id) + // For example, you can update a database or trigger an action const motorInfo = await Tank.findOne({ motor_id: motor_id }); + const motor_status = motorInfo.motor_status - console.log(motorInfo) - - - //return update; - - reply.send({ status_code: 200,status:motorInfo.motor_status}); + reply.send({ status_code: 200, motor_status:motor_status }); + } catch (err) { + throw boom.boomify(err); + } +}; +exports.writeMotorStatus = async (req, reply) => { + try { + const { motor_id, status, current, temp } = req.body; + + // Perform any necessary logic to handle motor status update from the device + + // For example, update a database with the new status, current, and temp values + let result; + + if (status === 'on') { + result = await Tank.findOneAndUpdate( + { motor_id: motor_id }, + { $set: { motor_status: "2" } } + ); + } else if (status === 'off') { + result = await Tank.findOneAndUpdate( + { motor_id: motor_id }, + { $set: { motor_stop_status: "1" } } + ); + } - } - catch (err) { + reply.send({ status_code: 200, message: `Motor ${motor_id} status updated to ${status}` }); + } catch (err) { throw boom.boomify(err); } }; -exports.motorspeed = async (req, reply) => { - +exports.changeMotorStatus = async (req, reply) => { try { const motor_id = req.body.motor_id; - const speed = req.body.speed; - - - - - const result = await Tank.findOneAndUpdate( - { motor_id: motor_id }, - { $set: { motor_speed: speed } } - ); - //return update; - console.log(result) - reply.send({ status_code: 200}); + const action =req.body.action + // Perform any necessary logic to handle motor status update from the device + // For example, update a database with the new status, current, and temp values + - } - catch (err) { + + const result = await Tank.findOneAndUpdate( + { motor_id: motor_id }, + { $set: { motor_status: action } }); + + + + reply.send({ status_code: 200}); + } catch (err) { throw boom.boomify(err); } }; @@ -1567,57 +1566,4 @@ exports.motortemperature = async (req, reply) => { catch (err) { throw boom.boomify(err); } -}; - - - - -exports.readMotorStatus = async (request, reply) => { - try { - const { motor_id, action } = request.query; - - const tank = await Tank.findOne({ motor_id }); - - if (!tank) { - return reply.code(404).send({ error: 'Tank not found' }); - } - - if (action === '1') { - tank.motor_status = '1'; - } else if (action === '2') { - tank.motor_status = '0'; - } else { - return reply.code(400).send({ error: 'Invalid action' }); - } - - await tank.save(); - - return tank; - } catch (error) { - request.log.error(error); - return reply.code(500).send({ error: 'Internal Server Error' }); - } -} - -exports.writeMotorStatus = async (request, reply) => { - try { - const { motor_id, status, current, temp } = request.body; - - const tank = await Tank.findOne({ motor_id }); - - if (!tank) { - return reply.code(404).send({ error: 'Tank not found' }); - } - - tank.motor_status = status; - tank.motor_temperature = temp || tank.motor_temperature; - - await tank.save(); - - return tank; - } catch (error) { - request.log.error(error); - return reply.code(500).send({ error: 'Internal Server Error' }); - } -} - +}; \ No newline at end of file diff --git a/src/controllers/userController.js b/src/controllers/userController.js index 3a5030f7..013960e8 100644 --- a/src/controllers/userController.js +++ b/src/controllers/userController.js @@ -2,7 +2,7 @@ // logger: true, // }); -const axios = require('axios'); +//const axios = require('axios'); const bcrypt = require("bcrypt"); const saltRounds = 10; const libphonenumberjs = require("libphonenumber-js"); diff --git a/src/handlers/userHandler.js b/src/handlers/userHandler.js index 97e82db7..ae23d3c2 100644 --- a/src/handlers/userHandler.js +++ b/src/handlers/userHandler.js @@ -129,6 +129,8 @@ exports.verifyUser = async (req, reply) => { throw boom.boomify(err); } }; + + exports.validatePhoneFormat = async (req, reply) => { try { var user = new User(req.body); @@ -360,6 +362,7 @@ exports.verifyPhone = async (req, reply) => { throw boom.boomify(err); } }; + exports.sendPhoneVerificationCode = async (req, reply) => { // Setting a regError in request so a user is not sent a verificaiton code if registration is not successful // checkign if regError property is available in the req body, if it exists , do not send the message @@ -545,7 +548,7 @@ exports.sendPasswordResetCode = async (req) => { // var authToken = twilioAuthToken; // Your Auth Token from www.twilio.com/console var client = new twilio(twilioAccountSid, twilioAuthToken); // var code = Math.floor(100000 + Math.random() * 900000); - code = user.passwordResetCode.toString(); + //code = user.passwordResetCode.toString(); // Message format to allow app to autopickup sms message and // verify the code. diff --git a/src/index.js b/src/index.js index c40a2383..e9df1586 100644 --- a/src/index.js +++ b/src/index.js @@ -1,7 +1,7 @@ const userController = require("./controllers/userController"); const { User,Counter, generateBookingId,resetCounter,generateCustomerId,ProfilePicture} = require('./models/User') -const tanksController = require("./controllers/tanksController"); +//const tanksController = require("./controllers/tanksController"); const tankersController = require("./controllers/tankersController.js"); const createConnectionController = require("./controllers/createConnectionController"); const cors = require("cors"); diff --git a/src/models/tanks.js b/src/models/tanks.js index 66750896..637134a8 100644 --- a/src/models/tanks.js +++ b/src/models/tanks.js @@ -71,6 +71,7 @@ const tanksSchema = new mongoose.Schema({ } }); + const motordataSchema = new mongoose.Schema({ diff --git a/src/routes/tanksRoute.js b/src/routes/tanksRoute.js index ad6edc6c..19376aa7 100644 --- a/src/routes/tanksRoute.js +++ b/src/routes/tanksRoute.js @@ -573,116 +573,65 @@ module.exports = function (fastify, opts, next) { - fastify.route({ - method: 'PUT', - url: '/api/motors/start', - + fastify.post('/api/motor/write', { schema: { tags: ["Tank"], - description: "This is to start motor using IOT", - summary: "This is to start motor using IOT", + description: "This is to Write the motor status", + summary: "This is to Write the motor status", body: { type: 'object', + required: ['motor_id', 'status'], properties: { motor_id: { type: 'string' }, - + status: { type: 'string', enum: ['on', 'off'] }, + current: { type: 'string' }, + temp: { type: 'string' }, }, - required: ["motor_id"] }, - + }, - handler: tanksController.startmotoriot + handler: tanksController.writeMotorStatus + }); - fastify.route({ - method: 'PUT', - url: '/api/motors/stop', - + fastify.get('/api/motor/read', { schema: { tags: ["Tank"], - description: "This is to stop motor using IOT", - summary: "This is to stop motor using IOT", - body: { + description: "This is to Read the motor status", + summary: "This is to Read the motor status", + querystring: { type: 'object', properties: { motor_id: { type: 'string' }, - + }, - required: ["motor_id"] + // required: ['motor_id'], }, - + }, - handler: tanksController.stopmotoriot + handler: tanksController.readMotorStatus }); - fastify.route({ - method: 'GET', - url: '/api/motors/status/:motor_id', - - schema: { - tags: ["Tank"], - description: "This is to get motor status", - summary: "This is to get motor status", - params: { - required: ["motor_id"], - type: "object", - properties: { - motor_id: { - type: "string", - description: "motor_id", - }, - }, - }, - - }, - handler: tanksController.motorstatus - }); - - fastify.route({ - method: 'PUT', - url: '/api/motors/speed', - + fastify.post('/api/motor/changeMotorStatus', { schema: { tags: ["Tank"], - description: "This is to change motor pumping speed using IOT", - summary: "This is to to change motor pumping speed using IOT", + description: "This is to change the motor status", + summary: "This is to change the motor status", + body: { type: 'object', properties: { motor_id: { type: 'string' }, - speed: { type: 'string' }, + action: { type: 'string' }, }, required: ["motor_id"] }, }, - handler: tanksController.motorspeed - }); - - fastify.route({ - method: 'GET', - url: '/api/motors/temperature/:motor_id', - - schema: { - tags: ["Tank"], - description: "This is to get motor temperature", - summary: "This is to get motor temperature", - params: { - required: ["motor_id"], - type: "object", - properties: { - motor_id: { - type: "string", - description: "motor_id", - }, - }, - }, - - }, - handler: tanksController.motortemperature + handler: tanksController.changeMotorStatus }); diff --git a/src/routes/usersRoute.js b/src/routes/usersRoute.js index 27ee224f..70f7b944 100644 --- a/src/routes/usersRoute.js +++ b/src/routes/usersRoute.js @@ -168,7 +168,7 @@ module.exports = function (fastify, opts, next) { }, preHandler: [ validationHandler.fieldCheck, - validationHandler.verifyUser, + // validationHandler.verifyUser, // validationHandler.validatePhoneFormat, validationHandler.validateEmailFormat, ],