|
|
|
@ -1492,89 +1492,172 @@ exports.getMasterSlaveSummary = async (req, reply) => {
|
|
|
|
|
// };
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// exports.getIotDataByCustomer = async (req, reply) => {
|
|
|
|
|
// try {
|
|
|
|
|
// const { customerId } = req.params;
|
|
|
|
|
|
|
|
|
|
// if (!customerId) {
|
|
|
|
|
// return reply.status(400).send({ error: "customerId is required" });
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
// console.log("Fetching data for customerId:", customerId);
|
|
|
|
|
|
|
|
|
|
// // Step 1: Fetch all sensors for the customer
|
|
|
|
|
// const sensors = await Insensors.find({ customerId });
|
|
|
|
|
|
|
|
|
|
// if (!sensors || sensors.length === 0) {
|
|
|
|
|
// return reply.send({ status_code: 404, message: "No sensors found for this customer", data: [] });
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
// // Step 2: Fetch latest IoT data for each sensor's hardwareId
|
|
|
|
|
// const enrichedSensors = await Promise.all(sensors.map(async (sensor) => {
|
|
|
|
|
// const { connected_to: hardwareId, masterName, location } = sensor;
|
|
|
|
|
|
|
|
|
|
// // Fetch the latest IoT data for this hardwareId
|
|
|
|
|
// const iotData = await IotData.findOne({ hardwareId })
|
|
|
|
|
// .sort({ date: -1 }) // Sort to get the latest data
|
|
|
|
|
// .lean();
|
|
|
|
|
|
|
|
|
|
// if (!iotData) {
|
|
|
|
|
// return {
|
|
|
|
|
// hardwareId,
|
|
|
|
|
// message: "No IoT data found",
|
|
|
|
|
// masterName,
|
|
|
|
|
// location,
|
|
|
|
|
// tanks: []
|
|
|
|
|
// };
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
// // Step 3: Get GSM status based on the latest IoT data
|
|
|
|
|
// const latestRecord = iotData;
|
|
|
|
|
// const indiaTime = moment.tz(latestRecord.date, "Asia/Kolkata");
|
|
|
|
|
// const connected_gsm_date = indiaTime.format("YYYY-MM-DD");
|
|
|
|
|
// const connected_gsm_time = indiaTime.format("HH:mm:ss");
|
|
|
|
|
|
|
|
|
|
// const now = moment.tz("Asia/Kolkata");
|
|
|
|
|
// const diffInMinutes = now.diff(indiaTime, "minutes");
|
|
|
|
|
// const isGSMConnected = diffInMinutes <= 1;
|
|
|
|
|
// const gsmStatus = isGSMConnected ? "GSM Connected" : "GSM Not Connected";
|
|
|
|
|
|
|
|
|
|
// // Step 4: Annotate each tank with LoRa connection status
|
|
|
|
|
// const tanksWithConnectionStatus = latestRecord.tanks.slice(0, 2).map(tank => { // Limit to 2 tanks
|
|
|
|
|
// const tankMoment = moment.tz(tank.date, "Asia/Kolkata");
|
|
|
|
|
// const tankDiff = now.diff(tankMoment, "minutes");
|
|
|
|
|
|
|
|
|
|
// const loraStatus = tankDiff <= 1 ? "LORA is connected" : "LORA is not connected";
|
|
|
|
|
// return {
|
|
|
|
|
// tankhardwareId: tank.tankhardwareId,
|
|
|
|
|
// tankName: sensor.tankName ?? null,
|
|
|
|
|
// tankLocation: sensor.tankLocation ?? null,
|
|
|
|
|
// masterName: sensor.masterName ?? null,
|
|
|
|
|
// location: sensor.location ?? null,
|
|
|
|
|
// loraMessage: loraStatus,
|
|
|
|
|
// latestTankData: tank
|
|
|
|
|
// };
|
|
|
|
|
// });
|
|
|
|
|
|
|
|
|
|
// // Step 5: Return enriched sensor data in the desired format
|
|
|
|
|
// return {
|
|
|
|
|
// hardwareId,
|
|
|
|
|
// message: gsmStatus,
|
|
|
|
|
// masterName,
|
|
|
|
|
// location,
|
|
|
|
|
// tanks: tanksWithConnectionStatus
|
|
|
|
|
// };
|
|
|
|
|
// }));
|
|
|
|
|
|
|
|
|
|
// // Step 6: Return the enriched data for the customer
|
|
|
|
|
// return reply.send({
|
|
|
|
|
// status_code: 200,
|
|
|
|
|
// message: "Success",
|
|
|
|
|
// data: enrichedSensors
|
|
|
|
|
// });
|
|
|
|
|
|
|
|
|
|
// } catch (err) {
|
|
|
|
|
// console.error("Error in getAllDataForCustomer:", err);
|
|
|
|
|
// return reply.status(500).send({ error: "Internal Server Error" });
|
|
|
|
|
// }
|
|
|
|
|
// };
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
exports.getIotDataByCustomer = async (req, reply) => {
|
|
|
|
|
try {
|
|
|
|
|
const { customerId } = req.params;
|
|
|
|
|
|
|
|
|
|
if (!customerId) {
|
|
|
|
|
return reply.status(400).send({ error: "customerId is required" });
|
|
|
|
|
return reply.code(400).send({ error: "customerId is required" });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
console.log("Fetching data for customerId:", customerId);
|
|
|
|
|
|
|
|
|
|
// Step 1: Fetch all sensors for the customer
|
|
|
|
|
// Step 1: Get all devices for this customer
|
|
|
|
|
const sensors = await Insensors.find({ customerId });
|
|
|
|
|
|
|
|
|
|
if (!sensors || sensors.length === 0) {
|
|
|
|
|
return reply.send({ status_code: 404, message: "No sensors found for this customer", data: [] });
|
|
|
|
|
if (!sensors.length) {
|
|
|
|
|
return reply.code(404).send({ message: "No sensors found for this customer." });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Step 2: Fetch latest IoT data for each sensor's hardwareId
|
|
|
|
|
const enrichedSensors = await Promise.all(sensors.map(async (sensor) => {
|
|
|
|
|
const { connected_to: hardwareId, masterName, location } = sensor;
|
|
|
|
|
// Step 2: Get all unique master hardwareIds from connected_to
|
|
|
|
|
const masterHardwareIds = [
|
|
|
|
|
...new Set(
|
|
|
|
|
sensors.map(s => s.connected_to?.trim()).filter(Boolean)
|
|
|
|
|
)
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
// Fetch the latest IoT data for this hardwareId
|
|
|
|
|
const iotData = await IotData.findOne({ hardwareId })
|
|
|
|
|
.sort({ date: -1 }) // Sort to get the latest data
|
|
|
|
|
.lean();
|
|
|
|
|
// Step 3: Loop over each master (connected_to) and build enriched data
|
|
|
|
|
const enrichedMasters = await Promise.all(masterHardwareIds.map(async (hardwareId) => {
|
|
|
|
|
const latestRecord = await IotData.findOne({ hardwareId }).sort({ date: -1 }).lean();
|
|
|
|
|
|
|
|
|
|
if (!iotData) {
|
|
|
|
|
if (!latestRecord) {
|
|
|
|
|
return {
|
|
|
|
|
hardwareId,
|
|
|
|
|
message: "No IoT data found",
|
|
|
|
|
masterName,
|
|
|
|
|
location,
|
|
|
|
|
tanks: []
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Step 3: Get GSM status based on the latest IoT data
|
|
|
|
|
const latestRecord = iotData;
|
|
|
|
|
const indiaTime = moment.tz(latestRecord.date, "Asia/Kolkata");
|
|
|
|
|
const connected_gsm_date = indiaTime.format("YYYY-MM-DD");
|
|
|
|
|
const connected_gsm_time = indiaTime.format("HH:mm:ss");
|
|
|
|
|
// Get all slaves connected to this master
|
|
|
|
|
const connectedSlaves = sensors.filter(sensor => sensor.connected_to?.trim() === hardwareId);
|
|
|
|
|
|
|
|
|
|
const now = moment.tz("Asia/Kolkata");
|
|
|
|
|
const diffInMinutes = now.diff(indiaTime, "minutes");
|
|
|
|
|
const isGSMConnected = diffInMinutes <= 1;
|
|
|
|
|
const gsmStatus = isGSMConnected ? "GSM Connected" : "GSM Not Connected";
|
|
|
|
|
const message = latestRecord.tanks.some(t => parseFloat(t.tankHeight || 0) > 0)
|
|
|
|
|
? "GSM is connected"
|
|
|
|
|
: "GSM is not connected";
|
|
|
|
|
|
|
|
|
|
// Step 4: Annotate each tank with LoRa connection status
|
|
|
|
|
const tanksWithConnectionStatus = latestRecord.tanks.slice(0, 2).map(tank => { // Limit to 2 tanks
|
|
|
|
|
const tankMoment = moment.tz(tank.date, "Asia/Kolkata");
|
|
|
|
|
const tankDiff = now.diff(tankMoment, "minutes");
|
|
|
|
|
const tanks = connectedSlaves.map(slave => {
|
|
|
|
|
const slaveId = slave.hardwareId?.trim();
|
|
|
|
|
const matchedTank = latestRecord.tanks.find(tank => tank.tankhardwareId === slaveId);
|
|
|
|
|
|
|
|
|
|
const loraMessage = matchedTank && parseFloat(matchedTank.tankHeight || 0) > 0
|
|
|
|
|
? "LORA is connected"
|
|
|
|
|
: "LORA is not connected";
|
|
|
|
|
|
|
|
|
|
const loraStatus = tankDiff <= 1 ? "LORA is connected" : "LORA is not connected";
|
|
|
|
|
return {
|
|
|
|
|
tankhardwareId: tank.tankhardwareId,
|
|
|
|
|
tankName: sensor.tankName ?? null,
|
|
|
|
|
tankLocation: sensor.tankLocation ?? null,
|
|
|
|
|
masterName: sensor.masterName ?? null,
|
|
|
|
|
location: sensor.location ?? null,
|
|
|
|
|
loraMessage: loraStatus,
|
|
|
|
|
latestTankData: tank
|
|
|
|
|
tankhardwareId: slaveId,
|
|
|
|
|
tankName: slave.tankName ?? null,
|
|
|
|
|
tankLocation: slave.tankLocation ?? null,
|
|
|
|
|
masterName: slave.masterName ?? null,
|
|
|
|
|
location: slave.location ?? null,
|
|
|
|
|
loraMessage,
|
|
|
|
|
latestTankData: matchedTank ?? null
|
|
|
|
|
};
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Step 5: Return enriched sensor data in the desired format
|
|
|
|
|
return {
|
|
|
|
|
hardwareId,
|
|
|
|
|
message: gsmStatus,
|
|
|
|
|
masterName,
|
|
|
|
|
location,
|
|
|
|
|
tanks: tanksWithConnectionStatus
|
|
|
|
|
message,
|
|
|
|
|
masterName: connectedSlaves[0]?.masterName ?? null,
|
|
|
|
|
location: connectedSlaves[0]?.location ?? null,
|
|
|
|
|
tanks
|
|
|
|
|
};
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
// Step 6: Return the enriched data for the customer
|
|
|
|
|
return reply.send({
|
|
|
|
|
status_code: 200,
|
|
|
|
|
message: "Success",
|
|
|
|
|
data: enrichedSensors
|
|
|
|
|
data: enrichedMasters
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
} catch (err) {
|
|
|
|
|
console.error("Error in getAllDataForCustomer:", err);
|
|
|
|
|
return reply.status(500).send({ error: "Internal Server Error" });
|
|
|
|
|
console.error("Error fetching IoT data by customerId:", err);
|
|
|
|
|
return reply.code(500).send({ error: "Internal Server Error" });
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|