|
|
@ -843,8 +843,6 @@ exports.getByHardwareAndTankId = async (req, reply) => {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
exports.getByHardwareAndTankIdSupport = async (req, reply) => {
|
|
|
|
exports.getByHardwareAndTankIdSupport = async (req, reply) => {
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
const { hardwareId, tankhardwareId } = req.params;
|
|
|
|
const { hardwareId, tankhardwareId } = req.params;
|
|
|
@ -855,9 +853,7 @@ exports.getByHardwareAndTankIdSupport = async (req, reply) => {
|
|
|
|
|
|
|
|
|
|
|
|
console.log("Fetching tank data for:", { hardwareId, tankhardwareId });
|
|
|
|
console.log("Fetching tank data for:", { hardwareId, tankhardwareId });
|
|
|
|
|
|
|
|
|
|
|
|
const latestData = await IotData.findOne({ hardwareId })
|
|
|
|
const latestData = await IotData.findOne({ hardwareId }).sort({ date: -1 }).lean();
|
|
|
|
.sort({ date: -1 })
|
|
|
|
|
|
|
|
.lean();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (!latestData || !Array.isArray(latestData.tanks)) {
|
|
|
|
if (!latestData || !Array.isArray(latestData.tanks)) {
|
|
|
|
return reply.code(404).send({ message: "No data found for given hardwareId and tankhardwareId" });
|
|
|
|
return reply.code(404).send({ message: "No data found for given hardwareId and tankhardwareId" });
|
|
|
@ -882,16 +878,17 @@ exports.getByHardwareAndTankIdSupport = async (req, reply) => {
|
|
|
|
const month = String(matchedTankDateObj.getMonth() + 1).padStart(2, '0');
|
|
|
|
const month = String(matchedTankDateObj.getMonth() + 1).padStart(2, '0');
|
|
|
|
const year = matchedTankDateObj.getFullYear();
|
|
|
|
const year = matchedTankDateObj.getFullYear();
|
|
|
|
const formattedDate = `${day}-${month}-${year}`;
|
|
|
|
const formattedDate = `${day}-${month}-${year}`;
|
|
|
|
|
|
|
|
|
|
|
|
matchedTank.date = formattedDate;
|
|
|
|
matchedTank.date = formattedDate;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const support_lora_last_check_time = moment.tz("Asia/Kolkata").format("DD-MM-YYYY HH:mm:ss");
|
|
|
|
|
|
|
|
|
|
|
|
const updateFields = {
|
|
|
|
const updateFields = {
|
|
|
|
connected_status: isLoraConnected ? "connected" : "disconnected"
|
|
|
|
connected_status: isLoraConnected ? "connected" : "disconnected",
|
|
|
|
|
|
|
|
support_lora_last_check_time
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
let connected_lora_date = null;
|
|
|
|
let connected_lora_date = null;
|
|
|
|
let connected_lora_time = null;
|
|
|
|
let connected_lora_time = null;
|
|
|
|
let support_lora_last_check_time = moment.tz("Asia/Kolkata").format("DD-MM-YYYY HH:mm:ss");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (isLoraConnected) {
|
|
|
|
if (isLoraConnected) {
|
|
|
|
connected_lora_date = formattedDate;
|
|
|
|
connected_lora_date = formattedDate;
|
|
|
@ -900,24 +897,23 @@ exports.getByHardwareAndTankIdSupport = async (req, reply) => {
|
|
|
|
updateFields.connected_lora_time = connected_lora_time;
|
|
|
|
updateFields.connected_lora_time = connected_lora_time;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
updateFields.support_lora_last_check_time = support_lora_last_check_time;
|
|
|
|
// Support both slave and master structure
|
|
|
|
|
|
|
|
const updatedSensor = await Insensors.findOneAndUpdate(
|
|
|
|
// 🔍 Add debugging to check if the document exists
|
|
|
|
{
|
|
|
|
const sensorToUpdate = await Insensors.findOne({
|
|
|
|
$or: [
|
|
|
|
connected_to: hardwareId,
|
|
|
|
{ connected_to: hardwareId, tankhardwareId: tankhardwareId }, // slave
|
|
|
|
hardwareId: tankhardwareId
|
|
|
|
{ hardwareId: tankhardwareId } // master
|
|
|
|
});
|
|
|
|
]
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
{ $set: updateFields },
|
|
|
|
|
|
|
|
{ new: true }
|
|
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
if (!sensorToUpdate) {
|
|
|
|
if (!updatedSensor) {
|
|
|
|
console.warn("No matching Insensors document found for update");
|
|
|
|
console.warn("No matching Insensors document found for update");
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
await Insensors.findOneAndUpdate(
|
|
|
|
console.log("Updated support_lora_last_check_time for:", updatedSensor.hardwareId);
|
|
|
|
{ connected_to: hardwareId, hardwareId: tankhardwareId },
|
|
|
|
|
|
|
|
{ $set: { ...updateFields, support_lora_last_check_time } },
|
|
|
|
|
|
|
|
{ new: true }
|
|
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const displayMessage = isLoraConnected ? "LoRa connected" : "LoRa not connected";
|
|
|
|
const displayMessage = isLoraConnected ? "LoRa connected" : "LoRa not connected";
|
|
|
|
|
|
|
|
|
|
|
@ -936,8 +932,7 @@ exports.getByHardwareAndTankIdSupport = async (req, reply) => {
|
|
|
|
return reply.status(500).send({ error: "Internal Server Error" });
|
|
|
|
return reply.status(500).send({ error: "Internal Server Error" });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
exports.getAllocatedSensorsByTank = async (req, reply) => {
|
|
|
|
exports.getAllocatedSensorsByTank = async (req, reply) => {
|
|
|
|