mirror of
https://github.com/SyncrowIOT/backend.git
synced 2025-07-16 10:46:17 +00:00
295 lines
8.1 KiB
TypeScript
295 lines
8.1 KiB
TypeScript
import { DeviceService } from '../services/device.service';
|
|
import {
|
|
Body,
|
|
Controller,
|
|
Get,
|
|
Post,
|
|
Query,
|
|
Param,
|
|
HttpException,
|
|
HttpStatus,
|
|
UseGuards,
|
|
Req,
|
|
Put,
|
|
} from '@nestjs/common';
|
|
import { ApiTags, ApiBearerAuth } from '@nestjs/swagger';
|
|
import { AddDeviceDto, UpdateDeviceInRoomDto } from '../dtos/add.device.dto';
|
|
import {
|
|
GetDeviceByRoomUuidDto,
|
|
GetDeviceLogsDto,
|
|
} from '../dtos/get.device.dto';
|
|
import {
|
|
ControlDeviceDto,
|
|
BatchControlDevicesDto,
|
|
BatchStatusDevicesDto,
|
|
} from '../dtos/control.device.dto';
|
|
import { CheckRoomGuard } from 'src/guards/room.guard';
|
|
import { CheckUserHavePermission } from 'src/guards/user.device.permission.guard';
|
|
import { CheckUserHaveControllablePermission } from 'src/guards/user.device.controllable.permission.guard';
|
|
import { JwtAuthGuard } from '@app/common/guards/jwt.auth.guard';
|
|
import { CheckDeviceGuard } from 'src/guards/device.guard';
|
|
import { SuperAdminRoleGuard } from 'src/guards/super.admin.role.guard';
|
|
|
|
@ApiTags('Device Module')
|
|
@Controller({
|
|
version: '1',
|
|
path: 'device',
|
|
})
|
|
export class DeviceController {
|
|
constructor(private readonly deviceService: DeviceService) {}
|
|
@ApiBearerAuth()
|
|
@UseGuards(SuperAdminRoleGuard, CheckDeviceGuard)
|
|
@Post()
|
|
async addDeviceUser(@Body() addDeviceDto: AddDeviceDto) {
|
|
try {
|
|
const device = await this.deviceService.addDeviceUser(addDeviceDto);
|
|
|
|
return {
|
|
statusCode: HttpStatus.CREATED,
|
|
success: true,
|
|
message: 'device added successfully',
|
|
data: device,
|
|
};
|
|
} catch (error) {
|
|
throw new HttpException(
|
|
error.message || 'Internal server error',
|
|
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
|
|
);
|
|
}
|
|
}
|
|
@ApiBearerAuth()
|
|
@UseGuards(JwtAuthGuard)
|
|
@Get('user/:userUuid')
|
|
async getDevicesByUser(@Param('userUuid') userUuid: string) {
|
|
try {
|
|
return await this.deviceService.getDevicesByUser(userUuid);
|
|
} catch (error) {
|
|
throw new HttpException(
|
|
error.message || 'Internal server error',
|
|
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
|
|
);
|
|
}
|
|
}
|
|
@ApiBearerAuth()
|
|
@UseGuards(JwtAuthGuard, CheckRoomGuard)
|
|
@Get('room')
|
|
async getDevicesByRoomId(
|
|
@Query() getDeviceByRoomUuidDto: GetDeviceByRoomUuidDto,
|
|
@Req() req: any,
|
|
) {
|
|
try {
|
|
const userUuid = req.user.uuid;
|
|
return await this.deviceService.getDevicesByRoomId(
|
|
getDeviceByRoomUuidDto,
|
|
userUuid,
|
|
);
|
|
} catch (error) {
|
|
throw new HttpException(
|
|
error.message || 'Internal server error',
|
|
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
|
|
);
|
|
}
|
|
}
|
|
@ApiBearerAuth()
|
|
@UseGuards(JwtAuthGuard)
|
|
@Get('unit/:unitUuid')
|
|
async getDevicesByUnitId(@Param('unitUuid') unitUuid: string) {
|
|
try {
|
|
return await this.deviceService.getDevicesByUnitId(unitUuid);
|
|
} catch (error) {
|
|
throw new HttpException(
|
|
error.message || 'Internal server error',
|
|
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
|
|
);
|
|
}
|
|
}
|
|
@ApiBearerAuth()
|
|
@UseGuards(JwtAuthGuard, CheckRoomGuard)
|
|
@Put('room')
|
|
async updateDeviceInRoom(
|
|
@Body() updateDeviceInRoomDto: UpdateDeviceInRoomDto,
|
|
) {
|
|
try {
|
|
const device = await this.deviceService.updateDeviceInRoom(
|
|
updateDeviceInRoomDto,
|
|
);
|
|
|
|
return {
|
|
statusCode: HttpStatus.CREATED,
|
|
success: true,
|
|
message: 'device updated in room successfully',
|
|
data: device,
|
|
};
|
|
} catch (error) {
|
|
throw new HttpException(
|
|
error.message || 'Internal server error',
|
|
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
|
|
);
|
|
}
|
|
}
|
|
|
|
@ApiBearerAuth()
|
|
@UseGuards(JwtAuthGuard, CheckUserHavePermission)
|
|
@Get(':deviceUuid')
|
|
async getDeviceDetailsByDeviceId(
|
|
@Param('deviceUuid') deviceUuid: string,
|
|
@Req() req: any,
|
|
) {
|
|
try {
|
|
const userUuid = req.user.uuid;
|
|
return await this.deviceService.getDeviceDetailsByDeviceId(
|
|
deviceUuid,
|
|
userUuid,
|
|
);
|
|
} catch (error) {
|
|
throw new HttpException(
|
|
error.message || 'Internal server error',
|
|
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
|
|
);
|
|
}
|
|
}
|
|
@ApiBearerAuth()
|
|
@UseGuards(JwtAuthGuard, CheckUserHavePermission)
|
|
@Get(':deviceUuid/functions')
|
|
async getDeviceInstructionByDeviceId(
|
|
@Param('deviceUuid') deviceUuid: string,
|
|
) {
|
|
try {
|
|
return await this.deviceService.getDeviceInstructionByDeviceId(
|
|
deviceUuid,
|
|
);
|
|
} catch (error) {
|
|
throw new HttpException(
|
|
error.message || 'Internal server error',
|
|
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
|
|
);
|
|
}
|
|
}
|
|
@ApiBearerAuth()
|
|
@UseGuards(JwtAuthGuard, CheckUserHavePermission)
|
|
@Get(':deviceUuid/functions/status')
|
|
async getDevicesInstructionStatus(@Param('deviceUuid') deviceUuid: string) {
|
|
try {
|
|
return await this.deviceService.getDevicesInstructionStatus(deviceUuid);
|
|
} catch (error) {
|
|
throw new HttpException(
|
|
error.message || 'Internal server error',
|
|
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
|
|
);
|
|
}
|
|
}
|
|
|
|
@ApiBearerAuth()
|
|
@UseGuards(JwtAuthGuard, CheckUserHaveControllablePermission)
|
|
@Post(':deviceUuid/control')
|
|
async controlDevice(
|
|
@Body() controlDeviceDto: ControlDeviceDto,
|
|
@Param('deviceUuid') deviceUuid: string,
|
|
) {
|
|
try {
|
|
return await this.deviceService.controlDevice(
|
|
controlDeviceDto,
|
|
deviceUuid,
|
|
);
|
|
} catch (error) {
|
|
throw new HttpException(
|
|
error.message || 'Internal server error',
|
|
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
|
|
);
|
|
}
|
|
}
|
|
@ApiBearerAuth()
|
|
@UseGuards(JwtAuthGuard)
|
|
@Post(':deviceUuid/firmware/:firmwareVersion')
|
|
async updateDeviceFirmware(
|
|
@Param('deviceUuid') deviceUuid: string,
|
|
@Param('firmwareVersion') firmwareVersion: number,
|
|
) {
|
|
try {
|
|
return await this.deviceService.updateDeviceFirmware(
|
|
deviceUuid,
|
|
firmwareVersion,
|
|
);
|
|
} catch (error) {
|
|
throw new HttpException(
|
|
error.message || 'Internal server error',
|
|
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
|
|
);
|
|
}
|
|
}
|
|
@ApiBearerAuth()
|
|
@UseGuards(JwtAuthGuard)
|
|
@Get('gateway/:gatewayUuid/devices')
|
|
async getDevicesInGateway(@Param('gatewayUuid') gatewayUuid: string) {
|
|
try {
|
|
return await this.deviceService.getDevicesInGateway(gatewayUuid);
|
|
} catch (error) {
|
|
throw new HttpException(
|
|
error.message || 'Internal server error',
|
|
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
|
|
);
|
|
}
|
|
}
|
|
@ApiBearerAuth()
|
|
@UseGuards(JwtAuthGuard)
|
|
@Get()
|
|
async getAllDevices() {
|
|
try {
|
|
return await this.deviceService.getAllDevices();
|
|
} catch (error) {
|
|
throw new HttpException(
|
|
error.message || 'Internal server error',
|
|
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
|
|
);
|
|
}
|
|
}
|
|
@ApiBearerAuth()
|
|
@UseGuards(JwtAuthGuard)
|
|
@Get('report-logs/:deviceUuid')
|
|
async getBuildingChildByUuid(
|
|
@Param('deviceUuid') deviceUuid: string,
|
|
@Query() query: GetDeviceLogsDto,
|
|
) {
|
|
try {
|
|
return await this.deviceService.getDeviceLogs(deviceUuid, query);
|
|
} catch (error) {
|
|
throw new HttpException(
|
|
error.message || 'Internal server error',
|
|
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
|
|
);
|
|
}
|
|
}
|
|
@ApiBearerAuth()
|
|
@UseGuards(JwtAuthGuard)
|
|
@Post('control/batch')
|
|
async batchControlDevices(
|
|
@Body() batchControlDevicesDto: BatchControlDevicesDto,
|
|
) {
|
|
try {
|
|
return await this.deviceService.batchControlDevices(
|
|
batchControlDevicesDto,
|
|
);
|
|
} catch (error) {
|
|
throw new HttpException(
|
|
error.message || 'Internal server error',
|
|
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
|
|
);
|
|
}
|
|
}
|
|
@ApiBearerAuth()
|
|
@UseGuards(JwtAuthGuard)
|
|
@Get('status/batch')
|
|
async batchStatusDevices(
|
|
@Query() batchStatusDevicesDto: BatchStatusDevicesDto,
|
|
) {
|
|
try {
|
|
return await this.deviceService.batchStatusDevices(batchStatusDevicesDto);
|
|
} catch (error) {
|
|
throw new HttpException(
|
|
error.message || 'Internal server error',
|
|
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
|
|
);
|
|
}
|
|
}
|
|
}
|