mirror of
https://github.com/SyncrowIOT/backend.git
synced 2025-07-10 07:07:21 +00:00
Enhance TuyaWebSocketService to handle environment-specific message extraction
This commit is contained in:
@ -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<string>('NODE_ENV') === 'development';
|
||||
|
||||
this.client = new TuyaWebsocket({
|
||||
accessId: this.configService.get<string>('tuya-config.TUYA_ACCESS_ID'),
|
||||
accessKey: this.configService.get<string>('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);
|
||||
// }
|
||||
}
|
||||
|
Reference in New Issue
Block a user