|
|
|
@ -5986,20 +5986,23 @@ async function processIotData(hw_Id, data) {
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
const { Motor_status, tanks } = data.objects;
|
|
|
|
const { Motor_status, tanks } = data.objects;
|
|
|
|
console.log(`📡 Processing data for hw_Id: ${hw_Id}`);
|
|
|
|
console.log(`📡 Processing data for hw_Id: ${hw_Id}`);
|
|
|
|
|
|
|
|
|
|
|
|
const currentDate = new Date();
|
|
|
|
const currentDate = new Date();
|
|
|
|
const date = currentDate.toISOString();
|
|
|
|
const date = currentDate.toISOString(); // ISO string for date
|
|
|
|
const time = currentDate.toLocaleTimeString('en-IN', { hour12: false, timeZone: 'Asia/Kolkata' });
|
|
|
|
const time = currentDate.toLocaleTimeString('en-IN', { hour12: false, timeZone: 'Asia/Kolkata' }); // Time in 'HH:MM:SS'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Create array of tank documents with current date and time
|
|
|
|
const tankDocuments = tanks.map(tank => ({
|
|
|
|
const tankDocuments = tanks.map(tank => ({
|
|
|
|
tankhardwareId: tank.Id,
|
|
|
|
tankhardwareId: tank.Id, // Updated to match the new format
|
|
|
|
tankHeight: tank.level,
|
|
|
|
tankHeight: tank.level, // Updated to match the new format
|
|
|
|
date,
|
|
|
|
date,
|
|
|
|
time
|
|
|
|
time
|
|
|
|
}));
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Save IoT data for the received tanks
|
|
|
|
const iotTankData = new IotData({
|
|
|
|
const iotTankData = new IotData({
|
|
|
|
hardwareId: hw_Id,
|
|
|
|
hardwareId: hw_Id, // Updated variable name
|
|
|
|
Motor_status,
|
|
|
|
Motor_status,
|
|
|
|
tanks: tankDocuments,
|
|
|
|
tanks: tankDocuments,
|
|
|
|
date,
|
|
|
|
date,
|
|
|
|
@ -6007,8 +6010,9 @@ async function processIotData(hw_Id, data) {
|
|
|
|
});
|
|
|
|
});
|
|
|
|
await iotTankData.save();
|
|
|
|
await iotTankData.save();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Delete excess records (keep only the latest three records)
|
|
|
|
const recordsToKeep = 3;
|
|
|
|
const recordsToKeep = 3;
|
|
|
|
const recordsToDelete = await IotData.find({ hardwareId: hw_Id })
|
|
|
|
const recordsToDelete = await IotData.find({ hardwareId: hw_Id }) // Updated variable name
|
|
|
|
.sort({ date: -1, time: -1 })
|
|
|
|
.sort({ date: -1, time: -1 })
|
|
|
|
.skip(recordsToKeep);
|
|
|
|
.skip(recordsToKeep);
|
|
|
|
|
|
|
|
|
|
|
|
@ -6016,38 +6020,58 @@ async function processIotData(hw_Id, data) {
|
|
|
|
await record.remove();
|
|
|
|
await record.remove();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Process each tank to update water level and connections
|
|
|
|
for (const tank of tanks) {
|
|
|
|
for (const tank of tanks) {
|
|
|
|
const { Id: tankhardwareId, level: tankHeight } = tank;
|
|
|
|
const { Id: tankhardwareId, level: tankHeight } = tank; // Updated to match the new format
|
|
|
|
const existingTank = await Tank.findOne({ hardwareId: hw_Id, tankhardwareId });
|
|
|
|
// Find the corresponding tank in the Tank schema using hardwareId and tankhardwareId
|
|
|
|
|
|
|
|
const existingTank = await Tank.findOne({ hardwareId: hw_Id, tankhardwareId }); // Updated variable name
|
|
|
|
if (!existingTank) continue;
|
|
|
|
if (!existingTank) continue;
|
|
|
|
|
|
|
|
|
|
|
|
const customerId = existingTank.customerId;
|
|
|
|
const customerId = existingTank.customerId;
|
|
|
|
const tank_name = existingTank.tankName;
|
|
|
|
const tank_name = existingTank.tankName;
|
|
|
|
|
|
|
|
|
|
|
|
const tankHeightInCm = (parseInt(existingTank.height.replace(/,/g, ''), 10)) * 30.48;
|
|
|
|
// Calculate water level using tank height and capacity
|
|
|
|
|
|
|
|
const tankHeightInCm = (parseInt(existingTank.height.replace(/,/g, ''), 10)) * 30.48; // Convert height to cm
|
|
|
|
const tank_height = parseInt(tankHeightInCm.toFixed(0), 10);
|
|
|
|
const tank_height = parseInt(tankHeightInCm.toFixed(0), 10);
|
|
|
|
const waterLevelHeight = tank_height - tankHeight;
|
|
|
|
const waterLevelHeight = tank_height - tankHeight;
|
|
|
|
const waterCapacityPerCm = parseInt(existingTank.waterCapacityPerCm.replace(/,/g, ''), 10);
|
|
|
|
const waterCapacityPerCm = parseInt(existingTank.waterCapacityPerCm.replace(/,/g, ''), 10);
|
|
|
|
|
|
|
|
|
|
|
|
const waterLevel = parseInt(waterLevelHeight * waterCapacityPerCm, 10);
|
|
|
|
const waterLevel = parseInt(waterLevelHeight * waterCapacityPerCm, 10); // Calculated water level
|
|
|
|
|
|
|
|
|
|
|
|
if (tankHeight > 0 && waterLevel >= 0) {
|
|
|
|
// Update water level in the existing tank
|
|
|
|
|
|
|
|
console.log(tankHeight,"this is located in tank controllers at iot-data mqtt sub ")
|
|
|
|
|
|
|
|
if (tankHeight>0 && waterLevel >= 0) {
|
|
|
|
existingTank.waterlevel = waterLevel;
|
|
|
|
existingTank.waterlevel = waterLevel;
|
|
|
|
await existingTank.save();
|
|
|
|
await existingTank.save();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Update linked tanks (input/output connections)
|
|
|
|
|
|
|
|
for (const outputConnection of existingTank.connections.outputConnections) {
|
|
|
|
|
|
|
|
const linkedTank = await Tank.findOne({ customerId, tankName: outputConnection.outputConnections, tankLocation: outputConnection.output_type });
|
|
|
|
|
|
|
|
if (linkedTank) {
|
|
|
|
|
|
|
|
for (const inputConnection of linkedTank.connections.inputConnections) {
|
|
|
|
|
|
|
|
if (inputConnection.inputConnections === tank_name) {
|
|
|
|
|
|
|
|
inputConnection.water_level = waterLevel; // Update water level for linked tank
|
|
|
|
|
|
|
|
await linkedTank.save(); // Save updated linked tank
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Update motor status
|
|
|
|
const status = Motor_status;
|
|
|
|
const status = Motor_status;
|
|
|
|
const motorTank = await Tank.findOne({ "connections.inputConnections.motor_id": hw_Id });
|
|
|
|
const motorTank = await Tank.findOne({ "connections.inputConnections.motor_id": hw_Id }); // Updated variable name
|
|
|
|
|
|
|
|
|
|
|
|
if (!motorTank) {
|
|
|
|
if (!motorTank) {
|
|
|
|
console.log('Motor not found for the specified motor_id');
|
|
|
|
console.log('Motor not found for the specified motor_id');
|
|
|
|
return;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const inputConnection = motorTank.connections.inputConnections.find(conn => conn.motor_id === hw_Id);
|
|
|
|
// Find the inputConnection for the motor and update motor status
|
|
|
|
|
|
|
|
const inputConnection = motorTank.connections.inputConnections.find(conn => conn.motor_id === hw_Id); // Updated variable name
|
|
|
|
if (inputConnection) {
|
|
|
|
if (inputConnection) {
|
|
|
|
inputConnection.motor_status = status;
|
|
|
|
inputConnection.motor_status = status; // Update motor status
|
|
|
|
if (inputConnection.motor_stop_status === "1" && status === 2 && inputConnection.motor_on_type !== "forced_manual") {
|
|
|
|
if (inputConnection.motor_stop_status === "1" && status === 2 && inputConnection.motor_on_type !== "forced_manual") {
|
|
|
|
const currentTime = moment().tz('Asia/Kolkata').format('DD-MMM-YYYY - HH:mm');
|
|
|
|
const currentTime = moment().tz('Asia/Kolkata').format('DD-MMM-YYYY - HH:mm');
|
|
|
|
inputConnection.motor_stop_status = "2";
|
|
|
|
inputConnection.motor_stop_status = "2";
|
|
|
|
@ -6058,13 +6082,16 @@ async function processIotData(hw_Id, data) {
|
|
|
|
if (inputConnection.motor_stop_status === "2" && status === 1) {
|
|
|
|
if (inputConnection.motor_stop_status === "2" && status === 1) {
|
|
|
|
const currentTime = moment().tz('Asia/Kolkata').format('DD-MMM-YYYY - HH:mm');
|
|
|
|
const currentTime = moment().tz('Asia/Kolkata').format('DD-MMM-YYYY - HH:mm');
|
|
|
|
inputConnection.motor_stop_status = "1";
|
|
|
|
inputConnection.motor_stop_status = "1";
|
|
|
|
|
|
|
|
inputConnection.motor_on_type = "manual";
|
|
|
|
inputConnection.stopTime = currentTime;
|
|
|
|
inputConnection.stopTime = currentTime;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
await motorTank.save();
|
|
|
|
await motorTank.save(); // Save the updated tank
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
console.log('✅ Data processed successfully for hardwareId:', hw_Id);
|
|
|
|
console.log('Data processed successfully for hardwareId:', hw_Id); // Updated variable name
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} catch (err) {
|
|
|
|
} catch (err) {
|
|
|
|
console.error('❌ Error processing IoT data:', err.message);
|
|
|
|
console.error('❌ Error processing IoT data:', err.message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|