bhaskar 2 years ago
commit 2b7a33f517

3
node_modules/.package-lock.json generated vendored

@ -10230,6 +10230,8 @@
"node": ">=0.10.0" "node": ">=0.10.0"
} }
}, },
<<<<<<< HEAD
=======
"node_modules/strtok3": { "node_modules/strtok3": {
"version": "7.0.0", "version": "7.0.0",
"resolved": "https://registry.npmjs.org/strtok3/-/strtok3-7.0.0.tgz", "resolved": "https://registry.npmjs.org/strtok3/-/strtok3-7.0.0.tgz",
@ -10246,6 +10248,7 @@
"url": "https://github.com/sponsors/Borewit" "url": "https://github.com/sponsors/Borewit"
} }
}, },
>>>>>>> 127251e2ebfc5c15d56a359b7f12754359034ee7
"node_modules/stubs": { "node_modules/stubs": {
"version": "3.0.0", "version": "3.0.0",
"resolved": "https://registry.npmjs.org/stubs/-/stubs-3.0.0.tgz", "resolved": "https://registry.npmjs.org/stubs/-/stubs-3.0.0.tgz",

6
package-lock.json generated

@ -10306,6 +10306,8 @@
"node": ">=0.10.0" "node": ">=0.10.0"
} }
}, },
<<<<<<< HEAD
=======
"node_modules/strtok3": { "node_modules/strtok3": {
"version": "7.0.0", "version": "7.0.0",
"resolved": "https://registry.npmjs.org/strtok3/-/strtok3-7.0.0.tgz", "resolved": "https://registry.npmjs.org/strtok3/-/strtok3-7.0.0.tgz",
@ -10322,6 +10324,7 @@
"url": "https://github.com/sponsors/Borewit" "url": "https://github.com/sponsors/Borewit"
} }
}, },
>>>>>>> 127251e2ebfc5c15d56a359b7f12754359034ee7
"node_modules/stubs": { "node_modules/stubs": {
"version": "3.0.0", "version": "3.0.0",
"resolved": "https://registry.npmjs.org/stubs/-/stubs-3.0.0.tgz", "resolved": "https://registry.npmjs.org/stubs/-/stubs-3.0.0.tgz",
@ -19371,6 +19374,8 @@
"is-utf8": "^0.2.0" "is-utf8": "^0.2.0"
} }
}, },
<<<<<<< HEAD
=======
"strtok3": { "strtok3": {
"version": "7.0.0", "version": "7.0.0",
"resolved": "https://registry.npmjs.org/strtok3/-/strtok3-7.0.0.tgz", "resolved": "https://registry.npmjs.org/strtok3/-/strtok3-7.0.0.tgz",
@ -19380,6 +19385,7 @@
"peek-readable": "^5.0.0" "peek-readable": "^5.0.0"
} }
}, },
>>>>>>> 127251e2ebfc5c15d56a359b7f12754359034ee7
"stubs": { "stubs": {
"version": "3.0.0", "version": "3.0.0",
"resolved": "https://registry.npmjs.org/stubs/-/stubs-3.0.0.tgz", "resolved": "https://registry.npmjs.org/stubs/-/stubs-3.0.0.tgz",

@ -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) => { exports.updateTanklevels = async (req, reply) => {
try { try {
const customerId = req.params.customerId; const customerId = req.params.customerId;
@ -639,7 +656,8 @@ exports.motorAction = async (req, reply) => {
console.log(sump_final_water_level,"2") console.log(sump_final_water_level,"2")
sump_water_levels = sump_water_levels.filter(tank => tank.supplier_tank !== supplier_tank); 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") { if (supplier_tank_type === "sump") {
@ -738,16 +756,18 @@ exports.motorAction = async (req, reply) => {
exports.consumption = async (req, reply) => { exports.consumption = async (req, reply) => {
try { try {
const customerId = req.params.customerId; const customerId = req.params.customerId;
const tanks = await Tank.find({ customerId }); const tanks = await Tank.find({ customerId,tankLocation:"overhead"});
const tankData = []; const tankData = [];
for (const tank of tanks) { for (const tank of tanks) {
const tankId = tank._id; const tankId = tank._id;
let capacity = parseInt(tank.capacity.replace(/,/g, ''), 10); const waterlevel_at_midnight = parseInt(tank.waterlevel_at_midnight.replace(/,/g, ''), 10);
let waterLevel = capacity*2+1700; // initial water level 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 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 }); tankData.push({ tankname, waterLevel: newWaterLevel });
@ -939,33 +959,33 @@ exports.calculateCapacity = async (req, reply) => {
exports.IotDevice = async (req, reply) => { exports.IotDevice = async (req, reply) => {
try { 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 // create a new tank document with the current date and time
const currentDate = new Date(); const currentDate = new Date();
const date = currentDate.toISOString(); // save the date as an ISO string const date = currentDate.toISOString(); // save the date as an ISO string
const time = currentDate.toLocaleTimeString('en-IN', {hour12:false, timeZone: 'Asia/Kolkata' }); const time = currentDate.toLocaleTimeString('en-IN', { hour12: false, timeZone: 'Asia/Kolkata' });
const tank = new IotData({ hardwareId, tankHeight, maxLevel, minLevel, mode, date, time });
// Create an array of tank documents
// save the document to MongoDB const tankDocuments = tanks.map(tank => ({
await tank.save(); 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 // create a new IOttank document with the provided data
const tanks = await IotData.find({ hardwareId }).sort({ date: -1, time: -1 }); 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 // save the document to MongoDB
if (tanks.length > 3) { await ottank.save();
const oldestDocuments = tanks.slice(3);
for (const doc of oldestDocuments) {
await IotData.deleteOne({ _id: doc._id });
}
}
// get the latest three tank documents for the current hardwareId sorted in descending order of date and time // get the latest document sorted in descending order of date and time
const latestTanks = await IotData.find({ hardwareId }).sort({ date: -1, time: -1 }).limit(3); const latestOttank = await IotData.findOne({ hardwareId }).sort({ date: -1, time: -1 });
// send the latest three documents in descending order of date and time // send the latest document
reply.code(200).send({ latestTanks: latestTanks.reverse() }); reply.code(200).send({ latestOttank });
} catch (err) { } catch (err) {
// send an error response // send an error response
reply.code(500).send({ error: err.message }); reply.code(500).send({ error: err.message });

@ -14,4 +14,5 @@ async function decodeToken(request) {
}); });
} }
module.exports = fp(decodeToken, { fastify: ">=1.0.0" }); module.exports = fp(decodeToken, { fastify: ">=1.0.0" });

@ -14,6 +14,7 @@ const schedule = require('node-schedule');
// Handle friend request creation // Handle friend request creation
exports.friendRequest = async (request, reply) => { exports.friendRequest = async (request, reply) => {

@ -915,6 +915,7 @@ exports.getCurrentSupplier = async (req, reply) => {
throw boom.boomify(err); throw boom.boomify(err);
} }
}; };
// Get all suppliers // Get all suppliers
// exports.getSuppliers = async (req, reply) => { // exports.getSuppliers = async (req, reply) => {
// const limit = parseInt(req.query.limit) || 100; // const limit = parseInt(req.query.limit) || 100;

@ -108,6 +108,7 @@ exports.verifyUser = async (req, reply) => {
// check if user exists in the system. If user exists , display message that // check if user exists in the system. If user exists , display message that
// phone number is not available // phone number is not available
userExists = await User.findOne({ phone: phone }); userExists = await User.findOne({ phone: phone });
if (userExists) { if (userExists) {
// return user exists message // return user exists message

@ -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({ const IOttankSchema = new mongoose.Schema({
hardwareId: { type: String}, hardwareId: { type: String },
tankHeight: { type: String, required: true }, mode: { type: Number, required: true },
maxLevel: { type: String, required: true }, tanks: [tankSchema],
minLevel: { type: String, required: true }, date: { type: String, required: true },
date: { type: String, required: true }, time: { type: String, required: true }
time: { type: String, required: true }, });
mode: { type: Number, required: true }
});
@ -102,3 +107,4 @@ const IotData = mongoose.model("IotData", IOttankSchema);
module.exports = { module.exports = {
Tank, MotorData,IotData Tank, MotorData,IotData
} }

@ -327,43 +327,46 @@ module.exports = function (fastify, opts, next) {
}); });
fastify.route({ fastify.route({
method: "POST", method: "POST",
url: "/api/APIWrite", url: "/api/APIWrite",
schema: { schema: {
tags: ["Tank"], tags: ["Tank"],
description: "This is for cretae IOT Device", description: "This is for creating an IOT Device",
summary: "This is for Create IOT Device.", summary: "this is to Create IOT Device",
body: {
body: { type: "object",
type: "object", properties: {
properties: { hardwareId: { type: "string" },
hardwareId: { type: "string" }, mode: { type: "string" },
tankHeight: { type: "string"}, tanks: {
maxLevel: { type: "string" }, type: "array",
minLevel: { type: "string" }, items: {
type: "object",
mode: { type: "string" }, 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: [ handler: tanksController.IotDevice
{ });
basicAuth: [],
},
],
},
// preHandler: [
// fastify.auth([fastify.operatorAuthenticate]),
// validationHandler.validatePhoneFormat,
// ],
handler: tanksController.IotDevice,
});
fastify.get("/api/APIRead", { fastify.get("/api/APIRead", {
schema: { schema: {
@ -543,6 +546,25 @@ module.exports = function (fastify, opts, next) {
preHandler: fastify.auth([fastify.authenticate]), preHandler: fastify.auth([fastify.authenticate]),
handler: tanksController.deletemotordatarecordsbefore7days, 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(); next();
} }

Loading…
Cancel
Save