support for gsm and lora checks

master^2
Bhaskar 5 months ago
parent 29ba148881
commit 99be0d4fcd

@ -660,7 +660,85 @@ exports.assignTeamMemberToQuotation = async (request, reply) => {
}
};
exports.getByHardwareIdSupport = async (req, reply) => {
try {
const { hardwareId } = req.params;
if (!hardwareId) {
return reply.status(400).send({ error: "hardwareId is required" });
}
console.log("Fetching details for hardwareId:", hardwareId);
const iotData = await IotData.find({ hardwareId })
.sort({ date: -1 })
.limit(3)
.lean();
if (!iotData || iotData.length === 0) {
return reply.send({ status_code: 404, message: "IoT Data not found", data: null });
}
const latestRecord = iotData[0];
const indiaTime = moment.tz(latestRecord.date, "Asia/Kolkata");
const connected_gsm_date = indiaTime.format("DD-MM-YYYY");
const connected_gsm_time = indiaTime.format("HH:mm:ss");
const now = moment.tz("Asia/Kolkata");
const diffInMinutes = now.diff(indiaTime, "minutes");
const isGSMConnected = diffInMinutes <= 1;
const gsmStatus = isGSMConnected ? "connected" : "disconnected";
const gsmLastCheckTime = now.format("DD-MM-YYYY HH:mm:ss"); // formatted current time
// ✅ Step 1: Update Insensors with GSM date/time/status and last check time
await Insensors.findOneAndUpdate(
{ connected_to: hardwareId },
{
$set: {
connected_gsm_date,
connected_gsm_time,
connected_status: gsmStatus,
support_gsm_last_check_time: gsmLastCheckTime
}
},
{ new: true }
);
// ✅ Step 2: Annotate tanks with LoRa connection status based on tank date
const tanksWithConnectionStatus = latestRecord.tanks.map(tank => {
const tankMoment = moment.tz(tank.date, "Asia/Kolkata");
const tankDiff = now.diff(tankMoment, "minutes");
return {
...tank,
connected_status: tankDiff <= 1 ? "connected" : "disconnected"
};
});
// ✅ Step 3: Response
return reply.send({
status_code: 200,
message: "Success",
data: {
hardwareId,
gsm_connected_status: gsmStatus,
gsmStatus: isGSMConnected ? "GSM Connected" : "GSM Not Connected",
connected_gsm_date,
connected_gsm_time,
support_gsm_last_check_time: gsmLastCheckTime,
tanks: tanksWithConnectionStatus,
date: latestRecord.date,
time: latestRecord.time
}
});
} catch (err) {
console.error("Error in getByHardwareId:", err);
return reply.status(500).send({ error: "Internal Server Error" });
}
};
exports.getByHardwareAndTankId = async (req, reply) => {
try {
@ -746,6 +824,87 @@ exports.getByHardwareAndTankId = async (req, reply) => {
};
exports.getByHardwareAndTankIdSupport = async (req, reply) => {
try {
const { hardwareId, tankhardwareId } = req.params;
if (!hardwareId || !tankhardwareId) {
return reply.status(400).send({ error: "Both hardwareId and tankhardwareId are required" });
}
console.log("Fetching tank data for:", { hardwareId, tankhardwareId });
const latestData = await IotData.findOne({ hardwareId })
.sort({ date: -1 })
.lean();
if (!latestData || !Array.isArray(latestData.tanks)) {
return reply.code(404).send({ message: "No data found for given hardwareId and tankhardwareId" });
}
const now = new Date();
const dataDate = new Date(latestData.date);
const diffInMs = now - dataDate;
const isGSMConnected = diffInMs <= 60000;
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;
const matchedTankDateObj = new Date(matchedTank.date);
const day = String(matchedTankDateObj.getDate()).padStart(2, '0');
const month = String(matchedTankDateObj.getMonth() + 1).padStart(2, '0');
const year = matchedTankDateObj.getFullYear();
const formattedDate = `${day}-${month}-${year}`;
matchedTank.date = formattedDate;
const updateFields = {
connected_status: isLoraConnected ? "connected" : "disconnected"
};
let connected_lora_date = null;
let connected_lora_time = null;
let support_lora_last_check_time = null;
if (isLoraConnected) {
connected_lora_date = formattedDate;
connected_lora_time = matchedTank.time || matchedTankDateObj.toTimeString().split(" ")[0];
updateFields.connected_lora_date = connected_lora_date;
updateFields.connected_lora_time = connected_lora_time;
}
// ✅ Format LoRa last check time in "YYYY-MM-DD HH:mm:ss"
support_lora_last_check_time = moment.tz("Asia/Kolkata").format("DD-MM-YYYY HH:mm:ss");
updateFields.support_lora_last_check_time = support_lora_last_check_time;
await Insensors.findOneAndUpdate(
{ connected_to: hardwareId, hardwareId: tankhardwareId },
{ $set: updateFields },
{ new: true }
);
const displayMessage = isLoraConnected ? "LoRa connected" : "LoRa not connected";
return reply.send({
status_code: 200,
message: displayMessage,
data: matchedTank,
lora_connected_status: updateFields.connected_status,
connected_lora_date,
connected_lora_time,
support_lora_last_check_time });
} catch (err) {
console.error("Error in getByHardwareAndTankId:", err);
return reply.status(500).send({ error: "Internal Server Error" });
}
};

@ -450,6 +450,8 @@ connected_lora_date: { type: String, default: null },
connected_lora_time: { type: String, default: null },
typeOfWater:{ type: String, default: null },
gsm_last_check_time : { type: String, default: null },
support_gsm_last_check_time : { type: String, default: null },
support_lora_last_check_time : { type: String, default: null },
lora_last_check_time : { type: String, default: null },
gsm_last_disconnect_time : { type: String, default: null },
lora_last_disconnect_time : { type: String, default: null },

@ -244,6 +244,22 @@ module.exports = function (fastify, opts, next) {
handler: installationController.getByHardwareId,
});
fastify.get("/api/getGsmCheckSupport/:hardwareId", {
schema: {
description: "Get GSM check details Support",
tags: ["Installation"],
summary: "Get GSM check details Support",
params: {
type: "object",
properties: {
hardwareId: { type: "string" },
},
required: ["hardwareId"],
},
},
handler: installationController.getByHardwareIdSupport,
});
fastify.get("/api/getLoraCheck/:hardwareId/:tankhardwareId", {
schema: {
description: "Get LORA check details",
@ -260,6 +276,22 @@ module.exports = function (fastify, opts, next) {
},
handler: installationController.getByHardwareAndTankId,
});
fastify.get("/api/getLoraCheckSupport/:hardwareId/:tankhardwareId", {
schema: {
description: "Get LORA check details Support",
tags: ["Installation"],
summary: "Get LORA check details Support",
params: {
type: "object",
properties: {
hardwareId: { type: "string" },
tankhardwareId: { type : "string"},
},
required: ["hardwareId","tankhardwareId"],
},
},
handler: installationController.getByHardwareAndTankIdSupport,
});
fastify.get("/api/getAllocatedSensorsByTank/:customerId/:tankName", {
schema: {

Loading…
Cancel
Save