diff --git a/src/controllers/tanksController.js b/src/controllers/tanksController.js index fec23961..c62ddef0 100644 --- a/src/controllers/tanksController.js +++ b/src/controllers/tanksController.js @@ -1023,6 +1023,114 @@ exports.motorAction = async (req, reply) => { +const motorActionAuto = async (req, reply) => { + try { + const customerId = req.params.customerId; + const action = req.body.action; + const motorId = req.body.motor_id; + const motor_on_type = req.body.motor_on_type + + if (!motorId) { + throw new Error("Motor ID is required."); + } + + let motorStopStatus; + + if (action === "start") { + await Tank.updateOne( + { customerId, "connections.inputConnections.motor_id": motorId }, + { + $set: { + "connections.inputConnections.$.motor_stop_status": "2", + "connections.inputConnections.$.startTime": req.body.startTime, + "connections.inputConnections.$.motor)on_type": "auto", + + } + } + ); + + ; + } + + if (action === "stop") { + await Tank.updateOne( + { customerId, "connections.inputConnections.motor_id": motorId }, + { + $set: { + "connections.inputConnections.$.motor_stop_status": "1", + "connections.inputConnections.$.stopTime": req.body.stopTime, + "connections.inputConnections.$.motor_on_type": null, + } + } + ); + } + + + + reply.code(200).send({ message: `Motor ${action === "start" ? "started" : "stopped"} successfully.` }); + } catch (err) { + console.error("Error in motorActionAuto:", err); + reply.code(500).send({ error: err.message }); + } +}; + + + +const checkAutoMode = async () => { + try { + const tanks = await Tank.find(); + + for (const tank of tanks) { + if (tank.auto_mode === "active") { + const waterLevel = parseInt(tank.waterlevel, 10); + const capacity = parseInt(tank.capacity, 10); + const autoMinPercentage = parseInt(tank.auto_min_percentage, 10); + const autoMaxPercentage = parseInt(tank.auto_max_percentage, 10); + const currentPercentage = (waterLevel / capacity) * 100; + + const now = moment().format('DD-MMM-YYYY-HH-mm'); + + if (capacity > 0) { + if (currentPercentage <= autoMinPercentage) { + await motorActionAuto({ + params: { customerId: tank.customerId }, + body: { + action: "start", + motor_id: tank.connections.inputConnections[0].motor_id, + motor_on_type: "auto", + startTime: now + } + }, { + code: (statusCode) => ({ send: (response) => console.log(response) }) + }); + } else if (currentPercentage >= autoMaxPercentage && tank.connections.inputConnections[0].motor_on_type === "auto") { + await motorActionAuto({ + params: { customerId: tank.customerId }, + body: { + action: "stop", + motor_id: tank.connections.inputConnections[0].motor_id, + motor_on_type: "auto", + stopTime: now + } + }, { + code: (statusCode) => ({ send: (response) => console.log(response) }) + }); + } + } + } + } + } catch (err) { + console.error("Error checking auto mode:", err); + } +}; + +// Set the interval to check every 15 seconds (15000 milliseconds) +setInterval(checkAutoMode, 15000); + + + + + // exports.calculateCapacity = async (req, reply) => { // try { diff --git a/src/routes/tanksRoute.js b/src/routes/tanksRoute.js index 1edcbcb3..6ac3d2bd 100644 --- a/src/routes/tanksRoute.js +++ b/src/routes/tanksRoute.js @@ -283,8 +283,7 @@ module.exports = function (fastify, opts, next) { from: { type: "string", default: null }, from_type: { type: "string" }, to_type: { type: "string" }, - action: { type: "string" }, - + action: { type: "string" }, startTime:{ type: "string" }, threshold_type:{type:"string"}, manual_threshold_litres:{type:"string"},