|
|
@ -2766,7 +2766,7 @@ exports.getIotDataByCustomerAndHardwareId = async (req, reply) => {
|
|
|
|
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." });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// console.log("sensors",sensors)
|
|
|
|
// Step 2: Get latest IoT data
|
|
|
|
// 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) {
|
|
|
@ -2786,9 +2786,12 @@ exports.getIotDataByCustomerAndHardwareId = async (req, reply) => {
|
|
|
|
|
|
|
|
|
|
|
|
// Step 4: Get all connected slaves
|
|
|
|
// 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);
|
|
|
|
|
|
|
|
// console.log("connectedSlaves",connectedSlaves)
|
|
|
|
|
|
|
|
|
|
|
|
// Step 5: Get orderMap for fallback master info
|
|
|
|
// Step 5: Get orderMap for fallback master info
|
|
|
|
const orders = await Order.find({ customerId }).lean();
|
|
|
|
const orders = await Order.find({ customerId }).lean();
|
|
|
|
|
|
|
|
// console.log("orders",orders)
|
|
|
|
|
|
|
|
|
|
|
|
const orderMap = {};
|
|
|
|
const orderMap = {};
|
|
|
|
orders.forEach(order => {
|
|
|
|
orders.forEach(order => {
|
|
|
|
order.master_connections?.forEach(connection => {
|
|
|
|
order.master_connections?.forEach(connection => {
|
|
|
@ -2801,6 +2804,7 @@ exports.getIotDataByCustomerAndHardwareId = async (req, reply) => {
|
|
|
|
|
|
|
|
|
|
|
|
// Step 6: Fallback master info from orderMap
|
|
|
|
// Step 6: Fallback master info from orderMap
|
|
|
|
const fallbackMasterInfo = orderMap[hardwareId] || { masterName: null, location: null };
|
|
|
|
const fallbackMasterInfo = orderMap[hardwareId] || { masterName: null, location: null };
|
|
|
|
|
|
|
|
console.log("fallbackMasterInfo",fallbackMasterInfo)
|
|
|
|
|
|
|
|
|
|
|
|
// Step 7: Map tanks data
|
|
|
|
// Step 7: Map tanks data
|
|
|
|
const tanks = connectedSlaves.map(slave => {
|
|
|
|
const tanks = connectedSlaves.map(slave => {
|
|
|
@ -2821,8 +2825,8 @@ 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 ?? fallbackMasterInfo.masterName,
|
|
|
|
masterName: fallbackMasterInfo.masterName,
|
|
|
|
// location: slave.location ?? fallbackMasterInfo.location,
|
|
|
|
location: fallbackMasterInfo.location,
|
|
|
|
message: loraMessage,
|
|
|
|
message: loraMessage,
|
|
|
|
latestTankData: matchedTank ?? null
|
|
|
|
latestTankData: matchedTank ?? null
|
|
|
|
};
|
|
|
|
};
|
|
|
@ -2835,8 +2839,8 @@ exports.getIotDataByCustomerAndHardwareId = async (req, reply) => {
|
|
|
|
data: {
|
|
|
|
data: {
|
|
|
|
hardwareId,
|
|
|
|
hardwareId,
|
|
|
|
message: gsmMessage,
|
|
|
|
message: gsmMessage,
|
|
|
|
// masterName: connectedSlaves[0]?.masterName ?? fallbackMasterInfo.masterName,
|
|
|
|
masterName: fallbackMasterInfo.masterName,
|
|
|
|
// location: connectedSlaves[0]?.location ?? fallbackMasterInfo.location,
|
|
|
|
location: fallbackMasterInfo.location,
|
|
|
|
tanks
|
|
|
|
tanks
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|