|
|
|
|
@ -1036,7 +1036,6 @@ exports.mastrerList = async (req, reply) => {
|
|
|
|
|
//const moment = require('moment'); // For time calculations
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
exports.getMasterSlaveSummary = async (req, reply) => {
|
|
|
|
|
try {
|
|
|
|
|
const { customerId } = req.params;
|
|
|
|
|
@ -1054,32 +1053,72 @@ exports.getMasterSlaveSummary = async (req, reply) => {
|
|
|
|
|
const hardwareId = device.hardwareId?.trim();
|
|
|
|
|
if (!hardwareId) return device.toObject();
|
|
|
|
|
|
|
|
|
|
// Fetch latest IoT data
|
|
|
|
|
const latestData = await IotData.findOne({ hardwareId })
|
|
|
|
|
.sort({ date: -1 })
|
|
|
|
|
.lean();
|
|
|
|
|
|
|
|
|
|
const enriched = device.toObject();
|
|
|
|
|
|
|
|
|
|
// Add GSM Connection info (date/time/status) for master
|
|
|
|
|
if (latestData?.date) {
|
|
|
|
|
const indiaTime = moment.tz(latestData.date, "Asia/Kolkata");
|
|
|
|
|
const date = indiaTime.format("DD-MM-YYYY");
|
|
|
|
|
const 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";
|
|
|
|
|
|
|
|
|
|
if (device.type === 'master') {
|
|
|
|
|
enriched.connected_gsm_date = date;
|
|
|
|
|
enriched.connected_gsm_time = time;
|
|
|
|
|
enriched.gsm_connected_status = gsmStatus;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Add LoRa Connection info for slaves (per-tank)
|
|
|
|
|
if (latestData?.tanks && Array.isArray(latestData.tanks)) {
|
|
|
|
|
const enrichedTanks = latestData.tanks.map(tank => {
|
|
|
|
|
const tankMoment = moment.tz(tank.date, "Asia/Kolkata");
|
|
|
|
|
const tankDiff = moment.tz("Asia/Kolkata").diff(tankMoment, "minutes");
|
|
|
|
|
const loraStatus = tankDiff <= 1 ? "connected" : "disconnected";
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
...tank,
|
|
|
|
|
connected_status: loraStatus
|
|
|
|
|
};
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
enriched.tanks = enrichedTanks;
|
|
|
|
|
|
|
|
|
|
// Update LoRa status for the device itself (if slave)
|
|
|
|
|
if (device.type === 'slave') {
|
|
|
|
|
const connectedTank = enrichedTanks.find(tank => tank.tankhardwareId === device.hardwareId);
|
|
|
|
|
enriched.lora_connected_status = connectedTank ? connectedTank.connected_status : "disconnected";
|
|
|
|
|
enriched.connected_lora_date = connectedTank ? connectedTank.date : null;
|
|
|
|
|
enriched.connected_lora_time = connectedTank ? connectedTank.time : null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Remove GSM fields for slave devices and LoRa fields for master devices
|
|
|
|
|
if (device.type === 'slave') {
|
|
|
|
|
enriched.connected_lora_date = date;
|
|
|
|
|
enriched.connected_lora_time = time;
|
|
|
|
|
delete enriched.connected_gsm_date;
|
|
|
|
|
delete enriched.connected_gsm_time;
|
|
|
|
|
delete enriched.gsm_connected_status;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (device.type === 'master') {
|
|
|
|
|
delete enriched.connected_lora_date;
|
|
|
|
|
delete enriched.connected_lora_time;
|
|
|
|
|
delete enriched.lora_connected_status;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return enriched;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Enrich all devices
|
|
|
|
|
// Enrich masters and slaves with timestamps and connection statuses
|
|
|
|
|
const enrichedMasters = await Promise.all(masters.map(enrichDeviceWithTimestamp));
|
|
|
|
|
const enrichedSlaves = await Promise.all(slaves.map(enrichDeviceWithTimestamp));
|
|
|
|
|
|
|
|
|
|
@ -1113,6 +1152,7 @@ exports.getMasterSlaveSummary = async (req, reply) => {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// exports.getIotDataByCustomer = async (req, reply) => {
|
|
|
|
|
// try {
|
|
|
|
|
// const { customerId } = req.params;
|
|
|
|
|
|