support team member gsm and lora check

master^2
Bhaskar 5 months ago
parent dc5adc5569
commit 161d3aace0

@ -750,6 +750,97 @@ exports.getByHardwareIdSupport = async (req, reply) => {
}
};
exports.getByHardwareIdSupportTeamMember = 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
// ✅ Update slaves connected to this master hardwareId
await Insensors.updateMany(
{ connected_to: hardwareId },
{
$set: {
connected_gsm_date,
connected_gsm_time,
connected_status: gsmStatus,
team_member_support_gsm_last_check_time: gsmLastCheckTime
}
}
);
// ✅ Update the master device itself (if it's a master)
await Insensors.updateOne(
{ hardwareId },
{
$set: {
connected_gsm_date,
connected_gsm_time,
connected_status: gsmStatus,
team_member_support_gsm_last_check_time: gsmLastCheckTime
}
}
);
// ✅ Annotate tanks with LoRa connection status
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"
};
});
// ✅ 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,
team_member_support_gsm_last_check_time: gsmLastCheckTime,
tanks: tanksWithConnectionStatus,
date: latestRecord.date,
time: latestRecord.time
}
});
} catch (err) {
console.error("Error in getByHardwareIdSupport:", err);
return reply.status(500).send({ error: "Internal Server Error" });
}
};
exports.getByHardwareAndTankId = async (req, reply) => {
try {
@ -934,6 +1025,97 @@ exports.getByHardwareAndTankIdSupport = async (req, reply) => {
};
exports.getByHardwareAndTankIdSupportTeamMember = 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 team_member_support_lora_last_check_time = moment.tz("Asia/Kolkata").format("DD-MM-YYYY HH:mm:ss");
const updateFields = {
connected_status: isLoraConnected ? "connected" : "disconnected",
team_member_support_lora_last_check_time
};
let connected_lora_date = null;
let connected_lora_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;
}
// Support both slave and master structure
const updatedSensor = await Insensors.findOneAndUpdate(
{
$or: [
{ connected_to: hardwareId, tankhardwareId: tankhardwareId }, // slave
{ hardwareId: tankhardwareId } // master
]
},
{ $set: updateFields },
{ new: true }
);
if (!updatedSensor) {
console.warn("No matching Insensors document found for update");
} else {
console.log("Updated support_lora_last_check_time for:", updatedSensor.hardwareId);
}
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,
team_member_support_lora_last_check_time
});
} catch (err) {
console.error("Error in getByHardwareAndTankIdSupport:", err);
return reply.status(500).send({ error: "Internal Server Error" });
}
};
exports.getAllocatedSensorsByTank = async (req, reply) => {
try {

@ -498,6 +498,8 @@ 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 },
team_member_support_gsm_last_check_time : { type: String, default: null },
team_member_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 },

@ -260,6 +260,21 @@ module.exports = function (fastify, opts, next) {
handler: installationController.getByHardwareIdSupport,
});
fastify.get("/api/getGsmCheckSupportTeamMember/:hardwareId", {
schema: {
description: "Get GSM check details Support Team Member",
tags: ["Installation"],
summary: "Get GSM check details Support Team Member",
params: {
type: "object",
properties: {
hardwareId: { type: "string" },
},
required: ["hardwareId"],
},
},
handler: installationController.getByHardwareIdSupportTeamMember,
});
fastify.get("/api/getLoraCheck/:hardwareId/:tankhardwareId", {
schema: {
description: "Get LORA check details",
@ -293,6 +308,22 @@ module.exports = function (fastify, opts, next) {
handler: installationController.getByHardwareAndTankIdSupport,
});
fastify.get("/api/getLoraCheckSupportTeamMember/:hardwareId/:tankhardwareId", {
schema: {
description: "Get LORA check details Support Team Member",
tags: ["Installation"],
summary: "Get LORA check details Support Team Member",
params: {
type: "object",
properties: {
hardwareId: { type: "string" },
tankhardwareId: { type : "string"},
},
required: ["hardwareId","tankhardwareId"],
},
},
handler: installationController.getByHardwareAndTankIdSupportTeamMember,
});
fastify.get("/api/getAllocatedSensorsByTank/:customerId/:tankName", {
schema: {
description: "Get allocated sensors by installationId, customerId, and tankName",

Loading…
Cancel
Save