ashok 3 months ago
commit 7205f0bdae

@ -2658,19 +2658,25 @@ exports.getMasterSlaveSummary = async (req, reply) => {
return reply.status(400).send({ error: "customerId is required" }); return reply.status(400).send({ error: "customerId is required" });
} }
// Fetch all master devices for the customer // Fetch all master devices from Insensors
const masters = await Insensors.find({ customerId, type: "master" }).lean(); const masters = await Insensors.find({ customerId, type: "master" }).lean();
// Fetch orders to map hardwareId to masterName/location // Fetch orders to build orderMap: hardwareId → { masterName, location, work_status }
const orders = await Order.find({ customerId }).lean(); const orders = await Order.find({ customerId }).lean();
const orderMap = {}; const orderMap = {}; // key: hardwareId
orders.forEach(order => { orders.forEach(order => {
if (Array.isArray(order.master_connections)) {
order.master_connections.forEach(connection => { order.master_connections.forEach(connection => {
if (connection.hardwareId) {
orderMap[connection.hardwareId] = { orderMap[connection.hardwareId] = {
masterName: connection.master_name || null, masterName: connection.master_name || null,
location: connection.location || null, location: connection.location || null,
work_status: connection.work_status || null
}; };
}
}); });
}
}); });
const result = []; const result = [];
@ -2678,11 +2684,16 @@ exports.getMasterSlaveSummary = async (req, reply) => {
for (const master of masters) { for (const master of masters) {
const orderInfo = orderMap[master.hardwareId] || {}; const orderInfo = orderMap[master.hardwareId] || {};
// Prefer Insensors info, fallback to order info // ✅ Only keep masters where work_status === 'active'
if (orderInfo.work_status !== 'active') {
continue; // skip this master
}
// Prefer Insensors name/location, fallback to order info
const masterName = master.masterName || orderInfo.masterName || null; const masterName = master.masterName || orderInfo.masterName || null;
const location = master.location || orderInfo.location || null; const location = master.location || orderInfo.location || null;
// Fetch the latest GSM data for the master device // Fetch latest GSM data
const latestGsmData = await IotData.findOne({ hardwareId: master.hardwareId }) const latestGsmData = await IotData.findOne({ hardwareId: master.hardwareId })
.sort({ date: -1, time: -1 }) .sort({ date: -1, time: -1 })
.lean(); .lean();
@ -2690,17 +2701,15 @@ exports.getMasterSlaveSummary = async (req, reply) => {
let connectedGsmDate = null; let connectedGsmDate = null;
let connectedGsmTime = null; let connectedGsmTime = null;
let gsmStatus = "unknown"; let gsmStatus = "unknown";
let gsmLastCheckTime = null;
let gsmLastDisconnect = master.gsm_last_disconnect_time || null; let gsmLastDisconnect = master.gsm_last_disconnect_time || null;
let gsmLastCheckTime = master.gsm_last_check_time || null;
if (latestGsmData?.date && latestGsmData?.time) { if (latestGsmData?.date && latestGsmData?.time) {
// Combine date + time and parse as Asia/Kolkata timezone
const indiaTime = moment.tz( const indiaTime = moment.tz(
`${moment(latestGsmData.date).format("YYYY-MM-DD")} ${latestGsmData.time}`, `${moment(latestGsmData.date).format("YYYY-MM-DD")} ${latestGsmData.time}`,
"YYYY-MM-DD HH:mm:ss", "YYYY-MM-DD HH:mm:ss",
"Asia/Kolkata" "Asia/Kolkata"
); );
connectedGsmDate = indiaTime.format("DD-MM-YYYY"); connectedGsmDate = indiaTime.format("DD-MM-YYYY");
connectedGsmTime = indiaTime.format("HH:mm:ss"); connectedGsmTime = indiaTime.format("HH:mm:ss");
@ -2708,14 +2717,12 @@ exports.getMasterSlaveSummary = async (req, reply) => {
const diffInMinutes = now.diff(indiaTime, "minutes"); const diffInMinutes = now.diff(indiaTime, "minutes");
gsmStatus = diffInMinutes <= 1 ? "connected" : "disconnected"; gsmStatus = diffInMinutes <= 1 ? "connected" : "disconnected";
gsmLastCheckTime =master.gsm_last_check_time;
if (gsmStatus === "disconnected") { if (gsmStatus === "disconnected") {
// Update disconnect time with latest disconnect timestamp
gsmLastDisconnect = `${connectedGsmDate} ${connectedGsmTime}`; gsmLastDisconnect = `${connectedGsmDate} ${connectedGsmTime}`;
} }
// Update master device with latest GSM info // Update master Insensors record
await Insensors.updateOne( await Insensors.updateOne(
{ hardwareId: master.hardwareId }, { hardwareId: master.hardwareId },
{ {
@ -2724,17 +2731,15 @@ exports.getMasterSlaveSummary = async (req, reply) => {
connected_gsm_date: connectedGsmDate, connected_gsm_date: connectedGsmDate,
connected_gsm_time: connectedGsmTime, connected_gsm_time: connectedGsmTime,
gsm_last_check_time: gsmLastCheckTime, gsm_last_check_time: gsmLastCheckTime,
gsm_last_disconnect_time: gsmLastDisconnect, gsm_last_disconnect_time: gsmLastDisconnect
}, }
} }
); );
} }
// Process connected slave devices for the master // Process connected slaves
const connectedSlaves = []; const connectedSlaves = [];
const slaves = await Insensors.find({ connected_to: master.hardwareId, type: "slave" }).lean(); const slaves = await Insensors.find({ connected_to: master.hardwareId, type: "slave" }).lean();
console.log("slaves",slaves)
for (const slave of slaves) { for (const slave of slaves) {
const now = moment.tz("Asia/Kolkata"); const now = moment.tz("Asia/Kolkata");
@ -2742,18 +2747,16 @@ exports.getMasterSlaveSummary = async (req, reply) => {
let connectedLoraTime = null; let connectedLoraTime = null;
let loraStatus = "disconnected"; let loraStatus = "disconnected";
let loraLastDisconnect = slave.lora_last_disconnect_time || null; let loraLastDisconnect = slave.lora_last_disconnect_time || null;
let loraLastCheckTime = slave.lora_last_check_time; let loraLastCheckTime = slave.lora_last_check_time || null;
let tankHeight = null; let typeOfWater = null;
// Fetch latest IotData for slave's hardwareId (NOT master's) // Fetch latest slave IotData (using master hardwareId because slave data comes there)
const slaveIot = await IotData.findOne({ hardwareId: slave.connected_to }) const slaveIot = await IotData.findOne({ hardwareId: slave.connected_to })
.sort({ date: -1, time: -1 }) .sort({ date: -1, time: -1 })
.lean(); .lean();
if (slaveIot?.tanks?.length && slave.tankhardwareId) { if (slaveIot?.tanks?.length && slave.tankhardwareId) {
const matchedTank = slaveIot.tanks.find( const matchedTank = slaveIot.tanks.find(t => t.tankhardwareId === slave.tankhardwareId);
t => t.tankhardwareId === slave.tankhardwareId
);
if (matchedTank) { if (matchedTank) {
const indiaTime = moment.tz( const indiaTime = moment.tz(
@ -2765,11 +2768,9 @@ exports.getMasterSlaveSummary = async (req, reply) => {
connectedLoraDate = indiaTime.format("DD-MM-YYYY"); connectedLoraDate = indiaTime.format("DD-MM-YYYY");
connectedLoraTime = indiaTime.format("HH:mm:ss"); connectedLoraTime = indiaTime.format("HH:mm:ss");
tankHeight = parseFloat(matchedTank.tankHeight) || 0;
const diffMinutes = now.diff(indiaTime, "minutes"); const diffMinutes = now.diff(indiaTime, "minutes");
const tankHeight = parseFloat(matchedTank.tankHeight) || 0;
// Connected if tankHeight > 0 and data not older than 5 mins
loraStatus = (tankHeight > 0 && diffMinutes <= 1) ? "connected" : "disconnected"; loraStatus = (tankHeight > 0 && diffMinutes <= 1) ? "connected" : "disconnected";
if (loraStatus === "disconnected") { if (loraStatus === "disconnected") {
@ -2777,15 +2778,18 @@ exports.getMasterSlaveSummary = async (req, reply) => {
} }
} }
} }
// Enrich with tank typeOfWater if exists
const matchedTankDetails = await Tank.findOne({ const matchedTankDetails = await Tank.findOne({
customerId, customerId,
tankhardwareId: slave.tankhardwareId, tankhardwareId: slave.tankhardwareId
}).lean(); }).lean();
if (matchedTankDetails?.typeOfWater) { if (matchedTankDetails?.typeOfWater) {
typeOfWater = matchedTankDetails.typeOfWater; typeOfWater = matchedTankDetails.typeOfWater;
} }
// Update DB with new status and timestamps
// Update slave Insensors record
await Insensors.updateOne( await Insensors.updateOne(
{ hardwareId: slave.hardwareId }, { hardwareId: slave.hardwareId },
{ {
@ -2794,8 +2798,8 @@ exports.getMasterSlaveSummary = async (req, reply) => {
connected_lora_date: connectedLoraDate, connected_lora_date: connectedLoraDate,
connected_lora_time: connectedLoraTime, connected_lora_time: connectedLoraTime,
lora_last_check_time: loraLastCheckTime, lora_last_check_time: loraLastCheckTime,
lora_last_disconnect_time: loraLastDisconnect, lora_last_disconnect_time: loraLastDisconnect
}, }
} }
); );
@ -2811,12 +2815,10 @@ exports.getMasterSlaveSummary = async (req, reply) => {
lora_last_disconnect_time: loraLastDisconnect, lora_last_disconnect_time: loraLastDisconnect,
type: slave.type || "slave", type: slave.type || "slave",
typeOfWater, typeOfWater,
connected_to: slave.connected_to || null, connected_to: slave.connected_to || null
}); });
} }
result.push({ result.push({
hardwareId: master.hardwareId, hardwareId: master.hardwareId,
masterName, masterName,
@ -2830,15 +2832,16 @@ exports.getMasterSlaveSummary = async (req, reply) => {
gsm_last_check_time: gsmLastCheckTime, gsm_last_check_time: gsmLastCheckTime,
gsm_last_disconnect_time: gsmLastDisconnect, gsm_last_disconnect_time: gsmLastDisconnect,
connected_lora_date: master.connected_lora_date || null, connected_lora_date: master.connected_lora_date || null,
connected_lora_time: master.connected_lora_time || null, connected_lora_time: master.connected_lora_time || null
}); });
} }
return reply.send({ return reply.send({
status_code: 200, status_code: 200,
message: "Master-slave summary retrieved successfully", message: "Master-slave summary retrieved successfully",
data: result, data: result
}); });
} catch (error) { } catch (error) {
console.error("Error in getMasterSlaveSummary:", error); console.error("Error in getMasterSlaveSummary:", error);
return reply.status(500).send({ error: "Internal Server Error" }); return reply.status(500).send({ error: "Internal Server Error" });
@ -2848,6 +2851,7 @@ exports.getMasterSlaveSummary = async (req, reply) => {
// 🔍 Helper to get tankHeight from latest IotData record // 🔍 Helper to get tankHeight from latest IotData record
async function getTankHeight(tankhardwareId) { async function getTankHeight(tankhardwareId) {
const iotData = await IotData.findOne({ 'tanks.tankhardwareId': tankhardwareId }) const iotData = await IotData.findOne({ 'tanks.tankhardwareId': tankhardwareId })

Loading…
Cancel
Save