mirror of
https://github.com/SyncrowIOT/backend.git
synced 2025-11-26 21:24:53 +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 { GroupRepository } from '@app/common/modules/group/repositories';
|
||||||
import { GroupDeviceRepository } from '@app/common/modules/group-device/repositories';
|
import { GroupDeviceRepository } from '@app/common/modules/group-device/repositories';
|
||||||
import { controlDeviceInterface } from 'src/device/interfaces/get.device.interface';
|
import { controlDeviceInterface } from 'src/device/interfaces/get.device.interface';
|
||||||
import { ControlDeviceDto } from 'src/device/dtos';
|
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class GroupService {
|
export class GroupService {
|
||||||
@ -84,6 +83,7 @@ export class GroupService {
|
|||||||
);
|
);
|
||||||
|
|
||||||
await Promise.all(groupDevicePromises);
|
await Promise.all(groupDevicePromises);
|
||||||
|
return { message: 'Group added successfully' };
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
if (err.code === '23505') {
|
if (err.code === '23505') {
|
||||||
throw new HttpException(
|
throw new HttpException(
|
||||||
@ -128,9 +128,9 @@ export class GroupService {
|
|||||||
throw error;
|
throw error;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
async controlDevice(controlDeviceDto: ControlDeviceDto) {
|
async controlDevice(deviceUuid: string, code: string, value: any) {
|
||||||
try {
|
try {
|
||||||
const response = await this.controlDeviceTuya(controlDeviceDto);
|
const response = await this.controlDeviceTuya(deviceUuid, code, value);
|
||||||
|
|
||||||
if (response.success) {
|
if (response.success) {
|
||||||
return response;
|
return response;
|
||||||
@ -145,17 +145,17 @@ export class GroupService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
async controlDeviceTuya(
|
async controlDeviceTuya(
|
||||||
controlDeviceDto: ControlDeviceDto,
|
deviceUuid: string,
|
||||||
|
code: string,
|
||||||
|
value: any,
|
||||||
): Promise<controlDeviceInterface> {
|
): Promise<controlDeviceInterface> {
|
||||||
try {
|
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({
|
const response = await this.tuya.request({
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
path,
|
path,
|
||||||
body: {
|
body: {
|
||||||
commands: [
|
commands: [{ code, value: value }],
|
||||||
{ code: controlDeviceDto.code, value: controlDeviceDto.value },
|
|
||||||
],
|
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -173,13 +173,14 @@ export class GroupService {
|
|||||||
try {
|
try {
|
||||||
await Promise.all(
|
await Promise.all(
|
||||||
devices.map(async (device) => {
|
devices.map(async (device) => {
|
||||||
return this.controlDevice({
|
return this.controlDevice(
|
||||||
deviceUuid: device.device.deviceTuyaUuid,
|
device.device.deviceTuyaUuid,
|
||||||
code: controlGroupDto.code,
|
controlGroupDto.code,
|
||||||
value: controlGroupDto.value,
|
controlGroupDto.value,
|
||||||
});
|
);
|
||||||
}),
|
}),
|
||||||
);
|
);
|
||||||
|
return { message: 'Group controlled successfully', success: true };
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
throw new HttpException(
|
throw new HttpException(
|
||||||
'Error controlling devices',
|
'Error controlling devices',
|
||||||
|
|||||||
@ -16,7 +16,6 @@ export class CheckProductUuidForAllDevicesGuard implements CanActivate {
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
const { deviceUuids } = req.body;
|
const { deviceUuids } = req.body;
|
||||||
console.log(deviceUuids);
|
|
||||||
|
|
||||||
await this.checkAllDevicesHaveSameProductUuid(deviceUuids);
|
await this.checkAllDevicesHaveSameProductUuid(deviceUuids);
|
||||||
|
|
||||||
@ -30,25 +29,27 @@ export class CheckProductUuidForAllDevicesGuard implements CanActivate {
|
|||||||
async checkAllDevicesHaveSameProductUuid(deviceUuids: string[]) {
|
async checkAllDevicesHaveSameProductUuid(deviceUuids: string[]) {
|
||||||
const firstDevice = await this.deviceRepository.findOne({
|
const firstDevice = await this.deviceRepository.findOne({
|
||||||
where: { uuid: deviceUuids[0] },
|
where: { uuid: deviceUuids[0] },
|
||||||
|
relations: ['productDevice'],
|
||||||
});
|
});
|
||||||
|
|
||||||
if (!firstDevice) {
|
if (!firstDevice) {
|
||||||
throw new BadRequestException('First device not found');
|
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++) {
|
for (let i = 1; i < deviceUuids.length; i++) {
|
||||||
const device = await this.deviceRepository.findOne({
|
const device = await this.deviceRepository.findOne({
|
||||||
where: { uuid: deviceUuids[i] },
|
where: { uuid: deviceUuids[i] },
|
||||||
|
relations: ['productDevice'],
|
||||||
});
|
});
|
||||||
|
|
||||||
if (!device) {
|
if (!device) {
|
||||||
throw new BadRequestException(`Device ${deviceUuids[i]} not found`);
|
throw new BadRequestException(`Device ${deviceUuids[i]} not found`);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (device.productDevice.uuid !== firstProductUuid) {
|
if (device.productDevice.prodType !== firstProductType) {
|
||||||
throw new BadRequestException(`Devices have different product UUIDs`);
|
throw new BadRequestException(`Devices have different product types`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -32,8 +32,6 @@ export class CheckRoomGuard implements CanActivate {
|
|||||||
const req = context.switchToHttp().getRequest();
|
const req = context.switchToHttp().getRequest();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
console.log(req.body);
|
|
||||||
|
|
||||||
if (req.query && req.query.roomUuid) {
|
if (req.query && req.query.roomUuid) {
|
||||||
const { roomUuid } = req.query;
|
const { roomUuid } = req.query;
|
||||||
await this.checkRoomIsFound(roomUuid);
|
await this.checkRoomIsFound(roomUuid);
|
||||||
@ -66,14 +64,11 @@ export class CheckRoomGuard implements CanActivate {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
async checkDeviceIsFoundFromTuya(deviceTuyaUuid: string) {
|
async checkDeviceIsFoundFromTuya(deviceTuyaUuid: string) {
|
||||||
console.log('deviceTuyaUuid: ', deviceTuyaUuid);
|
|
||||||
|
|
||||||
const path = `/v1.1/iot-03/devices/${deviceTuyaUuid}`;
|
const path = `/v1.1/iot-03/devices/${deviceTuyaUuid}`;
|
||||||
const response = await this.tuya.request({
|
const response = await this.tuya.request({
|
||||||
method: 'GET',
|
method: 'GET',
|
||||||
path,
|
path,
|
||||||
});
|
});
|
||||||
console.log('checkDeviceIsFoundFromTuya: ', response);
|
|
||||||
|
|
||||||
if (!response.success) {
|
if (!response.success) {
|
||||||
throw new NotFoundException('Device not found');
|
throw new NotFoundException('Device not found');
|
||||||
|
|||||||
Reference in New Issue
Block a user