diff --git a/libs/common/src/firebase/devices-status/services/devices-status.service.ts b/libs/common/src/firebase/devices-status/services/devices-status.service.ts index 3972b2a..b3ef843 100644 --- a/libs/common/src/firebase/devices-status/services/devices-status.service.ts +++ b/libs/common/src/firebase/devices-status/services/devices-status.service.ts @@ -102,28 +102,30 @@ export class DeviceStatusFirebaseService { } console.log(`📝 Total logs to insert: ${allLogs.length}`); - const chunkSize = 300; - let insertedCount = 0; - for (let i = 0; i < allLogs.length; i += chunkSize) { - const chunk = allLogs.slice(i, i + chunkSize); - try { - const result = await this.deviceStatusLogRepository - .createQueryBuilder() - .insert() - .into('device-status-log') // or use DeviceStatusLogEntity - .values(chunk) - .orIgnore() // skip duplicates - .execute(); + const insertLogsPromise = (async () => { + const chunkSize = 300; + let insertedCount = 0; - insertedCount += result.identifiers.length; - console.log( - `✅ Inserted ${result.identifiers.length} / ${chunk.length} logs (chunk)`, - ); - } catch (error) { - console.error('❌ Insert error (skipped chunk):', error.message); + for (let i = 0; i < allLogs.length; i += chunkSize) { + const chunk = allLogs.slice(i, i + chunkSize); + try { + const result = await this.deviceStatusLogRepository + .createQueryBuilder() + .insert() + .into('device-status-log') // or use DeviceStatusLogEntity + .values(chunk) + .orIgnore() // skip duplicates + .execute(); + + insertedCount += result.identifiers.length; + console.log( + `✅ Inserted ${result.identifiers.length} / ${chunk.length} logs (chunk)`, + ); + } catch (error) { + console.error('❌ Insert error (skipped chunk):', error.message); + } } - } console.log( `✅ Total logs inserted: ${insertedCount} / ${allLogs.length}`, @@ -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; } } diff --git a/libs/common/src/helper/services/tuya.web.socket.service.ts b/libs/common/src/helper/services/tuya.web.socket.service.ts index 63de80b..d30200f 100644 --- a/libs/common/src/helper/services/tuya.web.socket.service.ts +++ b/libs/common/src/helper/services/tuya.web.socket.service.ts @@ -19,13 +19,11 @@ export class TuyaWebSocketService implements OnModuleInit { }[] = []; private isProcessing = false; - private deviceCache: Map = new Map(); constructor( private readonly configService: ConfigService, private readonly deviceStatusFirebaseService: DeviceStatusFirebaseService, private readonly sosHandlerService: SosHandlerService, - private readonly deviceRepository: DeviceRepository, ) { this.isDevEnv = this.configService.get('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('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);