|
|
|
@ -1120,51 +1120,62 @@ exports.getIotDataByCustomer = async (req, reply) => {
|
|
|
|
|
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 });
|
|
|
|
|
|
|
|
|
|
if (!sensors || sensors.length === 0) {
|
|
|
|
|
return reply.code(404).send({ message: "No sensors found for this customer." });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Extract unique connected hardwareIds
|
|
|
|
|
// Extract unique hardwareIds
|
|
|
|
|
const hardwareIds = [
|
|
|
|
|
...new Set(
|
|
|
|
|
sensors
|
|
|
|
|
.map(sensor => sensor.connected_to?.trim())
|
|
|
|
|
.filter(Boolean)
|
|
|
|
|
),
|
|
|
|
|
sensors.map(sensor => sensor.connected_to?.trim()).filter(Boolean)
|
|
|
|
|
)
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
if (hardwareIds.length === 0) {
|
|
|
|
|
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 latestRecord = await IotData.findOne({ hardwareId })
|
|
|
|
|
.sort({ date: -1 })
|
|
|
|
|
.lean();
|
|
|
|
|
const latestRecord = await IotData.findOne({ hardwareId }).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";
|
|
|
|
|
if (latestRecord && Array.isArray(latestRecord.tanks)) {
|
|
|
|
|
const hasTankData = latestRecord.tanks.some(
|
|
|
|
|
tank => tank.tankHeight && parseFloat(tank.tankHeight) > 0
|
|
|
|
|
);
|
|
|
|
|
if (hasTankData) {
|
|
|
|
|
message = "GSM is connected";
|
|
|
|
|
}
|
|
|
|
|
if (latestRecord?.tanks?.some(tank => parseFloat(tank.tankHeight || 0) > 0)) {
|
|
|
|
|
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 {
|
|
|
|
|
hardwareId,
|
|
|
|
|
message,
|
|
|
|
|
latestRecord,
|
|
|
|
|
masterName: relatedSensor?.masterName ?? null,
|
|
|
|
|
location: relatedSensor?.location ?? null
|
|
|
|
|
gsmDate: latestRecord?.date,
|
|
|
|
|
tanks: tanksStatus
|
|
|
|
|
};
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
@ -1181,3 +1192,4 @@ exports.getIotDataByCustomer = async (req, reply) => {
|
|
|
|
|
return reply.code(500).send({ error: "Internal Server Error" });
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|