|
|
|
@ -2062,3 +2062,83 @@ exports.getIotDataByCustomer = async (req, reply) => {
|
|
|
|
|
return reply.code(500).send({ error: "Internal Server Error" });
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
exports.getIotDataByCustomerAndHardwareId = async (req, reply) => {
|
|
|
|
|
try {
|
|
|
|
|
const { customerId, hardwareId } = req.params;
|
|
|
|
|
|
|
|
|
|
if (!customerId || !hardwareId) {
|
|
|
|
|
return reply.code(400).send({ error: "Both customerId and hardwareId are required" });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Get all sensors for the customer
|
|
|
|
|
const sensors = await Insensors.find({ customerId });
|
|
|
|
|
|
|
|
|
|
if (!sensors.length) {
|
|
|
|
|
return reply.code(404).send({ message: "No sensors found for this customer." });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Get IoT data for that master hardwareId
|
|
|
|
|
const latestRecord = await IotData.findOne({ hardwareId }).sort({ date: -1 }).lean();
|
|
|
|
|
|
|
|
|
|
if (!latestRecord) {
|
|
|
|
|
return reply.code(404).send({
|
|
|
|
|
hardwareId,
|
|
|
|
|
message: "No IoT data found",
|
|
|
|
|
tanks: []
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const indiaTime = moment.tz(latestRecord.date, "Asia/Kolkata");
|
|
|
|
|
const now = moment.tz("Asia/Kolkata");
|
|
|
|
|
const diffInMinutes = now.diff(indiaTime, "minutes");
|
|
|
|
|
const gsmConnected = diffInMinutes <= 1;
|
|
|
|
|
const message = gsmConnected ? "GSM is connected" : "GSM is not connected";
|
|
|
|
|
|
|
|
|
|
// Get all slaves connected to this master
|
|
|
|
|
const connectedSlaves = sensors.filter(sensor => sensor.connected_to?.trim() === hardwareId);
|
|
|
|
|
|
|
|
|
|
// Enrich slave tanks with tank data and LoRa status
|
|
|
|
|
const tanks = connectedSlaves.map(slave => {
|
|
|
|
|
const slaveId = slave.hardwareId?.trim();
|
|
|
|
|
const matchedTank = latestRecord.tanks?.find(tank => tank.tankhardwareId === slaveId);
|
|
|
|
|
|
|
|
|
|
let loraConnected = false;
|
|
|
|
|
let loraMessage = "LORA is not connected";
|
|
|
|
|
|
|
|
|
|
if (matchedTank?.date && matchedTank.tankHeight !== "0") {
|
|
|
|
|
const tankTime = moment.tz(matchedTank.date, "Asia/Kolkata");
|
|
|
|
|
const loraDiff = now.diff(tankTime, "minutes");
|
|
|
|
|
loraConnected = loraDiff <= 1;
|
|
|
|
|
loraMessage = loraConnected ? "LORA is connected" : "LORA is not connected";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
tankhardwareId: slaveId,
|
|
|
|
|
tankName: slave.tankName ?? null,
|
|
|
|
|
tankLocation: slave.tankLocation ?? null,
|
|
|
|
|
masterName: slave.masterName ?? null,
|
|
|
|
|
location: slave.location ?? null,
|
|
|
|
|
loraMessage,
|
|
|
|
|
latestTankData: matchedTank ?? null
|
|
|
|
|
};
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return reply.send({
|
|
|
|
|
status_code: 200,
|
|
|
|
|
message: "Success",
|
|
|
|
|
data: {
|
|
|
|
|
hardwareId,
|
|
|
|
|
message,
|
|
|
|
|
masterName: connectedSlaves[0]?.masterName ?? null,
|
|
|
|
|
location: connectedSlaves[0]?.location ?? null,
|
|
|
|
|
tanks
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
} catch (err) {
|
|
|
|
|
console.error("Error fetching IoT data by customerId and hardwareId:", err);
|
|
|
|
|
return reply.code(500).send({ error: "Internal Server Error" });
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|