refactor: clean up unused services and optimize batch processing in DeviceStatusFirebaseService

This commit is contained in:
faris Aljohari
2025-06-25 03:20:12 -06:00
parent c48adb73b5
commit 43ab0030f0
3 changed files with 95 additions and 127 deletions

View File

@ -38,8 +38,8 @@ export class TuyaWebSocketService {
this.client.start();
}
// Trigger the queue processor every 2 seconds
setInterval(() => this.processQueue(), 10000);
// Trigger the queue processor every 15 seconds
setInterval(() => this.processQueue(), 15000);
}
private setupEventHandlers() {
@ -93,32 +93,30 @@ export class TuyaWebSocketService {
});
}
private async processQueue() {
if (this.isProcessing || this.messageQueue.length === 0) return;
if (this.isProcessing) {
console.log('⏳ Skipping: still processing previous batch');
return;
}
if (this.messageQueue.length === 0) return;
this.isProcessing = true;
const batch = [...this.messageQueue];
this.messageQueue = [];
console.log(`🔁 Processing batch of size: ${batch.length}`);
try {
for (const item of batch) {
if (this.sosHandlerService.isSosTriggered(item.status)) {
await this.sosHandlerService.handleSosEventOurDb(
item.devId,
item.logData,
);
} else {
await this.deviceStatusFirebaseService.addDeviceStatusToOurDb({
deviceTuyaUuid: item.devId,
status: item.status,
log: item.logData,
});
}
}
await this.deviceStatusFirebaseService.addBatchDeviceStatusToOurDb(
batch.map((item) => ({
deviceTuyaUuid: item.devId,
status: item.status,
log: item.logData,
})),
);
} catch (error) {
console.error('Error processing batch:', error);
// Re-add the batch to the queue for retry
this.messageQueue.unshift(...batch);
console.error('Error processing batch:', error);
this.messageQueue.unshift(...batch); // retry
} finally {
this.isProcessing = false;
}