From dd0a632331be0a59660468a27ff29d436d7842ce Mon Sep 17 00:00:00 2001 From: varun Date: Mon, 20 May 2024 03:42:14 -0400 Subject: [PATCH] change in iotdata if tank not found for tankhardwareId4 --- src/controllers/tanksController.js | 73 ++++++++++++++++++++++++++---- src/models/tanks.js | 3 +- src/routes/tanksRoute.js | 4 +- 3 files changed, 67 insertions(+), 13 deletions(-) diff --git a/src/controllers/tanksController.js b/src/controllers/tanksController.js index fec23961..7ec15385 100644 --- a/src/controllers/tanksController.js +++ b/src/controllers/tanksController.js @@ -871,6 +871,7 @@ exports.motorAction = async (req, reply) => { // 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") { @@ -900,15 +901,27 @@ exports.motorAction = async (req, reply) => { } else { + // Update the motor stop status to "2" for start action await Tank.updateOne( { customerId, "connections.inputConnections.motor_id": motorId }, - { $set: { "connections.inputConnections.$.motor_stop_status": "2" } } + { $set: { "connections.inputConnections.$.motor_stop_status": "2","connections.inputConnections.$.motor_on_type": req.body.on_type } } + ); + } + + if (action === "start"&&req.body.on_type==="auto") { + await Tank.updateOne( + { customerId, "connections.inputConnections.motor_id": motorId }, + { $set: { "connections.inputConnections.$.motor_stop_status": "2","connections.inputConnections.$.motor_on_type": req.body.on_type } } ); } + + + + // Check threshold settings if action is start - if (action === "start") { + if (action === "start"&&req.body.on_type==="manual") { if (req.body.threshold_type === "time") { // If threshold type is time, update threshold time // await Tank.updateOne( @@ -922,7 +935,7 @@ exports.motorAction = async (req, reply) => { if (index !== -1) { await Tank.updateOne( { customerId, "connections.inputConnections.motor_id": motorId }, - { $set: { [`connections.inputConnections.${index}.manual_threshold_time`]: req.body.manual_threshold_time, [`connections.inputConnections.${index}.startTime`]: req.body.startTime } } + { $set: { [`connections.inputConnections.${index}.manual_threshold_time`]: req.body.manual_threshold_time, [`connections.inputConnections.${index}.startTime`]: req.body.startTime,[`connections.inputConnections.${index}.motor_on_type`]: "manual" } } ); } } @@ -938,9 +951,9 @@ exports.motorAction = async (req, reply) => { { customerId, "connections.inputConnections.motor_id": motorId }, { $set: { - "connections.inputConnections.$.motor_stop_status": "1", - + "connections.inputConnections.$.motor_stop_status": "1", "connections.inputConnections.$.threshold_type": null, + "connections.inputConnections.$.motor_on_type": null, "connections.inputConnections.$.manual_threshold_time": null, "connections.inputConnections.$.manual_threshold_percentage": null } @@ -963,9 +976,6 @@ exports.motorAction = async (req, reply) => { const supplier_waterLevel = parseInt(supplier_tank_info.waterlevel, 10); - - - const capacity = parseInt(receiver_tank_info.capacity, 10); const waterLevel = parseInt(receiver_tank_info.waterlevel, 10); const desired_percentage = parseInt(req.body.manual_threshold_litres, 10); @@ -978,7 +988,7 @@ exports.motorAction = async (req, reply) => { if (index !== -1) { await Tank.updateOne( { customerId, "connections.inputConnections.motor_id": motorId }, - { $set: { [`connections.inputConnections.${index}.manual_threshold_percentage`]: supplier_threshold.toString(), [`connections.inputConnections.${index}.startTime`]: req.body.startTime } } + { $set: { [`connections.inputConnections.${index}.manual_threshold_percentage`]: supplier_threshold.toString(), [`connections.inputConnections.${index}.startTime`]: req.body.startTime,[`connections.inputConnections.${index}.motor_on_type`]: "manual" } } ); } } @@ -1000,7 +1010,7 @@ exports.motorAction = async (req, reply) => { { $set: { "connections.inputConnections.$.motor_stop_status": "1", - + "connections.inputConnections.$.motor_on_type": null, "connections.inputConnections.$.threshold_type": null, "connections.inputConnections.$.manual_threshold_time": null, "connections.inputConnections.$.manual_threshold_percentage": null @@ -1021,6 +1031,49 @@ exports.motorAction = async (req, reply) => { } }; +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); + + if (capacity > 0) { + const currentPercentage = (waterLevel / capacity) * 100; + + if (currentPercentage <= autoMinPercentage) { + // Call motorAction function + await motorAction({ + params: { customerId: tank.customerId }, + body: { + action: "start", + motor_id: tank.connections.inputConnections[0].motor_id, + to:tank.tankName, + from:tank.connections.inputConnections[0].inputConnections, + from_type:connections.inputConnections[0].input_type, + to_type:tank.tankLocation, + // startTime:, + on_type:"auto" + + + } + }, { + 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); diff --git a/src/models/tanks.js b/src/models/tanks.js index 53a6063f..91c0a671 100644 --- a/src/models/tanks.js +++ b/src/models/tanks.js @@ -68,7 +68,7 @@ const tanksSchema = new mongoose.Schema({ motor_status: { type: String, default: "0" }, auto_mode:{type:String,default:"inactive"}, motor_stop_status: { type: String, default: "1" }, - motor_on_type :{ type: String, default: "manual" }, + motor_on_type :{ type: String, default: null }, capacity:{ type: String ,default: null}, water_level:{ type: String ,default: null}, manual_threshold_percentage:{type: String, default: "90"}, @@ -92,6 +92,7 @@ const tanksSchema = new mongoose.Schema({ 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 1edcbcb3..7a646515 100644 --- a/src/routes/tanksRoute.js +++ b/src/routes/tanksRoute.js @@ -283,14 +283,14 @@ 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"}, manual_threshold_time:{type:"string"}, stopTime:{type:"string"}, motor_id:{type:"string"}, + on_type:{type:"string"} }, }, security: [