gsm connected or not

master^2
Bhaskar 6 months ago
parent 24295e24a8
commit 49fd7b1e42

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

Loading…
Cancel
Save