device.controller.ts
5.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import { Controller, Get, Post, Body, UseInterceptors, Query, UsePipes, Inject } from '@nestjs/common'
import { DeviceService } from './device.service'
import { TransformInterceptor } from '../../common/interceptors'
import { AddDeviceInfoDto, DeviceFindOneDto, DeviceQueryDto, DeviceStatusDto, UpdateDeviceInfoDto } from '../../dto'
import { PagesPipe, ValidationPipe } from '../../common/pipes'
import { OnEvent, EventEmitter2 } from '@nestjs/event-emitter'
import { WsAccess, Events, WsMessageType, Command, RESPONSE_CODE } from '../../common/enums'
import { ApiOperation, ApiParam, ApiTags } from '@nestjs/swagger'
@ApiTags('设备管理')
@Controller('device')
@UseInterceptors(TransformInterceptor)
// @UsePipes(new ValidationPipe({transform: true}))
@UsePipes(new ValidationPipe()) // 使用管道验证
export class DeviceController {
/**
* 构造函数,注入DeviceService和EventEmitter2
* @param param - 包含设备信息的DTO对象
* @returns 一个Promise,成功时解析为包含CommonTypes.IResponseBase接口的CommonTypes.IResData对象
*/
constructor(
@Inject(DeviceService)
private readonly deviceService: DeviceService,
@Inject(EventEmitter2)
private eventEmitter: EventEmitter2
) {}
/**
* 查找所有设备的方法
* 该方法接收一个包含查询条件的对象,并返回符合条件的设备列表
* @param param - 包含查询条件的对象
* @returns 一个Promise,成功时解析为包含CommonTypes.IResponseBase接口的CommonTypes.IResData对象
*/
@Get()
@UsePipes(new PagesPipe())
@ApiOperation({ summary: '获取所有设备列表' })
async findAll(@Query() params: DeviceQueryDto): Promise<CommonTypes.IResData> {
return await this.deviceService.findAll(params)
}
async finOne(@Query() params: DeviceFindOneDto): Promise<CommonTypes.IResData> {
return await this.deviceService.findOne(params)
}
/**
* 添加设备的方法
* 该方法接收一个包含设备信息的DTO对象,并返回添加后的设备信息
* @param param - 包含设备信息的DTO对象
* @returns 一个Promise,成功时解析为包含CommonTypes.IResponseBase接口的CommonTypes.IResData对象
*/
@Post('/add')
@ApiOperation({ summary: '添加设备' })
async addDevice(@Body() param: AddDeviceInfoDto): Promise<CommonTypes.IResponseBase> {
return await this.deviceService.addDevice(param)
}
/**
* 添加设备信息的方法
* 该方法接收一个包含设备信息的DTO对象,并返回添加后的设备信息
* @param param - 包含设备信息的DTO对象
* @returns 一个Promise,成功时解析为包含CommonTypes.IResponseBase接口的CommonTypes.IResData对象
*/
@Post('/addInfo')
@ApiOperation({ summary: '添加设备信息' })
async addDeviceInfo(@Body() param: UpdateDeviceInfoDto): Promise<CommonTypes.IResData> {
return await this.deviceService.addDeviceInfo(param)
}
/**
* 更新设备信息的方法
* 该方法接收一个包含设备信息的DTO对象,并返回更新后的设备信息
* @param param - 包含设备信息的DTO对象
* @returns 一个Promise,成功时解析为包含CommonTypes.IResponseBase接口的CommonTypes.IResData对象
*/
@Post('/updateInfo')
@ApiOperation({ summary: '修改设备信息' })
async updateDeviceInfo(@Body() param: UpdateDeviceInfoDto): Promise<CommonTypes.IResData> {
return await this.deviceService.updateDeviceInfo(param)
}
/**
* 更新设备状态的方法
* 该方法接收一个包含设备状态的DTO对象,并返回更新后的设备状态
* @param param - 包含设备状态的DTO对象
* @returns 一个Promise,成功时解析为包含CommonTypes.IResponseBase接口的CommonTypes.IResData对象
*/
@Post('/upsertStatus')
@OnEvent(Events.OnConnected)
@ApiOperation({ summary: '更新设备状态' })
async upsertDeviceStatus(@Body() param: DeviceStatusDto): Promise<void | CommonTypes.IResData> {
return await this.deviceService
.upsertDeviceStatus(param)
.then(async () => {
await this.deviceService.sendDeviceStatusMessage(param)
})
.catch(error => {
const out: WsTypes.WsConnError = { message: WsAccess.ConnFailure, device_id: param.device_id }
this.eventEmitter.emit(Events.OnConnectError, out)
})
}
@Post('/upsertFileRemovedTime')
@OnEvent(Events.OnLogFileRemoved)
@ApiOperation({ summary: '更新设备状态' })
async upsertFileRemovedTime(@Body() param: DeviceStatusDto): Promise<void | CommonTypes.IResData> {
return await this.deviceService.upsertFileRemovedTime(param)
}
/**
* 重置所有设备状态的方法
* 该方法接收一个包含设备状态的DTO对象,并返回更新后的设备状态
* @param param - 包含设备状态的DTO对象
* @returns 一个Promise,成功时解析为包含CommonTypes.IResponseBase接口的CommonTypes.IResData对象
*/
@Post('/offlineAll')
@OnEvent(Events.OnOfflineAll)
@ApiOperation({ summary: '修改所有设备状态为离线' })
async resetAllDeviceStatus(): Promise<void | CommonTypes.IResData> {
return await this.deviceService.resetAllDeviceStatus()
}
@Get('/task')
@OnEvent(Events.OnDailyTaskStart)
@ApiOperation({ summary: '每日任务' })
async task(): Promise<void | CommonTypes.IResData> {
return await this.deviceService.removeFilesTask()
}
@Get('/addAll')
@ApiOperation({ summary: '批量导入设备,用于初始化数据' })
async addAll(devices: Array<any>): Promise<void | CommonTypes.IResData> {
// return this.deviceService.addAll(devices)
}
}