master^2
Bhaskar 5 months ago
parent 0dfd77800f
commit 29a0ffdef8

@ -1716,6 +1716,7 @@ exports.mastrerList = async (req, reply) => {
exports.getMasterSlaveSummary = async (req, reply) => {
try {
const { customerId } = req.params;
@ -1760,7 +1761,7 @@ exports.getMasterSlaveSummary = async (req, reply) => {
let gsmLastDisconnect = master.gsm_last_disconnect_time || null;
if (latestGsmData?.date && latestGsmData?.time) {
const indiaTime = moment.tz(latestGsmData.date, "Asia/Kolkata");
const indiaTime = moment.tz(`${latestGsmData.date} ${latestGsmData.time}`, "Asia/Kolkata");
connectedGsmDate = indiaTime.format("DD-MM-YYYY");
connectedGsmTime = latestGsmData.time;
@ -1784,13 +1785,13 @@ exports.getMasterSlaveSummary = async (req, reply) => {
connected_gsm_date: connectedGsmDate,
connected_gsm_time: connectedGsmTime,
gsm_last_check_time: gsmLastCheckTime,
gsm_last_disconnect_time: gsmLastDisconnect // Update the disconnect time
gsm_last_disconnect_time: gsmLastDisconnect
}
}
);
}
// Save GSM disconnect time if status is disconnected and gsmLastDisconnect is set
// Save GSM disconnect time if status is disconnected and gsmLastDisconnect is set
if (gsmStatus === "disconnected" && gsmLastDisconnect) {
await Insensors.updateOne(
{ hardwareId: master.hardwareId },
@ -1806,25 +1807,24 @@ exports.getMasterSlaveSummary = async (req, reply) => {
const slaveIot = await IotData.findOne({ hardwareId: slave.hardwareId })
.sort({ date: -1, time: -1 })
.lean();
let connectedLoraDate = null;
let connectedLoraTime = null;
let loraStatus = "unknown";
let loraLastCheckTime = null;
let loraLastDisconnect = slave.lora_last_disconnect_time || null;
if (slaveIot?.date && slaveIot?.time) {
const indiaTime = moment.tz(`${slaveIot.date} ${slaveIot.time}`, "Asia/Kolkata");
connectedLoraDate = indiaTime.format("DD-MM-YYYY");
connectedLoraTime = slaveIot.time;
const now = moment.tz("Asia/Kolkata");
const diff = now.diff(indiaTime, "minutes");
loraStatus = diff <= 1 ? "connected" : "disconnected";
loraLastCheckTime = now.format("DD-MM-YYYY HH:mm:ss");
// Update slave record with new values
await Insensors.updateOne(
{ hardwareId: slave.hardwareId },
{
@ -1836,8 +1836,7 @@ exports.getMasterSlaveSummary = async (req, reply) => {
}
}
);
// Save disconnect time if applicable
if (loraStatus === "disconnected") {
const disconnectTime = `${connectedLoraDate} ${connectedLoraTime}`;
await Insensors.updateOne(
@ -1847,7 +1846,6 @@ exports.getMasterSlaveSummary = async (req, reply) => {
loraLastDisconnect = disconnectTime;
}
} else {
// If no IoT data is found, mark explicitly as disconnected
loraStatus = "disconnected";
loraLastDisconnect = loraLastDisconnect || null;
await Insensors.updateOne(
@ -1860,9 +1858,10 @@ exports.getMasterSlaveSummary = async (req, reply) => {
}
);
}
connectedSlaves.push({
hardwareId: slave.hardwareId,
tankhardwareId: slave.tankhardwareId || null, // Added here
tankName: slave.tankName || null,
location: slave.tankLocation || null,
connected_status: loraStatus,

Loading…
Cancel
Save