diff --git a/src/controllers/installationController.js b/src/controllers/installationController.js index b3d72e38..7ca2e1af 100644 --- a/src/controllers/installationController.js +++ b/src/controllers/installationController.js @@ -580,171 +580,172 @@ exports.assignTeamMemberToQuotation = async (request, reply) => { // const moment = require('moment-timezone'); - exports.getByHardwareId = 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("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 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", + exports.getByHardwareId = 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("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"; + + const gsmLastCheckTime = now.format("YYYY-MM-DD 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, - tanks: tanksWithConnectionStatus, - date: latestRecord.date, - time: latestRecord.time, + connected_status: gsmStatus, + gsm_last_check_time: gsmLastCheckTime } - }); - - } catch (err) { - console.error("Error in getByHardwareId:", err); - return reply.status(500).send({ error: "Internal Server Error" }); - } - }; - - - - - + }, + { new: true } + ); - exports.getByHardwareAndTankId = 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" }); - } - - // 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", + // ✅ 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" }; - - 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; + }); + + // ✅ 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" }); + } +}; + - // Step 4: Update Insensors collection - await Insensors.findOneAndUpdate( - { connected_to: hardwareId, hardwareId: tankhardwareId }, - { $set: updateFields }, - { new: true } - ); - - // 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" }); + +exports.getByHardwareAndTankId = 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 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" }); + } +}; + + diff --git a/src/models/store.js b/src/models/store.js index c3bb0035..2c9825e4 100644 --- a/src/models/store.js +++ b/src/models/store.js @@ -368,7 +368,9 @@ const insensorsSchema = new mongoose.Schema({ connected_gsm_date: { type: String, default: null }, 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 }, +lora_last_check_time : { type: String, default: null }, quality_check_details: [{ damage_check: { result: String }, stickering_check: { result: String },