import { Body, Controller, Request, Post, UseGuards, Get, ParseIntPipe, Param, Query, HttpCode, } from '@nestjs/common'; import { SensorsService } from './sensors.service'; import { ApiBearerAuth, ApiOkResponse, ApiOperation, ApiQuery, ApiTags } from '@nestjs/swagger'; import { JwtAuthGuard } from 'src/auth/jwt-auth.guard'; import { SensorGroupResponseDto } from './dto/sensor-group-response.dto'; import { AuthRequest } from 'src/common/interfaces/auth-request.interface'; import { CreateSensorGroupDto } from './dto/create-sensor-group.dto'; import { SensorResponseDto } from './dto/sensor-response.dto'; import { CreateSensorDto } from './dto/create-sensor.dto'; import { SensorDataResponseDto } from './dto/sensor-data-response.dto'; import { CreateSensorDataDto } from './dto/create-sensor-data.dto'; @ApiTags('센서') @ApiBearerAuth() @UseGuards(JwtAuthGuard) @Controller('sensors') export class SensorsController { constructor(private readonly sensorService: SensorsService) {} //#region Group @Post('sensor-groups') @HttpCode(200) @ApiOperation({ summary: '센서 그룹 생성' }) @ApiOkResponse({ description: '성공', type: SensorGroupResponseDto }) async createGroup( @Request() req: AuthRequest, @Body() dto: CreateSensorGroupDto, ): Promise { return this.sensorService.createGroup(req.user.userId, dto); } @Get('sensor-groups') @ApiOperation({ summary: '센서 그룹 목록' }) @ApiOkResponse({ description: '성공', type: [SensorGroupResponseDto] }) async myGroup(@Request() req: AuthRequest): Promise { return this.sensorService.findMyGroups(req.user.userId); } //#endregion //#region Sensor @Post('sensors') @HttpCode(200) @ApiOperation({ summary: '센서 생성' }) @ApiOkResponse({ description: '성공', type: SensorResponseDto }) async createSensor( @Request() req: AuthRequest, @Body() dto: CreateSensorDto, ): Promise { return this.sensorService.createSensor(req.user.userId, dto); } @Get('sensor-groups/:groupId/sensors') @ApiOperation({ summary: '센서 목록' }) @ApiOkResponse({ description: '성공', type: [SensorResponseDto] }) async sensorsInGroup( @Request() req: AuthRequest, @Param('groupId', ParseIntPipe) groupId: number, ): Promise { return this.sensorService.findSensorsInGroup(req.user.userId, groupId); } //#endregion //#region Data @Post('sensors/:sensorId/data') @HttpCode(200) @ApiOperation({ summary: '센서 데이터 생성' }) @ApiOkResponse({ description: '성공', type: SensorDataResponseDto }) async createData( @Request() req: AuthRequest, @Param('sensorId', ParseIntPipe) sensorId: number, @Body() dto: CreateSensorDataDto, ): Promise { return this.sensorService.createSensorData(req.user.userId, sensorId, dto); } @Get('sensors/:sensorId/data') @ApiOperation({ summary: '센서 데이터 조회' }) @ApiQuery({ name: 'from', required: false, description: 'ISO날짜' }) @ApiQuery({ name: 'to', required: false, description: 'ISO날짜' }) @ApiQuery({ name: 'limit', required: false, description: '조회 개수 (기본50, 최대 500)' }) @ApiOkResponse({ description: '성공', type: [SensorDataResponseDto] }) async getData( @Request() req: AuthRequest, @Param('sensorId', ParseIntPipe) sensorId: number, @Query('from') from?: string, @Query('to') to?: string, @Query('limit') limit?: string, ): Promise { const parsedFrom = from ? new Date(from) : undefined; const parsedTo = to ? new Date(to) : undefined; const parsedLimit = limit ? Number(limit) : undefined; return this.sensorService.findSensorData(req.user.userId, sensorId, { from: parsedFrom, to: parsedTo, limit: parsedLimit, }); } //#endregion }