mirror of
https://github.com/SyncrowIOT/backend.git
synced 2026-10-24 17:47:55 +00:00
Refactor controlDevice method for better readability
This commit is contained in:
@@ -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',
|
||||
|
||||
Reference in New Issue
Block a user