masterName location added in insensors

master^2
Bhaskar 6 months ago
parent eeedd2b28b
commit 3bfc0b3367

@ -1061,8 +1061,6 @@ exports.getMasterSlaveSummary = async (req, reply) => {
// } // }
// }; // };
exports.getIotDataByCustomer = async (req, reply) => { exports.getIotDataByCustomer = async (req, reply) => {
try { try {
const { customerId } = req.params; const { customerId } = req.params;
@ -1071,12 +1069,14 @@ exports.getIotDataByCustomer = async (req, reply) => {
return reply.code(400).send({ error: "customerId is required" }); return reply.code(400).send({ error: "customerId is required" });
} }
// Fetch sensors for the customer
const sensors = await Insensors.find({ customerId }); const sensors = await Insensors.find({ customerId });
if (!sensors || sensors.length === 0) { if (!sensors || sensors.length === 0) {
return reply.code(404).send({ message: "No sensors found for this customer." }); return reply.code(404).send({ message: "No sensors found for this customer." });
} }
// Extract unique connected hardwareIds
const hardwareIds = [ const hardwareIds = [
...new Set( ...new Set(
sensors sensors
@ -1089,12 +1089,15 @@ exports.getIotDataByCustomer = async (req, reply) => {
return reply.send({ status_code: 200, message: "No connected hardwareIds found", data: [] }); return reply.send({ status_code: 200, message: "No connected hardwareIds found", data: [] });
} }
// Fetch latest IoT data and merge with sensor info
const latestDataPromises = hardwareIds.map(async hardwareId => { const latestDataPromises = hardwareIds.map(async hardwareId => {
const latestRecord = await IotData.findOne({ hardwareId }) const latestRecord = await IotData.findOne({ hardwareId })
.sort({ date: -1 }) .sort({ date: -1 })
.lean(); .lean();
// Determine the message based on tanks data const relatedSensor = sensors.find(sensor => sensor.connected_to?.trim() === hardwareId);
// Determine GSM status message
let message = "GSM is not connected"; let message = "GSM is not connected";
if (latestRecord && Array.isArray(latestRecord.tanks)) { if (latestRecord && Array.isArray(latestRecord.tanks)) {
const hasTankData = latestRecord.tanks.some( const hasTankData = latestRecord.tanks.some(
@ -1109,7 +1112,8 @@ exports.getIotDataByCustomer = async (req, reply) => {
hardwareId, hardwareId,
message, message,
latestRecord, latestRecord,
masterName: relatedSensor?.masterName ?? null,
location: relatedSensor?.location ?? null
}; };
}); });
@ -1126,6 +1130,3 @@ exports.getIotDataByCustomer = async (req, reply) => {
return reply.code(500).send({ error: "Internal Server Error" }); return reply.code(500).send({ error: "Internal Server Error" });
} }
}; };

@ -362,7 +362,8 @@ const insensorsSchema = new mongoose.Schema({
tankLocation: { type: String, default: "0" }, tankLocation: { type: String, default: "0" },
connected_slave: { type: String, default: null }, connected_slave: { type: String, default: null },
connected_status: { type: String, enum: ["connected", "Not connected", "unknown"], default: "unknown" }, connected_status: { type: String, enum: ["connected", "Not connected", "unknown"], default: "unknown" },
masterName: { type: String, default: null },
location: { type: String, default: null },
quality_check_details: [{ quality_check_details: [{
damage_check: { result: String }, damage_check: { result: String },
stickering_check: { result: String }, stickering_check: { result: String },

Loading…
Cancel
Save