mirror of
https://github.com/SyncrowIOT/backend.git
synced 2025-07-10 07:07:21 +00:00
refactor: optimize log insertion and clean up device cache handling in TuyaWebSocketService
This commit is contained in:
@ -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;
|
||||
}
|
||||
}
|
||||
|
@ -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);
|
||||
|
Reference in New Issue
Block a user