|
|
|
@ -2845,97 +2845,118 @@ exports.getIotDataByCustomerAndHardwareId = async (req, reply) => {
|
|
|
|
|
//const moment = require("moment-timezone");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
exports.raiseATicket = async (req, reply) => {
|
|
|
|
|
try {
|
|
|
|
|
const { customerId, connected_to } = req.params;
|
|
|
|
|
// exports.raiseATicket = async (req, reply) => {
|
|
|
|
|
// try {
|
|
|
|
|
// const { customerId, connected_to } = req.params;
|
|
|
|
|
|
|
|
|
|
if (!customerId || !connected_to) {
|
|
|
|
|
return reply.code(400).send({ error: "customerId and connected_to are required" });
|
|
|
|
|
}
|
|
|
|
|
// if (!customerId || !connected_to) {
|
|
|
|
|
// return reply.code(400).send({ error: "customerId and connected_to are required" });
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
const sensors = await Insensors.find({ customerId }).lean();
|
|
|
|
|
const orders = await Order.find({ customerId }).lean();
|
|
|
|
|
// const sensors = await Insensors.find({ customerId }).lean();
|
|
|
|
|
// const orders = await Order.find({ customerId }).lean();
|
|
|
|
|
|
|
|
|
|
if (!sensors.length) {
|
|
|
|
|
return reply.code(404).send({ message: "No sensors found for this customer." });
|
|
|
|
|
}
|
|
|
|
|
// if (!sensors.length) {
|
|
|
|
|
// return reply.code(404).send({ message: "No sensors found for this customer." });
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
const masterSensor = sensors.find(s => s.hardwareId?.trim() === connected_to.trim() && s.type === "master");
|
|
|
|
|
if (!masterSensor) {
|
|
|
|
|
return reply.code(404).send({ message: "Master hardwareId not found." });
|
|
|
|
|
}
|
|
|
|
|
// const masterSensor = sensors.find(s => s.hardwareId?.trim() === connected_to.trim() && s.type === "master");
|
|
|
|
|
// if (!masterSensor) {
|
|
|
|
|
// return reply.code(404).send({ message: "Master hardwareId not found." });
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
const orderMap = {};
|
|
|
|
|
orders.forEach(order => {
|
|
|
|
|
order.master_connections.forEach(conn => {
|
|
|
|
|
orderMap[conn.hardwareId] = {
|
|
|
|
|
masterName: conn.master_name || null,
|
|
|
|
|
location: conn.location || null
|
|
|
|
|
};
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
// const orderMap = {};
|
|
|
|
|
// orders.forEach(order => {
|
|
|
|
|
// order.master_connections.forEach(conn => {
|
|
|
|
|
// orderMap[conn.hardwareId] = {
|
|
|
|
|
// masterName: conn.master_name || null,
|
|
|
|
|
// location: conn.location || null
|
|
|
|
|
// };
|
|
|
|
|
// });
|
|
|
|
|
// });
|
|
|
|
|
|
|
|
|
|
const now = moment.tz("Asia/Kolkata");
|
|
|
|
|
// const now = moment.tz("Asia/Kolkata");
|
|
|
|
|
|
|
|
|
|
function getDisconnectDuration(timeStr) {
|
|
|
|
|
if (!timeStr) return null;
|
|
|
|
|
const time = moment.tz(timeStr, "DD-MM-YYYY HH:mm:ss", "Asia/Kolkata");
|
|
|
|
|
return time.isValid() ? now.diff(time, "minutes") : null;
|
|
|
|
|
}
|
|
|
|
|
// function getDisconnectDuration(timeStr) {
|
|
|
|
|
// if (!timeStr) return null;
|
|
|
|
|
// const time = moment.tz(timeStr, "DD-MM-YYYY HH:mm:ss", "Asia/Kolkata");
|
|
|
|
|
// return time.isValid() ? now.diff(time, "minutes") : null;
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
const latestMasterRecord = await IotData.findOne({ hardwareId: connected_to }).sort({ date: -1 }).lean();
|
|
|
|
|
let masterConnectedStatus = "disconnected";
|
|
|
|
|
let lastDataTime = "No data";
|
|
|
|
|
let diffInMinutes = null;
|
|
|
|
|
// const latestMasterRecord = await IotData.findOne({ hardwareId: connected_to }).sort({ date: -1 }).lean();
|
|
|
|
|
// let masterConnectedStatus = "disconnected";
|
|
|
|
|
// let lastDataTime = "No data";
|
|
|
|
|
// let diffInMinutes = null;
|
|
|
|
|
|
|
|
|
|
if (latestMasterRecord?.date) {
|
|
|
|
|
const indiaTime = moment.tz(latestMasterRecord.date, "Asia/Kolkata");
|
|
|
|
|
diffInMinutes = now.diff(indiaTime, "minutes");
|
|
|
|
|
lastDataTime = indiaTime.format("DD-MM-YYYY HH:mm:ss");
|
|
|
|
|
if (diffInMinutes <= 1) masterConnectedStatus = "connected";
|
|
|
|
|
}
|
|
|
|
|
// if (latestMasterRecord?.date) {
|
|
|
|
|
// const indiaTime = moment.tz(latestMasterRecord.date, "Asia/Kolkata");
|
|
|
|
|
// diffInMinutes = now.diff(indiaTime, "minutes");
|
|
|
|
|
// lastDataTime = indiaTime.format("DD-MM-YYYY HH:mm:ss");
|
|
|
|
|
// if (diffInMinutes <= 1) masterConnectedStatus = "connected";
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
const connectedSlaves = sensors.filter(
|
|
|
|
|
s => s.connected_to?.trim() === connected_to.trim() && s.type === "slave"
|
|
|
|
|
);
|
|
|
|
|
// const connectedSlaves = sensors.filter(
|
|
|
|
|
// s => s.connected_to?.trim() === connected_to.trim() && s.type === "slave"
|
|
|
|
|
// );
|
|
|
|
|
|
|
|
|
|
const formattedSlaves = [];
|
|
|
|
|
const disconnectedSlaves = [];
|
|
|
|
|
// const formattedSlaves = [];
|
|
|
|
|
// const disconnectedSlaves = [];
|
|
|
|
|
|
|
|
|
|
for (const slave of connectedSlaves) {
|
|
|
|
|
const slaveData = latestMasterRecord?.tanks.find(t => t.tankhardwareId === slave.tankhardwareId);
|
|
|
|
|
let slaveStatus = "disconnected";
|
|
|
|
|
let lastSlaveDataTime = "No data";
|
|
|
|
|
let slaveDiff = null;
|
|
|
|
|
|
|
|
|
|
if (slaveData?.date) {
|
|
|
|
|
const slaveTime = moment.tz(slaveData.date, "Asia/Kolkata");
|
|
|
|
|
slaveDiff = now.diff(slaveTime, "minutes");
|
|
|
|
|
lastSlaveDataTime = slaveTime.format("DD-MM-YYYY HH:mm:ss");
|
|
|
|
|
if (slaveDiff <= 1) slaveStatus = "connected";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (slaveStatus === "disconnected") {
|
|
|
|
|
disconnectedSlaves.push({
|
|
|
|
|
slaveHardwareId: slave.tankhardwareId,
|
|
|
|
|
slaveName: slave.tankName || "Unknown Slave"
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
// for (const slave of connectedSlaves) {
|
|
|
|
|
// const slaveData = latestMasterRecord?.tanks.find(t => t.tankhardwareId === slave.tankhardwareId);
|
|
|
|
|
// let slaveStatus = "disconnected";
|
|
|
|
|
// let lastSlaveDataTime = "No data";
|
|
|
|
|
// let slaveDiff = null;
|
|
|
|
|
|
|
|
|
|
// if (slaveData?.date) {
|
|
|
|
|
// const slaveTime = moment.tz(slaveData.date, "Asia/Kolkata");
|
|
|
|
|
// slaveDiff = now.diff(slaveTime, "minutes");
|
|
|
|
|
// lastSlaveDataTime = slaveTime.format("DD-MM-YYYY HH:mm:ss");
|
|
|
|
|
// if (slaveDiff <= 1) slaveStatus = "connected";
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
// if (slaveStatus === "disconnected") {
|
|
|
|
|
// disconnectedSlaves.push({
|
|
|
|
|
// slaveHardwareId: slave.tankhardwareId,
|
|
|
|
|
// slaveName: slave.tankName || "Unknown Slave"
|
|
|
|
|
// });
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
formattedSlaves.push({
|
|
|
|
|
hardwareId: slave.hardwareId,
|
|
|
|
|
slaveName: slave.tankName || null,
|
|
|
|
|
location: slave.tankLocation || null,
|
|
|
|
|
type: "slave",
|
|
|
|
|
connected_status: slaveStatus,
|
|
|
|
|
last_data_time: lastSlaveDataTime,
|
|
|
|
|
diff_in_minutes: slaveDiff
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
// formattedSlaves.push({
|
|
|
|
|
// hardwareId: slave.hardwareId,
|
|
|
|
|
// slaveName: slave.tankName || null,
|
|
|
|
|
// location: slave.tankLocation || null,
|
|
|
|
|
// type: "slave",
|
|
|
|
|
// connected_status: slaveStatus,
|
|
|
|
|
// last_data_time: lastSlaveDataTime,
|
|
|
|
|
// diff_in_minutes: slaveDiff
|
|
|
|
|
// });
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
const issuesToAdd = [];
|
|
|
|
|
// const issuesToAdd = [];
|
|
|
|
|
|
|
|
|
|
// // ✅ Only raise a ticket if master is disconnected
|
|
|
|
|
// // if (masterConnectedStatus === "disconnected") {
|
|
|
|
|
// // const existingMasterTicket = await Support.findOne({
|
|
|
|
|
// // "issues.hardwareId": connected_to,
|
|
|
|
|
// // "issues.type": "GSM or LoRa Disconnected"
|
|
|
|
|
// // });
|
|
|
|
|
|
|
|
|
|
// // if (!existingMasterTicket) {
|
|
|
|
|
// // const slaveHardwareIds = disconnectedSlaves.map(s => s.slaveHardwareId);
|
|
|
|
|
// // const slaveNames = disconnectedSlaves.map(s => s.slaveName);
|
|
|
|
|
|
|
|
|
|
// // issuesToAdd.push({
|
|
|
|
|
// // type: "GSM or LoRa Disconnected",
|
|
|
|
|
// // masterHardwareId: connected_to,
|
|
|
|
|
// // hardwareId: connected_to, // Master hardwareId
|
|
|
|
|
// // hardwareIds: slaveHardwareIds, // Slave tankHardwareIds
|
|
|
|
|
// // slaveNames,
|
|
|
|
|
// // message: `Master ${connected_to} is disconnected along with ${slaveHardwareIds.length} slave(s)`
|
|
|
|
|
// // });
|
|
|
|
|
// // }
|
|
|
|
|
// // }
|
|
|
|
|
|
|
|
|
|
// ✅ Only raise a ticket if master is disconnected
|
|
|
|
|
// if (masterConnectedStatus === "disconnected") {
|
|
|
|
|
// const existingMasterTicket = await Support.findOne({
|
|
|
|
|
// "issues.hardwareId": connected_to,
|
|
|
|
@ -2946,235 +2967,214 @@ exports.raiseATicket = async (req, reply) => {
|
|
|
|
|
// const slaveHardwareIds = disconnectedSlaves.map(s => s.slaveHardwareId);
|
|
|
|
|
// const slaveNames = disconnectedSlaves.map(s => s.slaveName);
|
|
|
|
|
|
|
|
|
|
// // Check if disconnection is at least 15 minutes old
|
|
|
|
|
// if (diffInMinutes >= 15) {
|
|
|
|
|
// issuesToAdd.push({
|
|
|
|
|
// type: "GSM or LoRa Disconnected",
|
|
|
|
|
// masterHardwareId: connected_to,
|
|
|
|
|
// hardwareId: connected_to, // Master hardwareId
|
|
|
|
|
// hardwareIds: slaveHardwareIds, // Slave tankHardwareIds
|
|
|
|
|
// hardwareId: connected_to,
|
|
|
|
|
// hardwareIds: slaveHardwareIds,
|
|
|
|
|
// slaveNames,
|
|
|
|
|
// message: `Master ${connected_to} is disconnected along with ${slaveHardwareIds.length} slave(s)`
|
|
|
|
|
// message: `Master ${connected_to} is disconnected along with ${slaveHardwareIds.length} slave(s)`,
|
|
|
|
|
// disconnectedAt: lastDataTime // optional: for future tracking
|
|
|
|
|
// });
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
if (masterConnectedStatus === "disconnected") {
|
|
|
|
|
const existingMasterTicket = await Support.findOne({
|
|
|
|
|
"issues.hardwareId": connected_to,
|
|
|
|
|
"issues.type": "GSM or LoRa Disconnected"
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (!existingMasterTicket) {
|
|
|
|
|
const slaveHardwareIds = disconnectedSlaves.map(s => s.slaveHardwareId);
|
|
|
|
|
const slaveNames = disconnectedSlaves.map(s => s.slaveName);
|
|
|
|
|
|
|
|
|
|
// Check if disconnection is at least 15 minutes old
|
|
|
|
|
if (diffInMinutes >= 15) {
|
|
|
|
|
issuesToAdd.push({
|
|
|
|
|
type: "GSM or LoRa Disconnected",
|
|
|
|
|
masterHardwareId: connected_to,
|
|
|
|
|
hardwareId: connected_to,
|
|
|
|
|
hardwareIds: slaveHardwareIds,
|
|
|
|
|
slaveNames,
|
|
|
|
|
message: `Master ${connected_to} is disconnected along with ${slaveHardwareIds.length} slave(s)`,
|
|
|
|
|
disconnectedAt: lastDataTime // optional: for future tracking
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// if (issuesToAdd.length > 0) {
|
|
|
|
|
// const supportRecord = await Support.findOne({ supportId: "AWHYSU64" });
|
|
|
|
|
|
|
|
|
|
if (issuesToAdd.length > 0) {
|
|
|
|
|
const supportRecord = await Support.findOne({ supportId: "AWHYSU64" });
|
|
|
|
|
// if (supportRecord) {
|
|
|
|
|
// await Support.findOneAndUpdate(
|
|
|
|
|
// { supportId: "AWHYSU64" },
|
|
|
|
|
// {
|
|
|
|
|
// $push: { issues: { $each: issuesToAdd } },
|
|
|
|
|
// $set: { updatedAt: new Date(), lastTicketRaisedAt: moment().tz("Asia/Kolkata").format("YYYY-MM-DD HH:mm:ss") }
|
|
|
|
|
// }
|
|
|
|
|
// );
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
if (supportRecord) {
|
|
|
|
|
await Support.findOneAndUpdate(
|
|
|
|
|
{ supportId: "AWHYSU64" },
|
|
|
|
|
{
|
|
|
|
|
$push: { issues: { $each: issuesToAdd } },
|
|
|
|
|
$set: { updatedAt: new Date(), lastTicketRaisedAt: moment().tz("Asia/Kolkata").format("YYYY-MM-DD HH:mm:ss") }
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// const masterDetails = {
|
|
|
|
|
// hardwareId: connected_to,
|
|
|
|
|
// masterName: masterSensor.masterName || orderMap[connected_to]?.masterName || null,
|
|
|
|
|
// location: masterSensor.location || orderMap[connected_to]?.location || null,
|
|
|
|
|
// type: "master",
|
|
|
|
|
// connected_status: masterConnectedStatus,
|
|
|
|
|
// last_data_time: lastDataTime,
|
|
|
|
|
// diff_in_minutes: diffInMinutes
|
|
|
|
|
// };
|
|
|
|
|
|
|
|
|
|
const masterDetails = {
|
|
|
|
|
hardwareId: connected_to,
|
|
|
|
|
masterName: masterSensor.masterName || orderMap[connected_to]?.masterName || null,
|
|
|
|
|
location: masterSensor.location || orderMap[connected_to]?.location || null,
|
|
|
|
|
type: "master",
|
|
|
|
|
connected_status: masterConnectedStatus,
|
|
|
|
|
last_data_time: lastDataTime,
|
|
|
|
|
diff_in_minutes: diffInMinutes
|
|
|
|
|
};
|
|
|
|
|
// return reply.send({
|
|
|
|
|
// status_code: 200,
|
|
|
|
|
// message: "Checked connection and raised ticket if needed.",
|
|
|
|
|
// master: masterDetails,
|
|
|
|
|
// connected_slaves: formattedSlaves
|
|
|
|
|
// });
|
|
|
|
|
|
|
|
|
|
return reply.send({
|
|
|
|
|
status_code: 200,
|
|
|
|
|
message: "Checked connection and raised ticket if needed.",
|
|
|
|
|
master: masterDetails,
|
|
|
|
|
connected_slaves: formattedSlaves
|
|
|
|
|
});
|
|
|
|
|
// } catch (error) {
|
|
|
|
|
// console.error("Error raising ticket:", error);
|
|
|
|
|
// return reply.code(500).send({ error: "Internal server error" });
|
|
|
|
|
// }
|
|
|
|
|
// };
|
|
|
|
|
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error("Error raising ticket:", error);
|
|
|
|
|
return reply.code(500).send({ error: "Internal server error" });
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
exports.raiseATicket = async (req, reply) => {
|
|
|
|
|
try {
|
|
|
|
|
const { customerId, connected_to } = req.params;
|
|
|
|
|
|
|
|
|
|
// exports.raiseATicket = async (req, reply) => {
|
|
|
|
|
// try {
|
|
|
|
|
// const { customerId, connected_to } = req.params;
|
|
|
|
|
if (!customerId || !connected_to) {
|
|
|
|
|
return reply.code(400).send({ error: "customerId and connected_to are required" });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// if (!customerId || !connected_to) {
|
|
|
|
|
// return reply.code(400).send({ error: "customerId and connected_to are required" });
|
|
|
|
|
// }
|
|
|
|
|
const sensors = await Insensors.find({ customerId });
|
|
|
|
|
|
|
|
|
|
// const sensors = await Insensors.find({ customerId });
|
|
|
|
|
if (!sensors.length) {
|
|
|
|
|
return reply.code(404).send({ message: "No sensors found for this customer." });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// if (!sensors.length) {
|
|
|
|
|
// return reply.code(404).send({ message: "No sensors found for this customer." });
|
|
|
|
|
// }
|
|
|
|
|
const masterSensor = sensors.find(s => s.hardwareId?.trim() === connected_to.trim() && s.type === "master");
|
|
|
|
|
|
|
|
|
|
// const masterSensor = sensors.find(s => s.hardwareId?.trim() === connected_to.trim() && s.type === "master");
|
|
|
|
|
if (!masterSensor) {
|
|
|
|
|
return reply.code(404).send({ message: "Master hardwareId not found." });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// if (!masterSensor) {
|
|
|
|
|
// return reply.code(404).send({ message: "Master hardwareId not found." });
|
|
|
|
|
// }
|
|
|
|
|
const latestMasterRecord = await IotData.findOne({ hardwareId: connected_to }).sort({ date: -1 }).lean();
|
|
|
|
|
|
|
|
|
|
// const latestMasterRecord = await IotData.findOne({ hardwareId: connected_to }).sort({ date: -1 }).lean();
|
|
|
|
|
if (!latestMasterRecord) {
|
|
|
|
|
return reply.code(404).send({ message: "No IoT data found for this hardwareId." });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// if (!latestMasterRecord) {
|
|
|
|
|
// return reply.code(404).send({ message: "No IoT data found for this hardwareId." });
|
|
|
|
|
// }
|
|
|
|
|
const now = moment.tz("Asia/Kolkata");
|
|
|
|
|
const indiaTime = moment.tz(latestMasterRecord.date, "Asia/Kolkata");
|
|
|
|
|
const diffInMinutesMaster = now.diff(indiaTime, "minutes");
|
|
|
|
|
|
|
|
|
|
// const now = moment.tz("Asia/Kolkata");
|
|
|
|
|
// const indiaTime = moment.tz(latestMasterRecord.date, "Asia/Kolkata");
|
|
|
|
|
// const diffInMinutesMaster = now.diff(indiaTime, "minutes");
|
|
|
|
|
const masterDisconnected = diffInMinutesMaster > 1 ? [{
|
|
|
|
|
hardwareId: connected_to,
|
|
|
|
|
masterName: masterSensor.tankName || "Unknown Master",
|
|
|
|
|
connected_status: "disconnected",
|
|
|
|
|
last_seen_minutes_ago: diffInMinutesMaster
|
|
|
|
|
}] : [];
|
|
|
|
|
|
|
|
|
|
// const masterDisconnected = diffInMinutesMaster > 1 ? [{
|
|
|
|
|
// hardwareId: connected_to,
|
|
|
|
|
// masterName: masterSensor.tankName || "Unknown Master",
|
|
|
|
|
// connected_status: "disconnected",
|
|
|
|
|
// last_seen_minutes_ago: diffInMinutesMaster
|
|
|
|
|
// }] : [];
|
|
|
|
|
|
|
|
|
|
// const connectedSlaves = sensors.filter(sensor =>
|
|
|
|
|
// sensor.connected_to?.trim() === connected_to.trim() &&
|
|
|
|
|
// sensor.type === "slave"
|
|
|
|
|
// );
|
|
|
|
|
const connectedSlaves = sensors.filter(sensor =>
|
|
|
|
|
sensor.connected_to?.trim() === connected_to.trim() &&
|
|
|
|
|
sensor.type === "slave"
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// const disconnectedSlaves = [];
|
|
|
|
|
// const slaveStatusList = [];
|
|
|
|
|
const disconnectedSlaves = [];
|
|
|
|
|
const slaveStatusList = [];
|
|
|
|
|
|
|
|
|
|
// for (const slave of connectedSlaves) {
|
|
|
|
|
// const slaveId = slave.hardwareId?.trim();
|
|
|
|
|
// const matchedTank = latestMasterRecord.tanks.find(tank => tank.tankhardwareId === slaveId);
|
|
|
|
|
for (const slave of connectedSlaves) {
|
|
|
|
|
const slaveId = slave.tankhardwareId?.trim();
|
|
|
|
|
const matchedTank = latestMasterRecord.tanks.find(tank => tank.tankhardwareId === slaveId);
|
|
|
|
|
|
|
|
|
|
// let isDisconnected = true;
|
|
|
|
|
// let loraDiffInMinutes = null;
|
|
|
|
|
let isDisconnected = true;
|
|
|
|
|
let loraDiffInMinutes = null;
|
|
|
|
|
|
|
|
|
|
// if (matchedTank && matchedTank.date) {
|
|
|
|
|
// const tankTime = moment.tz(matchedTank.date, "Asia/Kolkata");
|
|
|
|
|
// loraDiffInMinutes = now.diff(tankTime, "minutes");
|
|
|
|
|
if (matchedTank && matchedTank.date) {
|
|
|
|
|
const tankTime = moment.tz(matchedTank.date, "Asia/Kolkata");
|
|
|
|
|
loraDiffInMinutes = now.diff(tankTime, "minutes");
|
|
|
|
|
|
|
|
|
|
// if (loraDiffInMinutes <= 1) {
|
|
|
|
|
// isDisconnected = false;
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
if (loraDiffInMinutes <= 1) {
|
|
|
|
|
isDisconnected = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// const slaveInfo = {
|
|
|
|
|
// hardwareId: connected_to,
|
|
|
|
|
// slaveHardwareId: slaveId,
|
|
|
|
|
// slaveName: slave.tankName || "Unknown Slave",
|
|
|
|
|
// connected_status: isDisconnected ? "disconnected" : "connected",
|
|
|
|
|
// last_seen_minutes_ago: loraDiffInMinutes
|
|
|
|
|
// };
|
|
|
|
|
const slaveInfo = {
|
|
|
|
|
hardwareId: connected_to,
|
|
|
|
|
slaveHardwareId: slaveId,
|
|
|
|
|
slaveName: slave.tankName || "Unknown Slave",
|
|
|
|
|
connected_status: isDisconnected ? "disconnected" : "connected",
|
|
|
|
|
last_seen_minutes_ago: loraDiffInMinutes
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// slaveStatusList.push(slaveInfo);
|
|
|
|
|
slaveStatusList.push(slaveInfo);
|
|
|
|
|
|
|
|
|
|
// if (isDisconnected) {
|
|
|
|
|
// disconnectedSlaves.push(slaveInfo);
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
if (isDisconnected) {
|
|
|
|
|
disconnectedSlaves.push(slaveInfo);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// const issuesToAdd = [];
|
|
|
|
|
const issuesToAdd = [];
|
|
|
|
|
|
|
|
|
|
// if (masterDisconnected.length > 0) {
|
|
|
|
|
// const existingGsmIssue = await Support.findOne({
|
|
|
|
|
// "issues.hardwareId": connected_to,
|
|
|
|
|
// "issues.type": "GSM Disconnected"
|
|
|
|
|
// });
|
|
|
|
|
if (masterDisconnected.length > 0) {
|
|
|
|
|
const existingGsmIssue = await Support.findOne({
|
|
|
|
|
"issues.hardwareId": connected_to,
|
|
|
|
|
"issues.type": "GSM Disconnected"
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// if (!existingGsmIssue) {
|
|
|
|
|
// issuesToAdd.push({
|
|
|
|
|
// type: "GSM Disconnected",
|
|
|
|
|
// hardwareId: connected_to,
|
|
|
|
|
// message: `Master GSM disconnected - ${connected_to}`
|
|
|
|
|
// });
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
if (!existingGsmIssue) {
|
|
|
|
|
issuesToAdd.push({
|
|
|
|
|
type: "GSM Disconnected",
|
|
|
|
|
hardwareId: connected_to,
|
|
|
|
|
message: `Master GSM disconnected - ${connected_to}`
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// const newHardwareIds = [];
|
|
|
|
|
// const newSlaveNames = [];
|
|
|
|
|
const newHardwareIds = [];
|
|
|
|
|
const newSlaveNames = [];
|
|
|
|
|
|
|
|
|
|
// for (const slave of disconnectedSlaves) {
|
|
|
|
|
// const existingSlaveIssue = await Support.findOne({
|
|
|
|
|
// "issues.hardwareIds": slave.slaveHardwareId,
|
|
|
|
|
// "issues.masterHardwareId": connected_to,
|
|
|
|
|
// "issues.type": "LoRa Disconnected"
|
|
|
|
|
// });
|
|
|
|
|
for (const slave of disconnectedSlaves) {
|
|
|
|
|
const existingSlaveIssue = await Support.findOne({
|
|
|
|
|
"issues.hardwareIds": slave.slaveHardwareId,
|
|
|
|
|
"issues.masterHardwareId": connected_to,
|
|
|
|
|
"issues.type": "LoRa Disconnected"
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// if (!existingSlaveIssue) {
|
|
|
|
|
// newHardwareIds.push(slave.slaveHardwareId);
|
|
|
|
|
// newSlaveNames.push(slave.slaveName);
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
if (!existingSlaveIssue) {
|
|
|
|
|
newHardwareIds.push(slave.slaveHardwareId);
|
|
|
|
|
newSlaveNames.push(slave.slaveName);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// if (newHardwareIds.length > 0) {
|
|
|
|
|
// issuesToAdd.push({
|
|
|
|
|
// type: "LoRa Disconnected",
|
|
|
|
|
// masterHardwareId: connected_to,
|
|
|
|
|
// hardwareIds: newHardwareIds,
|
|
|
|
|
// slaveNames: newSlaveNames,
|
|
|
|
|
// message: `Slaves LoRa disconnected under master ${connected_to}`
|
|
|
|
|
// });
|
|
|
|
|
// }
|
|
|
|
|
if (newHardwareIds.length > 0) {
|
|
|
|
|
issuesToAdd.push({
|
|
|
|
|
type: "LoRa Disconnected",
|
|
|
|
|
masterHardwareId: connected_to,
|
|
|
|
|
hardwareIds: newHardwareIds,
|
|
|
|
|
slaveNames: newSlaveNames,
|
|
|
|
|
message: `Slaves LoRa disconnected under master ${connected_to}`
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// if (issuesToAdd.length > 0) {
|
|
|
|
|
// const supportRecord = await Support.findOne({ supportId: "AWHYSU64" });
|
|
|
|
|
if (issuesToAdd.length > 0) {
|
|
|
|
|
const supportRecord = await Support.findOne({ supportId: "AWHYSU64" });
|
|
|
|
|
|
|
|
|
|
// if (supportRecord) {
|
|
|
|
|
// await Support.findOneAndUpdate(
|
|
|
|
|
// { _id: supportRecord._id },
|
|
|
|
|
// {
|
|
|
|
|
// $push: {
|
|
|
|
|
// issues: { $each: issuesToAdd }
|
|
|
|
|
// },
|
|
|
|
|
// updatedAt: new Date()
|
|
|
|
|
// },
|
|
|
|
|
// { new: true }
|
|
|
|
|
// );
|
|
|
|
|
// } else {
|
|
|
|
|
// console.error("Support record not found for supportId AWHYSU64");
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
if (supportRecord) {
|
|
|
|
|
await Support.findOneAndUpdate(
|
|
|
|
|
{ _id: supportRecord._id },
|
|
|
|
|
{
|
|
|
|
|
$push: {
|
|
|
|
|
issues: { $each: issuesToAdd }
|
|
|
|
|
},
|
|
|
|
|
updatedAt: new Date()
|
|
|
|
|
},
|
|
|
|
|
{ new: true }
|
|
|
|
|
);
|
|
|
|
|
} else {
|
|
|
|
|
console.error("Support record not found for supportId AWHYSU64");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// return reply.send({
|
|
|
|
|
// status_code: 200,
|
|
|
|
|
// message: "Checked connection and updated support if needed.",
|
|
|
|
|
// masterDisconnected,
|
|
|
|
|
// disconnectedSlaves,
|
|
|
|
|
// connectedSlaves: slaveStatusList
|
|
|
|
|
// });
|
|
|
|
|
return reply.send({
|
|
|
|
|
status_code: 200,
|
|
|
|
|
message: "Checked connection and updated support if needed.",
|
|
|
|
|
masterDisconnected,
|
|
|
|
|
disconnectedSlaves,
|
|
|
|
|
connectedSlaves: slaveStatusList
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// } catch (error) {
|
|
|
|
|
// console.error("Error raising ticket:", error);
|
|
|
|
|
// return reply.code(500).send({ error: "Internal server error" });
|
|
|
|
|
// }
|
|
|
|
|
// };
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error("Error raising ticket:", error);
|
|
|
|
|
return reply.code(500).send({ error: "Internal server error" });
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|