|
|
|
@ -1061,8 +1061,6 @@ exports.getMasterSlaveSummary = async (req, reply) => {
|
|
|
|
|
// }
|
|
|
|
|
// };
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
exports.getIotDataByCustomer = async (req, reply) => {
|
|
|
|
|
try {
|
|
|
|
|
const { customerId } = req.params;
|
|
|
|
@ -1071,12 +1069,14 @@ exports.getIotDataByCustomer = async (req, reply) => {
|
|
|
|
|
return reply.code(400).send({ error: "customerId is required" });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Fetch sensors for 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
|
|
|
|
|
const hardwareIds = [
|
|
|
|
|
...new Set(
|
|
|
|
|
sensors
|
|
|
|
@ -1089,12 +1089,15 @@ exports.getIotDataByCustomer = async (req, reply) => {
|
|
|
|
|
return reply.send({ status_code: 200, message: "No connected hardwareIds found", data: [] });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Fetch latest IoT data and merge with sensor info
|
|
|
|
|
const latestDataPromises = hardwareIds.map(async hardwareId => {
|
|
|
|
|
const latestRecord = await IotData.findOne({ hardwareId })
|
|
|
|
|
.sort({ date: -1 })
|
|
|
|
|
.lean();
|
|
|
|
|
|
|
|
|
|
// Determine the message based on tanks data
|
|
|
|
|
const relatedSensor = sensors.find(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(
|
|
|
|
@ -1109,7 +1112,8 @@ exports.getIotDataByCustomer = async (req, reply) => {
|
|
|
|
|
hardwareId,
|
|
|
|
|
message,
|
|
|
|
|
latestRecord,
|
|
|
|
|
|
|
|
|
|
masterName: relatedSensor?.masterName ?? null,
|
|
|
|
|
location: relatedSensor?.location ?? null
|
|
|
|
|
};
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
@ -1126,6 +1130,3 @@ exports.getIotDataByCustomer = async (req, reply) => {
|
|
|
|
|
return reply.code(500).send({ error: "Internal Server Error" });
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|