Refactor Device Module

This commit is contained in:
faris Aljohari
2024-04-27 23:27:40 +03:00
parent 165dfb575d
commit 8c790b0d96
14 changed files with 527 additions and 478 deletions
+15 -11
View File
@@ -43,7 +43,7 @@ export class GroupService {
const groupDevices = await this.groupDeviceRepository.find({
relations: ['group', 'device'],
where: {
device: { spaceUuid },
device: { spaceDevice: { uuid: spaceUuid } },
isActive: true,
},
});
@@ -129,22 +129,26 @@ export class GroupService {
}
}
async controlDevice(controlDeviceDto: ControlDeviceDto) {
const response = await this.controlDeviceTuya(controlDeviceDto);
try {
const response = await this.controlDeviceTuya(controlDeviceDto);
if (response.success) {
return response;
} else {
throw new HttpException(
response.msg || 'Unknown error',
HttpStatus.BAD_REQUEST,
);
if (response.success) {
return response;
} else {
throw new HttpException(
response.msg || 'Unknown error',
HttpStatus.BAD_REQUEST,
);
}
} catch (error) {
throw new HttpException('Device Not Found', HttpStatus.NOT_FOUND);
}
}
async controlDeviceTuya(
controlDeviceDto: ControlDeviceDto,
): Promise<controlDeviceInterface> {
try {
const path = `/v1.0/iot-03/devices/${controlDeviceDto.deviceId}/commands`;
const path = `/v1.0/iot-03/devices/${controlDeviceDto.deviceUuid}/commands`;
const response = await this.tuya.request({
method: 'POST',
path,
@@ -170,7 +174,7 @@ export class GroupService {
await Promise.all(
devices.map(async (device) => {
return this.controlDevice({
deviceId: device.device.deviceTuyaUuid,
deviceUuid: device.device.deviceTuyaUuid,
code: controlGroupDto.code,
value: controlGroupDto.value,
});