dynamically creating hardwareid and masterid

master^2
Varun 7 months ago
parent ea28c0067c
commit ba36f50f2e

@ -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) {

@ -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 });

@ -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: [],
},

Loading…
Cancel
Save