From 228dfcc014abc978977fbfa2d1e33dad7c08ab47 Mon Sep 17 00:00:00 2001 From: varun Date: Mon, 20 May 2024 05:22:34 -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, 13 insertions(+), 67 deletions(-) diff --git a/src/controllers/tanksController.js b/src/controllers/tanksController.js index 7ec15385..fec23961 100644 --- a/src/controllers/tanksController.js +++ b/src/controllers/tanksController.js @@ -871,7 +871,6 @@ 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") { @@ -901,27 +900,15 @@ 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","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 } } + { $set: { "connections.inputConnections.$.motor_stop_status": "2" } } ); } - - - - // Check threshold settings if action is start - if (action === "start"&&req.body.on_type==="manual") { + if (action === "start") { if (req.body.threshold_type === "time") { // If threshold type is time, update threshold time // await Tank.updateOne( @@ -935,7 +922,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,[`connections.inputConnections.${index}.motor_on_type`]: "manual" } } + { $set: { [`connections.inputConnections.${index}.manual_threshold_time`]: req.body.manual_threshold_time, [`connections.inputConnections.${index}.startTime`]: req.body.startTime } } ); } } @@ -951,9 +938,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 } @@ -976,6 +963,9 @@ 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); @@ -988,7 +978,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,[`connections.inputConnections.${index}.motor_on_type`]: "manual" } } + { $set: { [`connections.inputConnections.${index}.manual_threshold_percentage`]: supplier_threshold.toString(), [`connections.inputConnections.${index}.startTime`]: req.body.startTime } } ); } } @@ -1010,7 +1000,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 @@ -1031,49 +1021,6 @@ 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 91c0a671..53a6063f 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: null }, + 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"}, @@ -92,7 +92,6 @@ 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 7a646515..1edcbcb3 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: [