|
|
@ -4620,161 +4620,168 @@ exports.getBlockData = async (req, reply) => {
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const mqtt = require('mqtt');
|
|
|
|
const mqtt = require('mqtt');
|
|
|
|
|
|
|
|
const client = mqtt.connect('mqtt://35.207.198.4:1883'); // Connect to MQTT broker
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
client.on('connect', () => {
|
|
|
|
|
|
|
|
console.log('Connected to MQTT broker');
|
|
|
|
|
|
|
|
client.subscribe('water/iot-data', (err) => {
|
|
|
|
|
|
|
|
if (err) {
|
|
|
|
|
|
|
|
console.error('Error subscribing to topic:', err);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
console.log('Subscribed to water/iot-data topic');
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
const activeClients = {}; // Store unique clients by hw_Id
|
|
|
|
// Handling incoming MQTT messages
|
|
|
|
|
|
|
|
client.on('message', async (topic, message) => {
|
|
|
|
// Connect and process message for a specific hardware ID
|
|
|
|
console.log(`Message received on topic ${topic}:`, message.toString());
|
|
|
|
const connectAndProcessMessage = (hw_Id, data) => {
|
|
|
|
|
|
|
|
if (!activeClients[hw_Id]) {
|
|
|
|
|
|
|
|
const client = mqtt.connect('mqtt://35.207.198.4:1883', { clientId: `iot-client-${hw_Id}` });
|
|
|
|
|
|
|
|
activeClients[hw_Id] = client;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
client.on('connect', () => {
|
|
|
|
if (topic === 'water/iot-data') {
|
|
|
|
console.log(`Connected to MQTT broker for hw_Id: ${hw_Id}`);
|
|
|
|
try {
|
|
|
|
client.subscribe('water/iot-data', { qos: 1 }, (err) => {
|
|
|
|
const data = JSON.parse(message.toString());
|
|
|
|
if (err) {
|
|
|
|
const { hw_Id, Motor_status, tanks } = data.objects; // Updated variable names according to new format
|
|
|
|
console.error('Error subscribing for hw_Id:', hw_Id, err);
|
|
|
|
|
|
|
|
}
|
|
|
|
// Get the current date and time in the required format
|
|
|
|
|
|
|
|
const currentDate = new Date();
|
|
|
|
|
|
|
|
const date = currentDate.toISOString(); // ISO string for date
|
|
|
|
|
|
|
|
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 => ({
|
|
|
|
|
|
|
|
tankhardwareId: tank.Id, // Updated to match the new format
|
|
|
|
|
|
|
|
tankHeight: tank.level, // Updated to match the new format
|
|
|
|
|
|
|
|
date,
|
|
|
|
|
|
|
|
time
|
|
|
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Save IoT data for the received tanks
|
|
|
|
|
|
|
|
const iotTankData = new IotData({
|
|
|
|
|
|
|
|
hardwareId: hw_Id, // Updated variable name
|
|
|
|
|
|
|
|
Motor_status,
|
|
|
|
|
|
|
|
tanks: tankDocuments,
|
|
|
|
|
|
|
|
date,
|
|
|
|
|
|
|
|
time
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
await iotTankData.save();
|
|
|
|
|
|
|
|
|
|
|
|
client.on('close', () => {
|
|
|
|
|
|
|
|
console.log(`Disconnected MQTT client for hw_Id: ${hw_Id}`);
|
|
|
|
|
|
|
|
delete activeClients[hw_Id];
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
client.on('error', (err) => {
|
|
|
|
// Delete excess records (keep only the latest three records)
|
|
|
|
console.error(`Error for hw_Id ${hw_Id}:`, err);
|
|
|
|
const recordsToKeep = 3;
|
|
|
|
});
|
|
|
|
const recordsToDelete = await IotData.find({ hardwareId: hw_Id }) // Updated variable name
|
|
|
|
}
|
|
|
|
.sort({ date: -1, time: -1 })
|
|
|
|
|
|
|
|
.skip(recordsToKeep);
|
|
|
|
|
|
|
|
|
|
|
|
processIoTData(hw_Id, data);
|
|
|
|
for (const record of recordsToDelete) {
|
|
|
|
};
|
|
|
|
await record.remove();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Process IoT data and update database
|
|
|
|
// Process each tank to update water level and connections
|
|
|
|
const processIoTData = async (hw_Id, data) => {
|
|
|
|
for (const tank of tanks) {
|
|
|
|
console.log(`Processing data for hw_Id: ${hw_Id}`, data);
|
|
|
|
const { Id: tankhardwareId, level: tankHeight } = tank; // Updated to match the new format
|
|
|
|
const currentDate = new Date();
|
|
|
|
// Find the corresponding tank in the Tank schema using hardwareId and tankhardwareId
|
|
|
|
const date = currentDate.toISOString();
|
|
|
|
const existingTank = await Tank.findOne({ hardwareId: hw_Id, tankhardwareId }); // Updated variable name
|
|
|
|
const time = currentDate.toLocaleTimeString('en-IN', { hour12: false, timeZone: 'Asia/Kolkata' });
|
|
|
|
if (!existingTank) continue;
|
|
|
|
|
|
|
|
|
|
|
|
const tankDocuments = data.objects.tanks.map(tank => ({
|
|
|
|
const customerId = existingTank.customerId;
|
|
|
|
tankhardwareId: tank.Id,
|
|
|
|
const tank_name = existingTank.tankName;
|
|
|
|
tankHeight: tank.level,
|
|
|
|
|
|
|
|
date,
|
|
|
|
// Calculate water level using tank height and capacity
|
|
|
|
time
|
|
|
|
const tankHeightInCm = (parseInt(existingTank.height.replace(/,/g, ''), 10)) * 30.48; // Convert height to cm
|
|
|
|
}));
|
|
|
|
const tank_height = parseInt(tankHeightInCm.toFixed(0), 10);
|
|
|
|
|
|
|
|
const waterLevelHeight = tank_height - tankHeight;
|
|
|
|
const iotTankData = new IotData({
|
|
|
|
const waterCapacityPerCm = parseInt(existingTank.waterCapacityPerCm.replace(/,/g, ''), 10);
|
|
|
|
hardwareId: hw_Id,
|
|
|
|
|
|
|
|
Motor_status: data.objects.Motor_status,
|
|
|
|
const waterLevel = parseInt(waterLevelHeight * waterCapacityPerCm, 10); // Calculated water level
|
|
|
|
tanks: tankDocuments,
|
|
|
|
|
|
|
|
date,
|
|
|
|
// Update water level in the existing tank
|
|
|
|
time
|
|
|
|
if (waterLevel >= 0) {
|
|
|
|
});
|
|
|
|
existingTank.waterlevel = waterLevel;
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
await iotTankData.save();
|
|
|
|
// Update motor status
|
|
|
|
|
|
|
|
const status = Motor_status;
|
|
|
|
|
|
|
|
const motorTank = await Tank.findOne({ "connections.inputConnections.motor_id": hw_Id }); // Updated variable name
|
|
|
|
|
|
|
|
|
|
|
|
const recordsToKeep = 3;
|
|
|
|
if (!motorTank) {
|
|
|
|
const recordsToDelete = await IotData.find({ hardwareId: hw_Id })
|
|
|
|
console.log('Motor not found for the specified motor_id');
|
|
|
|
.sort({ date: -1, time: -1 })
|
|
|
|
return;
|
|
|
|
.skip(recordsToKeep);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
for (const record of recordsToDelete) {
|
|
|
|
// Find the inputConnection for the motor and update motor status
|
|
|
|
await record.remove();
|
|
|
|
const inputConnection = motorTank.connections.inputConnections.find(conn => conn.motor_id === hw_Id); // Updated variable name
|
|
|
|
}
|
|
|
|
if (inputConnection) {
|
|
|
|
|
|
|
|
inputConnection.motor_status = status; // Update motor status
|
|
|
|
|
|
|
|
console.log(inputConnection.motor_stop_status,"inputConnection.motor_stop_status")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (inputConnection.motor_stop_status === "1" && status === "2") {
|
|
|
|
|
|
|
|
console.log("got into forced manual")
|
|
|
|
|
|
|
|
console.log(inputConnection.motor_on_type,"before if motor on type")
|
|
|
|
|
|
|
|
// Check if motor_on_type is not already "forced_manual"
|
|
|
|
|
|
|
|
if (inputConnection.motor_on_type !== "forced_manual") {
|
|
|
|
|
|
|
|
inputConnection.motor_on_type = "forced_manual";
|
|
|
|
|
|
|
|
console.log("entered forced manual of if")
|
|
|
|
|
|
|
|
inputConnection.motor_stop_status = "2";
|
|
|
|
|
|
|
|
// Update startTime to the current time in the specified format
|
|
|
|
|
|
|
|
const currentTime = new Date();
|
|
|
|
|
|
|
|
const formattedTime = currentTime.toLocaleString('en-GB', {
|
|
|
|
|
|
|
|
day: '2-digit',
|
|
|
|
|
|
|
|
month: 'short',
|
|
|
|
|
|
|
|
year: 'numeric',
|
|
|
|
|
|
|
|
hour: '2-digit',
|
|
|
|
|
|
|
|
minute: '2-digit',
|
|
|
|
|
|
|
|
hour12: false,
|
|
|
|
|
|
|
|
}).replace(',', '');
|
|
|
|
|
|
|
|
inputConnection.startTime = formattedTime;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
for (const tank of data.objects.tanks) {
|
|
|
|
|
|
|
|
const { Id: tankhardwareId, level: tankHeight } = tank;
|
|
|
|
|
|
|
|
const existingTank = await Tank.findOne({ hardwareId: hw_Id, tankhardwareId });
|
|
|
|
|
|
|
|
if (!existingTank) continue;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const customerId = existingTank.customerId;
|
|
|
|
if (inputConnection.motor_stop_status === "2" && status === "1") {
|
|
|
|
const tank_name = existingTank.tankName;
|
|
|
|
console.log("got into forced manual stop")
|
|
|
|
|
|
|
|
console.log(inputConnection.motor_on_type,"before if motor on type stop")
|
|
|
|
|
|
|
|
// Check if motor_on_type is not already "forced_manual"
|
|
|
|
|
|
|
|
if (inputConnection.motor_on_type = "forced_manual") {
|
|
|
|
|
|
|
|
inputConnection.motor_on_type = "manual";
|
|
|
|
|
|
|
|
console.log("entered forced manual of if of stop")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Update startTime to the current time in the specified format
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
inputConnection.motor_stop_status = "1";
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const tankHeightInCm = parseInt(existingTank.height.replace(/,/g, ''), 10) * 30.48;
|
|
|
|
|
|
|
|
const tank_height = parseInt(tankHeightInCm.toFixed(0), 10);
|
|
|
|
|
|
|
|
const waterLevelHeight = tank_height - tankHeight;
|
|
|
|
|
|
|
|
const waterCapacityPerCm = parseInt(existingTank.waterCapacityPerCm.replace(/,/g, ''), 10);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const waterLevel = parseInt(waterLevelHeight * waterCapacityPerCm, 10);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (waterLevel >= 0) {
|
|
|
|
|
|
|
|
existingTank.waterlevel = waterLevel;
|
|
|
|
|
|
|
|
await existingTank.save();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
await linkedTank.save();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const motorTank = await Tank.findOne({ "connections.inputConnections.motor_id": hw_Id });
|
|
|
|
|
|
|
|
if (!motorTank) {
|
|
|
|
|
|
|
|
console.log('Motor not found for the specified motor_id');
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const inputConnection = motorTank.connections.inputConnections.find(conn => conn.motor_id === hw_Id);
|
|
|
|
|
|
|
|
if (inputConnection) {
|
|
|
|
|
|
|
|
inputConnection.motor_status = data.objects.Motor_status;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (inputConnection.motor_stop_status === "1" && data.objects.Motor_status === "2") {
|
|
|
|
|
|
|
|
if (inputConnection.motor_on_type !== "forced_manual") {
|
|
|
|
|
|
|
|
inputConnection.motor_on_type = "forced_manual";
|
|
|
|
|
|
|
|
inputConnection.motor_stop_status = "2";
|
|
|
|
|
|
|
|
const formattedTime = new Date().toLocaleString('en-GB', {
|
|
|
|
|
|
|
|
day: '2-digit',
|
|
|
|
|
|
|
|
month: 'short',
|
|
|
|
|
|
|
|
year: 'numeric',
|
|
|
|
|
|
|
|
hour: '2-digit',
|
|
|
|
|
|
|
|
minute: '2-digit',
|
|
|
|
|
|
|
|
hour12: false
|
|
|
|
|
|
|
|
}).replace(',', '');
|
|
|
|
|
|
|
|
inputConnection.startTime = formattedTime;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (inputConnection.motor_stop_status === "2" && data.objects.Motor_status === "1") {
|
|
|
|
await motorTank.save(); // Save the updated tank
|
|
|
|
if (inputConnection.motor_on_type === "forced_manual") {
|
|
|
|
|
|
|
|
inputConnection.motor_on_type = "manual";
|
|
|
|
|
|
|
|
inputConnection.motor_stop_status = "1";
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
await motorTank.save();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
console.log('Data processed successfully for hardwareId:', hw_Id);
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const mainClient = mqtt.connect('mqtt://35.207.198.4:1883', { clientId: `listener-client-${Date.now()}` });
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
mainClient.on('connect', () => {
|
|
|
|
console.log('Data processed successfully for hardwareId:', hw_Id); // Updated variable name
|
|
|
|
console.log('Main listener connected');
|
|
|
|
|
|
|
|
mainClient.subscribe('water/iot-data', { qos: 1 });
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
mainClient.on('message', (topic, message) => {
|
|
|
|
} catch (err) {
|
|
|
|
if (topic === 'water/iot-data') {
|
|
|
|
console.error('Error processing message:', err.message);
|
|
|
|
const data = JSON.parse(message.toString());
|
|
|
|
}
|
|
|
|
const { hw_Id } = data.objects;
|
|
|
|
|
|
|
|
connectAndProcessMessage(hw_Id, data);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|