|
|
|
@ -3983,6 +3983,61 @@ const cron = require("node-cron");
|
|
|
|
|
// };
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Your existing raiseATicketLikeLogic function must be accessible
|
|
|
|
|
|
|
|
|
|
// ⬇️ Include the function here or import it if it's in another file
|
|
|
|
|
const updateConnectedStatusOnly = async (customerId, hardwareId) => {
|
|
|
|
|
try {
|
|
|
|
|
const sensors = await Insensors.find({ customerId });
|
|
|
|
|
if (!sensors.length) return;
|
|
|
|
|
|
|
|
|
|
const now = moment.tz("Asia/Kolkata");
|
|
|
|
|
const latestRecord = await IotData.findOne({ hardwareId }).sort({ date: -1 }).lean();
|
|
|
|
|
if (!latestRecord) return;
|
|
|
|
|
|
|
|
|
|
const gsmTime = moment.tz(latestRecord.date, "Asia/Kolkata");
|
|
|
|
|
const gsmDiff = now.diff(gsmTime, "minutes");
|
|
|
|
|
const gsmConnected = gsmDiff <= 1;
|
|
|
|
|
|
|
|
|
|
const connectedSlaves = sensors.filter(s => s.connected_to?.trim() === hardwareId);
|
|
|
|
|
const tankMap = {};
|
|
|
|
|
(latestRecord.tanks || []).forEach(t => {
|
|
|
|
|
if (t.tankhardwareId) {
|
|
|
|
|
tankMap[t.tankhardwareId.trim()] = t;
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const allSlavesConnected = connectedSlaves.every(slave => {
|
|
|
|
|
const slaveId = slave.tankhardwareId?.trim();
|
|
|
|
|
const tank = tankMap[slaveId];
|
|
|
|
|
if (!tank || !tank.date || tank.tankHeight === "0") return false;
|
|
|
|
|
const loraTime = moment.tz(tank.date, "Asia/Kolkata");
|
|
|
|
|
return now.diff(loraTime, "minutes") <= 1;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const masterStatus = gsmConnected && allSlavesConnected ? "connected" : "disconnected";
|
|
|
|
|
await Insensors.updateOne({ hardwareId, customerId }, { $set: { connected_status: masterStatus } });
|
|
|
|
|
|
|
|
|
|
for (const slave of connectedSlaves) {
|
|
|
|
|
const slaveId = slave.tankhardwareId?.trim();
|
|
|
|
|
const tank = tankMap[slaveId];
|
|
|
|
|
|
|
|
|
|
let status = "disconnected";
|
|
|
|
|
if (tank && tank.date && tank.tankHeight !== "0") {
|
|
|
|
|
const loraTime = moment.tz(tank.date, "Asia/Kolkata");
|
|
|
|
|
if (now.diff(loraTime, "minutes") <= 1) status = "connected";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
await Insensors.updateOne({ hardwareId: slave.hardwareId }, { $set: { connected_status: status } });
|
|
|
|
|
}
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error("❌ updateConnectedStatusOnly error:", error);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const raiseATicketLikeLogic = async (supportRecord, masterHardwareId, slaveData = []) => {
|
|
|
|
|
const now = new Date();
|
|
|
|
|
const formattedNow = new Date(now.getTime() + 19800000) // +05:30 offset in ms
|
|
|
|
@ -4013,7 +4068,7 @@ const raiseATicketLikeLogic = async (supportRecord, masterHardwareId, slaveData
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const slaveHardwareIds = slaveData.map((slave) => slave.hardwareId);
|
|
|
|
|
const slaveHardwareIds = slaveData.map((slave) => slave.tankhardwareId);
|
|
|
|
|
const slaveNames = slaveData.map((slave) => slave.sensorName || slave.tankName || "");
|
|
|
|
|
|
|
|
|
|
const newIssue = {
|
|
|
|
@ -4260,7 +4315,7 @@ cron.schedule("*/1 * * * *", async () => {
|
|
|
|
|
const supportId = supportRecord.supportId;
|
|
|
|
|
if (!supportId) continue;
|
|
|
|
|
|
|
|
|
|
// Step 2: Find all master sensors (you can filter by zone, supportId etc. if required)
|
|
|
|
|
// Step 2: Find all master sensors
|
|
|
|
|
const allMasters = await Insensors.find({ type: "master" }).lean();
|
|
|
|
|
|
|
|
|
|
for (const master of allMasters) {
|
|
|
|
@ -4269,20 +4324,23 @@ cron.schedule("*/1 * * * *", async () => {
|
|
|
|
|
|
|
|
|
|
if (!customerId || !hardwareId) continue;
|
|
|
|
|
|
|
|
|
|
// Step 3: Find connected slaves for this master
|
|
|
|
|
// ✅ Update GSM and LoRa connection statuses
|
|
|
|
|
await updateConnectedStatusOnly(customerId, hardwareId);
|
|
|
|
|
|
|
|
|
|
// 🔄 Re-fetch updated master and slaves
|
|
|
|
|
const updatedMaster = await Insensors.findOne({ hardwareId, customerId }).lean();
|
|
|
|
|
const connectedSlaves = await Insensors.find({
|
|
|
|
|
connected_to: hardwareId,
|
|
|
|
|
type: "slave"
|
|
|
|
|
}).lean();
|
|
|
|
|
|
|
|
|
|
// Step 4: Check if master or any slave is disconnected
|
|
|
|
|
// Step 3: Check disconnections
|
|
|
|
|
const disconnectedSlaves = connectedSlaves.filter(
|
|
|
|
|
(s) => s.connected_status === "disconnected"
|
|
|
|
|
);
|
|
|
|
|
const masterIsDisconnected = updatedMaster.connected_status === "disconnected";
|
|
|
|
|
|
|
|
|
|
const masterIsDisconnected = master.connected_status === "disconnected";
|
|
|
|
|
|
|
|
|
|
// Step 5: Raise a ticket if needed
|
|
|
|
|
// Step 4: Raise ticket if needed
|
|
|
|
|
if (masterIsDisconnected || disconnectedSlaves.length > 0) {
|
|
|
|
|
await raiseATicketLikeLogic(supportRecord, hardwareId, disconnectedSlaves);
|
|
|
|
|
}
|
|
|
|
@ -4296,6 +4354,7 @@ cron.schedule("*/1 * * * *", async () => {
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
exports.raiseATicketBuildingDetails = async (req, reply) => {
|
|
|
|
|
try {
|
|
|
|
|
const { customerId, connected_to, installationId } = req.params;
|
|
|
|
|