|
|
@ -1952,10 +1952,9 @@ exports.getIotDataByCustomer = async (req, reply) => {
|
|
|
|
const masterHardwareIds = [
|
|
|
|
const masterHardwareIds = [
|
|
|
|
...new Set(sensors.map(s => s.connected_to?.trim()).filter(Boolean))
|
|
|
|
...new Set(sensors.map(s => s.connected_to?.trim()).filter(Boolean))
|
|
|
|
];
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
|
|
const enrichedMasters = await Promise.all(masterHardwareIds.map(async (hardwareId) => {
|
|
|
|
const enrichedMasters = await Promise.all(masterHardwareIds.map(async (hardwareId) => {
|
|
|
|
const latestRecord = await IotData.findOne({ hardwareId }).sort({ date: -1 }).lean();
|
|
|
|
const latestRecord = await IotData.findOne({ hardwareId }).sort({ date: -1 }).lean();
|
|
|
|
|
|
|
|
|
|
|
|
if (!latestRecord) {
|
|
|
|
if (!latestRecord) {
|
|
|
|
return {
|
|
|
|
return {
|
|
|
|
hardwareId,
|
|
|
|
hardwareId,
|
|
|
@ -1963,44 +1962,48 @@ exports.getIotDataByCustomer = async (req, reply) => {
|
|
|
|
tanks: []
|
|
|
|
tanks: []
|
|
|
|
};
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// ✅ Use timestamp to check GSM connection
|
|
|
|
// ✅ Use timestamp to check GSM connection
|
|
|
|
const indiaTime = moment.tz(latestRecord.date, "Asia/Kolkata");
|
|
|
|
const indiaTime = moment.tz(latestRecord.date, "Asia/Kolkata");
|
|
|
|
const now = moment.tz("Asia/Kolkata");
|
|
|
|
const now = moment.tz("Asia/Kolkata");
|
|
|
|
const diffInMinutes = now.diff(indiaTime, "minutes");
|
|
|
|
const diffInMinutes = now.diff(indiaTime, "minutes");
|
|
|
|
const gsmConnected = diffInMinutes <= 1;
|
|
|
|
const gsmConnected = diffInMinutes <= 1;
|
|
|
|
|
|
|
|
|
|
|
|
const message = gsmConnected ? "GSM is connected" : "GSM is not connected";
|
|
|
|
const message = gsmConnected ? "GSM is connected" : "GSM is not connected";
|
|
|
|
|
|
|
|
|
|
|
|
// Get all connected slaves
|
|
|
|
// Get all connected slaves
|
|
|
|
const connectedSlaves = sensors.filter(sensor => sensor.connected_to?.trim() === hardwareId);
|
|
|
|
const connectedSlaves = sensors.filter(sensor => sensor.connected_to?.trim() === hardwareId);
|
|
|
|
|
|
|
|
|
|
|
|
// Enrich tanks
|
|
|
|
// Enrich tanks
|
|
|
|
let tanks = connectedSlaves.map(slave => {
|
|
|
|
let tanks = connectedSlaves.map(slave => {
|
|
|
|
const slaveId = slave.hardwareId?.trim();
|
|
|
|
const slaveId = slave.hardwareId?.trim();
|
|
|
|
const matchedTank = latestRecord.tanks.find(tank => tank.tankhardwareId === slaveId);
|
|
|
|
const matchedTank = latestRecord.tanks.find(tank => tank.tankhardwareId === slaveId);
|
|
|
|
|
|
|
|
|
|
|
|
let loraConnected = false;
|
|
|
|
let loraConnected = false;
|
|
|
|
if (matchedTank?.date) {
|
|
|
|
let loraMessage = "LORA is not connected"; // Default message for LoRa not connected
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// If matchedTank exists and tankHeight is not zero, check LoRa connection
|
|
|
|
|
|
|
|
if (matchedTank?.date && matchedTank.tankHeight !== "0") {
|
|
|
|
const tankTime = moment.tz(matchedTank.date, "Asia/Kolkata");
|
|
|
|
const tankTime = moment.tz(matchedTank.date, "Asia/Kolkata");
|
|
|
|
const loraDiff = now.diff(tankTime, "minutes");
|
|
|
|
const loraDiff = now.diff(tankTime, "minutes");
|
|
|
|
loraConnected = loraDiff <= 1;
|
|
|
|
loraConnected = loraDiff <= 1;
|
|
|
|
|
|
|
|
loraMessage = loraConnected ? "LORA is connected" : "LORA is not connected";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
return {
|
|
|
|
tankhardwareId: slaveId,
|
|
|
|
tankhardwareId: slaveId,
|
|
|
|
tankName: slave.tankName ?? null,
|
|
|
|
tankName: slave.tankName ?? null,
|
|
|
|
tankLocation: slave.tankLocation ?? null,
|
|
|
|
tankLocation: slave.tankLocation ?? null,
|
|
|
|
masterName: slave.masterName ?? null,
|
|
|
|
masterName: slave.masterName ?? null,
|
|
|
|
location: slave.location ?? null,
|
|
|
|
location: slave.location ?? null,
|
|
|
|
loraMessage: loraConnected ? "LORA is connected" : "LORA is not connected",
|
|
|
|
loraMessage, // Updated LoRa message
|
|
|
|
latestTankData: matchedTank ?? null
|
|
|
|
latestTankData: matchedTank ?? null
|
|
|
|
};
|
|
|
|
};
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
// ✅ Remove the first tank
|
|
|
|
// ✅ Remove the first tank
|
|
|
|
tanks = tanks.slice(1);
|
|
|
|
tanks = tanks.slice(1);
|
|
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
return {
|
|
|
|
hardwareId,
|
|
|
|
hardwareId,
|
|
|
|
message,
|
|
|
|
message,
|
|
|
@ -2009,6 +2012,7 @@ exports.getIotDataByCustomer = async (req, reply) => {
|
|
|
|
tanks
|
|
|
|
tanks
|
|
|
|
};
|
|
|
|
};
|
|
|
|
}));
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return reply.send({
|
|
|
|
return reply.send({
|
|
|
|
status_code: 200,
|
|
|
|
status_code: 200,
|
|
|
|