nestjs - 04

Nestjs 自定义装饰器

用命令自动生成装饰器文件,亦可手动创建
nest g d decorator
// decorator.ts
import { SetMetadata, createParamDecorator, ExecutionContext, applyDecorators } from '@nestjs/common';
import type { Request } from 'express'
// 权限控制
export const Role = (...args: string[]) => SetMetadata('role', args);

// 获取URL
// @param data => 使用decorator时候传递的参数  context => 上下文
export const ReqUrl = createParamDecorator((data: string, context: ExecutionContext) => {
  const req = context.switchToHttp().getRequest<Request>()
  console.log('data:', data);
  return req.url
  // applyDecorators  组合装饰器,可以组合多种装饰器
  // eg return applyDecorators(Role, xx, xxxx)
})

Decorator 使用方法
import { Controller,  } from '@nestjs/common'
import { Role, ReqUrl } from './decorator'
@Controller()
export class DemoController {
  @Get()
  @Role('admin')
  findAll(@ReqUrl('data') url: string) {
    console.log('url:', url)
    return this.guardService.findAll();
  }
}
Node-s7 是一个用于连接和通信 Siemens S7 系列 PLC 的 Node.js 模块。在 NestJS 中使用 node-s7,你需要安装该模块并在你的代码中引入它。 以下是一个示例: 1. 安装 node-s7 模块 ```bash npm install node-s7 --save ``` 2. 在你的 NestJS 服务中引入 node-s7 ```typescript import { Injectable } from '@nestjs/common'; import { S7Client } from 'node-s7'; @Injectable() export class S7Service { private readonly client: S7Client; constructor() { this.client = new S7Client(); } async connect() { await this.client.ConnectTo( '192.168.1.10', // PLC IP 地址 0, // PLC 端口 1 // PLC 编号 ); } async readData() { const data = await this.client.DBRead(1, 0, 10); // 从 DB1 中读取 10 个字节数据 return data; } async writeData() { const data = Buffer.from([0x01, 0x02, 0x03, 0x04]); await this.client.DBWrite(1, 0, data); // 将数据写入 DB1 } async disconnect() { await this.client.Disconnect(); } } ``` 在上面的示例中,我们创建了一个 `S7Service` 类,该类包含了连接、读取和写入数据到 PLC 的方法。我们通过 `node-s7` 模块中的 `S7Client` 类来连接和通信 PLC。在 `connect` 方法中,我们连接到了 PLC,并在 `readData` 和 `writeData` 方法中读取和写入数据到 PLC。在 `disconnect` 方法中,我们断开了与 PLC 的连接。 你可以在控制器中使用 `S7Service` 类来读取和写入数据到 PLC。 ```typescript import { Controller, Get, Post } from '@nestjs/common'; import { S7Service } from './s7.service'; @Controller('plc') export class S7Controller { constructor(private readonly s7Service: S7Service) {} @Get('connect') async connect() { await this.s7Service.connect(); return { message: 'Connected to PLC' }; } @Get('read') async readData() { const data = await this.s7Service.readData(); return { data }; } @Post('write') async writeData() { await this.s7Service.writeData(); return { message: 'Data written to PLC' }; } @Get('disconnect') async disconnect() { await this.s7Service.disconnect(); return { message: 'Disconnected from PLC' }; } } ``` 在上面的示例中,我们创建了一个 `S7Controller` 控制器,并注入了 `S7Service` 服务。我们使用 `@Get` 和 `@Post` 装饰器来定义了连接、读取和写入数据、断开连接的路由。在每个路由方法中,我们调用了 `S7Service` 中对应的方法。 这就是在 NestJS 中使用 node-s7 的基本示例。你可以根据自己的需求进行修改和扩展。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值