|
|
@ -883,7 +883,7 @@ exports.motorAction = async (req, reply) => {
|
|
|
|
if (action === "stop") {
|
|
|
|
if (action === "stop") {
|
|
|
|
await Tank.updateOne(
|
|
|
|
await Tank.updateOne(
|
|
|
|
{ customerId, "connections.inputConnections.motor_id": motorId },
|
|
|
|
{ customerId, "connections.inputConnections.motor_id": motorId },
|
|
|
|
{ $set: { "connections.inputConnections.$.motor_stop_status": "1" } }
|
|
|
|
{ $set: { "connections.inputConnections.$.motor_stop_status": "1",real_motor_status:"4" } }
|
|
|
|
);
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
// Update the motor stop status to "2" for start action
|
|
|
|
// Update the motor stop status to "2" for start action
|
|
|
@ -899,7 +899,7 @@ exports.motorAction = async (req, reply) => {
|
|
|
|
// If threshold type is time, update threshold time
|
|
|
|
// If threshold type is time, update threshold time
|
|
|
|
await Tank.updateOne(
|
|
|
|
await Tank.updateOne(
|
|
|
|
{ customerId, "connections.inputConnections.motor_id": motorId },
|
|
|
|
{ customerId, "connections.inputConnections.motor_id": motorId },
|
|
|
|
{ $set: { "connections.inputConnections.$.manual_threshold_time": req.body.manual_threshold_time,startTime:req.body.startTime } }
|
|
|
|
{ $set: { "connections.inputConnections.$.manual_threshold_time": req.body.manual_threshold_time,startTime:req.body.startTime ,real_motor_status:"3"} }
|
|
|
|
);
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
// Start monitoring water level based on threshold time
|
|
|
|
// Start monitoring water level based on threshold time
|
|
|
@ -910,7 +910,7 @@ exports.motorAction = async (req, reply) => {
|
|
|
|
// Stop the motor pump
|
|
|
|
// Stop the motor pump
|
|
|
|
await Tank.updateOne(
|
|
|
|
await Tank.updateOne(
|
|
|
|
{ customerId, "connections.inputConnections.motor_id": motorId },
|
|
|
|
{ customerId, "connections.inputConnections.motor_id": motorId },
|
|
|
|
{ $set: { "connections.inputConnections.$.motor_stop_status": "1",manual_threshold_time:null } }
|
|
|
|
{ $set: { "connections.inputConnections.$.motor_stop_status": "1",manual_threshold_time:null,real_motor_status:"4" } }
|
|
|
|
);
|
|
|
|
);
|
|
|
|
clearInterval(intervalId); // Stop monitoring water level
|
|
|
|
clearInterval(intervalId); // Stop monitoring water level
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -940,7 +940,7 @@ exports.motorAction = async (req, reply) => {
|
|
|
|
// Stop the motor pump
|
|
|
|
// Stop the motor pump
|
|
|
|
await Tank.updateOne(
|
|
|
|
await Tank.updateOne(
|
|
|
|
{ customerId, "connections.inputConnections.motor_id": motorId },
|
|
|
|
{ customerId, "connections.inputConnections.motor_id": motorId },
|
|
|
|
{ $set: { "connections.inputConnections.$.motor_stop_status": "1",manual_threshold_percentage:null } }
|
|
|
|
{ $set: { "connections.inputConnections.$.motor_stop_status": "1",manual_threshold_percentage:null,real_motor_status:"4" } }
|
|
|
|
);
|
|
|
|
);
|
|
|
|
clearInterval(intervalId); // Stop monitoring water level
|
|
|
|
clearInterval(intervalId); // Stop monitoring water level
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -1203,156 +1203,171 @@ exports.calculateCapacity = async (req, reply) => {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// exports.IotDevice = async (req, reply) => {
|
|
|
|
exports.IotDevice = async (req, reply) => {
|
|
|
|
// try {
|
|
|
|
try {
|
|
|
|
// const { hardwareId, mode, tanks } = 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' });
|
|
|
|
|
|
|
|
|
|
|
|
// // Create an array of tank documents
|
|
|
|
// Create an array of tank documents
|
|
|
|
// const tankDocuments = tanks.map(tank => ({
|
|
|
|
const tankDocuments = tanks.map(tank => ({
|
|
|
|
// tankhardwareId: tank.tankhardwareId,
|
|
|
|
tankhardwareId: tank.tankhardwareId,
|
|
|
|
// tankHeight: tank.tankHeight,
|
|
|
|
tankHeight: tank.tankHeight,
|
|
|
|
// maxLevel: tank.maxLevel,
|
|
|
|
maxLevel: tank.maxLevel,
|
|
|
|
// minLevel: tank.minLevel,
|
|
|
|
minLevel: tank.minLevel,
|
|
|
|
// date: date,
|
|
|
|
date: date,
|
|
|
|
// time: time
|
|
|
|
time: time
|
|
|
|
// }));
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// // create a new IotData document with the provided data
|
|
|
|
// create a new IotData document with the provided data
|
|
|
|
// const ottank = new IotData({ hardwareId, mode, tanks: tankDocuments, date, time });
|
|
|
|
const ottank = new IotData({ hardwareId, mode, tanks: tankDocuments, date, time });
|
|
|
|
|
|
|
|
|
|
|
|
// // save the document to MongoDB
|
|
|
|
// save the document to MongoDB
|
|
|
|
// await ottank.save();
|
|
|
|
await ottank.save();
|
|
|
|
|
|
|
|
|
|
|
|
// // delete previous records except the three latest ones
|
|
|
|
// delete previous records except the three latest ones
|
|
|
|
// const previousRecords = await IotData.find({ hardwareId })
|
|
|
|
const previousRecords = await IotData.find({ hardwareId })
|
|
|
|
// .sort({ date: -1, time: -1 })
|
|
|
|
.sort({ date: -1, time: -1 })
|
|
|
|
// .skip(3); // skip the three latest documents
|
|
|
|
.skip(3); // skip the three latest documents
|
|
|
|
|
|
|
|
|
|
|
|
// for (const record of previousRecords) {
|
|
|
|
for (const record of previousRecords) {
|
|
|
|
// await record.remove();
|
|
|
|
await record.remove();
|
|
|
|
// }
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// // get the latest three documents sorted in descending order of date and time
|
|
|
|
// get the latest three documents sorted in descending order of date and time
|
|
|
|
// const latestOttanks = await IotData.find({ hardwareId })
|
|
|
|
const latestOttanks = await IotData.find({ hardwareId })
|
|
|
|
// .sort({ date: -1, time: -1 })
|
|
|
|
.sort({ date: -1, time: -1 })
|
|
|
|
// .limit(3);
|
|
|
|
.limit(3);
|
|
|
|
|
|
|
|
|
|
|
|
// // send the latest documents
|
|
|
|
// send the latest documents
|
|
|
|
// reply.code(200).send({ latestOttanks });
|
|
|
|
reply.code(200).send({ latestOttanks });
|
|
|
|
// } 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 });
|
|
|
|
// }
|
|
|
|
}
|
|
|
|
// };
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// exports.IotDevice = async (req, reply) => {
|
|
|
|
exports.IotDevice = async (req, reply) => {
|
|
|
|
// try {
|
|
|
|
try {
|
|
|
|
// const { hardwareId, mode, tanks } = 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' });
|
|
|
|
|
|
|
|
|
|
|
|
// // Create an array of tank documents
|
|
|
|
// Create an array of tank documents
|
|
|
|
// const tankDocuments = tanks.map(tank => ({
|
|
|
|
const tankDocuments = tanks.map(tank => ({
|
|
|
|
// tankhardwareId: tank.tankhardwareId,
|
|
|
|
tankhardwareId: tank.tankhardwareId,
|
|
|
|
// tankHeight: tank.tankHeight,
|
|
|
|
tankHeight: tank.tankHeight,
|
|
|
|
// maxLevel: tank.maxLevel,
|
|
|
|
maxLevel: tank.maxLevel,
|
|
|
|
// minLevel: tank.minLevel,
|
|
|
|
minLevel: tank.minLevel,
|
|
|
|
// date: date,
|
|
|
|
date: date,
|
|
|
|
// time: time
|
|
|
|
time: time
|
|
|
|
// }));
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
|
|
// // create a new IotData document with the provided data
|
|
|
|
// create a new IotData document with the provided data
|
|
|
|
// const ottank = new IotData({ hardwareId, mode, tanks: tankDocuments, date, time });
|
|
|
|
const ottank = new IotData({ hardwareId, mode, tanks: tankDocuments, date, time });
|
|
|
|
|
|
|
|
|
|
|
|
// // save the document to MongoDB
|
|
|
|
// save the document to MongoDB
|
|
|
|
// await ottank.save();
|
|
|
|
await ottank.save();
|
|
|
|
|
|
|
|
|
|
|
|
// // Delete excess records (keep only the latest three records)
|
|
|
|
// Delete excess records (keep only the latest three records)
|
|
|
|
// const recordsToKeep = 3;
|
|
|
|
const recordsToKeep = 3;
|
|
|
|
// const recordsToDelete = await IotData.find({ hardwareId })
|
|
|
|
const recordsToDelete = await IotData.find({ hardwareId })
|
|
|
|
// .sort({ date: -1, time: -1 })
|
|
|
|
.sort({ date: -1, time: -1 })
|
|
|
|
// .skip(recordsToKeep);
|
|
|
|
.skip(recordsToKeep);
|
|
|
|
|
|
|
|
|
|
|
|
// for (const record of recordsToDelete) {
|
|
|
|
for (const record of recordsToDelete) {
|
|
|
|
// await record.remove();
|
|
|
|
await record.remove();
|
|
|
|
// }
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// // Update waterlevel in tanksSchema for each tank
|
|
|
|
// Update waterlevel in tanksSchema for each tank
|
|
|
|
// for (const tank of tanks) {
|
|
|
|
for (const tank of tanks) {
|
|
|
|
// const { tankhardwareId, tankHeight } = tank;
|
|
|
|
const { tankhardwareId, tankHeight } = tank;
|
|
|
|
|
|
|
|
|
|
|
|
// // Find the corresponding tank in tanksSchema
|
|
|
|
// Find the corresponding tank in tanksSchema
|
|
|
|
// const existingTank = await Tank.findOne({ hardwareId, tankhardwareId });
|
|
|
|
const existingTank = await Tank.findOne({ hardwareId, tankhardwareId });
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// if (existingTank) {
|
|
|
|
if (existingTank) {
|
|
|
|
// // Update the waterlevel using the tankHeight value
|
|
|
|
// Update the waterlevel using the tankHeight value
|
|
|
|
|
|
|
|
|
|
|
|
// const tank_height1 = (parseInt(existingTank.height.replace(/,/g, ''), 10)) * 30.48;
|
|
|
|
const tank_height1 = (parseInt(existingTank.height.replace(/,/g, ''), 10)) * 30.48;
|
|
|
|
// console.log(tank_height1, 25);
|
|
|
|
console.log(tank_height1, 25);
|
|
|
|
|
|
|
|
|
|
|
|
// // The value of tank_height1 is a number, not a string, so you cannot use replace on it.
|
|
|
|
// 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.
|
|
|
|
// If you want to format it with commas, you can create a function to add commas to a number.
|
|
|
|
// function numberWithCommas(x) {
|
|
|
|
function numberWithCommas(x) {
|
|
|
|
// return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
|
|
|
|
return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
|
|
|
|
// }
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// // Now you can use the function to format the tank_height1 value with commas.
|
|
|
|
// Now you can use the function to format the tank_height1 value with commas.
|
|
|
|
// const formatted_tank_height1 = numberWithCommas(tank_height1);
|
|
|
|
const formatted_tank_height1 = numberWithCommas(tank_height1);
|
|
|
|
// console.log(formatted_tank_height1, 25);
|
|
|
|
console.log(formatted_tank_height1, 25);
|
|
|
|
|
|
|
|
|
|
|
|
// const tank_height = parseInt(formatted_tank_height1.replace(/,/g, ''), 10);
|
|
|
|
const tank_height = parseInt(formatted_tank_height1.replace(/,/g, ''), 10);
|
|
|
|
// console.log(tank_height);
|
|
|
|
console.log(tank_height);
|
|
|
|
// // console.log(tank_height,1)
|
|
|
|
// console.log(tank_height,1)
|
|
|
|
// const water_level_height = tank_height - tankHeight
|
|
|
|
const water_level_height = tank_height - tankHeight
|
|
|
|
// console.log(water_level_height,2)
|
|
|
|
console.log(water_level_height,2)
|
|
|
|
|
|
|
|
|
|
|
|
// const waterCapacityPerCm = parseInt(existingTank.waterCapacityPerCm.replace(/,/g, ''), 10)
|
|
|
|
const waterCapacityPerCm = parseInt(existingTank.waterCapacityPerCm.replace(/,/g, ''), 10)
|
|
|
|
// console.log(waterCapacityPerCm,3)
|
|
|
|
console.log(waterCapacityPerCm,3)
|
|
|
|
// const water_level = water_level_height * waterCapacityPerCm;
|
|
|
|
const water_level = water_level_height * waterCapacityPerCm;
|
|
|
|
// console.log(water_level, 4);
|
|
|
|
console.log(water_level, 4);
|
|
|
|
|
|
|
|
|
|
|
|
// // Function to add commas to a number
|
|
|
|
// Function to add commas to a number
|
|
|
|
// function numberWithCommas(x) {
|
|
|
|
function numberWithCommas(x) {
|
|
|
|
// return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
|
|
|
|
return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
|
|
|
|
// }
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// const formatted_water_level = numberWithCommas(water_level);
|
|
|
|
const formatted_water_level = numberWithCommas(water_level);
|
|
|
|
// console.log(formatted_water_level, 4);
|
|
|
|
console.log(formatted_water_level, 4);
|
|
|
|
|
|
|
|
|
|
|
|
// existingTank.waterlevel = parseInt(formatted_water_level.replace(/,/g, ''), 10);
|
|
|
|
existingTank.waterlevel = parseInt(formatted_water_level.replace(/,/g, ''), 10);
|
|
|
|
// console.log(existingTank.waterlevel);
|
|
|
|
console.log(existingTank.waterlevel);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// // Save the updated tank document
|
|
|
|
// Save the updated tank document
|
|
|
|
// await existingTank.save();
|
|
|
|
await existingTank.save();
|
|
|
|
// }
|
|
|
|
for (const outputConnection of existingTank.connections.outputConnections) {
|
|
|
|
// }
|
|
|
|
const linkedTank = await Tank.findOne({ customerId: customerId, tankName: outputConnection.outputConnections, tankLocation: outputConnection.output_type });
|
|
|
|
|
|
|
|
if (linkedTank) {
|
|
|
|
|
|
|
|
// linkedTank.waterlevel = existingTank.waterlevel;
|
|
|
|
|
|
|
|
//await linkedTank.save();
|
|
|
|
|
|
|
|
|
|
|
|
// // Send the latest three documents
|
|
|
|
// Update water level of tanks linked through input connections of the linked tank
|
|
|
|
// const latestOttanks = await IotData.find({ hardwareId })
|
|
|
|
for (const inputConnection of linkedTank.connections.inputConnections) {
|
|
|
|
// .sort({ date: -1, time: -1 });
|
|
|
|
if (inputConnection.inputConnections === tank_name) {
|
|
|
|
|
|
|
|
inputConnection.water_level = water_level.toString();
|
|
|
|
|
|
|
|
await linkedTank.save();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// reply.code(200).send({ latestOttanks });
|
|
|
|
// Send the latest three documents
|
|
|
|
// } catch (err) {
|
|
|
|
const latestOttanks = await IotData.find({ hardwareId })
|
|
|
|
// // send an error response
|
|
|
|
.sort({ date: -1, time: -1 });
|
|
|
|
// reply.code(500).send({ error: err.message });
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
// };
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
exports.IotDevice = async (req, reply) => {
|
|
|
|
reply.code(200).send({ latestOttanks });
|
|
|
|
|
|
|
|
} catch (err) {
|
|
|
|
|
|
|
|
// send an error response
|
|
|
|
|
|
|
|
reply.code(500).send({ error: err.message });
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
exports.IotDevice1 = async (req, reply) => {
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
const { hardwareId, mode, tanks } = req.body;
|
|
|
|
const { hardwareId, mode, tanks } = req.body;
|
|
|
|
|
|
|
|
|
|
|
@ -1925,6 +1940,7 @@ exports.writeMotorStatus = async (req, reply) => {
|
|
|
|
|
|
|
|
|
|
|
|
// Update the motor_status of the inputConnection
|
|
|
|
// Update the motor_status of the inputConnection
|
|
|
|
inputConnection.motor_status = status;
|
|
|
|
inputConnection.motor_status = status;
|
|
|
|
|
|
|
|
inputConnection.real_motor_status = status;
|
|
|
|
|
|
|
|
|
|
|
|
// Save the updated tank
|
|
|
|
// Save the updated tank
|
|
|
|
await tank.save();
|
|
|
|
await tank.save();
|
|
|
|