Refactor controlDevice method for better readability

This commit is contained in:
faris Aljohari
2024-04-28 09:37:11 +03:00
parent bdcc065d73
commit 307761c915
3 changed files with 19 additions and 22 deletions
+14 -13
View File
@@ -16,7 +16,6 @@ import { RenameGroupDto } from '../dtos/rename.group.dto copy';
import { GroupRepository } from '@app/common/modules/group/repositories';
import { GroupDeviceRepository } from '@app/common/modules/group-device/repositories';
import { controlDeviceInterface } from 'src/device/interfaces/get.device.interface';
import { ControlDeviceDto } from 'src/device/dtos';
@Injectable()
export class GroupService {
@@ -84,6 +83,7 @@ export class GroupService {
);
await Promise.all(groupDevicePromises);
return { message: 'Group added successfully' };
} catch (err) {
if (err.code === '23505') {
throw new HttpException(
@@ -128,9 +128,9 @@ export class GroupService {
throw error;
}
}
async controlDevice(controlDeviceDto: ControlDeviceDto) {
async controlDevice(deviceUuid: string, code: string, value: any) {
try {
const response = await this.controlDeviceTuya(controlDeviceDto);
const response = await this.controlDeviceTuya(deviceUuid, code, value);
if (response.success) {
return response;
@@ -145,17 +145,17 @@ export class GroupService {
}
}
async controlDeviceTuya(
controlDeviceDto: ControlDeviceDto,
deviceUuid: string,
code: string,
value: any,
): Promise<controlDeviceInterface> {
try {
const path = `/v1.0/iot-03/devices/${controlDeviceDto.deviceUuid}/commands`;
const path = `/v1.0/iot-03/devices/${deviceUuid}/commands`;
const response = await this.tuya.request({
method: 'POST',
path,
body: {
commands: [
{ code: controlDeviceDto.code, value: controlDeviceDto.value },
],
commands: [{ code, value: value }],
},
});
@@ -173,13 +173,14 @@ export class GroupService {
try {
await Promise.all(
devices.map(async (device) => {
return this.controlDevice({
deviceUuid: device.device.deviceTuyaUuid,
code: controlGroupDto.code,
value: controlGroupDto.value,
});
return this.controlDevice(
device.device.deviceTuyaUuid,
controlGroupDto.code,
controlGroupDto.value,
);
}),
);
return { message: 'Group controlled successfully', success: true };
} catch (error) {
throw new HttpException(
'Error controlling devices',