|
|
@ -240,60 +240,95 @@ exports.getConnectionsInfoOfParticularTank = async (req, reply) => {
|
|
|
|
//get tanks data by passing username
|
|
|
|
//get tanks data by passing username
|
|
|
|
exports.getTank = async (req, reply) => {
|
|
|
|
exports.getTank = async (req, reply) => {
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
await Tank.find({ customerId: req.query.customerId })
|
|
|
|
const customerId = req.query.customerId;
|
|
|
|
.exec()
|
|
|
|
if (!customerId) {
|
|
|
|
.then((docs) => {
|
|
|
|
return reply.send({ status_code: 400, error: "Missing customerId" });
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Use .lean() to get plain JavaScript objects, which are easier to modify
|
|
|
|
|
|
|
|
const tanks = await Tank.find({ customerId }).lean().exec();
|
|
|
|
|
|
|
|
|
|
|
|
let totalSwitchCount = 0;
|
|
|
|
let totalSwitchCount = 0;
|
|
|
|
let totalSensorCount = 0;
|
|
|
|
let totalSensorCount = 0;
|
|
|
|
|
|
|
|
let allSwitchConnectionsDetailed = []; // Array to hold the final consolidated list
|
|
|
|
|
|
|
|
|
|
|
|
const transformedDocs = docs.map((tank) => {
|
|
|
|
const transformedDocs = tanks.map((tank) => {
|
|
|
|
const inputConnections = tank.connections?.inputConnections || [];
|
|
|
|
const inputConnections = tank.connections?.inputConnections || [];
|
|
|
|
|
|
|
|
const currentToTank = tank.tankName || null; // Destination Tank Name
|
|
|
|
|
|
|
|
const currentToLocation = tank.tankLocation || null; // Destination Tank Location
|
|
|
|
|
|
|
|
|
|
|
|
// Count switches
|
|
|
|
// Filter only motor switch connections (raw data)
|
|
|
|
const switchCount = inputConnections.reduce((count, connection) => {
|
|
|
|
const switchConnectionsRaw = inputConnections.filter(conn => conn.inputismotor === true);
|
|
|
|
return count + (connection.inputismotor === true ? 1 : 0);
|
|
|
|
|
|
|
|
}, 0);
|
|
|
|
// Count switches for this tank and add to total
|
|
|
|
|
|
|
|
const switchCount = switchConnectionsRaw.length;
|
|
|
|
totalSwitchCount += switchCount;
|
|
|
|
totalSwitchCount += switchCount;
|
|
|
|
|
|
|
|
|
|
|
|
// Count sensors
|
|
|
|
// Count sensor requirement
|
|
|
|
if (tank.need_sensor?.toLowerCase() === "yes") {
|
|
|
|
if (String(tank.need_sensor).toLowerCase() === "yes") {
|
|
|
|
totalSensorCount++;
|
|
|
|
totalSensorCount++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Determine all_motor_status
|
|
|
|
// --- Create the detailed switch connection objects for the consolidated list ---
|
|
|
|
const allMotorStatus = inputConnections.some(connection => connection.motor_status === "2");
|
|
|
|
const switchConnectionsForThisTankDetailed = switchConnectionsRaw.map(conn => ({
|
|
|
|
|
|
|
|
// Spread all original properties from the switch connection
|
|
|
|
|
|
|
|
from_tank: conn.inputConnections || null, // Get source name from the connection itself
|
|
|
|
|
|
|
|
from_location: conn.input_type || null, // Get source type/location from the connection itself
|
|
|
|
|
|
|
|
to_tank: currentToTank, // Add destination tank name (parent object)
|
|
|
|
|
|
|
|
to_location: currentToLocation // Add destination location (parent object)
|
|
|
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
// Add the detailed connections from *this* tank to the overall list
|
|
|
|
|
|
|
|
allSwitchConnectionsDetailed.push(...switchConnectionsForThisTankDetailed);
|
|
|
|
|
|
|
|
// --- End detailed switch connection creation ---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Check if any motor is running (motor_status === "2") - keep as is
|
|
|
|
|
|
|
|
const isAnyMotorRunning = inputConnections.some(conn => conn.motor_status === "2");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Map original from_connections for display within the tank object (optional, but your original code did this)
|
|
|
|
|
|
|
|
const fromConnections = inputConnections.map(conn => ({
|
|
|
|
|
|
|
|
from_tank: conn.inputConnections || null,
|
|
|
|
|
|
|
|
from_type: conn.input_type || null,
|
|
|
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
|
|
// Add switch_count and all_motor_status to the response
|
|
|
|
// Return the transformed tank document for the 'data' array
|
|
|
|
return {
|
|
|
|
return {
|
|
|
|
...tank.toObject(), // Convert Mongoose document to plain object
|
|
|
|
...tank, // Include original tank fields
|
|
|
|
connections: {
|
|
|
|
connections: { // Keep the connections object structure
|
|
|
|
...tank.connections,
|
|
|
|
...tank.connections, // Include original connection details (source, input/output arrays)
|
|
|
|
switch_count: switchCount,
|
|
|
|
switch_count: switchCount, // Keep the count specific to this tank
|
|
|
|
|
|
|
|
// Decide if you want to keep the raw switch_connections here or remove it
|
|
|
|
|
|
|
|
// Keeping it might be useful for context within the specific tank object
|
|
|
|
|
|
|
|
switch_connections: switchConnectionsRaw, // Keep the raw list here
|
|
|
|
|
|
|
|
from_connections: fromConnections, // Keep this derived list here
|
|
|
|
},
|
|
|
|
},
|
|
|
|
all_motor_status: allMotorStatus, // Add all_motor_status field
|
|
|
|
all_motor_status: isAnyMotorRunning, // Keep this status flag
|
|
|
|
|
|
|
|
// You might not need these 'to_tank'/'to_location' fields at the root of the tank object anymore
|
|
|
|
|
|
|
|
// as they are now part of the consolidated switch_connections list, but keeping them doesn't hurt.
|
|
|
|
|
|
|
|
to_tank: currentToTank,
|
|
|
|
|
|
|
|
to_location: currentToLocation,
|
|
|
|
};
|
|
|
|
};
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Send the final response with the consolidated list
|
|
|
|
reply.send({
|
|
|
|
reply.send({
|
|
|
|
status_code: 200,
|
|
|
|
status_code: 200,
|
|
|
|
data: transformedDocs,
|
|
|
|
data: transformedDocs, // Array of processed tank objects
|
|
|
|
count: transformedDocs.length,
|
|
|
|
count: transformedDocs.length,
|
|
|
|
total_switch_count: totalSwitchCount,
|
|
|
|
total_switch_count: totalSwitchCount,
|
|
|
|
total_sensor_count: totalSensorCount,
|
|
|
|
total_sensor_count: totalSensorCount,
|
|
|
|
|
|
|
|
switch_connections: allSwitchConnectionsDetailed // The new consolidated list
|
|
|
|
});
|
|
|
|
});
|
|
|
|
})
|
|
|
|
|
|
|
|
.catch((err) => {
|
|
|
|
|
|
|
|
console.error(err);
|
|
|
|
|
|
|
|
reply.send({ status_code: 500, error: err.message });
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
} catch (err) {
|
|
|
|
} catch (err) {
|
|
|
|
console.error(err);
|
|
|
|
console.error("getTank error:", err);
|
|
|
|
reply.send({ status_code: 500, error: "Internal Server Error" });
|
|
|
|
reply.send({ status_code: 500, error: "Internal Server Error" });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
exports.getTanksensorcount = async (req, reply) => {
|
|
|
|
exports.getTanksensorcount = async (req, reply) => {
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
const { customerId } = req.params;
|
|
|
|
const { customerId } = req.params;
|
|
|
@ -6131,6 +6166,10 @@ async function processIotData(hw_Id, data) {
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
// Process each tank
|
|
|
|
// Process each tank
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for (const tank of tanks) {
|
|
|
|
for (const tank of tanks) {
|
|
|
|
const { Id: tankhardwareId, level: tankHeight } = tank;
|
|
|
|
const { Id: tankhardwareId, level: tankHeight } = tank;
|
|
|
|
const existingTank = await Tank.findOne({ hardwareId: hw_Id, tankhardwareId });
|
|
|
|
const existingTank = await Tank.findOne({ hardwareId: hw_Id, tankhardwareId });
|
|
|
|