diff --git a/src/controllers/installationController.js b/src/controllers/installationController.js index bd10eef7..3f7d5ff8 100644 --- a/src/controllers/installationController.js +++ b/src/controllers/installationController.js @@ -6,6 +6,8 @@ const { Deparments } = require("../models/Department"); const { Install, SensorStock, SensorQuotation, Order, Insensors, MasterSlaveData, ElectrictyWorkPictures, PlumbingWorkPictures, MaterialRecievedPictures } = require("../models/store"); const { Counter, User } = require("../models/User"); const { IotData, Tank } = require("../models/tanks"); +const moment = require('moment-timezone'); + const fastify = require("fastify")({ logger: true, //disableRequestLogging: true, @@ -576,6 +578,7 @@ exports.assignTeamMemberToQuotation = async (request, reply) => { } }; + exports.getByHardwareId = async (req, reply) => { try { const { hardwareId } = req.params; @@ -586,7 +589,6 @@ exports.assignTeamMemberToQuotation = async (request, reply) => { console.log("Fetching details for hardwareId:", hardwareId); - // Fetch latest 3 IoT records const iotData = await IotData.find({ hardwareId }) .sort({ date: -1 }) .limit(3) @@ -596,46 +598,48 @@ exports.assignTeamMemberToQuotation = async (request, reply) => { return reply.send({ status_code: 404, message: "IoT Data not found", data: null }); } - const latestRecord = iotData[0]; // Most recent record + const latestRecord = iotData[0]; - // Step 1: Get GSM Date from top-level date - const connected_gsm_date = new Date(latestRecord.date).toISOString().split('T')[0]; // yyyy-mm-dd + 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"); - // Step 2: Get GSM Time from first tank time if available - const firstTankTime = latestRecord.tanks?.[0]?.time || new Date(latestRecord.date).toTimeString().split(' ')[0]; - const connected_gsm_time = firstTankTime; - - // Step 3: Update Insensor - const updateResult = await Insensors.findOneAndUpdate( + // Step 1: Update connected_gsm fields in Insensors + await Insensors.findOneAndUpdate( { connected_to: hardwareId }, - { - $set: { - connected_gsm_date, - connected_gsm_time - } - }, + { $set: { connected_gsm_date, connected_gsm_time } }, { new: true } ); - if (!updateResult) { - console.log("No insensor found with connected_to =", hardwareId); - } else { - console.log("Updated connected_gsm_date/time:", connected_gsm_date, connected_gsm_time); - } - - // Step 4: Check if any tank has tankHeight > 0 - const isGSMConnected = latestRecord.tanks.some(tank => parseFloat(tank.tankHeight || "0") > 0); + // Step 2: Check GSM Connection - compare now with latestRecord.date + const now = moment.tz("Asia/Kolkata"); + const diffInMinutes = now.diff(indiaTime, "minutes"); + const isGSMConnected = diffInMinutes <= 1; const gsmConnectionMessage = isGSMConnected ? "GSM Connected" : "GSM Not Connected"; - // Add tanks back without individual connection status + // Step 3: Optional - Annotate each tank with 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" + }; + }); + + // Step 4: Return result return reply.send({ status_code: 200, message: "Success", data: { hardwareId, - gsmStatus: gsmConnectionMessage, // ✅ GSM connection message here - ...latestRecord, - tanks: latestRecord.tanks + gsmStatus: gsmConnectionMessage, + connected_gsm_date, + connected_gsm_time, + tanks: tanksWithConnectionStatus, + date: latestRecord.date, + time: latestRecord.time, } }); @@ -648,6 +652,7 @@ exports.assignTeamMemberToQuotation = async (request, reply) => { + exports.getByHardwareAndTankId = async (req, reply) => { try { @@ -680,7 +685,7 @@ exports.assignTeamMemberToQuotation = async (request, reply) => { connected_status: isConnected ? "connected" : "disconnected", }; - let connected_lora_date = null; + let connected_lora_date = null; let connected_lora_time = null; if (isConnected) { @@ -1000,7 +1005,7 @@ exports.mastrerList = async (req, reply) => { // } // } -const moment = require('moment'); // For time calculations +//const moment = require('moment'); // For time calculations exports.getMasterSlaveSummary = async (req, reply) => {