ashok 3 months ago
commit 9289a5ed4d

@ -2766,7 +2766,7 @@ exports.getIotDataByCustomerAndHardwareId = async (req, reply) => {
if (!sensors.length) {
return reply.code(404).send({ message: "No sensors found for this customer." });
}
// console.log("sensors",sensors)
// Step 2: Get latest IoT data
const latestRecord = await IotData.findOne({ hardwareId }).sort({ date: -1 }).lean();
if (!latestRecord) {
@ -2786,9 +2786,12 @@ exports.getIotDataByCustomerAndHardwareId = async (req, reply) => {
// Step 4: Get all connected slaves
const connectedSlaves = sensors.filter(sensor => sensor.connected_to?.trim() === hardwareId);
// console.log("connectedSlaves",connectedSlaves)
// Step 5: Get orderMap for fallback master info
const orders = await Order.find({ customerId }).lean();
// console.log("orders",orders)
const orderMap = {};
orders.forEach(order => {
order.master_connections?.forEach(connection => {
@ -2801,6 +2804,7 @@ exports.getIotDataByCustomerAndHardwareId = async (req, reply) => {
// Step 6: Fallback master info from orderMap
const fallbackMasterInfo = orderMap[hardwareId] || { masterName: null, location: null };
console.log("fallbackMasterInfo",fallbackMasterInfo)
// Step 7: Map tanks data
const tanks = connectedSlaves.map(slave => {
@ -2821,8 +2825,8 @@ exports.getIotDataByCustomerAndHardwareId = async (req, reply) => {
tankhardwareId: slaveId,
tankName: slave.tankName ?? null,
tankLocation: slave.tankLocation ?? null,
// masterName: slave.masterName ?? fallbackMasterInfo.masterName,
// location: slave.location ?? fallbackMasterInfo.location,
masterName: fallbackMasterInfo.masterName,
location: fallbackMasterInfo.location,
message: loraMessage,
latestTankData: matchedTank ?? null
};
@ -2835,8 +2839,8 @@ exports.getIotDataByCustomerAndHardwareId = async (req, reply) => {
data: {
hardwareId,
message: gsmMessage,
// masterName: connectedSlaves[0]?.masterName ?? fallbackMasterInfo.masterName,
// location: connectedSlaves[0]?.location ?? fallbackMasterInfo.location,
masterName: fallbackMasterInfo.masterName,
location: fallbackMasterInfo.location,
tanks
}
});

Loading…
Cancel
Save