mirror of
https://github.com/SyncrowIOT/backend.git
synced 2025-11-26 18:34:54 +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',
|
||||
|
||||
@ -16,7 +16,6 @@ export class CheckProductUuidForAllDevicesGuard implements CanActivate {
|
||||
|
||||
try {
|
||||
const { deviceUuids } = req.body;
|
||||
console.log(deviceUuids);
|
||||
|
||||
await this.checkAllDevicesHaveSameProductUuid(deviceUuids);
|
||||
|
||||
@ -30,25 +29,27 @@ export class CheckProductUuidForAllDevicesGuard implements CanActivate {
|
||||
async checkAllDevicesHaveSameProductUuid(deviceUuids: string[]) {
|
||||
const firstDevice = await this.deviceRepository.findOne({
|
||||
where: { uuid: deviceUuids[0] },
|
||||
relations: ['productDevice'],
|
||||
});
|
||||
|
||||
if (!firstDevice) {
|
||||
throw new BadRequestException('First device not found');
|
||||
}
|
||||
|
||||
const firstProductUuid = firstDevice.productDevice.uuid;
|
||||
const firstProductType = firstDevice.productDevice.prodType;
|
||||
|
||||
for (let i = 1; i < deviceUuids.length; i++) {
|
||||
const device = await this.deviceRepository.findOne({
|
||||
where: { uuid: deviceUuids[i] },
|
||||
relations: ['productDevice'],
|
||||
});
|
||||
|
||||
if (!device) {
|
||||
throw new BadRequestException(`Device ${deviceUuids[i]} not found`);
|
||||
}
|
||||
|
||||
if (device.productDevice.uuid !== firstProductUuid) {
|
||||
throw new BadRequestException(`Devices have different product UUIDs`);
|
||||
if (device.productDevice.prodType !== firstProductType) {
|
||||
throw new BadRequestException(`Devices have different product types`);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -32,8 +32,6 @@ export class CheckRoomGuard implements CanActivate {
|
||||
const req = context.switchToHttp().getRequest();
|
||||
|
||||
try {
|
||||
console.log(req.body);
|
||||
|
||||
if (req.query && req.query.roomUuid) {
|
||||
const { roomUuid } = req.query;
|
||||
await this.checkRoomIsFound(roomUuid);
|
||||
@ -66,14 +64,11 @@ export class CheckRoomGuard implements CanActivate {
|
||||
}
|
||||
}
|
||||
async checkDeviceIsFoundFromTuya(deviceTuyaUuid: string) {
|
||||
console.log('deviceTuyaUuid: ', deviceTuyaUuid);
|
||||
|
||||
const path = `/v1.1/iot-03/devices/${deviceTuyaUuid}`;
|
||||
const response = await this.tuya.request({
|
||||
method: 'GET',
|
||||
path,
|
||||
});
|
||||
console.log('checkDeviceIsFoundFromTuya: ', response);
|
||||
|
||||
if (!response.success) {
|
||||
throw new NotFoundException('Device not found');
|
||||
|
||||
Reference in New Issue
Block a user