|
|
@ -2761,6 +2761,7 @@ exports.getIotDataByCustomer = async (req, reply) => {
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
exports.getIotDataByCustomerAndHardwareId = async (req, reply) => {
|
|
|
|
exports.getIotDataByCustomerAndHardwareId = async (req, reply) => {
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
const { customerId, hardwareId } = req.params;
|
|
|
|
const { customerId, hardwareId } = req.params;
|
|
|
@ -2769,16 +2770,14 @@ exports.getIotDataByCustomerAndHardwareId = async (req, reply) => {
|
|
|
|
return reply.code(400).send({ error: "Both customerId and hardwareId are required" });
|
|
|
|
return reply.code(400).send({ error: "Both customerId and hardwareId are required" });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Get all sensors for the customer
|
|
|
|
// Step 1: Get all sensors
|
|
|
|
const sensors = await Insensors.find({ customerId });
|
|
|
|
const sensors = await Insensors.find({ customerId });
|
|
|
|
|
|
|
|
|
|
|
|
if (!sensors.length) {
|
|
|
|
if (!sensors.length) {
|
|
|
|
return reply.code(404).send({ message: "No sensors found for this customer." });
|
|
|
|
return reply.code(404).send({ message: "No sensors found for this customer." });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Get IoT data for that master hardwareId
|
|
|
|
// Step 2: Get latest IoT data
|
|
|
|
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 reply.code(404).send({
|
|
|
|
return reply.code(404).send({
|
|
|
|
hardwareId,
|
|
|
|
hardwareId,
|
|
|
@ -2787,18 +2786,34 @@ exports.getIotDataByCustomerAndHardwareId = async (req, reply) => {
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Step 3: Calculate GSM connection status
|
|
|
|
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 gsmMessage = gsmConnected ? "GSM is connected" : "GSM is not connected";
|
|
|
|
|
|
|
|
|
|
|
|
// Get all slaves connected to this master
|
|
|
|
// Step 4: 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 slave tanks with tank data and LoRa status
|
|
|
|
// Step 5: Get orderMap for fallback master info
|
|
|
|
|
|
|
|
const orders = await Order.find({ customerId }).lean();
|
|
|
|
|
|
|
|
const orderMap = {};
|
|
|
|
|
|
|
|
orders.forEach(order => {
|
|
|
|
|
|
|
|
order.master_connections?.forEach(connection => {
|
|
|
|
|
|
|
|
orderMap[connection.hardwareId] = {
|
|
|
|
|
|
|
|
masterName: connection.master_name || null,
|
|
|
|
|
|
|
|
location: connection.location || null
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Step 6: Fallback master info from orderMap
|
|
|
|
|
|
|
|
const fallbackMasterInfo = orderMap[hardwareId] || { masterName: null, location: null };
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Step 7: Map tanks data
|
|
|
|
const tanks = connectedSlaves.map(slave => {
|
|
|
|
const tanks = connectedSlaves.map(slave => {
|
|
|
|
const slaveId = slave.hardwareId?.trim();
|
|
|
|
const slaveId = slave.tankhardwareId?.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;
|
|
|
@ -2815,21 +2830,22 @@ exports.getIotDataByCustomerAndHardwareId = async (req, reply) => {
|
|
|
|
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 ?? fallbackMasterInfo.masterName,
|
|
|
|
location: slave.location ?? null,
|
|
|
|
location: slave.location ?? fallbackMasterInfo.location,
|
|
|
|
message: loraMessage,
|
|
|
|
message: loraMessage,
|
|
|
|
latestTankData: matchedTank ?? null
|
|
|
|
latestTankData: matchedTank ?? null
|
|
|
|
};
|
|
|
|
};
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Step 8: Send response
|
|
|
|
return reply.send({
|
|
|
|
return reply.send({
|
|
|
|
status_code: 200,
|
|
|
|
status_code: 200,
|
|
|
|
message: "Success",
|
|
|
|
message: "Success",
|
|
|
|
data: {
|
|
|
|
data: {
|
|
|
|
hardwareId,
|
|
|
|
hardwareId,
|
|
|
|
message,
|
|
|
|
message: gsmMessage,
|
|
|
|
masterName: connectedSlaves[0]?.masterName ?? null,
|
|
|
|
masterName: connectedSlaves[0]?.masterName ?? fallbackMasterInfo.masterName,
|
|
|
|
location: connectedSlaves[0]?.location ?? null,
|
|
|
|
location: connectedSlaves[0]?.location ?? fallbackMasterInfo.location,
|
|
|
|
tanks
|
|
|
|
tanks
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|