bhaskar 2 years ago
commit 609598a1b2

@ -21,16 +21,21 @@ var termination = chalk.bold.magenta;
// TODO // TODO
// Need to read database url from a environment variable. // Need to read database url from a environment variable.
// const databaseURL = "mongodb://armintatankdbuser:armintatank1@35.207.198.4:27017/arminta-tank-db; // 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"; // 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. // Next line not used , or no need to pass default db in the mongo connection url.
const defaultDatabase = "arminta-tank-db"; const defaultDatabase = "arminta-tank-db";
//export this function and imported by server.js //export this function and imported by server.js
async function dbConnection() { async function dbConnection() {
try { 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.connect(databaseURLNew)
db.connection.on("connected", function () { db.connection.on("connected", function () {
console.log( console.log(

@ -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 }); 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 { 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; //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 { // For example, you can update a database or trigger an action
const motor_id = req.params.motor_id;
console.log(motor_id)
const motorInfo = await Tank.findOne({ motor_id: motor_id }); 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, motor_status:motor_status });
} catch (err) {
throw boom.boomify(err);
}
};
reply.send({ status_code: 200,status:motorInfo.motor_status}); 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); throw boom.boomify(err);
} }
}; };
exports.motorspeed = async (req, reply) => { exports.changeMotorStatus = async (req, reply) => {
try { try {
const motor_id = req.body.motor_id; const motor_id = req.body.motor_id;
const speed = req.body.speed; 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
const result = await Tank.findOneAndUpdate( const result = await Tank.findOneAndUpdate(
{ motor_id: motor_id }, { motor_id: motor_id },
{ $set: { motor_speed: speed } } { $set: { motor_status: action } });
);
//return update;
console.log(result)
reply.send({ status_code: 200});
} reply.send({ status_code: 200});
catch (err) { } catch (err) {
throw boom.boomify(err); throw boom.boomify(err);
} }
}; };
@ -1568,56 +1567,3 @@ exports.motortemperature = async (req, reply) => {
throw boom.boomify(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' });
}
}

@ -2,7 +2,7 @@
// logger: true, // logger: true,
// }); // });
const axios = require('axios'); //const axios = require('axios');
const bcrypt = require("bcrypt"); const bcrypt = require("bcrypt");
const saltRounds = 10; const saltRounds = 10;
const libphonenumberjs = require("libphonenumber-js"); const libphonenumberjs = require("libphonenumber-js");

@ -129,6 +129,8 @@ exports.verifyUser = async (req, reply) => {
throw boom.boomify(err); throw boom.boomify(err);
} }
}; };
exports.validatePhoneFormat = async (req, reply) => { exports.validatePhoneFormat = async (req, reply) => {
try { try {
var user = new User(req.body); var user = new User(req.body);
@ -360,6 +362,7 @@ exports.verifyPhone = async (req, reply) => {
throw boom.boomify(err); throw boom.boomify(err);
} }
}; };
exports.sendPhoneVerificationCode = async (req, reply) => { exports.sendPhoneVerificationCode = async (req, reply) => {
// Setting a regError in request so a user is not sent a verificaiton code if registration is not successful // 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 // 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 authToken = twilioAuthToken; // Your Auth Token from www.twilio.com/console
var client = new twilio(twilioAccountSid, twilioAuthToken); var client = new twilio(twilioAccountSid, twilioAuthToken);
// var code = Math.floor(100000 + Math.random() * 900000); // 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 // Message format to allow app to autopickup sms message and
// verify the code. // verify the code.

@ -1,7 +1,7 @@
const userController = require("./controllers/userController"); const userController = require("./controllers/userController");
const { User,Counter, generateBookingId,resetCounter,generateCustomerId,ProfilePicture} = require('./models/User') 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 tankersController = require("./controllers/tankersController.js");
const createConnectionController = require("./controllers/createConnectionController"); const createConnectionController = require("./controllers/createConnectionController");
const cors = require("cors"); const cors = require("cors");

@ -73,6 +73,7 @@ const tanksSchema = new mongoose.Schema({
const motordataSchema = new mongoose.Schema({ const motordataSchema = new mongoose.Schema({
customerId: { type: String, default: null }, customerId: { type: String, default: null },
supplierTank: { type: String, default: null }, supplierTank: { type: String, default: null },

@ -573,116 +573,65 @@ module.exports = function (fastify, opts, next) {
fastify.route({ fastify.post('/api/motor/write', {
method: 'PUT',
url: '/api/motors/start',
schema: { schema: {
tags: ["Tank"], tags: ["Tank"],
description: "This is to start motor using IOT", description: "This is to Write the motor status",
summary: "This is to start motor using IOT", summary: "This is to Write the motor status",
body: { body: {
type: 'object', type: 'object',
required: ['motor_id', 'status'],
properties: { properties: {
motor_id: { type: 'string' }, 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: { schema: {
tags: ["Tank"], tags: ["Tank"],
description: "This is to stop motor using IOT", description: "This is to Read the motor status",
summary: "This is to stop motor using IOT", summary: "This is to Read the motor status",
body: { querystring: {
type: 'object', type: 'object',
properties: { properties: {
motor_id: { type: 'string' }, motor_id: { type: 'string' },
}, },
required: ["motor_id"] // required: ['motor_id'],
}, },
}, },
handler: tanksController.stopmotoriot handler: tanksController.readMotorStatus
}); });
fastify.route({ fastify.post('/api/motor/changeMotorStatus', {
method: 'GET',
url: '/api/motors/status/:motor_id',
schema: { schema: {
tags: ["Tank"], tags: ["Tank"],
description: "This is to get motor status", description: "This is to change the motor status",
summary: "This is to get motor status", summary: "This is to change the 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',
schema: {
tags: ["Tank"],
description: "This is to change motor pumping speed using IOT",
summary: "This is to to change motor pumping speed using IOT",
body: { body: {
type: 'object', type: 'object',
properties: { properties: {
motor_id: { type: 'string' }, motor_id: { type: 'string' },
speed: { type: 'string' }, action: { type: 'string' },
}, },
required: ["motor_id"] required: ["motor_id"]
}, },
}, },
handler: tanksController.motorspeed handler: tanksController.changeMotorStatus
});
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
}); });

@ -168,7 +168,7 @@ module.exports = function (fastify, opts, next) {
}, },
preHandler: [ preHandler: [
validationHandler.fieldCheck, validationHandler.fieldCheck,
validationHandler.verifyUser, // validationHandler.verifyUser,
// validationHandler.validatePhoneFormat, // validationHandler.validatePhoneFormat,
validationHandler.validateEmailFormat, validationHandler.validateEmailFormat,
], ],

Loading…
Cancel
Save