changes in iot waterlevel

master
varun 1 year ago
parent db95ed23c5
commit 52ec48e4c8

@ -452,72 +452,54 @@ const fastify = require("fastify")({
// };
exports.createConnections = async (req, body) => {
exports.createConnections = async (req, reply) => {
try {
const customerId = req.params.customerId;
const tankname = req.body.tankname;
// Retrieve the tank or return an error if it's not found
const tankInfo = await Tank.findOne({ customerId: customerId.toString(), tankName: tankname });
const usertobeInserted = req.body;
tankInfo.connections.source = tankInfo.tankName;
if (usertobeInserted.inputConnections) {
tankInfo.connections.inputConnections = usertobeInserted.inputConnections.map(connection => {
return {
inputConnections: connection.inputConnections,
input_type: connection.input_type,
motor_status: connection.motor_status || "0",
inputismotor: connection.hasOwnProperty("inputismotor") ? connection.inputismotor : false,
};
});
if (!tankInfo) {
return reply.send({ status_code: 404, error: "Tank not found" });
}
if (usertobeInserted.outputConnections) {
tankInfo.connections.outputConnections = usertobeInserted.outputConnections.map(connection => {
return {
outputConnections: connection.outputConnections,
output_type: connection.output_type,
outputismotor: connection.hasOwnProperty("outputismotor") ? connection.outputismotor : false,
};
});
// Update the source tank info
tankInfo.connections.source = tankname;
// Handle input connections
if (req.body.inputConnections) {
tankInfo.connections.inputConnections = req.body.inputConnections.map(connection => ({
inputConnections: connection.inputConnections,
input_type: connection.input_type,
motor_id: connection.motor_id || null,
motor_status: connection.motor_status || "0",
motor_stop_status: connection.motor_stop_status || "1",
inputismotor: connection.hasOwnProperty("inputismotor") ? connection.inputismotor : false,
capacity: connection.capacity || null,
water_level: connection.water_level || null
}));
}
const tank_connections = await tankInfo.save();
const connection_data_check = tank_connections.connections.inputConnections;
const sump_names = connection_data_check.map(d => d.inputConnections);
console.log(sump_names);
const connection_data = usertobeInserted.outputConnections;
console.log(connection_data, "connection_data");
for (const data of connection_data) {
if (data['output_type'] === "overhead") {
const tankName = data['outputConnections'];
if (sump_names.includes(tankname)) {
console.log(`${tankname} exists in ${sump_names}`);
} else {
const tankConnections = await Tank.findOneAndUpdate(
{ customerId: customerId.toString(), tankName: tankName },
{
$addToSet: {
'connections.inputConnections': {
$each: [{ inputConnections: tankname, input_type: 'sump', inputismotor: data.outputismotor || false }],
},
// 'connections.outputConnections': {
// $each: [{ outputConnections: tankname, output_type: 'overhead'}],
// },
},
},
{ new: true }
);
console.log("tankConnections", tankConnections.connections.inputConnections);
console.log("tankConnections", tankConnections.connections.outputConnections);
}
}
// Handle output connections
if (req.body.outputConnections) {
tankInfo.connections.outputConnections = req.body.outputConnections.map(connection => ({
outputConnections: connection.outputConnections,
output_type: connection.output_type,
motor_id: connection.motor_id || null,
motor_status: connection.motor_status || "0",
motor_stop_status: connection.motor_stop_status || "1",
outputismotor: connection.hasOwnProperty("outputismotor") ? connection.outputismotor : false,
capacity: connection.capacity || null,
water_level: connection.water_level || null
}));
}
return tank_connections;
// Save the updated tank connections
const updatedTankConnections = await tankInfo.save();
console.log("Updated Tank Connections:", updatedTankConnections.connections);
// Send the updated tank connections as the response
return reply.send({ status_code: 200, data: updatedTankConnections });
} catch (err) {
throw boom.boomify(err);
}
@ -525,6 +507,7 @@ exports.createConnections = async (req, body) => {
// exports.createConnections = async (req, body) => {
// try {
// const customerId = req.params.customerId;

@ -1180,12 +1180,10 @@ exports.IotDevice = async (req, reply) => {
try {
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 date = currentDate.toISOString();
const time = currentDate.toLocaleTimeString('en-IN', { hour12: false, timeZone: 'Asia/Kolkata' });
// Create an array of tank documents
const tankDocuments = tanks.map(tank => ({
tankhardwareId: tank.tankhardwareId,
tankHeight: tank.tankHeight,
@ -1195,13 +1193,9 @@ exports.IotDevice = async (req, reply) => {
time: time
}));
// create a new IotData document with the provided data
const ottank = new IotData({ hardwareId, mode, tanks: tankDocuments, date, time });
// save the document to MongoDB
await ottank.save();
// Delete excess records (keep only the latest three records)
const recordsToKeep = 3;
const recordsToDelete = await IotData.find({ hardwareId })
.sort({ date: -1, time: -1 })
@ -1211,70 +1205,44 @@ exports.IotDevice = async (req, reply) => {
await record.remove();
}
// Update waterlevel in tanksSchema for each tank
for (const tank of tanks) {
const { tankhardwareId, tankHeight } = tank;
// Find the corresponding tank in tanksSchema
const existingTank = await Tank.findOne({ hardwareId, tankhardwareId });
if (existingTank) {
// Update the waterlevel using the tankHeight value
const tank_height1 = (parseInt(existingTank.height.replace(/,/g, ''), 10)) * 30.48;
console.log(tank_height1, 25);
// The value of tank_height1 is a number, not a string, so you cannot use replace on it.
// If you want to format it with commas, you can create a function to add commas to a number.
function numberWithCommas(x) {
return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
}
// Now you can use the function to format the tank_height1 value with commas.
const formatted_tank_height1 = numberWithCommas(tank_height1);
console.log(formatted_tank_height1, 25);
const tank_height = parseInt(formatted_tank_height1.replace(/,/g, ''), 10);
console.log(tank_height);
// console.log(tank_height,1)
const water_level_height = tank_height - tankHeight
console.log(water_level_height,2)
const waterCapacityPerCm = parseInt(existingTank.waterCapacityPerCm.replace(/,/g, ''), 10)
console.log(waterCapacityPerCm,3)
const water_level = water_level_height * waterCapacityPerCm;
console.log(water_level, 4);
// Function to add commas to a number
function numberWithCommas(x) {
return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
}
if (!existingTank) continue;
const formatted_water_level = numberWithCommas(water_level);
console.log(formatted_water_level, 4);
const waterCapacityPerCm = parseInt(existingTank.waterCapacityPerCm.replace(/,/g, ''), 10);
const water_level = (parseInt(existingTank.height.replace(/,/g, ''), 10) * 30.48) - tankHeight * waterCapacityPerCm;
existingTank.waterlevel = water_level;
existingTank.waterlevel = parseInt(formatted_water_level.replace(/,/g, ''), 10);
console.log(existingTank.waterlevel);
await existingTank.save();
// Now, find and update the water level of tanks connected via outputConnections
if (existingTank.connections && existingTank.connections.outputConnections) {
for (const output of existingTank.connections.outputConnections) {
const connectedTank = await Tank.findOne({ tankName: output.outputConnections, customerId: output.customerId });
if (connectedTank) {
// Check for specific input connection for "Tank A" and update it
const inputConnectionIndex = connectedTank.connections.inputConnections.findIndex(input => input.inputConnections === existingTank.tankName);
// Save the updated tank document
await existingTank.save();
if (inputConnectionIndex > -1) {
connectedTank.connections.inputConnections[inputConnectionIndex].water_level = water_level.toString();
await connectedTank.save();
}
}
}
}
}
// Send the latest three documents
const latestOttanks = await IotData.find({ hardwareId })
.sort({ date: -1, time: -1 });
reply.code(200).send({ latestOttanks });
} catch (err) {
// send an error response
reply.code(500).send({ error: err.message });
}
};
// exports.getIotD = async(req, reply) => {
// try {
// await IotData.find({hardwareId: req.query.hardwareId})

@ -54,7 +54,10 @@ const tanksSchema = new mongoose.Schema({
total_water_added_from_midnight:{ type: String,default:"0" },
auto_min_percentage :{ type: String, default: "20" },
auto_max_percentage :{ type: String, default: "80" },
manual_threesold_percentage:{type: String, default: "90"},
manual_threshold_percentage:{type: String, default: "90"},
manual_threshold_time:{type: String, default: null},
threshold_type:{type: String, default: "percentage"},
connections: {
source: { type: String },
@ -68,6 +71,8 @@ const tanksSchema = new mongoose.Schema({
auto_mode:{type:String,default:"inactive"},
motor_stop_status: { type: String, default: "1" },
motor_on_type :{ type: String, default: "manual" },
capacity:{ type: String ,default: null},
water_level:{ type: String ,default: null},
}
],

@ -127,6 +127,8 @@ module.exports = function (fastify, opts, next) {
motor_id:{ type: "string" ,default: null},
motor_status: { type: "string", default: "1" },
motor_stop_status: { type: "string", default: "1" },
capacity:{ type: "string" ,default: null},
water_level:{ type: "string" ,default: null},
},
},
},
@ -142,6 +144,9 @@ module.exports = function (fastify, opts, next) {
motor_id:{ type: "string" ,default: null},
motor_status: { type: "string", default: "1" },
motor_stop_status: { type: "string", default: "1" },
capacity:{ type: "string" ,default: null},
water_level:{ type: "string" ,default: null},
},
},
},

@ -29,6 +29,7 @@ module.exports = function (fastify, opts, next) {
tankName: { type: "string" },
blockName: { type: "string"},
capacity: { type: "string" },
typeOfWater: { type: "string" },
waterCapacityPerCm:{ type: "string" },
tankLocation: { type: "string" },
@ -285,7 +286,9 @@ module.exports = function (fastify, opts, next) {
action: { type: "string" },
percentage: { type: "string",default: "100" },
startTime:{ type: "string" },
threesold_type:{type:"string"},
manual_threshold_percentage:{type:"string"},
manual_threshold_time:{type:"string"},
stop_at:{type:"number"}
},

Loading…
Cancel
Save