lora message added

master^2
Bhaskar 6 months ago
parent 0e6be3f54b
commit 4f1b44ea07

@ -1120,51 +1120,62 @@ exports.getIotDataByCustomer = async (req, reply) => {
return reply.code(400).send({ error: "customerId is required" }); return reply.code(400).send({ error: "customerId is required" });
} }
// Fetch sensors for the customer // Fetch all sensors of the customer
const sensors = await Insensors.find({ customerId }); const sensors = await Insensors.find({ customerId });
if (!sensors || sensors.length === 0) { if (!sensors || sensors.length === 0) {
return reply.code(404).send({ message: "No sensors found for this customer." }); return reply.code(404).send({ message: "No sensors found for this customer." });
} }
// Extract unique connected hardwareIds // Extract unique hardwareIds
const hardwareIds = [ const hardwareIds = [
...new Set( ...new Set(
sensors sensors.map(sensor => sensor.connected_to?.trim()).filter(Boolean)
.map(sensor => sensor.connected_to?.trim()) )
.filter(Boolean)
),
]; ];
if (hardwareIds.length === 0) { if (hardwareIds.length === 0) {
return reply.send({ status_code: 200, message: "No connected hardwareIds found", data: [] }); return reply.send({ status_code: 200, message: "No connected hardwareIds found", data: [] });
} }
// Fetch latest IoT data and merge with sensor info // Fetch latest IoT data and enrich it
const latestDataPromises = hardwareIds.map(async hardwareId => { const latestDataPromises = hardwareIds.map(async hardwareId => {
const latestRecord = await IotData.findOne({ hardwareId }) const latestRecord = await IotData.findOne({ hardwareId }).sort({ date: -1 }).lean();
.sort({ date: -1 })
.lean();
const relatedSensor = sensors.find(sensor => sensor.connected_to?.trim() === hardwareId); const relatedSensors = sensors.filter(sensor => sensor.connected_to?.trim() === hardwareId);
// Determine GSM status message
let message = "GSM is not connected"; let message = "GSM is not connected";
if (latestRecord && Array.isArray(latestRecord.tanks)) { if (latestRecord?.tanks?.some(tank => parseFloat(tank.tankHeight || 0) > 0)) {
const hasTankData = latestRecord.tanks.some(
tank => tank.tankHeight && parseFloat(tank.tankHeight) > 0
);
if (hasTankData) {
message = "GSM is connected"; message = "GSM is connected";
} }
}
// Prepare individual tank connection status
const tanksStatus = relatedSensors.map(sensor => {
const tankhardwareId = sensor.hardwareId?.trim();
const matchedTank = latestRecord?.tanks?.find(
tank => tank.tankhardwareId === tankhardwareId
);
const loraMessage =
matchedTank && parseFloat(matchedTank.tankHeight || 0) > 0
? "LoRa is connected"
: "LoRa is not connected";
return {
tankhardwareId,
masterName: sensor.masterName ?? null,
location: sensor.location ?? null,
loraMessage,
latestTankData: matchedTank || null
};
});
return { return {
hardwareId, hardwareId,
message, message,
latestRecord, gsmDate: latestRecord?.date,
masterName: relatedSensor?.masterName ?? null, tanks: tanksStatus
location: relatedSensor?.location ?? null
}; };
}); });
@ -1181,3 +1192,4 @@ exports.getIotDataByCustomer = async (req, reply) => {
return reply.code(500).send({ error: "Internal Server Error" }); return reply.code(500).send({ error: "Internal Server Error" });
} }
}; };

Loading…
Cancel
Save