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,28 +102,30 @@ export class DeviceStatusFirebaseService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
console.log(`📝 Total logs to insert: ${allLogs.length}`);
|
console.log(`📝 Total logs to insert: ${allLogs.length}`);
|
||||||
const chunkSize = 300;
|
|
||||||
let insertedCount = 0;
|
|
||||||
|
|
||||||
for (let i = 0; i < allLogs.length; i += chunkSize) {
|
const insertLogsPromise = (async () => {
|
||||||
const chunk = allLogs.slice(i, i + chunkSize);
|
const chunkSize = 300;
|
||||||
try {
|
let insertedCount = 0;
|
||||||
const result = await this.deviceStatusLogRepository
|
|
||||||
.createQueryBuilder()
|
|
||||||
.insert()
|
|
||||||
.into('device-status-log') // or use DeviceStatusLogEntity
|
|
||||||
.values(chunk)
|
|
||||||
.orIgnore() // skip duplicates
|
|
||||||
.execute();
|
|
||||||
|
|
||||||
insertedCount += result.identifiers.length;
|
for (let i = 0; i < allLogs.length; i += chunkSize) {
|
||||||
console.log(
|
const chunk = allLogs.slice(i, i + chunkSize);
|
||||||
`✅ Inserted ${result.identifiers.length} / ${chunk.length} logs (chunk)`,
|
try {
|
||||||
);
|
const result = await this.deviceStatusLogRepository
|
||||||
} catch (error) {
|
.createQueryBuilder()
|
||||||
console.error('❌ Insert error (skipped chunk):', error.message);
|
.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(
|
console.log(
|
||||||
`✅ Total logs inserted: ${insertedCount} / ${allLogs.length}`,
|
`✅ Total logs inserted: ${insertedCount} / ${allLogs.length}`,
|
||||||
@ -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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -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);
|
||||||
|
Reference in New Issue
Block a user