|
|
|
@ -915,15 +915,16 @@ exports.motorAction = async (req, reply) => {
|
|
|
|
|
clearInterval(intervalId); // Stop monitoring water level
|
|
|
|
|
}
|
|
|
|
|
}, 60000); // Check water level every minute
|
|
|
|
|
} else if (req.body.threshold_type === "percentage") {
|
|
|
|
|
} else if (req.body.threshold_type === "litres") {
|
|
|
|
|
// If threshold type is percentage, calculate percentage threshold
|
|
|
|
|
const receiver_tank_info = await Tank.findOne({ customerId, tankName: req.body.to, tankLocation: req.body.to_type.toLowerCase() });
|
|
|
|
|
if (!receiver_tank_info) {
|
|
|
|
|
throw new Error("Receiver tank not found.");
|
|
|
|
|
}
|
|
|
|
|
const capacity = parseInt(receiver_tank_info.capacity, 10);
|
|
|
|
|
const desired_percentage = parseInt(req.body.manual_threshold_percentage, 10);
|
|
|
|
|
const threshold_water_level = (capacity * desired_percentage) / 100;
|
|
|
|
|
const waterLevel = parseInt(receiver_tank_info.waterlevel, 10);
|
|
|
|
|
const desired_percentage = parseInt(req.body.manual_threshold_litres, 10);
|
|
|
|
|
const threshold_water_level = waterLevel+desired_percentage;
|
|
|
|
|
|
|
|
|
|
// Update water level threshold
|
|
|
|
|
await Tank.updateOne(
|
|
|
|
@ -1795,25 +1796,37 @@ exports.motorstatus = async (req, reply) => {
|
|
|
|
|
|
|
|
|
|
exports.readMotorStatus = async (req, reply) => {
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
|
|
const motor_id = req.query.motor_id;
|
|
|
|
|
const motor_id = req.query.motor_id;
|
|
|
|
|
console.log(motor_id)
|
|
|
|
|
|
|
|
|
|
// Perform any necessary logic based on action (1: Start, 2: Stop)
|
|
|
|
|
|
|
|
|
|
// For example, you can update a database or trigger an action
|
|
|
|
|
|
|
|
|
|
const motorInfo = await Tank.findOne({motor_id : motor_id });
|
|
|
|
|
|
|
|
|
|
if (!motorInfo) {
|
|
|
|
|
return reply.status(404).send({ status_code: 404, message: 'Tank not found for the specified motor_id' });
|
|
|
|
|
}
|
|
|
|
|
const tanks = await Tank.find({});
|
|
|
|
|
|
|
|
|
|
const motor_stop_status = motorInfo.motor_stop_status;
|
|
|
|
|
let motor_stop_status = null;
|
|
|
|
|
|
|
|
|
|
for (let tank of tanks) {
|
|
|
|
|
const inputConnections = tank.connections.inputConnections;
|
|
|
|
|
const motorConnection = inputConnections.find(conn => conn.motor_id === motor_id);
|
|
|
|
|
if (motorConnection) {
|
|
|
|
|
motor_stop_status = motorConnection.motor_stop_status;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!motor_stop_status) {
|
|
|
|
|
return reply.status(404).send({
|
|
|
|
|
status_code: 404,
|
|
|
|
|
message: 'Motor not found for the specified motor_id'
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
reply.send({ status_code: 200, motor_stop_status:motor_stop_status });
|
|
|
|
|
reply.send({
|
|
|
|
|
status_code: 200,
|
|
|
|
|
motor_stop_status: motor_stop_status
|
|
|
|
|
});
|
|
|
|
|
} catch (err) {
|
|
|
|
|
throw boom.boomify(err);
|
|
|
|
|
}
|
|
|
|
@ -1822,26 +1835,32 @@ exports.readMotorStatus = async (req, reply) => {
|
|
|
|
|
|
|
|
|
|
exports.readMotorStatusFromIot = async (req, reply) => {
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
|
|
const motor_id = req.query.motor_id;
|
|
|
|
|
const motor_id = req.query.motor_id;
|
|
|
|
|
console.log(motor_id)
|
|
|
|
|
|
|
|
|
|
// Perform any necessary logic based on action (1: Start, 2: Stop)
|
|
|
|
|
|
|
|
|
|
// For example, you can update a database or trigger an action
|
|
|
|
|
|
|
|
|
|
const motorInfo = await Tank.findOne({motor_id : motor_id });
|
|
|
|
|
// Find the tank that contains the specified motor_id in its inputConnections
|
|
|
|
|
const tank = await Tank.findOne({ "connections.inputConnections.motor_id": motor_id });
|
|
|
|
|
|
|
|
|
|
if (!motorInfo) {
|
|
|
|
|
return reply.status(404).send({ status_code: 404, message: 'Tank not found for the specified motor_id' });
|
|
|
|
|
if (!tank) {
|
|
|
|
|
return reply.status(404).send({
|
|
|
|
|
status_code: 404,
|
|
|
|
|
message: 'Motor not found for the specified motor_id'
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const motor_status = motorInfo.motor_status;
|
|
|
|
|
const motor_stop_status = motorInfo.motor_stop_status;
|
|
|
|
|
|
|
|
|
|
// Find the inputConnection with the specified motor_id
|
|
|
|
|
const inputConnection = tank.connections.inputConnections.find(conn => conn.motor_id === motor_id);
|
|
|
|
|
|
|
|
|
|
// Extract motor_status and motor_stop_status from the inputConnection
|
|
|
|
|
const motor_status = inputConnection.motor_status;
|
|
|
|
|
const motor_stop_status = inputConnection.motor_stop_status;
|
|
|
|
|
|
|
|
|
|
reply.send({ status_code: 200, motor_status:motor_status ,motor_stop_status:motor_stop_status});
|
|
|
|
|
// Send the response with motor_status and motor_stop_status
|
|
|
|
|
reply.send({
|
|
|
|
|
status_code: 200,
|
|
|
|
|
motor_status: motor_status,
|
|
|
|
|
motor_stop_status: motor_stop_status
|
|
|
|
|
});
|
|
|
|
|
} catch (err) {
|
|
|
|
|
throw boom.boomify(err);
|
|
|
|
|
}
|
|
|
|
@ -1849,6 +1868,7 @@ exports.readMotorStatusFromIot = async (req, reply) => {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// exports.writeMotorStatus = async (req, reply) => {
|
|
|
|
|
// try {
|
|
|
|
|
// const motor_id = req.body.motor_id;
|
|
|
|
@ -1885,22 +1905,29 @@ exports.writeMotorStatus = async (req, reply) => {
|
|
|
|
|
const motor_id = req.body.motor_id;
|
|
|
|
|
const status = req.body.status;
|
|
|
|
|
|
|
|
|
|
// Perform any necessary logic to handle motor status update from the device
|
|
|
|
|
// Find the tank that contains the specified motor_id in its inputConnections
|
|
|
|
|
const tank = await Tank.findOne({ "connections.inputConnections.motor_id": motor_id });
|
|
|
|
|
|
|
|
|
|
// For example, update a database with the new status, current, and temp values
|
|
|
|
|
const result = await Tank.findOneAndUpdate(
|
|
|
|
|
{ motor_id: motor_id },
|
|
|
|
|
{ $set: { motor_status: status }
|
|
|
|
|
});
|
|
|
|
|
if (!tank) {
|
|
|
|
|
return reply.status(404).send({
|
|
|
|
|
status_code: 404,
|
|
|
|
|
message: 'Motor not found for the specified motor_id'
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Fetch the motor_status for the given motor_id
|
|
|
|
|
const updatedMotor = await Tank.findOne({ motor_id: motor_id });
|
|
|
|
|
// Find the inputConnection with the specified motor_id
|
|
|
|
|
const inputConnection = tank.connections.inputConnections.find(conn => conn.motor_id === motor_id);
|
|
|
|
|
|
|
|
|
|
// Send the response with motor_stop_status and motor_status
|
|
|
|
|
// Update the motor_status of the inputConnection
|
|
|
|
|
inputConnection.motor_status = status;
|
|
|
|
|
|
|
|
|
|
// Save the updated tank
|
|
|
|
|
await tank.save();
|
|
|
|
|
|
|
|
|
|
// Send the response with the updated motor_status
|
|
|
|
|
reply.send({
|
|
|
|
|
status_code: 200,
|
|
|
|
|
motor_status: status,
|
|
|
|
|
// Assuming motor_status is a field in your Tank model
|
|
|
|
|
motor_status: status
|
|
|
|
|
});
|
|
|
|
|
} catch (err) {
|
|
|
|
|
throw boom.boomify(err);
|
|
|
|
|