|
|
@ -3,7 +3,7 @@ const bcrypt = require('bcrypt');
|
|
|
|
const jwt = require('jsonwebtoken');
|
|
|
|
const jwt = require('jsonwebtoken');
|
|
|
|
const customJwtAuth = require("../customAuthJwt");
|
|
|
|
const customJwtAuth = require("../customAuthJwt");
|
|
|
|
const { Deparments } = require("../models/Department");
|
|
|
|
const { Deparments } = require("../models/Department");
|
|
|
|
const { Install, SensorStock, SensorQuotation, Order, Insensors, MasterSlaveData, ElectrictyWorkPictures, PlumbingWorkPictures, MaterialRecievedPictures } = require("../models/store");
|
|
|
|
const { Install, SensorStock, SensorQuotation, Order, Insensors, MasterSlaveData, ElectrictyWorkPictures, PlumbingWorkPictures, MaterialRecievedPictures, Support } = require("../models/store");
|
|
|
|
const { Counter, User } = require("../models/User");
|
|
|
|
const { Counter, User } = require("../models/User");
|
|
|
|
const { IotData, Tank } = require("../models/tanks");
|
|
|
|
const { IotData, Tank } = require("../models/tanks");
|
|
|
|
const moment = require('moment-timezone');
|
|
|
|
const moment = require('moment-timezone');
|
|
|
@ -2286,3 +2286,128 @@ exports.getIotDataByCustomerAndHardwareId = async (req, reply) => {
|
|
|
|
return reply.code(500).send({ error: "Internal Server Error" });
|
|
|
|
return reply.code(500).send({ error: "Internal Server Error" });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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" });
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const sensors = await Insensors.find({ customerId });
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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()));
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (!masterSensor) {
|
|
|
|
|
|
|
|
return reply.code(404).send({ message: "Master hardwareId not found." });
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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." });
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Debugging log to check the fetched date and current time
|
|
|
|
|
|
|
|
console.log("Latest Master Record Date:", latestMasterRecord.date);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const indiaTime = moment.tz(latestMasterRecord.date, "Asia/Kolkata");
|
|
|
|
|
|
|
|
const now = moment.tz("Asia/Kolkata");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Log the current time and difference
|
|
|
|
|
|
|
|
console.log("Current Time:", now.format("DD-MM-YYYY HH:mm:ss"));
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const diffInMinutesMaster = now.diff(indiaTime, "minutes");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Log the time difference
|
|
|
|
|
|
|
|
console.log("Time difference in minutes (Master):", diffInMinutesMaster);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Update masterDisconnected to store details
|
|
|
|
|
|
|
|
const masterDisconnected = diffInMinutesMaster > 1 ? [{
|
|
|
|
|
|
|
|
hardwareId: connected_to,
|
|
|
|
|
|
|
|
masterName: masterSensor.tankName,
|
|
|
|
|
|
|
|
disconnectedAt: moment().tz("Asia/Kolkata").format("DD-MM-YYYY HH:mm:ss"),
|
|
|
|
|
|
|
|
}] : [];
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Log the disconnected status
|
|
|
|
|
|
|
|
console.log("Master Disconnected:", masterDisconnected);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const connectedSlaves = sensors.filter(sensor => sensor.connected_to?.trim() === connected_to.trim());
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const disconnectedSlaves = [];
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for (const slave of connectedSlaves) {
|
|
|
|
|
|
|
|
const slaveId = slave.hardwareId?.trim();
|
|
|
|
|
|
|
|
const matchedTank = latestMasterRecord.tanks.find(tank => tank.tankhardwareId === slaveId);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (matchedTank && matchedTank.date) {
|
|
|
|
|
|
|
|
const tankTime = moment.tz(matchedTank.date, "Asia/Kolkata");
|
|
|
|
|
|
|
|
const loraDiffInMinutes = now.diff(tankTime, "minutes");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (loraDiffInMinutes > 1) {
|
|
|
|
|
|
|
|
disconnectedSlaves.push({
|
|
|
|
|
|
|
|
slaveHardwareId: slaveId,
|
|
|
|
|
|
|
|
slaveName: slave.tankName
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const issuesToAdd = [];
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (masterDisconnected.length > 0) {
|
|
|
|
|
|
|
|
issuesToAdd.push({
|
|
|
|
|
|
|
|
type: "GSM Disconnected",
|
|
|
|
|
|
|
|
hardwareId: connected_to,
|
|
|
|
|
|
|
|
message: `Master GSM disconnected - ${connected_to}`,
|
|
|
|
|
|
|
|
disconnectedAt: moment().tz("Asia/Kolkata").format("DD-MM-YYYY HH:mm:ss")
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (disconnectedSlaves.length > 0) {
|
|
|
|
|
|
|
|
issuesToAdd.push({
|
|
|
|
|
|
|
|
type: "LoRa Disconnected",
|
|
|
|
|
|
|
|
hardwareIds: disconnectedSlaves.map(d => d.slaveHardwareId),
|
|
|
|
|
|
|
|
slaveNames: disconnectedSlaves.map(d => d.slaveName),
|
|
|
|
|
|
|
|
message: `Slaves LoRa disconnected`,
|
|
|
|
|
|
|
|
disconnectedAt: moment().tz("Asia/Kolkata").format("DD-MM-YYYY HH:mm:ss")
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return reply.send({
|
|
|
|
|
|
|
|
status_code: 200,
|
|
|
|
|
|
|
|
message: "Checked connection and updated support if needed.",
|
|
|
|
|
|
|
|
masterDisconnected,
|
|
|
|
|
|
|
|
disconnectedSlaves
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} catch (error) {
|
|
|
|
|
|
|
|
console.error("Error raising ticket:", error);
|
|
|
|
|
|
|
|
return reply.code(500).send({ error: "Internal server error" });
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|