master^2
Bhaskar 5 months ago
parent 29a0ffdef8
commit 4483f5920f

@ -1715,8 +1715,6 @@ exports.mastrerList = async (req, reply) => {
// };
exports.getMasterSlaveSummary = async (req, reply) => {
try {
const { customerId } = req.params;
@ -1771,12 +1769,12 @@ exports.getMasterSlaveSummary = async (req, reply) => {
gsmStatus = diffInMinutes <= 1 ? "connected" : "disconnected";
gsmLastCheckTime = now.format("DD-MM-YYYY HH:mm:ss");
// If disconnected, update the disconnect time based on the latest GSM data
// If disconnected and no previous disconnect time, set it
if (gsmStatus === "disconnected" && !gsmLastDisconnect) {
gsmLastDisconnect = `${connectedGsmDate} ${connectedGsmTime}`;
}
// Update master record with latest GSM info
// Update master record with latest GSM info
await Insensors.updateOne(
{ hardwareId: master.hardwareId },
{
@ -1791,7 +1789,7 @@ exports.getMasterSlaveSummary = async (req, reply) => {
);
}
// Save GSM disconnect time if status is disconnected and gsmLastDisconnect is set
// Save GSM disconnect time if disconnected
if (gsmStatus === "disconnected" && gsmLastDisconnect) {
await Insensors.updateOne(
{ hardwareId: master.hardwareId },
@ -1799,7 +1797,7 @@ exports.getMasterSlaveSummary = async (req, reply) => {
);
}
// ➤ Now handle slaves
// Handle slaves
const connectedSlaves = [];
const slaves = await Insensors.find({ connected_to: master.hardwareId, type: "slave" }).lean();
@ -1861,14 +1859,15 @@ exports.getMasterSlaveSummary = async (req, reply) => {
connectedSlaves.push({
hardwareId: slave.hardwareId,
tankhardwareId: slave.tankhardwareId || null, // Added here
tankhardwareId: slave.tankhardwareId || null,
tankName: slave.tankName || null,
location: slave.tankLocation || null,
connected_status: loraStatus,
connected_lora_date: connectedLoraDate,
connected_lora_time: connectedLoraTime,
lora_last_disconnect_time: loraLastDisconnect,
type: slave.type || "slave"
type: slave.type || "slave",
connected_to: slave.connected_to || null // <-- Added connected_to here
});
}
@ -2616,6 +2615,7 @@ exports.getIotDataByCustomerAndHardwareId = async (req, reply) => {
//const moment = require("moment-timezone");
exports.raiseATicket = async (req, reply) => {
try {
const { customerId, connected_to } = req.params;
@ -2646,9 +2646,22 @@ exports.raiseATicket = async (req, reply) => {
});
});
const latestMasterRecord = await IotData.findOne({ hardwareId: connected_to }).sort({ date: -1 }).lean();
const now = moment.tz("Asia/Kolkata");
// Helper function to calculate disconnect duration in minutes from a timestamp string
function getDisconnectDuration(timeStr) {
if (!timeStr) return null;
const time = moment.tz(timeStr, "DD-MM-YYYY HH:mm:ss", "Asia/Kolkata");
if (!time.isValid()) return null;
return now.diff(time, "minutes");
}
// Check master disconnect durations (both GSM and LoRa)
const masterGsmDisconnectDuration = getDisconnectDuration(masterSensor.gsm_last_disconnect_time);
const masterLoraDisconnectDuration = getDisconnectDuration(masterSensor.lora_last_disconnect_time);
// Latest master record to check connection status by data date
const latestMasterRecord = await IotData.findOne({ hardwareId: connected_to }).sort({ date: -1 }).lean();
let masterConnectedStatus = "disconnected";
let lastDataTime = "No data";
let diffInMinutes = null;
@ -2660,6 +2673,7 @@ exports.raiseATicket = async (req, reply) => {
masterConnectedStatus = diffInMinutes <= 1 ? "connected" : "disconnected";
}
// Filter slaves connected to this master
const connectedSlaves = sensors.filter(s =>
s.connected_to?.trim() === connected_to.trim() && s.type === "slave"
);
@ -2668,6 +2682,11 @@ exports.raiseATicket = async (req, reply) => {
const disconnectedSlaves = [];
for (const slave of connectedSlaves) {
// Check both GSM and LoRa disconnect durations for slave
const slaveGsmDisconnectDuration = getDisconnectDuration(slave.gsm_last_disconnect_time);
const slaveLoraDisconnectDuration = getDisconnectDuration(slave.lora_last_disconnect_time);
// Latest slave tank data from master record's tanks array
const slaveData = latestMasterRecord?.tanks.find(t => t.tankhardwareId === slave.hardwareId);
let slaveStatus = "disconnected";
let lastSlaveDataTime = "No data";
@ -2683,7 +2702,9 @@ exports.raiseATicket = async (req, reply) => {
if (slaveStatus === "disconnected") {
disconnectedSlaves.push({
slaveHardwareId: slave.hardwareId,
slaveName: slave.tankName || "Unknown Slave"
slaveName: slave.tankName || "Unknown Slave",
gsmDisconnectedDuration: slaveGsmDisconnectDuration,
loraDisconnectedDuration: slaveLoraDisconnectDuration
});
}
@ -2698,19 +2719,24 @@ exports.raiseATicket = async (req, reply) => {
});
}
// Raise tickets if needed
// Raise tickets only if disconnected for more than 15 mins in either GSM or LoRa
const issuesToAdd = [];
if (masterConnectedStatus === "disconnected") {
// Master ticket condition
if (
masterConnectedStatus === "disconnected" &&
((masterGsmDisconnectDuration !== null && masterGsmDisconnectDuration > 15) ||
(masterLoraDisconnectDuration !== null && masterLoraDisconnectDuration > 15))
) {
const existing = await Support.findOne({
"issues.hardwareId": connected_to,
"issues.type": "GSM Disconnected"
"issues.type": "GSM or LoRa Disconnected"
});
if (!existing) {
issuesToAdd.push({
type: "GSM Disconnected",
type: "GSM or LoRa Disconnected",
hardwareId: connected_to,
message: `Master GSM disconnected - ${connected_to}`
message: `Master GSM or LoRa disconnected for more than 15 minutes - ${connected_to}`
});
}
}
@ -2718,37 +2744,42 @@ exports.raiseATicket = async (req, reply) => {
const newHardwareIds = [];
const newSlaveNames = [];
// Slave tickets condition
for (const s of disconnectedSlaves) {
const existing = await Support.findOne({
"issues.hardwareIds": s.slaveHardwareId,
"issues.masterHardwareId": connected_to,
"issues.type": "LoRa Disconnected"
});
if (!existing) {
newHardwareIds.push(s.slaveHardwareId);
newSlaveNames.push(s.slaveName);
const gsmDisconnectedMoreThan15 = s.gsmDisconnectedDuration !== null && s.gsmDisconnectedDuration > 15;
const loraDisconnectedMoreThan15 = s.loraDisconnectedDuration !== null && s.loraDisconnectedDuration > 15;
if (gsmDisconnectedMoreThan15 || loraDisconnectedMoreThan15) {
const existing = await Support.findOne({
"issues.hardwareIds": s.slaveHardwareId,
"issues.masterHardwareId": connected_to,
"issues.type": "LoRa or GSM Disconnected"
});
if (!existing) {
newHardwareIds.push(s.slaveHardwareId);
newSlaveNames.push(s.slaveName);
}
}
}
if (newHardwareIds.length > 0) {
issuesToAdd.push({
type: "LoRa Disconnected",
type: "LoRa or GSM Disconnected",
masterHardwareId: connected_to,
hardwareIds: newHardwareIds,
slaveNames: newSlaveNames,
message: `Slaves LoRa disconnected under master ${connected_to}`
message: `Slaves LoRa or GSM disconnected for more than 15 minutes under master ${connected_to}`
});
}
// Update support collection if there are new issues
if (issuesToAdd.length > 0) {
const supportRecord = await Support.findOne({ supportId: "AWHYSU64" });
if (supportRecord) {
await Support.findOneAndUpdate(
{ _id: supportRecord._id },
{
$push: {
issues: { $each: issuesToAdd }
},
$push: { issues: { $each: issuesToAdd } },
updatedAt: new Date()
}
);
@ -2771,6 +2802,7 @@ exports.raiseATicket = async (req, reply) => {
master: masterDetails,
connected_slaves: formattedSlaves
});
} catch (error) {
console.error("Error raising ticket:", error);
return reply.code(500).send({ error: "Internal server error" });
@ -2778,6 +2810,7 @@ exports.raiseATicket = async (req, reply) => {
};
// exports.raiseATicket = async (req, reply) => {
// try {
// const { customerId, connected_to } = req.params;

Loading…
Cancel
Save