|
|
|
@ -920,6 +920,8 @@ exports.mastrerList = async (req, reply) => {
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
const moment = require('moment'); // For time calculations
|
|
|
|
|
|
|
|
|
|
exports.getMasterSlaveSummary = async (req, reply) => {
|
|
|
|
|
try {
|
|
|
|
|
const { customerId } = req.params;
|
|
|
|
@ -929,12 +931,43 @@ exports.getMasterSlaveSummary = async (req, reply) => {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Get all devices for customer
|
|
|
|
|
const allDevices = await Insensors.find({ customerId }).lean();
|
|
|
|
|
const allDevices = await Insensors.find({ customerId });
|
|
|
|
|
|
|
|
|
|
// Separate masters and slaves
|
|
|
|
|
const masters = allDevices.filter(device => device.type === 'master');
|
|
|
|
|
const slaves = allDevices.filter(device => device.type === 'slave');
|
|
|
|
|
|
|
|
|
|
// Update connected_status for each sensor
|
|
|
|
|
const updateStatusPromises = allDevices.map(async device => {
|
|
|
|
|
const hwId = device.connected_to;
|
|
|
|
|
if (!hwId) return;
|
|
|
|
|
|
|
|
|
|
const latestData = await IotData.findOne({ hardwareId: hwId })
|
|
|
|
|
.sort({ date: -1 })
|
|
|
|
|
.lean();
|
|
|
|
|
|
|
|
|
|
let status = "disconnected";
|
|
|
|
|
|
|
|
|
|
if (latestData?.date) {
|
|
|
|
|
const now = moment();
|
|
|
|
|
const lastSeen = moment(latestData.date);
|
|
|
|
|
const diff = now.diff(lastSeen, 'minutes');
|
|
|
|
|
if (diff <= 10) {
|
|
|
|
|
status = "connected";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
await Insensors.updateOne(
|
|
|
|
|
{ _id: device._id },
|
|
|
|
|
{ $set: { connected_status: status } }
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
device.connected_status = status; // Also update in-memory object
|
|
|
|
|
console.log(" device.connected_status", device.connected_status)
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
await Promise.all(updateStatusPromises);
|
|
|
|
|
|
|
|
|
|
// Map masters to their connected slaves based on `connected_to` match
|
|
|
|
|
const enrichedMasters = masters.map(master => {
|
|
|
|
|
const masterConnectionId = master.connected_to;
|
|
|
|
@ -944,7 +977,7 @@ exports.getMasterSlaveSummary = async (req, reply) => {
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
...master,
|
|
|
|
|
...master.toObject(),
|
|
|
|
|
connected_slave_count: connectedSlaves.length,
|
|
|
|
|
connected_slaves: connectedSlaves
|
|
|
|
|
};
|
|
|
|
@ -965,7 +998,7 @@ exports.getMasterSlaveSummary = async (req, reply) => {
|
|
|
|
|
message: "Internal Server Error"
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// exports.getIotDataByCustomer = async (req, reply) => {
|
|
|
|
|