|
|
|
@ -8078,3 +8078,74 @@ exports.compareMeasuredHeight = async (req, reply) => {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// generateAndSaveTankExcel();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async function publishAllTankMotorsForCustomer(customerId) {
|
|
|
|
|
try {
|
|
|
|
|
const tanks = await Tank.find({ customerId, status: 'active' }).lean();
|
|
|
|
|
|
|
|
|
|
for (const tank of tanks) {
|
|
|
|
|
const { tankName, tankLocation, connections } = tank;
|
|
|
|
|
const inputConnections = connections?.inputConnections || [];
|
|
|
|
|
|
|
|
|
|
for (const conn of inputConnections) {
|
|
|
|
|
if (!conn.motor_id || conn.status !== 'active') continue;
|
|
|
|
|
|
|
|
|
|
let time = 0;
|
|
|
|
|
|
|
|
|
|
if (conn.motor_status === '2') {
|
|
|
|
|
const now = moment().tz('Asia/Kolkata');
|
|
|
|
|
|
|
|
|
|
if (conn.startTime) {
|
|
|
|
|
const start = moment(conn.startTime, 'DD-MMM-YYYY - HH:mm');
|
|
|
|
|
|
|
|
|
|
if (start.isValid()) {
|
|
|
|
|
const hoursElapsed = moment.duration(now.diff(start)).asHours();
|
|
|
|
|
|
|
|
|
|
if (hoursElapsed <= 12) {
|
|
|
|
|
time = Math.floor(moment.duration(now.diff(start)).asMinutes());
|
|
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
console.log(`⏳ Skipped motor_id ${conn.motor_id} — startTime older than 12 hours`);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
console.warn(`⚠️ Invalid startTime format for motor_id ${conn.motor_id}: ${conn.startTime}`);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
console.log(`⚠️ startTime is null for motor_id ${conn.motor_id}, sending time = 0`);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const payload = {
|
|
|
|
|
tankName,
|
|
|
|
|
tankLocation,
|
|
|
|
|
motor_status: conn.motor_status,
|
|
|
|
|
customerId,
|
|
|
|
|
time,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const topic = `water/motor-status/${conn.motor_id}`;
|
|
|
|
|
client.publish(topic, JSON.stringify(payload), { qos: 1 }, (err) => {
|
|
|
|
|
if (err) {
|
|
|
|
|
console.error(`❌ Failed to publish to ${topic}:`, err.message);
|
|
|
|
|
} else {
|
|
|
|
|
console.log(`📤 Published to ${topic}:`, payload);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} catch (err) {
|
|
|
|
|
console.error(`❌ Error publishing motor data for customer ${customerId}:`, err.message);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 🕒 Call every 30 seconds
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Run every 30 seconds
|
|
|
|
|
setInterval(() => {
|
|
|
|
|
publishAllTankMotorsForCustomer('AWSUSKY4');
|
|
|
|
|
}, 30000);
|
|
|
|
|
|
|
|
|
|