diff --git a/src/controllers/installationController.js b/src/controllers/installationController.js index fa9ad4a7..b28dfbf4 100644 --- a/src/controllers/installationController.js +++ b/src/controllers/installationController.js @@ -576,35 +576,63 @@ exports.assignTeamMemberToQuotation = async (request, reply) => { } }; - 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); - // Fetch only required fields to reduce response size - // const data = await Collection.findOne({ hardwareId }) - // .select("hardwareId mode tanks date time") // Select only needed fields - // .lean(); - - const data = await IotData.find({ hardwareId }); - + // Fetch the latest 3 records sorted by date descending + const iotData = await IotData.find({ hardwareId }) + .sort({ date: -1 }) + .limit(3) + .lean(); - if (!data) { - return reply.send({ status_code: 404, message: "Data not found", data: null }); + if (!iotData || iotData.length === 0) { + return reply.send({ status_code: 404, message: "IoT Data not found", data: null }); } - return reply.send({ status_code: 200, message: "Success", data }); + const latestRecord = iotData[0]; // Most recent one + + // Format current timestamp or use `latestRecord.date` + const now = new Date(latestRecord.date || Date.now()); + const connected_gsm_date = now.toISOString().split('T')[0]; // yyyy-mm-dd + const connected_gsm_time = now.toTimeString().split(' ')[0]; // hh:mm:ss + + // Update insensor where connected_to = hardwareId + await Insensors.findOneAndUpdate( + { connected_to: hardwareId }, + { + connected_gsm_time, + connected_gsm_date + }, + { new: true } + ); + + // Optionally fetch updated insensor if you want to include it in response + const sensor = await Insensors.findOne({ connected_to: hardwareId }); + + return reply.send({ + status_code: 200, + message: "Success", + data: { + latest_3_records: iotData, + connected_gsm_time, + connected_gsm_date, + sensorInfo: sensor || null + } + }); + } catch (err) { console.error("Error:", err); return reply.status(500).send({ error: "Internal Server Error" }); } }; + exports.getByHardwareAndTankId = async (req, reply) => { @@ -617,17 +645,39 @@ exports.assignTeamMemberToQuotation = async (request, reply) => { console.log("Fetching details for:", { hardwareId, tankhardwareId }); - // Correct query using $elemMatch + // Query with projection to match the specific tank const data = await IotData.findOne( { hardwareId, tanks: { $elemMatch: { tankhardwareId } } }, - { "tanks.$": 1, hardwareId: 1, mode: 1, date: 1, time: 1 } // Projection to return only matched tank - ); + { "tanks.$": 1, hardwareId: 1, mode: 1, date: 1, time: 1 } + ).lean(); if (!data) { return reply.send({ status_code: 404, message: "Data not found", data: null }); } - return reply.send({ status_code: 200, message: "Success", data }); + // Use the IotData's date/time if available, otherwise fallback to now + const now = new Date(data.date || Date.now()); + const connected_lora_date = now.toISOString().split("T")[0]; // yyyy-mm-dd + const connected_lora_time = now.toTimeString().split(" ")[0]; // hh:mm:ss + + // Update the related insensor with LoRa connected time and date + await Insensors.findOneAndUpdate( + { connected_to: hardwareId, hardwareId: tankhardwareId }, + { + connected_lora_date, + connected_lora_time + }, + { new: true } + ); + + return reply.send({ + status_code: 200, + message: "Success", + data, + connected_lora_date, + connected_lora_time + }); + } catch (err) { console.error("Error:", err); return reply.status(500).send({ error: "Internal Server Error" }); diff --git a/src/models/store.js b/src/models/store.js index 52aac643..d0ce1671 100644 --- a/src/models/store.js +++ b/src/models/store.js @@ -364,6 +364,11 @@ const insensorsSchema = new mongoose.Schema({ connected_status: { type: String, enum: ["connected", "Not connected", "unknown"], default: "unknown" }, masterName: { type: String, default: null }, location: { type: String, default: null }, + connected_gsm_time: { type: String, default: null }, +connected_gsm_date: { type: String, default: null }, +connected_lora_date: { type: String, default: null }, +connected_lora_time: { type: String, default: null }, + quality_check_details: [{ damage_check: { result: String }, stickering_check: { result: String },