mirror of
https://github.com/SyncrowIOT/backend.git
synced 2025-11-26 08:54:54 +00:00
refactor: 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()
|
@Injectable()
|
||||||
export class TuyaWebSocketService {
|
export class TuyaWebSocketService {
|
||||||
private client: any; // Adjust type according to your TuyaWebsocket client
|
private client: any;
|
||||||
|
private readonly isDevEnv: boolean;
|
||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
private readonly configService: ConfigService,
|
private readonly configService: ConfigService,
|
||||||
private readonly deviceStatusFirebaseService: DeviceStatusFirebaseService,
|
private readonly deviceStatusFirebaseService: DeviceStatusFirebaseService,
|
||||||
) {
|
) {
|
||||||
// Initialize the TuyaWebsocket client
|
this.isDevEnv =
|
||||||
|
this.configService.get<string>('NODE_ENV') === 'development';
|
||||||
|
|
||||||
this.client = new TuyaWebsocket({
|
this.client = new TuyaWebsocket({
|
||||||
accessId: this.configService.get<string>('tuya-config.TUYA_ACCESS_ID'),
|
accessId: this.configService.get<string>('tuya-config.TUYA_ACCESS_ID'),
|
||||||
accessKey: this.configService.get<string>('tuya-config.TUYA_ACCESS_KEY'),
|
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) => {
|
this.client.message(async (ws: WebSocket, message: any) => {
|
||||||
try {
|
try {
|
||||||
|
const { devId, status, logData } = this.extractMessageData(message);
|
||||||
|
|
||||||
await this.deviceStatusFirebaseService.addDeviceStatusToFirebase({
|
await this.deviceStatusFirebaseService.addDeviceStatusToFirebase({
|
||||||
deviceTuyaUuid: message.payload.data.bizData.devId,
|
deviceTuyaUuid: devId,
|
||||||
status: message.payload.data.bizData.properties,
|
status: status,
|
||||||
log: message.payload.data.bizData,
|
log: logData,
|
||||||
});
|
});
|
||||||
|
|
||||||
this.client.ackMessage(message.messageId);
|
this.client.ackMessage(message.messageId);
|
||||||
@ -69,4 +74,31 @@ export class TuyaWebSocketService {
|
|||||||
console.error('WebSocket error:', error);
|
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