|
|
|
@ -1932,6 +1932,95 @@ exports.getTankDetailsByMaster = async (req, reply) => {
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
exports.getSlaveTankDetails = async (req, reply) => {
|
|
|
|
|
try {
|
|
|
|
|
const { customerId, hardwareId, tankHardwareId } = req.params;
|
|
|
|
|
|
|
|
|
|
if (!customerId || !hardwareId || !tankHardwareId) {
|
|
|
|
|
return reply.code(400).send({
|
|
|
|
|
success: false,
|
|
|
|
|
message: 'customerId, hardwareId, and tankHardwareId are required'
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Step 1: find slave device (type: slave)
|
|
|
|
|
const slaveDevice = await Insensors.findOne({ customerId, connected_to: hardwareId, type: 'slave' }).lean();
|
|
|
|
|
if (!slaveDevice) {
|
|
|
|
|
return reply.code(404).send({
|
|
|
|
|
success: false,
|
|
|
|
|
message: 'Slave device not found'
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Step 2: find master hardwareId from slave.connected_to
|
|
|
|
|
const masterHardwareId = slaveDevice.connected_to;
|
|
|
|
|
if (!masterHardwareId) {
|
|
|
|
|
return reply.code(404).send({
|
|
|
|
|
success: false,
|
|
|
|
|
message: 'Slave device has no connected_to (master hardwareId)'
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Step 3: find master info from Order.master_connections
|
|
|
|
|
const order = await Order.findOne({ customerId, "master_connections.hardwareId": masterHardwareId }).lean();
|
|
|
|
|
if (!order) {
|
|
|
|
|
return reply.code(404).send({
|
|
|
|
|
success: false,
|
|
|
|
|
message: 'Order not found for connected master'
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const masterInfo = order.master_connections.find(m => m.hardwareId === masterHardwareId);
|
|
|
|
|
|
|
|
|
|
const masterName = masterInfo ? masterInfo.master_name : null;
|
|
|
|
|
const location = masterInfo ? masterInfo.location : null;
|
|
|
|
|
|
|
|
|
|
// Step 4: get slave's tank details
|
|
|
|
|
const tank = await Tank.findOne({ customerId, hardwareId, tankhardwareId: tankHardwareId }).lean();
|
|
|
|
|
if (!tank) {
|
|
|
|
|
return reply.code(404).send({
|
|
|
|
|
success: false,
|
|
|
|
|
message: 'Tank not found for slave'
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Step 5: parse dimensions
|
|
|
|
|
const tankHeightFeet = parseInt(tank.height, 10);
|
|
|
|
|
const tankWidthFeet = parseInt(tank.width, 10);
|
|
|
|
|
const tankLengthFeet = parseInt(tank.length, 10);
|
|
|
|
|
|
|
|
|
|
return reply.send({
|
|
|
|
|
success: true,
|
|
|
|
|
data: {
|
|
|
|
|
masterName,
|
|
|
|
|
location,
|
|
|
|
|
slaveHardwareId: hardwareId,
|
|
|
|
|
tankHardwareId,
|
|
|
|
|
tankName: tank.tankName,
|
|
|
|
|
blockName: tank.blockName,
|
|
|
|
|
shape: tank.shape,
|
|
|
|
|
capacity: tank.capacity,
|
|
|
|
|
waterCapacityPerCm: tank.waterCapacityPerCm,
|
|
|
|
|
typeOfWater: tank.typeOfWater,
|
|
|
|
|
tankLocation: tank.tankLocation,
|
|
|
|
|
height: tankHeightFeet,
|
|
|
|
|
width: tankWidthFeet,
|
|
|
|
|
length: tankLengthFeet,
|
|
|
|
|
heightInCm: tankHeightFeet * 30.48,
|
|
|
|
|
widthInCm: tankWidthFeet * 30.48,
|
|
|
|
|
lengthInCm: tankLengthFeet * 30.48
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error('Error fetching slave tank details:', error);
|
|
|
|
|
reply.code(500).send({
|
|
|
|
|
success: false,
|
|
|
|
|
message: 'Internal server error'
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|