ashok 7 months ago
commit e0c0dc3e6b

@ -580,170 +580,171 @@ exports.assignTeamMemberToQuotation = async (request, reply) => {
// const moment = require('moment-timezone'); // const moment = require('moment-timezone');
exports.getByHardwareId = async (req, reply) => { exports.getByHardwareId = async (req, reply) => {
try { try {
const { hardwareId } = req.params; const { hardwareId } = req.params;
if (!hardwareId) { if (!hardwareId) {
return reply.status(400).send({ error: "hardwareId is required" }); return reply.status(400).send({ error: "hardwareId is required" });
} }
console.log("Fetching details for hardwareId:", hardwareId); console.log("Fetching details for hardwareId:", hardwareId);
const iotData = await IotData.find({ hardwareId }) const iotData = await IotData.find({ hardwareId })
.sort({ date: -1 }) .sort({ date: -1 })
.limit(3) .limit(3)
.lean(); .lean();
if (!iotData || iotData.length === 0) { if (!iotData || iotData.length === 0) {
return reply.send({ status_code: 404, message: "IoT Data not found", data: null }); return reply.send({ status_code: 404, message: "IoT Data not found", data: null });
} }
const latestRecord = iotData[0]; const latestRecord = iotData[0];
const indiaTime = moment.tz(latestRecord.date, "Asia/Kolkata");
const connected_gsm_date = indiaTime.format("YYYY-MM-DD");
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";
// ✅ Step 1: Update Insensors with GSM date/time/status
await Insensors.findOneAndUpdate(
{ connected_to: hardwareId },
{
$set: {
connected_gsm_date,
connected_gsm_time,
connected_status: gsmStatus
}
},
{ new: true }
);
// ✅ Step 2: Annotate tanks with LoRa connection status based on tank date const indiaTime = moment.tz(latestRecord.date, "Asia/Kolkata");
const tanksWithConnectionStatus = latestRecord.tanks.map(tank => { const connected_gsm_date = indiaTime.format("YYYY-MM-DD");
const tankMoment = moment.tz(tank.date, "Asia/Kolkata"); const connected_gsm_time = indiaTime.format("HH:mm:ss");
const tankDiff = now.diff(tankMoment, "minutes");
return { const now = moment.tz("Asia/Kolkata");
...tank, const diffInMinutes = now.diff(indiaTime, "minutes");
connected_status: tankDiff <= 1 ? "connected" : "disconnected" const isGSMConnected = diffInMinutes <= 1;
}; const gsmStatus = isGSMConnected ? "connected" : "disconnected";
});
// ✅ Step 3: Response const gsmLastCheckTime = now.format("YYYY-MM-DD HH:mm:ss"); // formatted current time
return reply.send({
status_code: 200, // ✅ Step 1: Update Insensors with GSM date/time/status and last check time
message: "Success", await Insensors.findOneAndUpdate(
data: { { connected_to: hardwareId },
hardwareId, {
gsm_connected_status: gsmStatus, $set: {
gsmStatus: isGSMConnected ? "GSM Connected" : "GSM Not Connected",
connected_gsm_date, connected_gsm_date,
connected_gsm_time, connected_gsm_time,
tanks: tanksWithConnectionStatus, connected_status: gsmStatus,
date: latestRecord.date, gsm_last_check_time: gsmLastCheckTime
time: latestRecord.time,
} }
}); },
{ new: true }
);
} catch (err) { // ✅ Step 2: Annotate tanks with LoRa connection status based on tank date
console.error("Error in getByHardwareId:", err); const tanksWithConnectionStatus = latestRecord.tanks.map(tank => {
return reply.status(500).send({ error: "Internal Server Error" }); 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,
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) => { exports.getByHardwareAndTankId = async (req, reply) => {
try { try {
const { hardwareId, tankhardwareId } = req.params; const { hardwareId, tankhardwareId } = req.params;
if (!hardwareId || !tankhardwareId) { if (!hardwareId || !tankhardwareId) {
return reply.status(400).send({ error: "Both hardwareId and tankhardwareId are required" }); return reply.status(400).send({ error: "Both hardwareId and tankhardwareId are required" });
} }
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 }) .sort({ date: -1 })
.lean(); .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" });
} }
// Step 1: Check GSM Connection - if data is fresh within 1 minute const now = new Date();
const now = new Date(); const dataDate = new Date(latestData.date);
const dataDate = new Date(latestData.date); const diffInMs = now - dataDate;
const diffInMs = now - dataDate; const isGSMConnected = diffInMs <= 60000;
const isGSMConnected = diffInMs <= 60000; // 60 seconds
// Step 2: Find the matching tank const matchedTank = latestData.tanks.find(tank => tank.tankhardwareId === tankhardwareId);
const matchedTank = latestData.tanks.find(tank => tank.tankhardwareId === tankhardwareId);
if (!matchedTank) { if (!matchedTank) {
return reply.code(404).send({ message: "Tank not found in latest record" }); return reply.code(404).send({ message: "Tank not found in latest record" });
} }
// Step 3: Determine LoRa connection status const tankHeight = parseFloat(matchedTank.tankHeight || "0");
const tankHeight = parseFloat(matchedTank.tankHeight || "0"); const isLoraConnected = isGSMConnected && tankHeight > 0;
const isLoraConnected = isGSMConnected && tankHeight > 0;
const matchedTankDateObj = new Date(matchedTank.date); const matchedTankDateObj = new Date(matchedTank.date);
const day = String(matchedTankDateObj.getDate()).padStart(2, '0'); const day = String(matchedTankDateObj.getDate()).padStart(2, '0');
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}`;
// Add formatted date to matchedTank matchedTank.date = formattedDate;
matchedTank.date = formattedDate;
const updateFields = { const updateFields = {
connected_status: isLoraConnected ? "connected" : "disconnected", connected_status: isLoraConnected ? "connected" : "disconnected"
}; };
let connected_lora_date = null;
let connected_lora_time = null;
let lora_last_check_time = null;
let connected_lora_date = null; if (isLoraConnected) {
let connected_lora_time = null; 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;
}
if (isLoraConnected) { // ✅ Format LoRa last check time in "YYYY-MM-DD HH:mm:ss"
connected_lora_date = formattedDate; lora_last_check_time = moment.tz("Asia/Kolkata").format("YYYY-MM-DD HH:mm:ss");
connected_lora_time = matchedTank.time || matchedTankDateObj.toTimeString().split(" ")[0]; updateFields.lora_last_check_time = lora_last_check_time;
updateFields.connected_lora_date = connected_lora_date; await Insensors.findOneAndUpdate(
updateFields.connected_lora_time = connected_lora_time; { connected_to: hardwareId, hardwareId: tankhardwareId },
} { $set: updateFields },
{ new: true }
);
// Step 4: Update Insensors collection const displayMessage = isLoraConnected ? "LoRa connected" : "LoRa not connected";
await Insensors.findOneAndUpdate(
{ connected_to: hardwareId, hardwareId: tankhardwareId },
{ $set: updateFields },
{ new: true }
);
// Step 5: Prepare response return reply.send({
const displayMessage = isLoraConnected ? "LoRa connected" : "LoRa not connected"; status_code: 200,
message: displayMessage,
data: matchedTank,
lora_connected_status: updateFields.connected_status,
connected_lora_date,
connected_lora_time,
lora_last_check_time
});
return reply.send({ } catch (err) {
status_code: 200, console.error("Error in getByHardwareAndTankId:", err);
message: displayMessage, return reply.status(500).send({ error: "Internal Server Error" });
data: matchedTank, }
lora_connected_status: updateFields.connected_status, };
connected_lora_date,
connected_lora_time
});
} catch (err) {
console.error("Error in getByHardwareAndTankId:", err);
return reply.status(500).send({ error: "Internal Server Error" });
}
};

@ -368,7 +368,9 @@ const insensorsSchema = new mongoose.Schema({
connected_gsm_date: { type: String, default: null }, connected_gsm_date: { type: String, default: null },
connected_lora_date: { type: String, default: null }, connected_lora_date: { type: String, default: null },
connected_lora_time: { type: String, default: null }, connected_lora_time: { type: String, default: null },
typeOfWater:{ type: String, default: null },
gsm_last_check_time : { type: String, default: null },
lora_last_check_time : { type: String, default: null },
quality_check_details: [{ quality_check_details: [{
damage_check: { result: String }, damage_check: { result: String },
stickering_check: { result: String }, stickering_check: { result: String },

Loading…
Cancel
Save