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

View File

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