From 327d6786567a557d451631e00a5fd11b90160321 Mon Sep 17 00:00:00 2001 From: faris Aljohari <83524184+farisaljohari@users.noreply.github.com> Date: Fri, 28 Mar 2025 03:40:09 +0300 Subject: [PATCH] Enhance TuyaWebSocketService to handle environment-specific message extraction --- .../services/tuya.web.socket.service.ts | 42 ++++++++++++++++--- 1 file changed, 37 insertions(+), 5 deletions(-) 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 d117d62..4bf3787 100644 --- a/libs/common/src/helper/services/tuya.web.socket.service.ts +++ b/libs/common/src/helper/services/tuya.web.socket.service.ts @@ -5,13 +5,16 @@ import { DeviceStatusFirebaseService } from '@app/common/firebase/devices-status @Injectable() export class TuyaWebSocketService { - private client: any; // Adjust type according to your TuyaWebsocket client + private client: any; + private readonly isDevEnv: boolean; constructor( private readonly configService: ConfigService, private readonly deviceStatusFirebaseService: DeviceStatusFirebaseService, ) { - // Initialize the TuyaWebsocket client + this.isDevEnv = + this.configService.get('NODE_ENV') === 'development'; + this.client = new TuyaWebsocket({ accessId: this.configService.get('tuya-config.TUYA_ACCESS_ID'), accessKey: this.configService.get('tuya-config.TUYA_ACCESS_KEY'), @@ -37,10 +40,12 @@ export class TuyaWebSocketService { this.client.message(async (ws: WebSocket, message: any) => { try { + const { devId, status, logData } = this.extractMessageData(message); + await this.deviceStatusFirebaseService.addDeviceStatusToFirebase({ - deviceTuyaUuid: message.payload.data.bizData.devId, - status: message.payload.data.bizData.properties, - log: message.payload.data.bizData, + deviceTuyaUuid: devId, + status: status, + log: logData, }); this.client.ackMessage(message.messageId); @@ -69,4 +74,31 @@ export class TuyaWebSocketService { console.error('WebSocket error:', error); }); } + private extractMessageData(message: any): { + devId: string; + status: any; + logData: any; + } { + const payloadData = message.payload.data; + + if (this.isDevEnv) { + return { + devId: payloadData.bizData?.devId, + status: payloadData.bizData?.properties, + logData: payloadData.bizData, + }; + } else { + return { + devId: payloadData.devId, + status: payloadData.status, + logData: payloadData, + }; + } + } + + // private logDeviceData(devId: string, status: any, logData: any): void { + // console.log('Device ID:', devId); + // console.log('Status:', status); + // console.log('Full Data:', logData); + // } }