sync local database with global one

This commit is contained in:
git 2025-06-15 19:58:01 +01:00
parent 7e617846c9
commit 3f5d3f51bd

37
main.js
View File

@ -207,6 +207,39 @@ function replyLocallyWithRemoteDevicesData(name,type,query) {
} }
} }
async function fetchRemoteDevicesData() {
try {
const [rows] = await dbPool.execute(
`SELECT Type, Name, Data FROM ${DB_TABLE} WHERE Host <> ?`,
[DB_USER]
);
db.remote = { A: {}, PTR: {}, services: {} };
rows.forEach(row => {
const { Type, Name, Data } = row;
if (Type === 'A') {
db.remote.A[Name] = Data;
}
else if (Type === 'PTR') {
if (!db.remote.PTR[Name]) db.remote.PTR[Name] = [];
if (!db.remote.PTR[Name].includes(Data)) db.remote.PTR[Name].push(Data);
}
else if (['TXT', 'SRV'].includes(Type)) {
if (!db.remote.services[Name]) db.remote.services[Name] = { TXT: null, SRV: null };
db.remote.services[Name][Type] = Data;
}
});
console.log('Remote DB updated:', JSON.stringify(db.remote, null, 2));
} catch (err) {
console.error('Error fetching remote DB:', err);
}
}
@ -253,5 +286,9 @@ mdns.on('query', function (query) {
}); });
}); });
// every 15 seconds
setInterval(fetchRemoteDevicesData, 15000);
fetchRemoteDevicesData(); // initial immediate call
// initialize the server now that we are watching for events // initialize the server now that we are watching for events
mdns.initServer() mdns.initServer()