From ba36f50f2e22e3ef4bdd19ed335c2cc5738d3ebb Mon Sep 17 00:00:00 2001 From: Varun Date: Wed, 12 Mar 2025 12:07:49 +0530 Subject: [PATCH 1/2] dynamically creating hardwareid and masterid --- src/controllers/storeController.js | 70 ++++++++++++++++++++++-------- src/controllers/tanksController.js | 2 +- src/routes/storeRoute.js | 21 ++++++++- 3 files changed, 72 insertions(+), 21 deletions(-) diff --git a/src/controllers/storeController.js b/src/controllers/storeController.js index 0be9b09a..680c7f1c 100644 --- a/src/controllers/storeController.js +++ b/src/controllers/storeController.js @@ -1173,12 +1173,63 @@ exports.installmotorswitch = async (request, reply) => { }; +exports.generateHardwareMasterId = async (req, reply) => { + try { + const { from, to, type } = req.body; + const sensorType = type.toLowerCase(); + const fromInt = parseInt(from, 10); + const toInt = parseInt(to, 10); + + if (isNaN(fromInt) || isNaN(toInt) || fromInt > toInt) { + return reply.code(400).send({ message: 'Invalid from/to values' }); + } + + // Fetch pending sensors of the given type + const pendingSensors = await Insensors.find({ status: 'pending', type: sensorType, hardwareId: null }); + + if (!pendingSensors.length) { + return reply.code(404).send({ message: 'No pending sensors found for the given type' }); + } + + let hardwareIdSequence = fromInt; + const date = moment().format('MM-DD'); + + for (let sensor of pendingSensors) { + if (hardwareIdSequence > toInt) break; + + sensor.hardwareId = hardwareIdSequence.toString().padStart(8, '0'); + + let mater_seq_id; + if (sensorType === 'master') { + mater_seq_id = await generatewaterlevelsensorId(); + sensor.masterId = `AS${date}MALOV1${mater_seq_id}`; + } else if (sensorType === 'slave') { + mater_seq_id = await generatewaterlevelslavesensorId(); + sensor.masterId = `AS${date}SLLOV1${mater_seq_id}`; + } else if (sensorType === 'sensor') { + mater_seq_id = await generatewaterlevelheightsensorId(); + sensor.masterId = `AS${date}SELOV1${mater_seq_id}`; + } + + await sensor.save(); // Save updated sensor in the database + hardwareIdSequence++; + } + + return reply.code(200).send({ message: 'HardwareId and MasterId assigned successfully' }); + } catch (error) { + console.error('Error generating IDs:', error); + return reply.code(500).send({ message: 'Internal Server Error' }); + } +}; + + + exports.updateSensorById = async (req, reply) => { try { const { _id } = req.params; let updateData = req.body; - const allowedFields = ["model", "type", "hardwareId_company", "hardwareId"]; + const allowedFields = ["model", "type", "hardwareId_company", "hardwareId","masterId"]; // Filter out unwanted fields and convert type to lowercase if present const filteredUpdateData = Object.keys(updateData) @@ -1188,23 +1239,6 @@ exports.updateSensorById = async (req, reply) => { return obj; }, {}); - // Generate masterId if type is provided - if (filteredUpdateData.type) { - let mater_seq_id; - const date = moment().format("MM-DD"); - - if (filteredUpdateData.type === "master") { - mater_seq_id = await generatewaterlevelsensorId(); - filteredUpdateData.masterId = `AS${date}MALOV1${mater_seq_id}`; - } else if (filteredUpdateData.type === "slave") { - mater_seq_id = await generatewaterlevelslavesensorId(); - filteredUpdateData.masterId = `AS${date}SLLOV1${mater_seq_id}`; - } else if (filteredUpdateData.type === "sensor") { - mater_seq_id = await generatewaterlevelheightsensorId(); - filteredUpdateData.masterId = `AS${date}SELOV1${mater_seq_id}`; - } - } - const updatedSensor = await Insensors.findByIdAndUpdate(_id, filteredUpdateData, { new: true }); if (!updatedSensor) { diff --git a/src/controllers/tanksController.js b/src/controllers/tanksController.js index d118c8d4..e9fd7f8f 100644 --- a/src/controllers/tanksController.js +++ b/src/controllers/tanksController.js @@ -4542,7 +4542,7 @@ exports.IotDeviceforstandalonedevice = async (req, reply) => { } const status = req.body.Motor_status; - + console.log(status,"status") // Find the tank that contains the specified motor_id in its inputConnections const tank = await Tank.findOne({ "connections.inputConnections.motor_id": hardwareId }); diff --git a/src/routes/storeRoute.js b/src/routes/storeRoute.js index 9bd66cf1..952368db 100644 --- a/src/routes/storeRoute.js +++ b/src/routes/storeRoute.js @@ -1128,6 +1128,23 @@ fastify.delete("/api/deleteSensorById/:_id", { handler: storeController.deleteSensorById, }); +fastify.post('/api/generateHardwareMasterId', { + schema: { + description: 'Generate hardwareId and masterId dynamically for pending sensors', + tags: ['Store-Data'], + summary: 'Assign hardwareId and masterId to pending sensors dynamically', + body: { + type: 'object', + required: ['from', 'to', 'type'], + properties: { + from: { type: 'string', description: 'Starting hardwareId (e.g., 00000020)' }, + to: { type: 'string', description: 'Ending hardwareId (e.g., 00000030)' }, + type: { type: 'string', description: 'Type of sensor' }, + }, + }, + }, + handler: storeController.generateHardwareMasterId, +}); fastify.post("/api/updateSensorById/:_id", { schema: { @@ -1149,7 +1166,7 @@ fastify.post("/api/updateSensorById/:_id", { properties: { model: { type: "string", description: "Model of the sensor" }, type: { type: "string", description: "Type of sensor" }, - + masterId: { type: "string"}, hardwareId_company: { type: "string", description: "Company name of hardware ID" }, hardwareId: { type: "string", nullable: true, description: "Hardware ID (if applicable)" }, @@ -1771,7 +1788,7 @@ fastify.post("/api/acceptquotation/:quotationId", { }, required: ["action"], }, - security: [ + security: [ { basicAuth: [], }, From c2372897c6fe5786a9eafbda03460d9014cb4d84 Mon Sep 17 00:00:00 2001 From: Varun Date: Wed, 12 Mar 2025 12:08:22 +0530 Subject: [PATCH 2/2] changes --- src/controllers/storeController.js | 1 + src/routes/storeRoute.js | 1 + 2 files changed, 2 insertions(+) diff --git a/src/controllers/storeController.js b/src/controllers/storeController.js index 8e3a068f..cda67bc0 100644 --- a/src/controllers/storeController.js +++ b/src/controllers/storeController.js @@ -1173,6 +1173,7 @@ exports.installmotorswitch = async (request, reply) => { }; + exports.generateHardwareMasterId = async (req, reply) => { try { const { from, to, type } = req.body; diff --git a/src/routes/storeRoute.js b/src/routes/storeRoute.js index 952368db..9ddd61e2 100644 --- a/src/routes/storeRoute.js +++ b/src/routes/storeRoute.js @@ -1143,6 +1143,7 @@ fastify.post('/api/generateHardwareMasterId', { }, }, }, + handler: storeController.generateHardwareMasterId, });