|
|
|
@ -1248,6 +1248,87 @@ exports.mastrerList = async (req, reply) => {
|
|
|
|
|
// }
|
|
|
|
|
// };
|
|
|
|
|
|
|
|
|
|
// exports.getMasterSlaveSummary = async (req, reply) => {
|
|
|
|
|
// try {
|
|
|
|
|
// const { customerId } = req.params;
|
|
|
|
|
|
|
|
|
|
// if (!customerId) {
|
|
|
|
|
// return reply.status(400).send({ error: 'customerId is required' });
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
// // Fetch all devices for the customer
|
|
|
|
|
// const allDevices = await Insensors.find({ customerId }).lean();
|
|
|
|
|
|
|
|
|
|
// // Group devices
|
|
|
|
|
// const masters = allDevices.filter(dev => dev.type === 'master');
|
|
|
|
|
// const slaves = allDevices.filter(dev => dev.type === 'slave');
|
|
|
|
|
|
|
|
|
|
// // Create a map of slaves by connected_to hardwareId
|
|
|
|
|
// const slaveMap = {};
|
|
|
|
|
// for (const slave of slaves) {
|
|
|
|
|
// const masterId = slave.connected_to;
|
|
|
|
|
// if (!slaveMap[masterId]) {
|
|
|
|
|
// slaveMap[masterId] = [];
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
// const loraTime = slave.connected_lora_time || null;
|
|
|
|
|
// const loraDate = slave.connected_lora_date || null;
|
|
|
|
|
|
|
|
|
|
// slaveMap[masterId].push({
|
|
|
|
|
// hardwareId: slave.hardwareId,
|
|
|
|
|
// tankName: slave.tankName || null,
|
|
|
|
|
// location: slave.tankLocation || null,
|
|
|
|
|
// connected_status: slave.connected_status || 'disconnected',
|
|
|
|
|
// connected_lora_time: loraTime,
|
|
|
|
|
// connected_lora_date: loraDate
|
|
|
|
|
// });
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
// const response = [];
|
|
|
|
|
|
|
|
|
|
// for (const master of masters) {
|
|
|
|
|
// // Fetch latest IoT data for this master
|
|
|
|
|
// const latestData = await IotData.findOne({ hardwareId: master.hardwareId })
|
|
|
|
|
// .sort({ date: -1 })
|
|
|
|
|
// .lean();
|
|
|
|
|
|
|
|
|
|
// const enriched = {
|
|
|
|
|
// ...master,
|
|
|
|
|
// connected_status: master.connected_status || 'disconnected',
|
|
|
|
|
// connected_slave_count: slaveMap[master.hardwareId]?.length || 0,
|
|
|
|
|
// connected_slaves: slaveMap[master.hardwareId] || []
|
|
|
|
|
// };
|
|
|
|
|
|
|
|
|
|
// // Use saved GSM fields from Insensors
|
|
|
|
|
// if (master.gsm_last_check_time) {
|
|
|
|
|
// enriched.gsm_last_check_time = master.gsm_last_check_time;
|
|
|
|
|
|
|
|
|
|
// const indiaTime = moment.tz(master.gsm_last_check_time, 'Asia/Kolkata');
|
|
|
|
|
// enriched.connected_gsm_date = indiaTime.format('DD-MM-YYYY');
|
|
|
|
|
// enriched.connected_gsm_time = indiaTime.format('HH:mm:ss');
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
// // If LoRa timestamps are available from master (some masters act like slaves too)
|
|
|
|
|
// if (master.connected_lora_date && master.connected_lora_time) {
|
|
|
|
|
// enriched.connected_lora_date = master.connected_lora_date;
|
|
|
|
|
// enriched.connected_lora_time = master.connected_lora_time;
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
// response.push(enriched);
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
// return reply.send({
|
|
|
|
|
// status_code: 200,
|
|
|
|
|
// message: 'Master-slave summary retrieved successfully',
|
|
|
|
|
// data: response
|
|
|
|
|
// });
|
|
|
|
|
// } catch (err) {
|
|
|
|
|
// console.error('Error in getMasterSlaveSummary:', err);
|
|
|
|
|
// return reply.status(500).send({ error: 'Internal Server Error' });
|
|
|
|
|
// }
|
|
|
|
|
// };
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
exports.getMasterSlaveSummary = async (req, reply) => {
|
|
|
|
|
try {
|
|
|
|
|
const { customerId } = req.params;
|
|
|
|
@ -1280,7 +1361,12 @@ exports.getMasterSlaveSummary = async (req, reply) => {
|
|
|
|
|
location: slave.tankLocation || null,
|
|
|
|
|
connected_status: slave.connected_status || 'disconnected',
|
|
|
|
|
connected_lora_time: loraTime,
|
|
|
|
|
connected_lora_date: loraDate
|
|
|
|
|
connected_lora_date: loraDate,
|
|
|
|
|
type: slave.type || 'N/A', // Add 'type' field
|
|
|
|
|
typeOfWater: slave.typeOfWater || 'N/A', // Add 'typeOfWater' field
|
|
|
|
|
lora_last_check_time: slave.connected_lora_date && slave.connected_lora_time
|
|
|
|
|
? `${slave.connected_lora_date} ${slave.connected_lora_time}`
|
|
|
|
|
: null // Add 'lora_last_check_time' field
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -1329,8 +1415,6 @@ exports.getMasterSlaveSummary = async (req, reply) => {
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// exports.getIotDataByCustomer = async (req, reply) => {
|
|
|
|
|
// try {
|
|
|
|
|
// const { customerId } = req.params;
|
|
|
|
|