From 8b981fee1c12031e44a9f85293245285c2144a1a Mon Sep 17 00:00:00 2001 From: Varun Date: Mon, 25 Nov 2024 11:10:47 +0530 Subject: [PATCH 1/2] changes in tanks edit --- src/routes/tanksRoute.js | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/src/routes/tanksRoute.js b/src/routes/tanksRoute.js index 4b312761..1521856c 100644 --- a/src/routes/tanksRoute.js +++ b/src/routes/tanksRoute.js @@ -76,12 +76,16 @@ module.exports = function (fastify, opts, next) { // required: ['phone'], properties: { - - blockName: { type: "string", default: null }, - capacity: { type: "string" }, - typeOfWater: { type: "string", default: null }, - type: { type: "string" }, - tankLocation: { type: "string", default: null }, + tankhardwareId: { type: "string" }, + hardwareId: { type: "string" }, + tankName: { type: "string" }, + blockName: { type: "string"}, + capacity: { type: "string" }, + // customerId: { type: "string" }, + typeOfWater: { type: "string" }, + waterCapacityPerCm:{ type: "string" }, + tankLocation: { type: "string" }, + height:{ type: "string" }, }, }, security: [ From 4aca575e8697724babac014ad3f6edf96733956d Mon Sep 17 00:00:00 2001 From: Varun Date: Tue, 26 Nov 2024 11:48:08 +0530 Subject: [PATCH 2/2] changes for unit testing in iot --- src/controllers/tanksController.js | 46 ++++++++++++++++++++++++++---- 1 file changed, 40 insertions(+), 6 deletions(-) diff --git a/src/controllers/tanksController.js b/src/controllers/tanksController.js index 40152312..ff7e52fd 100644 --- a/src/controllers/tanksController.js +++ b/src/controllers/tanksController.js @@ -2002,18 +2002,20 @@ else if (currentWaterLevel >= highWaterThreshold) { reply.code(200).send({ message: "Motor started successfully." }); } else if (action === "stop") { motorStopStatus = "1"; // If action is stop, set stop status to "1" - eventEmitter.emit('motorStop', fcmToken, motorId, currentWaterLevel,blockName, tankName,stopTime,motorOnType); + // eventEmitter.emit('motorStop', fcmToken, motorId, currentWaterLevel,blockName, tankName,stopTime,motorOnType); } else { throw new Error("Invalid action provided."); } // If action is stop, immediately update motor status and perform stop operations if (action === "stop") { + console.log("enterted stop") await Tank.updateOne( { customerId, "connections.inputConnections.motor_id": motorId }, { $set: { "connections.inputConnections.$.motor_stop_status": "1", + "connections.inputConnections.$.motor_on_type": "manual", "connections.inputConnections.$.stopTime": req.body.stopTime, "connections.inputConnections.$.threshold_type": null, "connections.inputConnections.$.manual_threshold_time": null, @@ -2112,6 +2114,7 @@ else if (currentWaterLevel >= highWaterThreshold) { { $set: { "connections.inputConnections.$.motor_stop_status": "1", + "connections.inputConnections.$.threshold_type": null, "connections.inputConnections.$.manual_threshold_time": null, "connections.inputConnections.$.manual_threshold_percentage": null @@ -3358,6 +3361,7 @@ exports.IotDevice = async (req, reply) => { exports.IotDeviceforstandalonedevice = async (req, reply) => { try { + console.log("entered post for iotstandalone") const { hardwareId, Motor_status, tanks } = req.body; // create a new tank document with the current date and time @@ -3447,10 +3451,33 @@ exports.IotDeviceforstandalonedevice = async (req, reply) => { // Find the inputConnection with the specified motor_id const inputConnection = tank.connections.inputConnections.find(conn => conn.motor_id === hardwareId); - + +if (inputConnection) { // Update the motor_status of the inputConnection inputConnection.motor_status = status; - + + // Check if motor_stop_status is "1" and status is "2" + if (inputConnection.motor_stop_status === "1" && status === "2") { + // Check if motor_on_type is not already "forced_manual" + if (inputConnection.motor_on_type !== "forced_manual") { + inputConnection.motor_on_type = "forced_manual"; + + // Update startTime to the current time in the specified format + const currentTime = new Date(); + const formattedTime = currentTime.toLocaleString('en-GB', { + day: '2-digit', + month: 'short', + year: 'numeric', + hour: '2-digit', + minute: '2-digit', + hour12: false, + }).replace(',', ''); + inputConnection.startTime = formattedTime; + } + } + +} + // Save the updated tank await tank.save(); @@ -4173,7 +4200,7 @@ exports.motorstatus = async (req, reply) => { exports.readMotorStatus = async (req, reply) => { try { const motor_id = req.query.motor_id; - console.log(motor_id) + console.log("entered read api for iot") // Perform any necessary logic based on action (1: Start, 2: Stop) @@ -4187,9 +4214,16 @@ exports.readMotorStatus = async (req, reply) => { const inputConnections = tank.connections.inputConnections; const motorConnection = inputConnections.find(conn => conn.motor_id === motor_id); if (motorConnection) { - motor_stop_status = motorConnection.motor_stop_status; + // Check if motor_on_type is "forced_manual" and motor_stop_status is "1" + if (motorConnection.motor_on_type === "forced_manual" && motorConnection.motor_stop_status === "1") { + motor_stop_status = "2"; // Send motor_stop_status as "2" + } else { + motor_stop_status = motorConnection.motor_stop_status; // Otherwise, assign its original value + } + break; - } + } + } if (!motor_stop_status) {