TYEP of water and last check time added

master^2
Bhaskar 6 months ago
parent dcd7a998f8
commit b89d316e21

@ -580,171 +580,172 @@ 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 indiaTime = moment.tz(latestRecord.date, "Asia/Kolkata");
const connected_gsm_date = indiaTime.format("YYYY-MM-DD"); const connected_gsm_date = indiaTime.format("YYYY-MM-DD");
const connected_gsm_time = indiaTime.format("HH:mm:ss"); const connected_gsm_time = indiaTime.format("HH:mm:ss");
const now = moment.tz("Asia/Kolkata"); const now = moment.tz("Asia/Kolkata");
const diffInMinutes = now.diff(indiaTime, "minutes"); const diffInMinutes = now.diff(indiaTime, "minutes");
const isGSMConnected = diffInMinutes <= 1; const isGSMConnected = diffInMinutes <= 1;
const gsmStatus = isGSMConnected ? "connected" : "disconnected"; const gsmStatus = isGSMConnected ? "connected" : "disconnected";
// ✅ Step 1: Update Insensors with GSM date/time/status const gsmLastCheckTime = now.format("YYYY-MM-DD HH:mm:ss"); // formatted current time
await Insensors.findOneAndUpdate(
{ connected_to: hardwareId }, // ✅ Step 1: Update Insensors with GSM date/time/status and last check time
{ await Insensors.findOneAndUpdate(
$set: { { connected_to: hardwareId },
connected_gsm_date, {
connected_gsm_time, $set: {
connected_status: gsmStatus
}
},
{ 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_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) { );
console.error("Error in getByHardwareId:", err);
return reply.status(500).send({ error: "Internal Server Error" });
}
};
exports.getByHardwareAndTankId = async (req, reply) => { // ✅ Step 2: Annotate tanks with LoRa connection status based on tank date
try { const tanksWithConnectionStatus = latestRecord.tanks.map(tank => {
const { hardwareId, tankhardwareId } = req.params; const tankMoment = moment.tz(tank.date, "Asia/Kolkata");
const tankDiff = now.diff(tankMoment, "minutes");
if (!hardwareId || !tankhardwareId) {
return reply.status(400).send({ error: "Both hardwareId and tankhardwareId are required" }); return {
} ...tank,
connected_status: tankDiff <= 1 ? "connected" : "disconnected"
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" });
}
// Step 1: Check GSM Connection - if data is fresh within 1 minute
const now = new Date();
const dataDate = new Date(latestData.date);
const diffInMs = now - dataDate;
const isGSMConnected = diffInMs <= 60000; // 60 seconds
// Step 2: Find the matching tank
const matchedTank = latestData.tanks.find(tank => tank.tankhardwareId === tankhardwareId);
if (!matchedTank) {
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 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}`;
// Add formatted date to matchedTank
matchedTank.date = formattedDate;
const updateFields = {
connected_status: isLoraConnected ? "connected" : "disconnected",
}; };
});
let connected_lora_date = null;
let connected_lora_time = null; // ✅ Step 3: Response
return reply.send({
if (isLoraConnected) { status_code: 200,
connected_lora_date = formattedDate; message: "Success",
connected_lora_time = matchedTank.time || matchedTankDateObj.toTimeString().split(" ")[0]; data: {
hardwareId,
updateFields.connected_lora_date = connected_lora_date; gsm_connected_status: gsmStatus,
updateFields.connected_lora_time = connected_lora_time; 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" });
}
};
// Step 4: Update Insensors collection
await Insensors.findOneAndUpdate( exports.getByHardwareAndTankId = async (req, reply) => {
{ connected_to: hardwareId, hardwareId: tankhardwareId }, try {
{ $set: updateFields }, const { hardwareId, tankhardwareId } = req.params;
{ new: true }
); if (!hardwareId || !tankhardwareId) {
return reply.status(400).send({ error: "Both hardwareId and tankhardwareId are required" });
// Step 5: Prepare response
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
});
} catch (err) {
console.error("Error in getByHardwareAndTankId:", err);
return reply.status(500).send({ error: "Internal Server Error" });
} }
};
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 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"
lora_last_check_time = moment.tz("Asia/Kolkata").format("YYYY-MM-DD HH:mm:ss");
updateFields.lora_last_check_time = 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,
lora_last_check_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