ashok 3 months ago
commit 2f4cfb92f1

@ -4776,7 +4776,7 @@ async function getTankHeight(tankhardwareId) {
exports.getIotDataByCustomer = async (req, reply) => { exports.getIotDataByCustomer = async (req, reply) => {
try { try {
const { customerId } = req.params; const { customerId, hardwareId } = req.params;
if (!customerId) { if (!customerId) {
return reply.code(400).send({ error: "customerId is required" }); return reply.code(400).send({ error: "customerId is required" });
@ -4789,11 +4789,21 @@ exports.getIotDataByCustomer = async (req, reply) => {
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 only master hardware IDs from Insensors directly // ✅ Filter master sensors
const masterSensors = sensors.filter(s => s.type === 'master'); let masterSensors = sensors.filter(s => s.type === 'master');
// ✅ If hardwareId is provided, filter to that master only
if (hardwareId) {
masterSensors = masterSensors.filter(m => m.hardwareId?.trim() === hardwareId.trim());
}
if (!masterSensors.length) {
return reply.code(404).send({ message: "No master found for the given hardwareId or customer." });
}
const masterHardwareIds = masterSensors.map(m => m.hardwareId?.trim()); const masterHardwareIds = masterSensors.map(m => m.hardwareId?.trim());
// ✅ Map for masterName/location from Order // ✅ Build map of masterName/location from Order
const orders = await Order.find({ customerId }).lean(); const orders = await Order.find({ customerId }).lean();
const orderMap = {}; const orderMap = {};
orders.forEach(order => { orders.forEach(order => {
@ -4805,7 +4815,7 @@ exports.getIotDataByCustomer = async (req, reply) => {
}); });
}); });
// ✅ Prepare final enriched master data // ✅ Enrich each master with latest IoT data
const enrichedMasters = await Promise.all(masterHardwareIds.map(async (hardwareId) => { const enrichedMasters = await Promise.all(masterHardwareIds.map(async (hardwareId) => {
const latestRecord = await IotData.findOne({ hardwareId }).sort({ date: -1 }).lean(); const latestRecord = await IotData.findOne({ hardwareId }).sort({ date: -1 }).lean();
@ -4821,23 +4831,22 @@ exports.getIotDataByCustomer = async (req, reply) => {
}; };
} }
// ✅ Check GSM status // ✅ 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 message = gsmConnected ? "GSM is connected" : "GSM is not connected";
// ✅ Get slaves connected to this master // ✅ Find slaves connected to this master
const connectedSlaves = sensors.filter(sensor => sensor.connected_to?.trim() === hardwareId); const connectedSlaves = sensors.filter(sensor => sensor.connected_to?.trim() === hardwareId);
// ✅ Prepare tank info // ✅ Enrich each slave/tank
const tanks = connectedSlaves.map(slave => { const tanks = connectedSlaves.map(slave => {
const slaveId = slave.tankhardwareId?.trim(); const slaveId = slave.tankhardwareId?.trim();
const matchedTank = latestRecord.tanks?.find(t => t.tankhardwareId === slaveId); const matchedTank = latestRecord.tanks?.find(t => t.tankhardwareId === slaveId);
let loraMessage = "LORA is not connected"; let loraMessage = "LORA is not connected";
if (matchedTank?.date && matchedTank.tankHeight !== "0") { if (matchedTank?.date && matchedTank.tankHeight !== "0") {
const tankTime = moment.tz(matchedTank.date, "Asia/Kolkata"); const tankTime = moment.tz(matchedTank.date, "Asia/Kolkata");
const loraDiff = now.diff(tankTime, "minutes"); const loraDiff = now.diff(tankTime, "minutes");
@ -4878,6 +4887,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;

@ -772,7 +772,7 @@ fastify.post(
handler: installationController.getPendingMasterSlaveSummary, handler: installationController.getPendingMasterSlaveSummary,
}); });
fastify.get("/api/getAllmasterlistwithslaves/:customerId", { fastify.get("/api/getAllmasterlistwithslaves/:customerId/:hardwareId", {
schema: { schema: {
description: "Get All check masrter connected slave data with full info", description: "Get All check masrter connected slave data with full info",
tags: ["Installation"], tags: ["Installation"],
@ -781,6 +781,7 @@ fastify.post(
type: "object", type: "object",
properties: { properties: {
customerId: { type: "string" }, customerId: { type: "string" },
hardwareId: { type: "string" },
}, },
required: [ "customerId"], required: [ "customerId"],

Loading…
Cancel
Save