|
|
|
|
@ -749,6 +749,7 @@ exports.getByHardwareAndTankId = async (req, reply) => {
|
|
|
|
|
|
|
|
|
|
console.log("📡 Fetching data for:", { hardwareId, tankhardwareId });
|
|
|
|
|
|
|
|
|
|
// Get the latest IoT data for the master hardwareId
|
|
|
|
|
const latestData = await IotData.findOne({ hardwareId }).sort({ date: -1 }).lean();
|
|
|
|
|
|
|
|
|
|
if (!latestData || !Array.isArray(latestData.tanks)) {
|
|
|
|
|
@ -757,18 +758,28 @@ exports.getByHardwareAndTankId = async (req, reply) => {
|
|
|
|
|
|
|
|
|
|
const now = new Date();
|
|
|
|
|
const dataDate = new Date(latestData.date);
|
|
|
|
|
|
|
|
|
|
// Check master GSM connection (threshold 60 sec)
|
|
|
|
|
const isGSMConnected = now - dataDate <= 60000;
|
|
|
|
|
|
|
|
|
|
// Find the tank for the slave
|
|
|
|
|
const matchedTank = latestData.tanks.find(tank => tank.tankhardwareId === tankhardwareId);
|
|
|
|
|
|
|
|
|
|
if (!matchedTank) {
|
|
|
|
|
return reply.code(404).send({ message: "Tank not found in latest record" });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const tankHeight = parseFloat(matchedTank.tankHeight || "0");
|
|
|
|
|
const isLoraConnected = isGSMConnected && tankHeight > 0;
|
|
|
|
|
|
|
|
|
|
// Format date and time
|
|
|
|
|
// If master is disconnected => slave is disconnected too
|
|
|
|
|
let isLoraConnected = false;
|
|
|
|
|
if (!isGSMConnected) {
|
|
|
|
|
isLoraConnected = false;
|
|
|
|
|
} else {
|
|
|
|
|
// Otherwise use tankHeight to determine LoRa connection
|
|
|
|
|
isLoraConnected = tankHeight > 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Format tank date/time to IST
|
|
|
|
|
const matchedTankDateObj = new Date(matchedTank.date);
|
|
|
|
|
const formattedDate = moment(matchedTankDateObj).tz("Asia/Kolkata").format("DD-MM-YYYY");
|
|
|
|
|
const formattedTime = matchedTank.time || moment(matchedTankDateObj).tz("Asia/Kolkata").format("HH:mm:ss");
|
|
|
|
|
@ -776,33 +787,31 @@ exports.getByHardwareAndTankId = async (req, reply) => {
|
|
|
|
|
matchedTank.date = formattedDate;
|
|
|
|
|
matchedTank.time = formattedTime;
|
|
|
|
|
|
|
|
|
|
// Prepare update object for Insensors collection
|
|
|
|
|
const updateFields = {
|
|
|
|
|
connected_status: isLoraConnected ? "connected" : "disconnected",
|
|
|
|
|
connected_lora_date: formattedDate,
|
|
|
|
|
connected_lora_time: formattedTime,
|
|
|
|
|
lora_last_check_time: moment().tz("Asia/Kolkata").format("DD-MM-YYYY HH:mm:ss")
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Find and update Insensors
|
|
|
|
|
const sensorDoc = await Insensors.findOne({
|
|
|
|
|
hardwareId: tankhardwareId,
|
|
|
|
|
tankhardwareId: tankhardwareId,
|
|
|
|
|
connected_to: hardwareId
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (!sensorDoc) {
|
|
|
|
|
console.warn("⚠️ No Insensors doc found for LoRa update:", { tankhardwareId, connected_to: hardwareId });
|
|
|
|
|
|
|
|
|
|
// Optional fallback update only by hardwareId
|
|
|
|
|
const fallbackDoc = await Insensors.findOne({ hardwareId: tankhardwareId });
|
|
|
|
|
|
|
|
|
|
const fallbackDoc = await Insensors.findOne({ tankhardwareId: tankhardwareId });
|
|
|
|
|
if (fallbackDoc) {
|
|
|
|
|
await Insensors.updateOne({ _id: fallbackDoc._id }, { $set: updateFields });
|
|
|
|
|
console.log("⚠️ Fallback Insensors updated by hardwareId:", fallbackDoc._id);
|
|
|
|
|
console.log("⚠️ Fallback Insensors updated by tankhardwareId:", fallbackDoc._id);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
await Insensors.updateOne({ _id: sensorDoc._id }, { $set: updateFields });
|
|
|
|
|
console.log("✅ Insensors LoRa status updated:", sensorDoc._id);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return reply.send({
|
|
|
|
|
status_code: 200,
|
|
|
|
|
message: isLoraConnected ? "LoRa connected" : "LoRa not connected",
|
|
|
|
|
@ -823,6 +832,7 @@ exports.getByHardwareAndTankId = async (req, reply) => {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
exports.getByHardwareAndTankIdSupport = async (req, reply) => {
|
|
|
|
|
try {
|
|
|
|
|
const { hardwareId, tankhardwareId } = req.params;
|
|
|
|
|
|