refactor: optimize log insertion and clean up device cache handling in TuyaWebSocketService

This commit is contained in:
faris Aljohari
2025-06-25 18:57:56 -06:00
parent 04bd156df1
commit f80d097ff8
2 changed files with 24 additions and 39 deletions

View File

@ -102,6 +102,8 @@ export class DeviceStatusFirebaseService {
}
console.log(`📝 Total logs to insert: ${allLogs.length}`);
const insertLogsPromise = (async () => {
const chunkSize = 300;
let insertedCount = 0;
@ -153,7 +155,6 @@ export class DeviceStatusFirebaseService {
// Return null if device not found or no UUID
return null;
} catch (error) {
console.error('❌ Error in addDeviceStatusToFirebase:', error);
return null;
}
}

View File

@ -19,13 +19,11 @@ export class TuyaWebSocketService implements OnModuleInit {
}[] = [];
private isProcessing = false;
private deviceCache: Map<string, any> = new Map();
constructor(
private readonly configService: ConfigService,
private readonly deviceStatusFirebaseService: DeviceStatusFirebaseService,
private readonly sosHandlerService: SosHandlerService,
private readonly deviceRepository: DeviceRepository,
) {
this.isDevEnv =
this.configService.get<string>('NODE_ENV') === 'development';
@ -38,11 +36,6 @@ export class TuyaWebSocketService implements OnModuleInit {
maxRetryTimes: 100,
});
this.loadAllActiveDevices();
// Reload device cache every 1 hour
setInterval(() => this.loadAllActiveDevices(), 60 * 60 * 1000);
if (this.configService.get<string>('tuya-config.TRUN_ON_TUYA_SOCKET')) {
this.setupEventHandlers();
this.client.start();
@ -74,7 +67,6 @@ export class TuyaWebSocketService implements OnModuleInit {
}
private setupEventHandlers() {
// Event handlers
this.client.open(() => {
console.log('open');
});
@ -89,19 +81,14 @@ export class TuyaWebSocketService implements OnModuleInit {
const device = this.deviceCache.get(devId);
if (!device) {
// console.warn(`⚠️ Device not found in cache: ${devId}`);
// console.log(⛔ Unknown device: ${devId}, message ignored.);
this.client.ackMessage(message.messageId);
return;
}
if (this.sosHandlerService.isSosTriggered(status)) {
await this.sosHandlerService.handleSosEventFirebase(
devId,
logData,
this.deviceCache,
);
await this.sosHandlerService.handleSosEventFirebase(devId, logData);
} else {
// Firebase real-time update
await this.deviceStatusFirebaseService.addDeviceStatusToFirebase({
deviceTuyaUuid: devId,
status,
@ -116,10 +103,9 @@ export class TuyaWebSocketService implements OnModuleInit {
// Acknowledge the message
this.client.ackMessage(message.messageId);
} catch (error) {
console.error('Error receiving message:', error);
console.error('Error receiving message:', error);
}
});
this.client.reconnect(() => {
console.log('reconnect');
});
@ -156,14 +142,12 @@ export class TuyaWebSocketService implements OnModuleInit {
try {
await this.deviceStatusFirebaseService.addBatchDeviceStatusToOurDb(
batch.map((item) => ({
batch.map((item) => ({
deviceTuyaUuid: item.devId,
status: item.status,
log: item.logData,
device: item.device,
})),
this.deviceCache,
);
} catch (error) {
console.error('❌ Error processing batch:', error);