From e38c93664d72a210b4e90fbcc899edba5869e56e Mon Sep 17 00:00:00 2001 From: Varun Date: Tue, 1 Apr 2025 11:23:05 +0530 Subject: [PATCH] changes for start and stop test in install --- src/controllers/tanksController.js | 44 ++++++++++++++++++++++++++++++ src/routes/tanksRoute.js | 43 +++++++++++++++++++++++++++++ 2 files changed, 87 insertions(+) diff --git a/src/controllers/tanksController.js b/src/controllers/tanksController.js index b0b0fd2c..bdddffb8 100644 --- a/src/controllers/tanksController.js +++ b/src/controllers/tanksController.js @@ -2768,6 +2768,50 @@ exports.getPumpsAndUsers = async (req, reply) => { }; +exports.motoractiontest = async (req, reply) => { + try { + const { customerId } = req.params; + const { motor_id, action } = req.body; + + // Assuming you have a MongoDB model or connection + const customer = await db.collection('customers').findOne({ _id: customerId }); + + if (!customer) { + return reply.status(404).send({ success: false, message: "Customer not found" }); + } + + let motorFound = false; + + // Traverse through tanks and their input connections + for (const tank of customer.tanks || []) { + for (const inputConnection of tank.inputconnections || []) { + if (inputConnection.motor_id === motor_id) { + motorFound = true; + + if (action === 'start') { + await this.publishMotorStopStatus(motor_id, "2"); + } else if (action === 'stop') { + await this.publishMotorStopStatus(motor_id, "1"); + } else { + return reply.status(400).send({ success: false, message: "Invalid action" }); + } + + return reply.send({ success: true, message: `Motor ${action} command sent.` }); + } + } + } + + if (!motorFound) { + return reply.status(404).send({ success: false, message: "Motor ID not found" }); + } + + } catch (error) { + console.error("Error fetching data:", error); + return reply.status(500).send({ success: false, message: "Internal Server Error" }); + } +}; + + const motorIntervals = {}; async function calculateTotalPumpedWater(customerId, motorId, start_instance_id) { diff --git a/src/routes/tanksRoute.js b/src/routes/tanksRoute.js index 083c4f6f..3f098f0b 100644 --- a/src/routes/tanksRoute.js +++ b/src/routes/tanksRoute.js @@ -372,6 +372,49 @@ module.exports = function (fastify, opts, next) { }); + fastify.route({ + method: "PUT", + url: "/api/motorActiontest/:customerId", + schema: { + tags: ["Install"], + summary: "This is for start and stop test", + params: { + required: ["customerId"], + type: "object", + properties: { + customerId: { + type: "string", + description: "customerId", + }, + }, + }, + + + body: { + type: "object", + // required: ['phone'], + properties: { + + motor_id:{type:"string"}, + action:{type:"string"}, + + }, + }, + security: [ + { + basicAuth: [], + }, + ], + }, + // preHandler: [ + // fastify.auth([fastify.operatorAuthenticate]), + // validationHandler.validatePhoneFormat, + // ], + //preHandler: fastify.auth([fastify.authenticate]), + handler: tanksController.motoractiontest, + }); + + // fastify.route({ // method: "PUT", // url: "/api/consumption/:customerId",