|
|
@ -2678,19 +2678,12 @@ exports.raiseATicket = async (req, reply) => {
|
|
|
|
|
|
|
|
|
|
|
|
const now = moment.tz("Asia/Kolkata");
|
|
|
|
const now = moment.tz("Asia/Kolkata");
|
|
|
|
|
|
|
|
|
|
|
|
// Helper function to calculate disconnect duration in minutes from a timestamp string
|
|
|
|
|
|
|
|
function getDisconnectDuration(timeStr) {
|
|
|
|
function getDisconnectDuration(timeStr) {
|
|
|
|
if (!timeStr) return null;
|
|
|
|
if (!timeStr) return null;
|
|
|
|
const time = moment.tz(timeStr, "DD-MM-YYYY HH:mm:ss", "Asia/Kolkata");
|
|
|
|
const time = moment.tz(timeStr, "DD-MM-YYYY HH:mm:ss", "Asia/Kolkata");
|
|
|
|
if (!time.isValid()) return null;
|
|
|
|
return time.isValid() ? now.diff(time, "minutes") : 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();
|
|
|
|
const latestMasterRecord = await IotData.findOne({ hardwareId: connected_to }).sort({ date: -1 }).lean();
|
|
|
|
let masterConnectedStatus = "disconnected";
|
|
|
|
let masterConnectedStatus = "disconnected";
|
|
|
|
let lastDataTime = "No data";
|
|
|
|
let lastDataTime = "No data";
|
|
|
@ -2700,24 +2693,18 @@ exports.raiseATicket = async (req, reply) => {
|
|
|
|
const indiaTime = moment.tz(latestMasterRecord.date, "Asia/Kolkata");
|
|
|
|
const indiaTime = moment.tz(latestMasterRecord.date, "Asia/Kolkata");
|
|
|
|
diffInMinutes = now.diff(indiaTime, "minutes");
|
|
|
|
diffInMinutes = now.diff(indiaTime, "minutes");
|
|
|
|
lastDataTime = indiaTime.format("DD-MM-YYYY HH:mm:ss");
|
|
|
|
lastDataTime = indiaTime.format("DD-MM-YYYY HH:mm:ss");
|
|
|
|
masterConnectedStatus = diffInMinutes <= 1 ? "connected" : "disconnected";
|
|
|
|
if (diffInMinutes <= 1) masterConnectedStatus = "connected";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Filter slaves connected to this master
|
|
|
|
const connectedSlaves = sensors.filter(
|
|
|
|
const connectedSlaves = sensors.filter(s =>
|
|
|
|
s => s.connected_to?.trim() === connected_to.trim() && s.type === "slave"
|
|
|
|
s.connected_to?.trim() === connected_to.trim() && s.type === "slave"
|
|
|
|
|
|
|
|
);
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
const formattedSlaves = [];
|
|
|
|
const formattedSlaves = [];
|
|
|
|
const disconnectedSlaves = [];
|
|
|
|
const disconnectedSlaves = [];
|
|
|
|
|
|
|
|
|
|
|
|
for (const slave of connectedSlaves) {
|
|
|
|
for (const slave of connectedSlaves) {
|
|
|
|
// Check both GSM and LoRa disconnect durations for slave
|
|
|
|
const slaveData = latestMasterRecord?.tanks.find(t => t.tankhardwareId === slave.tankhardwareId);
|
|
|
|
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 slaveStatus = "disconnected";
|
|
|
|
let lastSlaveDataTime = "No data";
|
|
|
|
let lastSlaveDataTime = "No data";
|
|
|
|
let slaveDiff = null;
|
|
|
|
let slaveDiff = null;
|
|
|
@ -2726,15 +2713,13 @@ exports.raiseATicket = async (req, reply) => {
|
|
|
|
const slaveTime = moment.tz(slaveData.date, "Asia/Kolkata");
|
|
|
|
const slaveTime = moment.tz(slaveData.date, "Asia/Kolkata");
|
|
|
|
slaveDiff = now.diff(slaveTime, "minutes");
|
|
|
|
slaveDiff = now.diff(slaveTime, "minutes");
|
|
|
|
lastSlaveDataTime = slaveTime.format("DD-MM-YYYY HH:mm:ss");
|
|
|
|
lastSlaveDataTime = slaveTime.format("DD-MM-YYYY HH:mm:ss");
|
|
|
|
slaveStatus = slaveDiff <= 1 ? "connected" : "disconnected";
|
|
|
|
if (slaveDiff <= 1) slaveStatus = "connected";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (slaveStatus === "disconnected") {
|
|
|
|
if (slaveStatus === "disconnected") {
|
|
|
|
disconnectedSlaves.push({
|
|
|
|
disconnectedSlaves.push({
|
|
|
|
slaveHardwareId: slave.hardwareId,
|
|
|
|
slaveHardwareId: slave.tankhardwareId,
|
|
|
|
slaveName: slave.tankName || "Unknown Slave",
|
|
|
|
slaveName: slave.tankName || "Unknown Slave"
|
|
|
|
gsmDisconnectedDuration: slaveGsmDisconnectDuration,
|
|
|
|
|
|
|
|
loraDisconnectedDuration: slaveLoraDisconnectDuration
|
|
|
|
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -2749,60 +2734,30 @@ exports.raiseATicket = async (req, reply) => {
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Raise tickets only if disconnected for more than 15 mins in either GSM or LoRa
|
|
|
|
|
|
|
|
const issuesToAdd = [];
|
|
|
|
const issuesToAdd = [];
|
|
|
|
|
|
|
|
|
|
|
|
// Master ticket condition
|
|
|
|
// ✅ Only raise a ticket if master is disconnected
|
|
|
|
if (
|
|
|
|
if (masterConnectedStatus === "disconnected") {
|
|
|
|
masterConnectedStatus === "disconnected" &&
|
|
|
|
const existingMasterTicket = await Support.findOne({
|
|
|
|
((masterGsmDisconnectDuration !== null && masterGsmDisconnectDuration > 15) ||
|
|
|
|
|
|
|
|
(masterLoraDisconnectDuration !== null && masterLoraDisconnectDuration > 15))
|
|
|
|
|
|
|
|
) {
|
|
|
|
|
|
|
|
const existing = await Support.findOne({
|
|
|
|
|
|
|
|
"issues.hardwareId": connected_to,
|
|
|
|
"issues.hardwareId": connected_to,
|
|
|
|
"issues.type": "GSM or LoRa Disconnected"
|
|
|
|
"issues.type": "GSM or LoRa Disconnected"
|
|
|
|
});
|
|
|
|
});
|
|
|
|
if (!existing) {
|
|
|
|
|
|
|
|
issuesToAdd.push({
|
|
|
|
|
|
|
|
type: "GSM or LoRa Disconnected",
|
|
|
|
|
|
|
|
hardwareId: connected_to,
|
|
|
|
|
|
|
|
message: `Master GSM or LoRa disconnected for more than 15 minutes - ${connected_to}`
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const newHardwareIds = [];
|
|
|
|
if (!existingMasterTicket) {
|
|
|
|
const newSlaveNames = [];
|
|
|
|
const slaveHardwareIds = disconnectedSlaves.map(s => s.slaveHardwareId);
|
|
|
|
|
|
|
|
const slaveNames = disconnectedSlaves.map(s => s.slaveName);
|
|
|
|
|
|
|
|
|
|
|
|
// Slave tickets condition
|
|
|
|
issuesToAdd.push({
|
|
|
|
for (const s of disconnectedSlaves) {
|
|
|
|
type: "GSM or LoRa Disconnected",
|
|
|
|
const gsmDisconnectedMoreThan15 = s.gsmDisconnectedDuration !== null && s.gsmDisconnectedDuration > 15;
|
|
|
|
masterHardwareId: connected_to,
|
|
|
|
const loraDisconnectedMoreThan15 = s.loraDisconnectedDuration !== null && s.loraDisconnectedDuration > 15;
|
|
|
|
hardwareId: connected_to, // Master hardwareId
|
|
|
|
|
|
|
|
hardwareIds: slaveHardwareIds, // Slave tankHardwareIds
|
|
|
|
if (gsmDisconnectedMoreThan15 || loraDisconnectedMoreThan15) {
|
|
|
|
slaveNames,
|
|
|
|
const existing = await Support.findOne({
|
|
|
|
message: `Master ${connected_to} is disconnected along with ${slaveHardwareIds.length} slave(s)`
|
|
|
|
"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 or GSM Disconnected",
|
|
|
|
|
|
|
|
masterHardwareId: connected_to,
|
|
|
|
|
|
|
|
hardwareIds: newHardwareIds,
|
|
|
|
|
|
|
|
slaveNames: newSlaveNames,
|
|
|
|
|
|
|
|
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) {
|
|
|
|
if (issuesToAdd.length > 0) {
|
|
|
|
const supportRecord = await Support.findOne({ supportId: "AWHYSU64" });
|
|
|
|
const supportRecord = await Support.findOne({ supportId: "AWHYSU64" });
|
|
|
|
if (supportRecord) {
|
|
|
|
if (supportRecord) {
|
|
|
@ -2828,7 +2783,7 @@ exports.raiseATicket = async (req, reply) => {
|
|
|
|
|
|
|
|
|
|
|
|
return reply.send({
|
|
|
|
return reply.send({
|
|
|
|
status_code: 200,
|
|
|
|
status_code: 200,
|
|
|
|
message: "Checked connection and updated support if needed.",
|
|
|
|
message: "Checked connection and raised ticket if needed.",
|
|
|
|
master: masterDetails,
|
|
|
|
master: masterDetails,
|
|
|
|
connected_slaves: formattedSlaves
|
|
|
|
connected_slaves: formattedSlaves
|
|
|
|
});
|
|
|
|
});
|
|
|
@ -2841,6 +2796,7 @@ exports.raiseATicket = async (req, reply) => {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// exports.raiseATicket = async (req, reply) => {
|
|
|
|
// exports.raiseATicket = async (req, reply) => {
|
|
|
|
// try {
|
|
|
|
// try {
|
|
|
|
// const { customerId, connected_to } = req.params;
|
|
|
|
// const { customerId, connected_to } = req.params;
|
|
|
|