diff --git a/src/controllers/tanksController.js b/src/controllers/tanksController.js index f683b42c..c9462ff8 100644 --- a/src/controllers/tanksController.js +++ b/src/controllers/tanksController.js @@ -854,6 +854,86 @@ exports.consumption = async (req, reply) => { } }; + + +//const moment = require('moment'); // Import moment.js for date/time operations + +exports.motoraction1 = async (req, reply) => { + try { + const customerId = req.params.customerId; + const action = req.body.action; + const motorId = req.body.motor_id; + + // Ensure motor_id is provided + if (!motorId) { + throw new Error("Motor ID is required."); + } + + // Determine the motor stop status based on the action + let motorStopStatus; + if (action === "start") { + motorStopStatus = "2"; // If action is start, set stop status to "2" + } else if (action === "stop") { + motorStopStatus = "1"; // If action is stop, set stop status to "1" + } else { + throw new Error("Invalid action provided."); + } + + // Update the motor stop status + await Tank.updateOne( + { customerId, "connections.inputConnections.motor_id": motorId }, + { $set: { "connections.inputConnections.$.motor_stop_status": motorStopStatus } } + ); + + // Check threshold settings + if (req.body.threshold_type === "time") { + // If threshold type is time, update threshold time + await Tank.updateOne( + { customerId, "connections.inputConnections.motor_id": motorId }, + { $set: { "connections.inputConnections.$.manual_threshold_time": req.body.manual_threshold_time } } + ); + } else if (req.body.threshold_type === "percentage") { + // 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; + + // Update water level threshold + await Tank.updateOne( + { customerId, "connections.inputConnections.motor_id": motorId }, + { $set: { "connections.inputConnections.$.water_level_threshold": threshold_water_level.toString() } } + ); + + // Start monitoring water level if action is start + if (action === "start") { + const intervalId = setInterval(async () => { + const receiver_tank_info = await Tank.findOne({ customerId, tankName: req.body.to, tankLocation: req.body.to_type.toLowerCase() }); + const current_water_level = parseInt(receiver_tank_info.water_level, 10); + if (current_water_level >= threshold_water_level) { + // Water level reached threshold, stop the motor + await Tank.updateOne( + { customerId, "connections.inputConnections.motor_id": motorId }, + { $set: { "connections.inputConnections.$.motor_stop_status": "1" } } + ); + clearInterval(intervalId); // Stop monitoring water level + } + }, 60000); // Check water level every minute + } + } + + // Respond with success message + reply.code(200).send({ message: `Motor ${action === "start" ? "started" : "stopped"} successfully.` }); + } catch (err) { + // Handle errors + throw boom.boomify(err); + } +}; + + // exports.calculateCapacity = async (req, reply) => { // try { // const shape = req.body.shape diff --git a/src/routes/tanksRoute.js b/src/routes/tanksRoute.js index 7f350d6d..0d18fcf1 100644 --- a/src/routes/tanksRoute.js +++ b/src/routes/tanksRoute.js @@ -289,8 +289,8 @@ module.exports = function (fastify, opts, next) { threesold_type:{type:"string"}, manual_threshold_percentage:{type:"string"}, manual_threshold_time:{type:"string"}, - stop_at:{type:"number"} - + stop_at:{type:"number"}, + motor_id:{type:"string"}, }, }, security: [