增加设备管理
continuous-integration/drone/push Build is passing Details

main
rustdreamer 2 years ago
parent a85b112e84
commit 2cfc69da4d

@ -15,6 +15,7 @@ import { v4 as uuid } from 'uuid';
import { verificationService } from './lib/face'; import { verificationService } from './lib/face';
import { join } from 'path'; import { join } from 'path';
import { AreaEntity } from './area.entity'; import { AreaEntity } from './area.entity';
import { DeviceEntity } from './device.entity';
export class Electronics { export class Electronics {
@ApiProperty({ description: "电子产品名称" }) @ApiProperty({ description: "电子产品名称" })
@ -254,6 +255,74 @@ export class DeleteArea {
public id: string; public id: string;
} }
export class DeviceDto extends CommonPageArgs {
@ApiPropertyOptional({ description: "根据名称模糊搜索", required: false })
@IsString()
@IsOptional()
search?: string
}
export class CreateDevice {
@ApiProperty({ description: "" })
@IsString()
@IsNotEmpty()
public name: string;
@ApiProperty({ description: "" })
@IsString()
@IsOptional()
public cn: string;
@ApiProperty({ description: "" })
@IsString()
@IsOptional()
public ip: string;
@ApiProperty({ description: "" })
@IsString()
@IsOptional()
public area_id: string;
@ApiProperty({ description: "" })
@IsString()
@IsOptional()
public address: string;
}
export class UpdateDevice {
@ApiProperty({ description: "" })
@IsString()
@IsNotEmpty()
public name: string;
@ApiProperty({ description: "" })
@IsString()
@IsOptional()
public cn: string;
@ApiProperty({ description: "" })
@IsString()
@IsOptional()
public ip: string;
@ApiProperty({ description: "" })
@IsString()
@IsOptional()
public area_id: string;
@ApiProperty({ description: "" })
@IsString()
@IsOptional()
public address: string;
}
export class DeleteDevice {
@ApiProperty({ description: "" })
@IsString()
@IsNotEmpty()
public id: string;
}
export class QueryVisitorByNumberDto { export class QueryVisitorByNumberDto {
@ApiPropertyOptional({ description: "身份证号", required: true }) @ApiPropertyOptional({ description: "身份证号", required: true })
@ -276,6 +345,8 @@ export class AppController {
private readonly nodeVisitorRepository: Repository<NodeVisitorEntity>, private readonly nodeVisitorRepository: Repository<NodeVisitorEntity>,
@InjectRepository(AreaEntity) @InjectRepository(AreaEntity)
private readonly areaRepository: Repository<AreaEntity>, private readonly areaRepository: Repository<AreaEntity>,
@InjectRepository(DeviceEntity)
private readonly deviceRepository: Repository<DeviceEntity>,
@InjectRepository(NodeOtherVisitorEntity) @InjectRepository(NodeOtherVisitorEntity)
private readonly nodeOtherVisitorRepository: Repository<NodeOtherVisitorEntity>, private readonly nodeOtherVisitorRepository: Repository<NodeOtherVisitorEntity>,
) { } ) { }
@ -702,6 +773,132 @@ export class AppController {
return { list, count } return { list, count }
} }
@Post("/device")
async createDevice(@Body() data: CreateDevice, @Req() req: any) {
if (!req.headers.authorization) {
throw new BadRequestException("无权限")
}
try {
// console.log(`${process.env.RUST_URI}/api/viewer`)
const result = await axios.get(`${process.env.RUST_URI}/api/viewer`, {
headers: {
authorization: req.headers.authorization
}
})
// console.log("result", result)
} catch (e) {
// console.log(e)
throw new BadRequestException("无权限, 请联系管理员")
}
return await this.dataSource.transaction(async transactionalEntityManager => {
const device = new DeviceEntity();
device.name = data.name;
device.cn = data.cn;
device.ip = data.ip;
device.area_id = data.area_id;
device.address = data.address;
const result = await transactionalEntityManager.save(device);
return { statusCode: 201, data: result };
});
}
@Put("/device/:id")
async updateDevice(@Param() { id }: any, @Body() data: UpdateDevice, @Req() req: any) {
if (!req.headers.authorization) {
throw new BadRequestException("无权限")
}
try {
// console.log(`${process.env.RUST_URI}/api/viewer`)
const result = await axios.get(`${process.env.RUST_URI}/api/viewer`, {
headers: {
authorization: req.headers.authorization
}
})
// console.log("result", result)
} catch (e) {
// console.log(e)
throw new BadRequestException("无权限, 请联系管理员")
}
const device = await this.deviceRepository.findOne({ where: { id } });
if (!device) {
throw new NotFoundException("device not found");
}
device.name = data.name;
device.cn = data.cn;
device.ip = data.ip;
device.area_id = data.area_id;
device.address = data.address;
const result = await this.deviceRepository.save(device);
return result;
}
@Delete("/device/:id")
async deleteDevice(@Param() { id }: any, @Req() req: any) {
if (!req.headers.authorization) {
throw new BadRequestException("无权限")
}
try {
// console.log(`${process.env.RUST_URI}/api/viewer`)
const result = await axios.get(`${process.env.RUST_URI}/api/viewer`, {
headers: {
authorization: req.headers.authorization
}
})
// console.log("result", result)
} catch (e) {
// console.log(e)
throw new BadRequestException("无权限, 请联系管理员")
}
const result = await this.deviceRepository.findOne({ where: { id } });
if (!result) {
throw new NotFoundException("device not found");
}
await this.deviceRepository.delete(id);
return result;
}
@Get("/devices")
@ApiBearerAuth()
async devices(@Query() query_data: AreaDto, @Req() req: any) {
// console.log("req", req.headers)
if (!req.headers.authorization) {
throw new BadRequestException("无权限")
}
try {
// console.log(`${process.env.RUST_URI}/api/viewer`)
const result = await axios.get(`${process.env.RUST_URI}/api/viewer`, {
headers: {
authorization: req.headers.authorization
}
})
// console.log("result", result)
} catch (e) {
// console.log(e)
throw new BadRequestException("无权限, 请联系管理员")
}
const where: any = {};
const query = this.areaRepository.createQueryBuilder('area');
query.where(where);
if (query_data.search) {
const string = `%${query_data.search}%`;
const fields = ['name'];
const searchString = fields.join(' like :search OR area.');
query.where(`area.${searchString} like :search`, {
search: string,
});
}
const order_key = 'area.created_date';
let order_value: any = 'DESC';
const [list, count] = await query
.skip(query_data.skip)
.take(query_data.take)
.orderBy(order_key, order_value)
.getManyAndCount();
return { list, count }
}
@Put("/visitor/number") @Put("/visitor/number")
async updateVisitor(@Body() data: UpdateVisitorNumber) { async updateVisitor(@Body() data: UpdateVisitorNumber) {
const node_other_visitor = await this.nodeOtherVisitorRepository.findOne({ where: { id: data.id } }) const node_other_visitor = await this.nodeOtherVisitorRepository.findOne({ where: { id: data.id } })

@ -13,6 +13,7 @@ import { VisitorEntity } from './visitor.entity';
import { NodeVisitorEntity } from './node-visitor.entity'; import { NodeVisitorEntity } from './node-visitor.entity';
import { NodeOtherVisitorEntity } from './node-other-visitor.entity'; import { NodeOtherVisitorEntity } from './node-other-visitor.entity';
import { AreaEntity } from './area.entity'; import { AreaEntity } from './area.entity';
import { DeviceEntity } from './device.entity';
@Module({ @Module({
imports: [ imports: [
@ -24,7 +25,8 @@ import { AreaEntity } from './area.entity';
VisitorEntity, VisitorEntity,
NodeVisitorEntity, NodeVisitorEntity,
NodeOtherVisitorEntity, NodeOtherVisitorEntity,
AreaEntity AreaEntity,
DeviceEntity
]), ]),
ScheduleModule.forRoot(), ScheduleModule.forRoot(),
RoleModule, RoleModule,

@ -0,0 +1,28 @@
import { BaseEntity, Column, Entity, Index, JoinColumn, OneToMany, OneToOne } from 'typeorm';
import { createHmac } from 'crypto';
import { Base } from './common';
import { ApiProperty } from '@nestjs/swagger';
// 设备
@Entity({ name: 'device' })
export class DeviceEntity extends Base {
@ApiProperty({ description: "name" })
@Column({ length: 150, nullable: false, type: 'char' })
public name: string;
@ApiProperty({ description: "cn" })
@Column({ length: 150, nullable: true, type: 'char' })
public cn?: string;
@ApiProperty({ description: "ip" })
@Column({ length: 150, nullable: true, type: 'char' })
public ip?: string;
@ApiProperty({ description: "" })
@Column({ length: 36, nullable: true })
public area_id?: string;
@ApiProperty({ description: "设备地址" })
@Column({ length: 255, nullable: true })
public address?: string;
}
Loading…
Cancel
Save