mirror of
https://github.com/SyncrowIOT/backend.git
synced 2025-07-10 23:27:31 +00:00
finished refactor devices module
This commit is contained in:
4
libs/common/src/constants/batch-device.enum.ts
Normal file
4
libs/common/src/constants/batch-device.enum.ts
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
export enum BatchDeviceTypeEnum {
|
||||||
|
RESET = 'RESET',
|
||||||
|
COMMAND = 'COMMAND',
|
||||||
|
}
|
@ -473,14 +473,9 @@ export class ControllerRoute {
|
|||||||
public static readonly ROUTE = 'device';
|
public static readonly ROUTE = 'device';
|
||||||
|
|
||||||
static ACTIONS = class {
|
static ACTIONS = class {
|
||||||
public static readonly ADD_DEVICE_TO_USER_SUMMARY = 'Add device to user';
|
public static readonly ADD_DEVICE_SUMMARY = 'Add new device';
|
||||||
public static readonly ADD_DEVICE_TO_USER_DESCRIPTION =
|
public static readonly ADD_DEVICE_DESCRIPTION =
|
||||||
'This endpoint adds a device to a user in the system.';
|
'This endpoint adds a new device in the system.';
|
||||||
|
|
||||||
public static readonly GET_DEVICES_BY_USER_SUMMARY =
|
|
||||||
'Get devices by user UUID';
|
|
||||||
public static readonly GET_DEVICES_BY_USER_DESCRIPTION =
|
|
||||||
'This endpoint retrieves all devices associated with a specific user.';
|
|
||||||
|
|
||||||
public static readonly GET_DEVICES_BY_SPACE_UUID_SUMMARY =
|
public static readonly GET_DEVICES_BY_SPACE_UUID_SUMMARY =
|
||||||
'Get devices by space UUID';
|
'Get devices by space UUID';
|
||||||
@ -552,16 +547,16 @@ export class ControllerRoute {
|
|||||||
'This endpoint retrieves the status of a specific power clamp device.';
|
'This endpoint retrieves the status of a specific power clamp device.';
|
||||||
|
|
||||||
public static readonly ADD_SCENE_TO_DEVICE_SUMMARY =
|
public static readonly ADD_SCENE_TO_DEVICE_SUMMARY =
|
||||||
'Add scene to device (4 Scene and 6 Scene devices only)';
|
'Add scene to device';
|
||||||
public static readonly ADD_SCENE_TO_DEVICE_DESCRIPTION =
|
public static readonly ADD_SCENE_TO_DEVICE_DESCRIPTION =
|
||||||
'This endpoint adds a scene to a specific switch device.';
|
'This endpoint adds a scene to a specific switch device.';
|
||||||
|
|
||||||
public static readonly GET_SCENES_BY_DEVICE_SUMMARY =
|
public static readonly GET_SCENES_BY_DEVICE_SUMMARY =
|
||||||
'Get scenes by device (4 Scene and 6 Scene devices only)';
|
'Get scenes by device';
|
||||||
public static readonly GET_SCENES_BY_DEVICE_DESCRIPTION =
|
public static readonly GET_SCENES_BY_DEVICE_DESCRIPTION =
|
||||||
'This endpoint retrieves all scenes associated with a specific switch device.';
|
'This endpoint retrieves all scenes associated with a specific switch device.';
|
||||||
public static readonly DELETE_SCENES_BY_SWITCH_NAME_SUMMARY =
|
public static readonly DELETE_SCENES_BY_SWITCH_NAME_SUMMARY =
|
||||||
'Delete scenes by device uuid and switch name (4 Scene and 6 Scene devices only)';
|
'Delete scenes by device uuid and switch name';
|
||||||
public static readonly DELETE_SCENES_BY_SWITCH_NAME_DESCRIPTION =
|
public static readonly DELETE_SCENES_BY_SWITCH_NAME_DESCRIPTION =
|
||||||
'This endpoint deletes all scenes associated with a specific switch device.';
|
'This endpoint deletes all scenes associated with a specific switch device.';
|
||||||
};
|
};
|
||||||
|
@ -123,6 +123,7 @@ export class AutomationService {
|
|||||||
);
|
);
|
||||||
const formattedCondition = await this.prepareConditions(
|
const formattedCondition = await this.prepareConditions(
|
||||||
params.conditions,
|
params.conditions,
|
||||||
|
projectUuid,
|
||||||
);
|
);
|
||||||
|
|
||||||
const response = await this.tuyaService.createAutomation(
|
const response = await this.tuyaService.createAutomation(
|
||||||
@ -585,7 +586,10 @@ export class AutomationService {
|
|||||||
updateAutomationDto;
|
updateAutomationDto;
|
||||||
try {
|
try {
|
||||||
const formattedActions = await this.prepareActions(actions, projectUuid);
|
const formattedActions = await this.prepareActions(actions, projectUuid);
|
||||||
const formattedCondition = await this.prepareConditions(conditions);
|
const formattedCondition = await this.prepareConditions(
|
||||||
|
conditions,
|
||||||
|
projectUuid,
|
||||||
|
);
|
||||||
const response = await this.tuyaService.updateAutomation(
|
const response = await this.tuyaService.updateAutomation(
|
||||||
automationUuid,
|
automationUuid,
|
||||||
spaceTuyaUuid,
|
spaceTuyaUuid,
|
||||||
@ -722,6 +726,7 @@ export class AutomationService {
|
|||||||
const device = await this.deviceService.getDeviceByDeviceUuid(
|
const device = await this.deviceService.getDeviceByDeviceUuid(
|
||||||
action.entity_id,
|
action.entity_id,
|
||||||
false,
|
false,
|
||||||
|
projectUuid,
|
||||||
);
|
);
|
||||||
if (device) {
|
if (device) {
|
||||||
action.entity_id = device.deviceTuyaUuid;
|
action.entity_id = device.deviceTuyaUuid;
|
||||||
@ -759,7 +764,10 @@ export class AutomationService {
|
|||||||
return convertedData;
|
return convertedData;
|
||||||
}
|
}
|
||||||
|
|
||||||
private async prepareConditions(conditions: Condition[]) {
|
private async prepareConditions(
|
||||||
|
conditions: Condition[],
|
||||||
|
projectUuid: string,
|
||||||
|
) {
|
||||||
const convertedData = convertKeysToSnakeCase(conditions);
|
const convertedData = convertKeysToSnakeCase(conditions);
|
||||||
await Promise.all(
|
await Promise.all(
|
||||||
convertedData.map(async (condition) => {
|
convertedData.map(async (condition) => {
|
||||||
@ -767,6 +775,7 @@ export class AutomationService {
|
|||||||
const device = await this.deviceService.getDeviceByDeviceUuid(
|
const device = await this.deviceService.getDeviceByDeviceUuid(
|
||||||
condition.entity_id,
|
condition.entity_id,
|
||||||
false,
|
false,
|
||||||
|
projectUuid,
|
||||||
);
|
);
|
||||||
if (device) {
|
if (device) {
|
||||||
condition.entity_id = device.deviceTuyaUuid;
|
condition.entity_id = device.deviceTuyaUuid;
|
||||||
|
@ -6,25 +6,23 @@ import {
|
|||||||
Post,
|
Post,
|
||||||
Query,
|
Query,
|
||||||
Param,
|
Param,
|
||||||
HttpStatus,
|
|
||||||
UseGuards,
|
UseGuards,
|
||||||
Req,
|
|
||||||
Put,
|
Put,
|
||||||
Delete,
|
Delete,
|
||||||
|
Req,
|
||||||
} from '@nestjs/common';
|
} from '@nestjs/common';
|
||||||
import { ApiTags, ApiBearerAuth, ApiOperation } from '@nestjs/swagger';
|
import { ApiTags, ApiBearerAuth, ApiOperation } from '@nestjs/swagger';
|
||||||
import {
|
import {
|
||||||
AddDeviceDto,
|
AddDeviceDto,
|
||||||
AddSceneToFourSceneDeviceDto,
|
AddSceneToFourSceneDeviceDto,
|
||||||
|
AssignDeviceToSpaceDto,
|
||||||
UpdateDeviceDto,
|
UpdateDeviceDto,
|
||||||
UpdateDeviceInSpaceDto,
|
|
||||||
} from '../dtos/add.device.dto';
|
} from '../dtos/add.device.dto';
|
||||||
import { GetDeviceLogsDto } from '../dtos/get.device.dto';
|
import { GetDeviceLogsDto } from '../dtos/get.device.dto';
|
||||||
import {
|
import {
|
||||||
ControlDeviceDto,
|
ControlDeviceDto,
|
||||||
BatchControlDevicesDto,
|
BatchControlDevicesDto,
|
||||||
BatchStatusDevicesDto,
|
BatchStatusDevicesDto,
|
||||||
BatchFactoryResetDevicesDto,
|
|
||||||
GetSceneFourSceneDeviceDto,
|
GetSceneFourSceneDeviceDto,
|
||||||
} from '../dtos/control.device.dto';
|
} from '../dtos/control.device.dto';
|
||||||
import { CheckRoomGuard } from 'src/guards/room.guard';
|
import { CheckRoomGuard } from 'src/guards/room.guard';
|
||||||
@ -50,65 +48,35 @@ export class DeviceController {
|
|||||||
@Permissions('SPACE_DEVICE_ASSIGN_DEVICE_TO_SPACE')
|
@Permissions('SPACE_DEVICE_ASSIGN_DEVICE_TO_SPACE')
|
||||||
@Post()
|
@Post()
|
||||||
@ApiOperation({
|
@ApiOperation({
|
||||||
summary: ControllerRoute.DEVICE.ACTIONS.ADD_DEVICE_TO_USER_SUMMARY,
|
summary: ControllerRoute.DEVICE.ACTIONS.ADD_DEVICE_SUMMARY,
|
||||||
description: ControllerRoute.DEVICE.ACTIONS.ADD_DEVICE_TO_USER_DESCRIPTION,
|
description: ControllerRoute.DEVICE.ACTIONS.ADD_DEVICE_DESCRIPTION,
|
||||||
})
|
})
|
||||||
async addDeviceUser(@Body() addDeviceDto: AddDeviceDto) {
|
async addNewDevice(
|
||||||
const device = await this.deviceService.addDeviceUser(addDeviceDto);
|
@Body() addDeviceDto: AddDeviceDto,
|
||||||
|
@Req() req: any,
|
||||||
return {
|
): Promise<BaseResponseDto> {
|
||||||
statusCode: HttpStatus.CREATED,
|
const projectUuid = req.user.project.uuid;
|
||||||
success: true,
|
return await this.deviceService.addNewDevice(addDeviceDto, projectUuid);
|
||||||
message: 'device added successfully',
|
|
||||||
data: device,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
@ApiBearerAuth()
|
|
||||||
@UseGuards(PermissionsGuard)
|
|
||||||
@Permissions('DEVICE_VIEW')
|
|
||||||
@Get('user/:userUuid')
|
|
||||||
@ApiOperation({
|
|
||||||
summary: ControllerRoute.DEVICE.ACTIONS.GET_DEVICES_BY_USER_SUMMARY,
|
|
||||||
description: ControllerRoute.DEVICE.ACTIONS.GET_DEVICES_BY_USER_DESCRIPTION,
|
|
||||||
})
|
|
||||||
async getDevicesByUser(@Param('userUuid') userUuid: string) {
|
|
||||||
return await this.deviceService.getDevicesByUser(userUuid);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@ApiBearerAuth()
|
|
||||||
@UseGuards(PermissionsGuard)
|
|
||||||
@Permissions('SPACE_DEVICE_VIEW_DEVICE_IN_SPACE')
|
|
||||||
@Get('space/:spaceUuid')
|
|
||||||
@ApiOperation({
|
|
||||||
summary: ControllerRoute.DEVICE.ACTIONS.GET_DEVICES_BY_SPACE_UUID_SUMMARY,
|
|
||||||
description:
|
|
||||||
ControllerRoute.DEVICE.ACTIONS.GET_DEVICES_BY_SPACE_UUID_DESCRIPTION,
|
|
||||||
})
|
|
||||||
async getDevicesByUnitId(@Param('spaceUuid') spaceUuid: string) {
|
|
||||||
return await this.deviceService.getDevicesBySpaceUuid(spaceUuid);
|
|
||||||
}
|
|
||||||
@ApiBearerAuth()
|
@ApiBearerAuth()
|
||||||
@UseGuards(PermissionsGuard, CheckRoomGuard)
|
@UseGuards(PermissionsGuard, CheckRoomGuard)
|
||||||
@Permissions('SUBSPACE_DEVICE_UPDATE_DEVICE_IN_SUBSPACE')
|
@Permissions('SUBSPACE_DEVICE_UPDATE_DEVICE_IN_SUBSPACE')
|
||||||
@Put('space')
|
@Post(':deviceUuid/space/:spaceUuid')
|
||||||
@ApiOperation({
|
@ApiOperation({
|
||||||
summary: ControllerRoute.DEVICE.ACTIONS.UPDATE_DEVICE_IN_ROOM_SUMMARY,
|
summary: ControllerRoute.DEVICE.ACTIONS.UPDATE_DEVICE_IN_ROOM_SUMMARY,
|
||||||
description:
|
description:
|
||||||
ControllerRoute.DEVICE.ACTIONS.UPDATE_DEVICE_IN_ROOM_DESCRIPTION,
|
ControllerRoute.DEVICE.ACTIONS.UPDATE_DEVICE_IN_ROOM_DESCRIPTION,
|
||||||
})
|
})
|
||||||
async updateDeviceInRoom(
|
async transferDeviceInSpaces(
|
||||||
@Body() updateDeviceInSpaceDto: UpdateDeviceInSpaceDto,
|
@Param() updateDeviceInSpaceDto: AssignDeviceToSpaceDto,
|
||||||
) {
|
@Req() req: any,
|
||||||
const device = await this.deviceService.updateDeviceInSpace(
|
): Promise<BaseResponseDto> {
|
||||||
|
const projectUuid = req.user.project.uuid;
|
||||||
|
return await this.deviceService.transferDeviceInSpaces(
|
||||||
updateDeviceInSpaceDto,
|
updateDeviceInSpaceDto,
|
||||||
|
projectUuid,
|
||||||
);
|
);
|
||||||
|
|
||||||
return {
|
|
||||||
statusCode: HttpStatus.CREATED,
|
|
||||||
success: true,
|
|
||||||
message: 'device updated in room successfully',
|
|
||||||
data: device,
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@ApiBearerAuth()
|
@ApiBearerAuth()
|
||||||
@ -122,11 +90,11 @@ export class DeviceController {
|
|||||||
async getDeviceDetailsByDeviceId(
|
async getDeviceDetailsByDeviceId(
|
||||||
@Param('deviceUuid') deviceUuid: string,
|
@Param('deviceUuid') deviceUuid: string,
|
||||||
@Req() req: any,
|
@Req() req: any,
|
||||||
) {
|
): Promise<BaseResponseDto> {
|
||||||
const userUuid = req.user.uuid;
|
const projectUuid = req.user.project.uuid;
|
||||||
return await this.deviceService.getDeviceDetailsByDeviceId(
|
return await this.deviceService.getDeviceDetailsByDeviceId(
|
||||||
deviceUuid,
|
deviceUuid,
|
||||||
userUuid,
|
projectUuid,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@ApiBearerAuth()
|
@ApiBearerAuth()
|
||||||
@ -140,18 +108,14 @@ export class DeviceController {
|
|||||||
async updateDevice(
|
async updateDevice(
|
||||||
@Param('deviceUuid') deviceUuid: string,
|
@Param('deviceUuid') deviceUuid: string,
|
||||||
@Body() updateDeviceDto: UpdateDeviceDto,
|
@Body() updateDeviceDto: UpdateDeviceDto,
|
||||||
) {
|
@Req() req: any,
|
||||||
const device = await this.deviceService.updateDevice(
|
): Promise<BaseResponseDto> {
|
||||||
|
const projectUuid = req.user.project.uuid;
|
||||||
|
return await this.deviceService.updateDevice(
|
||||||
deviceUuid,
|
deviceUuid,
|
||||||
updateDeviceDto,
|
updateDeviceDto,
|
||||||
|
projectUuid,
|
||||||
);
|
);
|
||||||
|
|
||||||
return {
|
|
||||||
statusCode: HttpStatus.CREATED,
|
|
||||||
success: true,
|
|
||||||
message: 'device updated successfully',
|
|
||||||
data: device,
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@ApiBearerAuth()
|
@ApiBearerAuth()
|
||||||
@ -165,8 +129,13 @@ export class DeviceController {
|
|||||||
})
|
})
|
||||||
async getDeviceInstructionByDeviceId(
|
async getDeviceInstructionByDeviceId(
|
||||||
@Param('deviceUuid') deviceUuid: string,
|
@Param('deviceUuid') deviceUuid: string,
|
||||||
|
@Req() req: any,
|
||||||
) {
|
) {
|
||||||
return await this.deviceService.getDeviceInstructionByDeviceId(deviceUuid);
|
const projectUuid = req.user.project.uuid;
|
||||||
|
return await this.deviceService.getDeviceInstructionByDeviceId(
|
||||||
|
deviceUuid,
|
||||||
|
projectUuid,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
@ApiBearerAuth()
|
@ApiBearerAuth()
|
||||||
@UseGuards(PermissionsGuard)
|
@UseGuards(PermissionsGuard)
|
||||||
@ -176,14 +145,18 @@ export class DeviceController {
|
|||||||
summary: ControllerRoute.DEVICE.ACTIONS.GET_DEVICE_STATUS_SUMMARY,
|
summary: ControllerRoute.DEVICE.ACTIONS.GET_DEVICE_STATUS_SUMMARY,
|
||||||
description: ControllerRoute.DEVICE.ACTIONS.GET_DEVICE_STATUS_DESCRIPTION,
|
description: ControllerRoute.DEVICE.ACTIONS.GET_DEVICE_STATUS_DESCRIPTION,
|
||||||
})
|
})
|
||||||
async getDevicesInstructionStatus(@Param('deviceUuid') deviceUuid: string) {
|
async getDevicesInstructionStatus(
|
||||||
return await this.deviceService.getDevicesInstructionStatus(deviceUuid);
|
@Param('deviceUuid') deviceUuid: string,
|
||||||
|
@Req() req: any,
|
||||||
|
): Promise<BaseResponseDto> {
|
||||||
|
const projectUuid = req.user.project.uuid;
|
||||||
|
return await this.deviceService.getDevicesStatus(deviceUuid, projectUuid);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ApiBearerAuth()
|
@ApiBearerAuth()
|
||||||
@UseGuards(PermissionsGuard)
|
@UseGuards(PermissionsGuard)
|
||||||
@Permissions('DEVICE_SINGLE_CONTROL')
|
@Permissions('DEVICE_SINGLE_CONTROL')
|
||||||
@Post(':deviceUuid/control')
|
@Post(':deviceUuid/command')
|
||||||
@ApiOperation({
|
@ApiOperation({
|
||||||
summary: ControllerRoute.DEVICE.ACTIONS.CONTROL_DEVICE_SUMMARY,
|
summary: ControllerRoute.DEVICE.ACTIONS.CONTROL_DEVICE_SUMMARY,
|
||||||
description: ControllerRoute.DEVICE.ACTIONS.CONTROL_DEVICE_DESCRIPTION,
|
description: ControllerRoute.DEVICE.ACTIONS.CONTROL_DEVICE_DESCRIPTION,
|
||||||
@ -191,8 +164,14 @@ export class DeviceController {
|
|||||||
async controlDevice(
|
async controlDevice(
|
||||||
@Body() controlDeviceDto: ControlDeviceDto,
|
@Body() controlDeviceDto: ControlDeviceDto,
|
||||||
@Param('deviceUuid') deviceUuid: string,
|
@Param('deviceUuid') deviceUuid: string,
|
||||||
) {
|
@Req() req: any,
|
||||||
return await this.deviceService.controlDevice(controlDeviceDto, deviceUuid);
|
): Promise<BaseResponseDto> {
|
||||||
|
const projectUuid = req.user.project.uuid;
|
||||||
|
return await this.deviceService.controlDevice(
|
||||||
|
controlDeviceDto,
|
||||||
|
deviceUuid,
|
||||||
|
projectUuid,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
@ApiBearerAuth()
|
@ApiBearerAuth()
|
||||||
@UseGuards(PermissionsGuard)
|
@UseGuards(PermissionsGuard)
|
||||||
@ -206,10 +185,13 @@ export class DeviceController {
|
|||||||
async updateDeviceFirmware(
|
async updateDeviceFirmware(
|
||||||
@Param('deviceUuid') deviceUuid: string,
|
@Param('deviceUuid') deviceUuid: string,
|
||||||
@Param('firmwareVersion') firmwareVersion: number,
|
@Param('firmwareVersion') firmwareVersion: number,
|
||||||
) {
|
@Req() req: any,
|
||||||
|
): Promise<BaseResponseDto> {
|
||||||
|
const projectUuid = req.user.project.uuid;
|
||||||
return await this.deviceService.updateDeviceFirmware(
|
return await this.deviceService.updateDeviceFirmware(
|
||||||
deviceUuid,
|
deviceUuid,
|
||||||
firmwareVersion,
|
firmwareVersion,
|
||||||
|
projectUuid,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@ApiBearerAuth()
|
@ApiBearerAuth()
|
||||||
@ -221,14 +203,21 @@ export class DeviceController {
|
|||||||
description:
|
description:
|
||||||
ControllerRoute.DEVICE.ACTIONS.GET_DEVICES_IN_GATEWAY_DESCRIPTION,
|
ControllerRoute.DEVICE.ACTIONS.GET_DEVICES_IN_GATEWAY_DESCRIPTION,
|
||||||
})
|
})
|
||||||
async getDevicesInGateway(@Param('gatewayUuid') gatewayUuid: string) {
|
async getDevicesInGateway(
|
||||||
return await this.deviceService.getDevicesInGateway(gatewayUuid);
|
@Param('gatewayUuid') gatewayUuid: string,
|
||||||
|
@Req() req: any,
|
||||||
|
): Promise<BaseResponseDto> {
|
||||||
|
const projectUuid = req.user.project.uuid;
|
||||||
|
return await this.deviceService.getDevicesInGateway(
|
||||||
|
gatewayUuid,
|
||||||
|
projectUuid,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ApiBearerAuth()
|
@ApiBearerAuth()
|
||||||
@UseGuards(PermissionsGuard)
|
@UseGuards(PermissionsGuard)
|
||||||
@Permissions('DEVICE_VIEW')
|
@Permissions('DEVICE_VIEW')
|
||||||
@Get('report-logs/:deviceUuid')
|
@Get(':deviceUuid/report-logs')
|
||||||
@ApiOperation({
|
@ApiOperation({
|
||||||
summary: ControllerRoute.DEVICE.ACTIONS.GET_DEVICE_LOGS_SUMMARY,
|
summary: ControllerRoute.DEVICE.ACTIONS.GET_DEVICE_LOGS_SUMMARY,
|
||||||
description: ControllerRoute.DEVICE.ACTIONS.GET_DEVICE_LOGS_DESCRIPTION,
|
description: ControllerRoute.DEVICE.ACTIONS.GET_DEVICE_LOGS_DESCRIPTION,
|
||||||
@ -236,13 +225,19 @@ export class DeviceController {
|
|||||||
async getBuildingChildByUuid(
|
async getBuildingChildByUuid(
|
||||||
@Param('deviceUuid') deviceUuid: string,
|
@Param('deviceUuid') deviceUuid: string,
|
||||||
@Query() query: GetDeviceLogsDto,
|
@Query() query: GetDeviceLogsDto,
|
||||||
) {
|
@Req() req: any,
|
||||||
return await this.deviceService.getDeviceLogs(deviceUuid, query);
|
): Promise<BaseResponseDto> {
|
||||||
|
const projectUuid = req.user.project.uuid;
|
||||||
|
return await this.deviceService.getDeviceLogs(
|
||||||
|
deviceUuid,
|
||||||
|
query,
|
||||||
|
projectUuid,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
@ApiBearerAuth()
|
@ApiBearerAuth()
|
||||||
@UseGuards(PermissionsGuard)
|
@UseGuards(PermissionsGuard)
|
||||||
@Permissions('DEVICE_BATCH_CONTROL')
|
@Permissions('DEVICE_BATCH_CONTROL')
|
||||||
@Post('control/batch')
|
@Post('batch')
|
||||||
@ApiOperation({
|
@ApiOperation({
|
||||||
summary: ControllerRoute.DEVICE.ACTIONS.BATCH_CONTROL_DEVICES_SUMMARY,
|
summary: ControllerRoute.DEVICE.ACTIONS.BATCH_CONTROL_DEVICES_SUMMARY,
|
||||||
description:
|
description:
|
||||||
@ -250,13 +245,18 @@ export class DeviceController {
|
|||||||
})
|
})
|
||||||
async batchControlDevices(
|
async batchControlDevices(
|
||||||
@Body() batchControlDevicesDto: BatchControlDevicesDto,
|
@Body() batchControlDevicesDto: BatchControlDevicesDto,
|
||||||
|
@Req() req: any,
|
||||||
) {
|
) {
|
||||||
return await this.deviceService.batchControlDevices(batchControlDevicesDto);
|
const projectUuid = req.user.project.uuid;
|
||||||
|
return await this.deviceService.batchControlDevices(
|
||||||
|
batchControlDevicesDto,
|
||||||
|
projectUuid,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
@ApiBearerAuth()
|
@ApiBearerAuth()
|
||||||
@UseGuards(PermissionsGuard)
|
@UseGuards(PermissionsGuard)
|
||||||
@Permissions('DEVICE_BATCH_CONTROL')
|
@Permissions('DEVICE_BATCH_CONTROL')
|
||||||
@Get('status/batch')
|
@Get('batch')
|
||||||
@ApiOperation({
|
@ApiOperation({
|
||||||
summary: ControllerRoute.DEVICE.ACTIONS.BATCH_STATUS_DEVICES_SUMMARY,
|
summary: ControllerRoute.DEVICE.ACTIONS.BATCH_STATUS_DEVICES_SUMMARY,
|
||||||
description:
|
description:
|
||||||
@ -264,41 +264,15 @@ export class DeviceController {
|
|||||||
})
|
})
|
||||||
async batchStatusDevices(
|
async batchStatusDevices(
|
||||||
@Query() batchStatusDevicesDto: BatchStatusDevicesDto,
|
@Query() batchStatusDevicesDto: BatchStatusDevicesDto,
|
||||||
) {
|
@Req() req: any,
|
||||||
return await this.deviceService.batchStatusDevices(batchStatusDevicesDto);
|
): Promise<BaseResponseDto> {
|
||||||
}
|
const projectUuid = req.user.project.uuid;
|
||||||
@ApiBearerAuth()
|
return await this.deviceService.batchStatusDevices(
|
||||||
@UseGuards(PermissionsGuard)
|
batchStatusDevicesDto,
|
||||||
@Permissions('DEVICE_DELETE')
|
projectUuid,
|
||||||
@Post('factory/reset/:deviceUuid')
|
|
||||||
@ApiOperation({
|
|
||||||
summary: ControllerRoute.DEVICE.ACTIONS.BATCH_FACTORY_RESET_DEVICES_SUMMARY,
|
|
||||||
description:
|
|
||||||
ControllerRoute.DEVICE.ACTIONS.BATCH_FACTORY_RESET_DEVICES_DESCRIPTION,
|
|
||||||
})
|
|
||||||
async batchFactoryResetDevices(
|
|
||||||
@Body() batchFactoryResetDevicesDto: BatchFactoryResetDevicesDto,
|
|
||||||
) {
|
|
||||||
return await this.deviceService.batchFactoryResetDevices(
|
|
||||||
batchFactoryResetDevicesDto,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
@ApiBearerAuth()
|
|
||||||
@UseGuards(PermissionsGuard)
|
|
||||||
@Permissions('DEVICE_VIEW')
|
|
||||||
@Get(':powerClampUuid/power-clamp/status')
|
|
||||||
@ApiOperation({
|
|
||||||
summary: ControllerRoute.DEVICE.ACTIONS.GET_POWER_CLAMP_STATUS_SUMMARY,
|
|
||||||
description:
|
|
||||||
ControllerRoute.DEVICE.ACTIONS.GET_POWER_CLAMP_STATUS_DESCRIPTION,
|
|
||||||
})
|
|
||||||
async getPowerClampInstructionStatus(
|
|
||||||
@Param('powerClampUuid') powerClampUuid: string,
|
|
||||||
) {
|
|
||||||
return await this.deviceService.getPowerClampInstructionStatus(
|
|
||||||
powerClampUuid,
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ApiBearerAuth()
|
@ApiBearerAuth()
|
||||||
@UseGuards(PermissionsGuard, CheckFourAndSixSceneDeviceTypeGuard)
|
@UseGuards(PermissionsGuard, CheckFourAndSixSceneDeviceTypeGuard)
|
||||||
@Permissions('DEVICE_SINGLE_CONTROL')
|
@Permissions('DEVICE_SINGLE_CONTROL')
|
||||||
@ -310,18 +284,14 @@ export class DeviceController {
|
|||||||
async addSceneToSceneDevice(
|
async addSceneToSceneDevice(
|
||||||
@Param('deviceUuid') deviceUuid: string,
|
@Param('deviceUuid') deviceUuid: string,
|
||||||
@Body() addSceneToFourSceneDeviceDto: AddSceneToFourSceneDeviceDto,
|
@Body() addSceneToFourSceneDeviceDto: AddSceneToFourSceneDeviceDto,
|
||||||
) {
|
@Req() req: any,
|
||||||
const device = await this.deviceService.addSceneToSceneDevice(
|
): Promise<BaseResponseDto> {
|
||||||
|
const projectUuid = req.user.project.uuid;
|
||||||
|
return await this.deviceService.addSceneToSceneDevice(
|
||||||
deviceUuid,
|
deviceUuid,
|
||||||
addSceneToFourSceneDeviceDto,
|
addSceneToFourSceneDeviceDto,
|
||||||
|
projectUuid,
|
||||||
);
|
);
|
||||||
|
|
||||||
return {
|
|
||||||
statusCode: HttpStatus.CREATED,
|
|
||||||
success: true,
|
|
||||||
message: `scene added successfully to device ${deviceUuid}`,
|
|
||||||
data: device,
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
@ApiBearerAuth()
|
@ApiBearerAuth()
|
||||||
@UseGuards(PermissionsGuard, CheckFourAndSixSceneDeviceTypeGuard)
|
@UseGuards(PermissionsGuard, CheckFourAndSixSceneDeviceTypeGuard)
|
||||||
@ -335,10 +305,13 @@ export class DeviceController {
|
|||||||
async getScenesBySceneDevice(
|
async getScenesBySceneDevice(
|
||||||
@Param('deviceUuid') deviceUuid: string,
|
@Param('deviceUuid') deviceUuid: string,
|
||||||
@Query() getSceneFourSceneDeviceDto: GetSceneFourSceneDeviceDto,
|
@Query() getSceneFourSceneDeviceDto: GetSceneFourSceneDeviceDto,
|
||||||
) {
|
@Req() req: any,
|
||||||
|
): Promise<BaseResponseDto> {
|
||||||
|
const projectUuid = req.user.project.uuid;
|
||||||
return await this.deviceService.getScenesBySceneDevice(
|
return await this.deviceService.getScenesBySceneDevice(
|
||||||
deviceUuid,
|
deviceUuid,
|
||||||
getSceneFourSceneDeviceDto,
|
getSceneFourSceneDeviceDto,
|
||||||
|
projectUuid,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@ApiBearerAuth()
|
@ApiBearerAuth()
|
||||||
@ -354,7 +327,13 @@ export class DeviceController {
|
|||||||
async deleteSceneFromSceneDevice(
|
async deleteSceneFromSceneDevice(
|
||||||
@Param() param: DeviceSceneParamDto,
|
@Param() param: DeviceSceneParamDto,
|
||||||
@Query() query: DeleteSceneFromSceneDeviceDto,
|
@Query() query: DeleteSceneFromSceneDeviceDto,
|
||||||
|
@Req() req: any,
|
||||||
): Promise<BaseResponseDto> {
|
): Promise<BaseResponseDto> {
|
||||||
return await this.deviceService.deleteSceneFromSceneDevice(param, query);
|
const projectUuid = req.user.project.uuid;
|
||||||
|
return await this.deviceService.deleteSceneFromSceneDevice(
|
||||||
|
param,
|
||||||
|
query,
|
||||||
|
projectUuid,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -10,16 +10,8 @@ export class AddDeviceDto {
|
|||||||
@IsString()
|
@IsString()
|
||||||
@IsNotEmpty()
|
@IsNotEmpty()
|
||||||
public deviceTuyaUuid: string;
|
public deviceTuyaUuid: string;
|
||||||
|
|
||||||
@ApiProperty({
|
|
||||||
description: 'userUuid',
|
|
||||||
required: true,
|
|
||||||
})
|
|
||||||
@IsString()
|
|
||||||
@IsNotEmpty()
|
|
||||||
public userUuid: string;
|
|
||||||
}
|
}
|
||||||
export class UpdateDeviceInSpaceDto {
|
export class AssignDeviceToSpaceDto {
|
||||||
@ApiProperty({
|
@ApiProperty({
|
||||||
description: 'deviceUuid',
|
description: 'deviceUuid',
|
||||||
required: true,
|
required: true,
|
||||||
|
@ -1,5 +1,12 @@
|
|||||||
|
import { BatchDeviceTypeEnum } from '@app/common/constants/batch-device.enum';
|
||||||
import { ApiProperty } from '@nestjs/swagger';
|
import { ApiProperty } from '@nestjs/swagger';
|
||||||
import { IsArray, IsNotEmpty, IsOptional, IsString } from 'class-validator';
|
import {
|
||||||
|
IsArray,
|
||||||
|
IsEnum,
|
||||||
|
IsNotEmpty,
|
||||||
|
IsOptional,
|
||||||
|
IsString,
|
||||||
|
} from 'class-validator';
|
||||||
|
|
||||||
export class ControlDeviceDto {
|
export class ControlDeviceDto {
|
||||||
@ApiProperty({
|
@ApiProperty({
|
||||||
@ -17,6 +24,15 @@ export class ControlDeviceDto {
|
|||||||
public value: any;
|
public value: any;
|
||||||
}
|
}
|
||||||
export class BatchControlDevicesDto {
|
export class BatchControlDevicesDto {
|
||||||
|
@ApiProperty({
|
||||||
|
description: 'Operation type',
|
||||||
|
enum: BatchDeviceTypeEnum,
|
||||||
|
required: true,
|
||||||
|
})
|
||||||
|
@IsEnum(BatchDeviceTypeEnum)
|
||||||
|
@IsNotEmpty()
|
||||||
|
public operationType: BatchDeviceTypeEnum;
|
||||||
|
|
||||||
@ApiProperty({
|
@ApiProperty({
|
||||||
description: 'devicesUuid',
|
description: 'devicesUuid',
|
||||||
required: true,
|
required: true,
|
||||||
|
@ -64,13 +64,15 @@ export interface GetDeviceDetailsFunctionsStatusInterface {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export interface DeviceInstructionResponse {
|
export interface DeviceInstructionResponse {
|
||||||
productUuid: string;
|
data: {
|
||||||
productType: string;
|
productUuid: string;
|
||||||
functions: {
|
productType: string;
|
||||||
code: string;
|
functions: {
|
||||||
values: any[];
|
code: string;
|
||||||
dataType: string;
|
values: any[];
|
||||||
}[];
|
dataType: string;
|
||||||
|
}[];
|
||||||
|
};
|
||||||
}
|
}
|
||||||
export interface updateDeviceFirmwareInterface {
|
export interface updateDeviceFirmwareInterface {
|
||||||
success: boolean;
|
success: boolean;
|
||||||
|
File diff suppressed because it is too large
Load Diff
@ -15,14 +15,17 @@ export class CheckFourAndSixSceneDeviceTypeGuard implements CanActivate {
|
|||||||
async canActivate(context: ExecutionContext): Promise<boolean> {
|
async canActivate(context: ExecutionContext): Promise<boolean> {
|
||||||
const request = context.switchToHttp().getRequest();
|
const request = context.switchToHttp().getRequest();
|
||||||
const deviceUuid = request.params.deviceUuid;
|
const deviceUuid = request.params.deviceUuid;
|
||||||
|
const projectUuid = request.user.project.uuid;
|
||||||
if (!deviceUuid) {
|
if (!deviceUuid) {
|
||||||
throw new BadRequestException('Device UUID is required');
|
throw new BadRequestException('Device UUID is required');
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const deviceDetails =
|
const deviceDetails = await this.deviceService.getDeviceByDeviceUuid(
|
||||||
await this.deviceService.getDeviceByDeviceUuid(deviceUuid);
|
deviceUuid,
|
||||||
|
true,
|
||||||
|
projectUuid,
|
||||||
|
);
|
||||||
|
|
||||||
if (
|
if (
|
||||||
deviceDetails.productDevice.prodType !== ProductType.FOUR_S &&
|
deviceDetails.productDevice.prodType !== ProductType.FOUR_S &&
|
||||||
|
@ -8,6 +8,7 @@ import {
|
|||||||
Param,
|
Param,
|
||||||
Post,
|
Post,
|
||||||
Put,
|
Put,
|
||||||
|
Req,
|
||||||
UseGuards,
|
UseGuards,
|
||||||
} from '@nestjs/common';
|
} from '@nestjs/common';
|
||||||
import { ApiTags, ApiBearerAuth, ApiOperation } from '@nestjs/swagger';
|
import { ApiTags, ApiBearerAuth, ApiOperation } from '@nestjs/swagger';
|
||||||
@ -42,8 +43,13 @@ export class SceneController {
|
|||||||
})
|
})
|
||||||
async addTapToRunScene(
|
async addTapToRunScene(
|
||||||
@Body() addSceneTapToRunDto: AddSceneTapToRunDto,
|
@Body() addSceneTapToRunDto: AddSceneTapToRunDto,
|
||||||
|
@Req() req: any,
|
||||||
): Promise<BaseResponseDto> {
|
): Promise<BaseResponseDto> {
|
||||||
return await this.sceneService.createScene(addSceneTapToRunDto);
|
const projectUuid = req.user.project.uuid;
|
||||||
|
return await this.sceneService.createScene(
|
||||||
|
addSceneTapToRunDto,
|
||||||
|
projectUuid,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ApiBearerAuth()
|
@ApiBearerAuth()
|
||||||
@ -100,10 +106,13 @@ export class SceneController {
|
|||||||
async updateTapToRunScene(
|
async updateTapToRunScene(
|
||||||
@Body() updateSceneTapToRunDto: UpdateSceneTapToRunDto,
|
@Body() updateSceneTapToRunDto: UpdateSceneTapToRunDto,
|
||||||
@Param() param: SceneParamDto,
|
@Param() param: SceneParamDto,
|
||||||
|
@Req() req: any,
|
||||||
) {
|
) {
|
||||||
|
const projectUuid = req.user.project.uuid;
|
||||||
return await this.sceneService.updateTapToRunScene(
|
return await this.sceneService.updateTapToRunScene(
|
||||||
updateSceneTapToRunDto,
|
updateSceneTapToRunDto,
|
||||||
param.sceneUuid,
|
param.sceneUuid,
|
||||||
|
projectUuid,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -60,13 +60,18 @@ export class SceneService {
|
|||||||
|
|
||||||
async createScene(
|
async createScene(
|
||||||
addSceneTapToRunDto: AddSceneTapToRunDto,
|
addSceneTapToRunDto: AddSceneTapToRunDto,
|
||||||
|
projectUuid: string,
|
||||||
): Promise<BaseResponseDto> {
|
): Promise<BaseResponseDto> {
|
||||||
try {
|
try {
|
||||||
const { spaceUuid } = addSceneTapToRunDto;
|
const { spaceUuid } = addSceneTapToRunDto;
|
||||||
|
|
||||||
const space = await this.getSpaceByUuid(spaceUuid);
|
const space = await this.getSpaceByUuid(spaceUuid);
|
||||||
|
|
||||||
const scene = await this.create(space.spaceTuyaUuid, addSceneTapToRunDto);
|
const scene = await this.create(
|
||||||
|
space.spaceTuyaUuid,
|
||||||
|
addSceneTapToRunDto,
|
||||||
|
projectUuid,
|
||||||
|
);
|
||||||
|
|
||||||
return new SuccessResponseDto({
|
return new SuccessResponseDto({
|
||||||
message: `Successfully created new scene with uuid ${scene.uuid}`,
|
message: `Successfully created new scene with uuid ${scene.uuid}`,
|
||||||
@ -90,6 +95,7 @@ export class SceneService {
|
|||||||
async create(
|
async create(
|
||||||
spaceTuyaUuid: string,
|
spaceTuyaUuid: string,
|
||||||
addSceneTapToRunDto: AddSceneTapToRunDto,
|
addSceneTapToRunDto: AddSceneTapToRunDto,
|
||||||
|
projectUuid: string,
|
||||||
): Promise<SceneEntity> {
|
): Promise<SceneEntity> {
|
||||||
const { iconUuid, showInHomePage, spaceUuid } = addSceneTapToRunDto;
|
const { iconUuid, showInHomePage, spaceUuid } = addSceneTapToRunDto;
|
||||||
|
|
||||||
@ -107,6 +113,7 @@ export class SceneService {
|
|||||||
const response = await this.createSceneExternalService(
|
const response = await this.createSceneExternalService(
|
||||||
spaceTuyaUuid,
|
spaceTuyaUuid,
|
||||||
addSceneTapToRunDto,
|
addSceneTapToRunDto,
|
||||||
|
projectUuid,
|
||||||
);
|
);
|
||||||
|
|
||||||
const scene = await this.sceneRepository.save({
|
const scene = await this.sceneRepository.save({
|
||||||
@ -137,10 +144,11 @@ export class SceneService {
|
|||||||
spaceTuyaUuid: string,
|
spaceTuyaUuid: string,
|
||||||
sceneTuyaUuid: string,
|
sceneTuyaUuid: string,
|
||||||
updateSceneTapToRunDto: UpdateSceneTapToRunDto,
|
updateSceneTapToRunDto: UpdateSceneTapToRunDto,
|
||||||
|
projectUuid: string,
|
||||||
) {
|
) {
|
||||||
const { sceneName, decisionExpr, actions } = updateSceneTapToRunDto;
|
const { sceneName, decisionExpr, actions } = updateSceneTapToRunDto;
|
||||||
try {
|
try {
|
||||||
const formattedActions = await this.prepareActions(actions);
|
const formattedActions = await this.prepareActions(actions, projectUuid);
|
||||||
|
|
||||||
const response = (await this.tuyaService.updateTapToRunScene(
|
const response = (await this.tuyaService.updateTapToRunScene(
|
||||||
sceneTuyaUuid,
|
sceneTuyaUuid,
|
||||||
@ -178,10 +186,11 @@ export class SceneService {
|
|||||||
async createSceneExternalService(
|
async createSceneExternalService(
|
||||||
spaceTuyaUuid: string,
|
spaceTuyaUuid: string,
|
||||||
addSceneTapToRunDto: AddSceneTapToRunDto,
|
addSceneTapToRunDto: AddSceneTapToRunDto,
|
||||||
|
projectUuid: string,
|
||||||
) {
|
) {
|
||||||
const { sceneName, decisionExpr, actions } = addSceneTapToRunDto;
|
const { sceneName, decisionExpr, actions } = addSceneTapToRunDto;
|
||||||
try {
|
try {
|
||||||
const formattedActions = await this.prepareActions(actions);
|
const formattedActions = await this.prepareActions(actions, projectUuid);
|
||||||
|
|
||||||
const response = (await this.tuyaService.addTapToRunScene(
|
const response = (await this.tuyaService.addTapToRunScene(
|
||||||
spaceTuyaUuid,
|
spaceTuyaUuid,
|
||||||
@ -324,6 +333,7 @@ export class SceneService {
|
|||||||
async updateTapToRunScene(
|
async updateTapToRunScene(
|
||||||
updateSceneTapToRunDto: UpdateSceneTapToRunDto,
|
updateSceneTapToRunDto: UpdateSceneTapToRunDto,
|
||||||
sceneUuid: string,
|
sceneUuid: string,
|
||||||
|
projectUuid: string,
|
||||||
) {
|
) {
|
||||||
try {
|
try {
|
||||||
const scene = await this.findScene(sceneUuid);
|
const scene = await this.findScene(sceneUuid);
|
||||||
@ -340,6 +350,7 @@ export class SceneService {
|
|||||||
space.spaceTuyaUuid,
|
space.spaceTuyaUuid,
|
||||||
scene.sceneTuyaUuid,
|
scene.sceneTuyaUuid,
|
||||||
addSceneTapToRunDto,
|
addSceneTapToRunDto,
|
||||||
|
projectUuid,
|
||||||
);
|
);
|
||||||
|
|
||||||
if (!updateTuyaSceneResponse.success) {
|
if (!updateTuyaSceneResponse.success) {
|
||||||
@ -573,7 +584,10 @@ export class SceneService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private async prepareActions(actions: Action[]): Promise<ConvertedAction[]> {
|
private async prepareActions(
|
||||||
|
actions: Action[],
|
||||||
|
projectUuid: string,
|
||||||
|
): Promise<ConvertedAction[]> {
|
||||||
const convertedData = convertKeysToSnakeCase(actions) as ConvertedAction[];
|
const convertedData = convertKeysToSnakeCase(actions) as ConvertedAction[];
|
||||||
|
|
||||||
await Promise.all(
|
await Promise.all(
|
||||||
@ -582,6 +596,7 @@ export class SceneService {
|
|||||||
const device = await this.deviceService.getDeviceByDeviceUuid(
|
const device = await this.deviceService.getDeviceByDeviceUuid(
|
||||||
action.entity_id,
|
action.entity_id,
|
||||||
false,
|
false,
|
||||||
|
projectUuid,
|
||||||
);
|
);
|
||||||
if (device) {
|
if (device) {
|
||||||
action.entity_id = device.deviceTuyaUuid;
|
action.entity_id = device.deviceTuyaUuid;
|
||||||
|
Reference in New Issue
Block a user