Get single check masrter connected slave data

masterName location details added
master^2
Bhaskar 4 months ago
parent b48edf73a1
commit a20576d6f3

@ -2761,6 +2761,7 @@ exports.getIotDataByCustomer = async (req, reply) => {
};
exports.getIotDataByCustomerAndHardwareId = async (req, reply) => {
try {
const { customerId, hardwareId } = req.params;
@ -2769,16 +2770,14 @@ exports.getIotDataByCustomerAndHardwareId = async (req, reply) => {
return reply.code(400).send({ error: "Both customerId and hardwareId are required" });
}
// Get all sensors for the customer
// Step 1: Get all sensors
const sensors = await Insensors.find({ customerId });
if (!sensors.length) {
return reply.code(404).send({ message: "No sensors found for this customer." });
}
// Get IoT data for that master hardwareId
// Step 2: Get latest IoT data
const latestRecord = await IotData.findOne({ hardwareId }).sort({ date: -1 }).lean();
if (!latestRecord) {
return reply.code(404).send({
hardwareId,
@ -2787,18 +2786,34 @@ exports.getIotDataByCustomerAndHardwareId = async (req, reply) => {
});
}
// Step 3: Calculate GSM connection status
const indiaTime = moment.tz(latestRecord.date, "Asia/Kolkata");
const now = moment.tz("Asia/Kolkata");
const diffInMinutes = now.diff(indiaTime, "minutes");
const gsmConnected = diffInMinutes <= 1;
const message = gsmConnected ? "GSM is connected" : "GSM is not connected";
const gsmMessage = gsmConnected ? "GSM is connected" : "GSM is not connected";
// Get all slaves connected to this master
// Step 4: Get all connected slaves
const connectedSlaves = sensors.filter(sensor => sensor.connected_to?.trim() === hardwareId);
// Enrich slave tanks with tank data and LoRa status
// Step 5: Get orderMap for fallback master info
const orders = await Order.find({ customerId }).lean();
const orderMap = {};
orders.forEach(order => {
order.master_connections?.forEach(connection => {
orderMap[connection.hardwareId] = {
masterName: connection.master_name || null,
location: connection.location || null
};
});
});
// Step 6: Fallback master info from orderMap
const fallbackMasterInfo = orderMap[hardwareId] || { masterName: null, location: null };
// Step 7: Map tanks data
const tanks = connectedSlaves.map(slave => {
const slaveId = slave.hardwareId?.trim();
const slaveId = slave.tankhardwareId?.trim();
const matchedTank = latestRecord.tanks?.find(tank => tank.tankhardwareId === slaveId);
let loraConnected = false;
@ -2815,21 +2830,22 @@ exports.getIotDataByCustomerAndHardwareId = async (req, reply) => {
tankhardwareId: slaveId,
tankName: slave.tankName ?? null,
tankLocation: slave.tankLocation ?? null,
masterName: slave.masterName ?? null,
location: slave.location ?? null,
masterName: slave.masterName ?? fallbackMasterInfo.masterName,
location: slave.location ?? fallbackMasterInfo.location,
message: loraMessage,
latestTankData: matchedTank ?? null
};
});
// Step 8: Send response
return reply.send({
status_code: 200,
message: "Success",
data: {
hardwareId,
message,
masterName: connectedSlaves[0]?.masterName ?? null,
location: connectedSlaves[0]?.location ?? null,
message: gsmMessage,
masterName: connectedSlaves[0]?.masterName ?? fallbackMasterInfo.masterName,
location: connectedSlaves[0]?.location ?? fallbackMasterInfo.location,
tanks
}
});

@ -228,6 +228,9 @@ const installationschema = new mongoose.Schema({
departmentName: { type: String, default: null },
zone: { type: String, default: null },
type: { type: String },
dateOfLogin: { type: String, default: null },
timeOfLogin: { type: String, default: null },
currentTime: { type: Date, default: Date.now },
lastTicketRaisedAt: {type : String},
issues: [{ type: Object }], // existing issues array
masterDisconnected: [{ // new field for master disconnected details

Loading…
Cancel
Save