From 750b66d4144dd7bf781693125c8c5e61474ebe8b Mon Sep 17 00:00:00 2001 From: varun Date: Tue, 30 Apr 2024 02:27:28 -0400 Subject: [PATCH] changing auto mode for tank --- src/controllers/tanksController.js | 27 ++++++++++++++++++ src/models/tanks.js | 3 +- src/routes/tanksRoute.js | 45 ++++++++++++++++++++++++++++++ 3 files changed, 74 insertions(+), 1 deletion(-) diff --git a/src/controllers/tanksController.js b/src/controllers/tanksController.js index f1a51a64..a77b5ac4 100644 --- a/src/controllers/tanksController.js +++ b/src/controllers/tanksController.js @@ -1625,4 +1625,31 @@ exports.motortemperature = async (req, reply) => { } }; +exports.update_auto_mode = async (req, reply) => { + try { + const customerId = req.params.customerId; + const { motor_id, auto_min_percentage, auto_max_percentage, auto_mode } = req.body; + + // Update inputConnections' auto_mode + await Tank.updateOne( + { customerId: customerId, "connections.inputConnections.motor_id": motor_id }, + { $set: { "connections.inputConnections.$.auto_mode": auto_mode } } + ); + + // Update auto_min_percentage and auto_max_percentage + await Tank.updateMany( + { customerId: customerId }, + { + $set: { + "auto_min_percentage": auto_min_percentage, + "auto_max_percentage": auto_max_percentage + } + } + ); + + reply.send({ status_code: 200, message: "Auto mode and percentages updated successfully." }); + } catch (error) { + throw boom.boomify(error); + } +}; diff --git a/src/models/tanks.js b/src/models/tanks.js index b2f2a6c8..3e86ee4b 100644 --- a/src/models/tanks.js +++ b/src/models/tanks.js @@ -36,7 +36,6 @@ const tanksSchema = new mongoose.Schema({ tankhardwareId: { type: String }, hardwareId_type: { type: String }, - motor_id:{ type: String ,default: null}, hardwareId_company: { type: String }, customerId: { type: String, default: null }, tankName: { type: String, default: null }, @@ -56,6 +55,7 @@ const tanksSchema = new mongoose.Schema({ auto_min_percentage :{ type: String, default: "20" }, auto_max_percentage :{ type: String, default: "80" }, manual_threesold_percentage:{type: String, default: "90"}, + connections: { source: { type: String }, inputConnections: [ @@ -65,6 +65,7 @@ const tanksSchema = new mongoose.Schema({ inputismotor: { type: Boolean, }, motor_id:{ type: String ,default: null}, 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" }, diff --git a/src/routes/tanksRoute.js b/src/routes/tanksRoute.js index 7b85b847..4d1aa186 100644 --- a/src/routes/tanksRoute.js +++ b/src/routes/tanksRoute.js @@ -369,6 +369,9 @@ module.exports = function (fastify, opts, next) { handler: tanksController.IotDevice }); + + + fastify.get("/api/APIRead", { @@ -730,6 +733,48 @@ module.exports = function (fastify, opts, next) { // handler: tanksController.readMotorStatus // }); + + fastify.route({ + method: "PUT", + url: "/api/update_auto_mode/:customerId", + schema: { + tags: ["Tank"], + summary: "This is for changing auto mode for motor with min and max levels", + params: { + required: ["customerId"], + type: "object", + properties: { + customerId: { + type: "string", + description: "customerId", + }, + }, + }, + + body: { + type: "object", + // required: ['phone'], + properties: { + motor_id: { type: "string", default: null }, + auto_min_percentage: { type: "string", default: null }, + auto_max_percentage: { type: "string", default: null }, + auto_mode: { type: "string", default: null }, + + }, + }, + security: [ + { + basicAuth: [], + }, + ], + }, + // preHandler: [ + // fastify.auth([fastify.operatorAuthenticate]), + // validationHandler.validatePhoneFormat, + // ], + //preHandler: fastify.auth([fastify.authenticate]), + handler: tanksController.update_auto_mode, + }); next();