|
|
|
@ -1018,41 +1018,42 @@ exports.getMasterSlaveSummary = async (req, reply) => {
|
|
|
|
|
|
|
|
|
|
// Update connection status and timestamps
|
|
|
|
|
const updateStatusPromises = allDevices.map(async device => {
|
|
|
|
|
let checkHardwareId = device.hardwareId?.trim();
|
|
|
|
|
if (!checkHardwareId) return;
|
|
|
|
|
|
|
|
|
|
// For slave, check connection via master's hardwareId (i.e., connected_to)
|
|
|
|
|
if (device.type === 'slave') {
|
|
|
|
|
checkHardwareId = device.connected_to?.trim();
|
|
|
|
|
if (!checkHardwareId) return;
|
|
|
|
|
let checkHardwareId;
|
|
|
|
|
|
|
|
|
|
if (device.type === 'master') {
|
|
|
|
|
checkHardwareId = device.hardwareId?.trim();
|
|
|
|
|
} else if (device.type === 'slave') {
|
|
|
|
|
checkHardwareId = device.hardwareId?.trim(); // Use slave's own hardwareId
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (!checkHardwareId) return;
|
|
|
|
|
|
|
|
|
|
const latestData = await IotData.findOne({ hardwareId: checkHardwareId })
|
|
|
|
|
.sort({ date: -1 })
|
|
|
|
|
.lean();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let status = "disconnected";
|
|
|
|
|
let updateFields = {};
|
|
|
|
|
|
|
|
|
|
const updateFields = {};
|
|
|
|
|
|
|
|
|
|
if (latestData?.date) {
|
|
|
|
|
const now = moment();
|
|
|
|
|
const lastSeen = moment(latestData.date);
|
|
|
|
|
const diff = now.diff(lastSeen, 'minutes');
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (diff <= 10) {
|
|
|
|
|
status = "connected";
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const indiaTime = moment.tz(latestData.date, "Asia/Kolkata");
|
|
|
|
|
const date = indiaTime.format("YYYY-MM-DD");
|
|
|
|
|
const time = indiaTime.format("HH:mm:ss");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (device.type === 'master') {
|
|
|
|
|
updateFields.connected_gsm_date = date;
|
|
|
|
|
updateFields.connected_gsm_time = time;
|
|
|
|
|
device.connected_gsm_date = date;
|
|
|
|
|
device.connected_gsm_time = time;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (device.type === 'slave') {
|
|
|
|
|
updateFields.connected_lora_date = date;
|
|
|
|
|
updateFields.connected_lora_time = time;
|
|
|
|
@ -1061,11 +1062,13 @@ exports.getMasterSlaveSummary = async (req, reply) => {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
updateFields.connected_status = status;
|
|
|
|
|
await Insensors.updateOne({ _id: device._id }, { $set: updateFields });
|
|
|
|
|
device.connected_status = status;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
await Promise.all(updateStatusPromises);
|
|
|
|
|
|
|
|
|
|