master^2
Bhaskar 5 months ago
parent 4280e1f4a8
commit 29374545d7

@ -1615,6 +1615,116 @@ exports.mastrerList = async (req, reply) => {
// return reply.status(500).send({ error: 'Internal Server Error' });
// }
// };
// exports.getMasterSlaveSummary = async (req, reply) => {
// try {
// const { customerId } = req.params;
// if (!customerId) {
// return reply.status(400).send({ error: 'customerId is required' });
// }
// // Fetch all masters for the customer from the Insensors schema
// const masters = await Insensors.find({ customerId, type: 'master' }).lean();
// // Fetch orders for the customer to map master connections
// const orders = await Order.find({ customerId }).lean();
// const orderMap = {};
// // Mapping master hardwareId from the orders schema to masterName and location
// orders.forEach(order => {
// order.master_connections.forEach(connection => {
// orderMap[connection.hardwareId] = {
// masterName: connection.master_name || 'Unknown',
// location: connection.location || 'Unknown'
// };
// });
// });
// const result = [];
// // Loop through each master and enrich it with data
// for (const master of masters) {
// const orderInfo = orderMap[master.hardwareId] || {}; // Get order info based on hardwareId
// const masterName = orderInfo.masterName || 'Unknown';
// const location = orderInfo.location || 'Unknown';
// const latestGsmData = await IotData.findOne({ hardwareId: master.hardwareId })
// .sort({ date: -1, time: -1 })
// .lean();
// let connectedGsmDate = null;
// let connectedGsmTime = null;
// if (latestGsmData?.date && latestGsmData?.time) {
// const indiaTime = moment.tz(latestGsmData.date, 'Asia/Kolkata');
// connectedGsmDate = indiaTime.format('DD-MM-YYYY');
// connectedGsmTime = latestGsmData.time;
// }
// let gsmLastDisconnect = master.gsm_last_disconnect_time;
// if (master.connected_status === 'disconnected' && connectedGsmDate && connectedGsmTime) {
// const disconnectTime = `${connectedGsmDate} ${connectedGsmTime}`;
// await Insensors.updateOne({ hardwareId: master.hardwareId }, { $set: { gsm_last_disconnect_time: disconnectTime } });
// gsmLastDisconnect = disconnectTime;
// }
// const connectedSlaves = [];
// const slaves = await Insensors.find({ connected_to: master.hardwareId, type: 'slave' }).lean();
// for (const slave of slaves) {
// const slaveIot = await IotData.findOne({ hardwareId: slave.hardwareId }).sort({ date: -1, time: -1 }).lean();
// const loraDate = slave.connected_lora_date || (slaveIot?.date ? moment.tz(slaveIot.date, 'Asia/Kolkata').format('DD-MM-YYYY') : null);
// const loraTime = slave.connected_lora_time || slaveIot?.time || null;
// let loraLastDisconnect = slave.lora_last_disconnect_time;
// if (slave.connected_status === 'disconnected' && loraDate && loraTime) {
// const disconnectTime = `${loraDate} ${loraTime}`;
// await Insensors.updateOne({ hardwareId: slave.hardwareId }, { $set: { lora_last_disconnect_time: disconnectTime } });
// loraLastDisconnect = disconnectTime;
// }
// connectedSlaves.push({
// hardwareId: slave.hardwareId,
// tankName: slave.tankName,
// location: slave.tankLocation,
// connected_status: slave.connected_status,
// connected_lora_date: loraDate,
// connected_lora_time: loraTime,
// lora_last_disconnect_time: loraLastDisconnect,
// type: slave.type || 'slave'
// });
// }
// result.push({
// hardwareId: master.hardwareId,
// masterName,
// location,
// type: master.type || 'master',
// connected_status: master.connected_status,
// connected_slave_count: connectedSlaves.length,
// connected_slaves: connectedSlaves,
// connected_gsm_date: connectedGsmDate,
// connected_gsm_time: connectedGsmTime,
// gsm_last_check_time: master.gsm_last_check_time || null,
// gsm_last_disconnect_time: gsmLastDisconnect,
// connected_lora_date: master.connected_lora_date || null,
// connected_lora_time: master.connected_lora_time || null
// });
// }
// return reply.send({
// status_code: 200,
// message: 'Master-slave summary retrieved successfully',
// data: result
// });
// } catch (error) {
// console.error('Error in getMasterSlaveSummary:', error);
// return reply.status(500).send({ error: 'Internal Server Error' });
// }
// };
exports.getMasterSlaveSummary = async (req, reply) => {
try {
const { customerId } = req.params;
@ -1623,30 +1733,30 @@ exports.getMasterSlaveSummary = async (req, reply) => {
return reply.status(400).send({ error: 'customerId is required' });
}
// Fetch all masters for the customer from the Insensors schema
// Fetch all masters for the customer
const masters = await Insensors.find({ customerId, type: 'master' }).lean();
// Fetch orders for the customer to map master connections
// Fetch orders to map hardwareId to masterName/location
const orders = await Order.find({ customerId }).lean();
const orderMap = {};
// Mapping master hardwareId from the orders schema to masterName and location
orders.forEach(order => {
order.master_connections.forEach(connection => {
orderMap[connection.hardwareId] = {
masterName: connection.master_name || 'Unknown',
location: connection.location || 'Unknown'
masterName: connection.master_name || null,
location: connection.location || null
};
});
});
const result = [];
// Loop through each master and enrich it with data
for (const master of masters) {
const orderInfo = orderMap[master.hardwareId] || {}; // Get order info based on hardwareId
const masterName = orderInfo.masterName || 'Unknown';
const location = orderInfo.location || 'Unknown';
const orderInfo = orderMap[master.hardwareId] || {};
// Prefer values from Insensors; fallback to orderMap
const masterName = master.masterName || orderInfo.masterName || null;
const location = master.location || orderInfo.location || null;
const latestGsmData = await IotData.findOne({ hardwareId: master.hardwareId })
.sort({ date: -1, time: -1 })
@ -1663,7 +1773,10 @@ exports.getMasterSlaveSummary = async (req, reply) => {
let gsmLastDisconnect = master.gsm_last_disconnect_time;
if (master.connected_status === 'disconnected' && connectedGsmDate && connectedGsmTime) {
const disconnectTime = `${connectedGsmDate} ${connectedGsmTime}`;
await Insensors.updateOne({ hardwareId: master.hardwareId }, { $set: { gsm_last_disconnect_time: disconnectTime } });
await Insensors.updateOne(
{ hardwareId: master.hardwareId },
{ $set: { gsm_last_disconnect_time: disconnectTime } }
);
gsmLastDisconnect = disconnectTime;
}
@ -1671,15 +1784,22 @@ exports.getMasterSlaveSummary = async (req, reply) => {
const slaves = await Insensors.find({ connected_to: master.hardwareId, type: 'slave' }).lean();
for (const slave of slaves) {
const slaveIot = await IotData.findOne({ hardwareId: slave.hardwareId }).sort({ date: -1, time: -1 }).lean();
const slaveIot = await IotData.findOne({ hardwareId: slave.hardwareId })
.sort({ date: -1, time: -1 })
.lean();
const loraDate = slave.connected_lora_date || (slaveIot?.date ? moment.tz(slaveIot.date, 'Asia/Kolkata').format('DD-MM-YYYY') : null);
const loraDate =
slave.connected_lora_date ||
(slaveIot?.date ? moment.tz(slaveIot.date, 'Asia/Kolkata').format('DD-MM-YYYY') : null);
const loraTime = slave.connected_lora_time || slaveIot?.time || null;
let loraLastDisconnect = slave.lora_last_disconnect_time;
if (slave.connected_status === 'disconnected' && loraDate && loraTime) {
const disconnectTime = `${loraDate} ${loraTime}`;
await Insensors.updateOne({ hardwareId: slave.hardwareId }, { $set: { lora_last_disconnect_time: disconnectTime } });
await Insensors.updateOne(
{ hardwareId: slave.hardwareId },
{ $set: { lora_last_disconnect_time: disconnectTime } }
);
loraLastDisconnect = disconnectTime;
}
@ -1724,6 +1844,7 @@ exports.getMasterSlaveSummary = async (req, reply) => {
}
};
// 🔍 Helper to get tankHeight from latest IotData record
async function getTankHeight(hardwareId) {
const iotData = await IotData.findOne({ 'tanks.tankhardwareId': hardwareId })

Loading…
Cancel
Save