Merge pull request #428 from SyncrowIOT/add-queue-event-handler

Implement message queue for TuyaWebSocketService and batch processing
This commit is contained in:
faljawhary
2025-06-23 07:35:22 -06:00
committed by GitHub
2 changed files with 82 additions and 51 deletions

View File

@ -226,49 +226,49 @@ export class DeviceStatusFirebaseService {
}); });
await this.deviceStatusLogRepository.save(newLogs); await this.deviceStatusLogRepository.save(newLogs);
// if (addDeviceStatusDto.productType === ProductType.PC) { if (addDeviceStatusDto.productType === ProductType.PC) {
// const energyCodes = new Set([ const energyCodes = new Set([
// PowerClampEnergyEnum.ENERGY_CONSUMED, PowerClampEnergyEnum.ENERGY_CONSUMED,
// PowerClampEnergyEnum.ENERGY_CONSUMED_A, PowerClampEnergyEnum.ENERGY_CONSUMED_A,
// PowerClampEnergyEnum.ENERGY_CONSUMED_B, PowerClampEnergyEnum.ENERGY_CONSUMED_B,
// PowerClampEnergyEnum.ENERGY_CONSUMED_C, PowerClampEnergyEnum.ENERGY_CONSUMED_C,
// ]); ]);
// const energyStatus = addDeviceStatusDto?.log?.properties?.find((status) => const energyStatus = addDeviceStatusDto?.log?.properties?.find((status) =>
// energyCodes.has(status.code), energyCodes.has(status.code),
// ); );
// if (energyStatus) { if (energyStatus) {
// await this.powerClampService.updateEnergyConsumedHistoricalData( await this.powerClampService.updateEnergyConsumedHistoricalData(
// addDeviceStatusDto.deviceUuid, addDeviceStatusDto.deviceUuid,
// ); );
// } }
// } }
// if ( if (
// addDeviceStatusDto.productType === ProductType.CPS || addDeviceStatusDto.productType === ProductType.CPS ||
// addDeviceStatusDto.productType === ProductType.WPS addDeviceStatusDto.productType === ProductType.WPS
// ) { ) {
// const occupancyCodes = new Set([PresenceSensorEnum.PRESENCE_STATE]); const occupancyCodes = new Set([PresenceSensorEnum.PRESENCE_STATE]);
// const occupancyStatus = addDeviceStatusDto?.log?.properties?.find( const occupancyStatus = addDeviceStatusDto?.log?.properties?.find(
// (status) => occupancyCodes.has(status.code), (status) => occupancyCodes.has(status.code),
// ); );
// if (occupancyStatus) { if (occupancyStatus) {
// await this.occupancyService.updateOccupancySensorHistoricalData( await this.occupancyService.updateOccupancySensorHistoricalData(
// addDeviceStatusDto.deviceUuid, addDeviceStatusDto.deviceUuid,
// ); );
// await this.occupancyService.updateOccupancySensorHistoricalDurationData( await this.occupancyService.updateOccupancySensorHistoricalDurationData(
// addDeviceStatusDto.deviceUuid, addDeviceStatusDto.deviceUuid,
// ); );
// } }
// } }
// if (addDeviceStatusDto.productType === ProductType.AQI) { if (addDeviceStatusDto.productType === ProductType.AQI) {
// await this.aqiDataService.updateAQISensorHistoricalData( await this.aqiDataService.updateAQISensorHistoricalData(
// addDeviceStatusDto.deviceUuid, addDeviceStatusDto.deviceUuid,
// ); );
// } }
// Return the updated data // Return the updated data
const snapshot: DataSnapshot = await get(dataRef); const snapshot: DataSnapshot = await get(dataRef);
return snapshot.val(); return snapshot.val();

View File

@ -9,6 +9,14 @@ export class TuyaWebSocketService {
private client: any; private client: any;
private readonly isDevEnv: boolean; private readonly isDevEnv: boolean;
private messageQueue: {
devId: string;
status: any;
logData: any;
}[] = [];
private isProcessing = false;
constructor( constructor(
private readonly configService: ConfigService, private readonly configService: ConfigService,
private readonly deviceStatusFirebaseService: DeviceStatusFirebaseService, private readonly deviceStatusFirebaseService: DeviceStatusFirebaseService,
@ -26,12 +34,12 @@ export class TuyaWebSocketService {
}); });
if (this.configService.get<string>('tuya-config.TRUN_ON_TUYA_SOCKET')) { if (this.configService.get<string>('tuya-config.TRUN_ON_TUYA_SOCKET')) {
// Set up event handlers
this.setupEventHandlers(); this.setupEventHandlers();
// Start receiving messages
this.client.start(); this.client.start();
} }
// Trigger the queue processor every 2 seconds
setInterval(() => this.processQueue(), 10000);
} }
private setupEventHandlers() { private setupEventHandlers() {
@ -44,19 +52,13 @@ export class TuyaWebSocketService {
try { try {
const { devId, status, logData } = this.extractMessageData(message); const { devId, status, logData } = this.extractMessageData(message);
if (this.sosHandlerService.isSosTriggered(status)) { // Push to internal queue
await this.sosHandlerService.handleSosEvent(devId, logData); this.messageQueue.push({ devId, status, logData });
} else {
await this.deviceStatusFirebaseService.addDeviceStatusToFirebase({
deviceTuyaUuid: devId,
status: status,
log: logData,
});
}
// Acknowledge the message
this.client.ackMessage(message.messageId); this.client.ackMessage(message.messageId);
} catch (error) { } catch (error) {
console.error('Error processing message:', error); console.error('Error receiving message:', error);
} }
}); });
@ -80,6 +82,35 @@ export class TuyaWebSocketService {
console.error('WebSocket error:', error); console.error('WebSocket error:', error);
}); });
} }
private async processQueue() {
if (this.isProcessing || this.messageQueue.length === 0) return;
this.isProcessing = true;
const batch = [...this.messageQueue];
this.messageQueue = [];
try {
for (const item of batch) {
if (this.sosHandlerService.isSosTriggered(item.status)) {
await this.sosHandlerService.handleSosEvent(item.devId, item.logData);
} else {
await this.deviceStatusFirebaseService.addDeviceStatusToFirebase({
deviceTuyaUuid: item.devId,
status: item.status,
log: item.logData,
});
}
}
} catch (error) {
console.error('Error processing batch:', error);
// Re-add the batch to the queue for retry
this.messageQueue.unshift(...batch);
} finally {
this.isProcessing = false;
}
}
private extractMessageData(message: any): { private extractMessageData(message: any): {
devId: string; devId: string;
status: any; status: any;