diff --git a/src/controllers/createConnectionController.js b/src/controllers/createConnectionController.js index adbeb54b..a1a58c27 100644 --- a/src/controllers/createConnectionController.js +++ b/src/controllers/createConnectionController.js @@ -569,7 +569,7 @@ exports.createConnections = async (req, body) => { 'connections.inputConnections': { $each: [{ inputConnections: tankname, - input_type: 'sump', + input_type: 'sump', inputismotor: data.outputismotor || false, motor_id: data.motor_id || null, water_level: tankInfo.waterlevel || null, diff --git a/src/controllers/tanksController.js b/src/controllers/tanksController.js index cc396adf..ec3561b9 100644 --- a/src/controllers/tanksController.js +++ b/src/controllers/tanksController.js @@ -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({}); + + 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; + } } - - const motor_stop_status = motorInfo.motor_stop_status; - + 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) + // 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, 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' }); + 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); diff --git a/src/models/tanks.js b/src/models/tanks.js index 853e5774..86f4d718 100644 --- a/src/models/tanks.js +++ b/src/models/tanks.js @@ -54,9 +54,7 @@ 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_threshold_percentage:{type: String, default: "90"}, - manual_threshold_time:{type: String, default: null}, - threshold_type:{type: String, default: "percentage"}, + connections: { @@ -73,6 +71,9 @@ const tanksSchema = new mongoose.Schema({ motor_on_type :{ type: String, default: "manual" }, capacity:{ type: String ,default: null}, water_level:{ type: String ,default: null}, + manual_threshold_percentage:{type: String, default: "90"}, + manual_threshold_time:{type: String, default: null}, + threshold_type:{type: String, default: "percentage"}, } ], @@ -86,6 +87,9 @@ const tanksSchema = new mongoose.Schema({ motor_stop_status: { type: String, default: "1" }, capacity:{ type: String ,default: null}, water_level:{ type: String ,default: null}, + manual_threshold_percentage:{type: String, default: "90"}, + manual_threshold_time:{type: String, default: null}, + threshold_type:{type: String, default: "percentage"}, } ] diff --git a/src/routes/tanksRoute.js b/src/routes/tanksRoute.js index ef8c5190..b21a9539 100644 --- a/src/routes/tanksRoute.js +++ b/src/routes/tanksRoute.js @@ -284,10 +284,10 @@ module.exports = function (fastify, opts, next) { from_type: { type: "string" }, to_type: { type: "string" }, action: { type: "string" }, - percentage: { type: "string",default: "100" }, + startTime:{ type: "string" }, threshold_type:{type:"string"}, - manual_threshold_percentage:{type:"string"}, + manual_threshold_litres:{type:"string"}, manual_threshold_time:{type:"string"}, stop_at:{type:"number"}, motor_id:{type:"string"},