bhaskar 2 years ago
commit 2b7a33f517

3
node_modules/.package-lock.json generated vendored

@ -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",

6
package-lock.json generated

@ -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",

@ -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 tankname = tank.tankName
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 });
const time = currentDate.toLocaleTimeString('en-IN', { hour12: false, timeZone: 'Asia/Kolkata' });
// save the document to MongoDB
await tank.save();
// 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 });
}));
// 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 });
}
}
// create a new IOttank document with the provided data
const ottank = new IotData({ hardwareId, mode, tanks: tankDocuments, date, time });
// 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 });

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

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

@ -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;

@ -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

@ -81,15 +81,20 @@ const motordataSchema = new mongoose.Schema({
const IOttankSchema = new mongoose.Schema({
hardwareId: { type: String},
const tankSchema = new mongoose.Schema({
tankhardwareId: { type: String },
tankHeight: { type: String, required: true },
maxLevel: { type: String, required: true },
minLevel: { type: String, required: true },
minLevel: { type: String, required: true }
});
const IOttankSchema = new mongoose.Schema({
hardwareId: { type: String },
mode: { type: Number, required: true },
tanks: [tankSchema],
date: { type: String, required: true },
time: { type: String, required: true },
mode: { type: Number, required: true }
});
time: { type: String, required: true }
});
@ -102,3 +107,4 @@ const IotData = mongoose.model("IotData", IOttankSchema);
module.exports = {
Tank, MotorData,IotData
}

@ -328,42 +328,45 @@ 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.",
description: "This is for creating an IOT Device",
summary: "this is to Create IOT Device",
body: {
type: "object",
properties: {
hardwareId: { type: "string" },
tankHeight: { type: "string"},
maxLevel: { type: "string" },
minLevel: { 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: [],
},
],
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();
}

Loading…
Cancel
Save