status added

master^2
Bhaskar 6 months ago
parent 35d081d9cc
commit eeedd2b28b

@ -920,6 +920,8 @@ exports.mastrerList = async (req, reply) => {
// } // }
// } // }
const moment = require('moment'); // For time calculations
exports.getMasterSlaveSummary = async (req, reply) => { exports.getMasterSlaveSummary = async (req, reply) => {
try { try {
const { customerId } = req.params; const { customerId } = req.params;
@ -929,12 +931,43 @@ exports.getMasterSlaveSummary = async (req, reply) => {
} }
// Get all devices for customer // Get all devices for customer
const allDevices = await Insensors.find({ customerId }).lean(); const allDevices = await Insensors.find({ customerId });
// Separate masters and slaves // Separate masters and slaves
const masters = allDevices.filter(device => device.type === 'master'); const masters = allDevices.filter(device => device.type === 'master');
const slaves = allDevices.filter(device => device.type === 'slave'); 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 // Map masters to their connected slaves based on `connected_to` match
const enrichedMasters = masters.map(master => { const enrichedMasters = masters.map(master => {
const masterConnectionId = master.connected_to; const masterConnectionId = master.connected_to;
@ -944,7 +977,7 @@ exports.getMasterSlaveSummary = async (req, reply) => {
); );
return { return {
...master, ...master.toObject(),
connected_slave_count: connectedSlaves.length, connected_slave_count: connectedSlaves.length,
connected_slaves: connectedSlaves connected_slaves: connectedSlaves
}; };
@ -965,7 +998,7 @@ exports.getMasterSlaveSummary = async (req, reply) => {
message: "Internal Server Error" message: "Internal Server Error"
}); });
} }
} };
// exports.getIotDataByCustomer = async (req, reply) => { // exports.getIotDataByCustomer = async (req, reply) => {

@ -361,7 +361,8 @@ const insensorsSchema = new mongoose.Schema({
tankName: { type: String, default: "0" }, tankName: { type: String, default: "0" },
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" },
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