项目初始化时可以对所有接口进行信息记录(比如配合注解收集接口权限信息存入数据库、生成接口文档、等等)

配合自定义注解和Swagger2注解进行权限资源初始化。


import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;

import javax.annotation.PostConstruct;

import org.crown.common.annotations.Resources;
import org.crown.model.entity.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;

import com.baomidou.mybatisplus.core.toolkit.ArrayUtils;

import io.swagger.annotations.ApiOperation;
import liquibase.util.MD5Util;

/**
 * <p>
 * 服务mapping扫描
 * </p>
 *
 * @author Caratacus
 */
@Service
public class ScanMappings {

    @Autowired
    private IResourceService resourceService;

    @Autowired
    private RequestMappingHandlerMapping handlerMapping;

    private String[] emptyArray = new String[]{""};

    /**
     * 扫描资源插入数据库
     */
    @PostConstruct
    public void doScan() {
        resourceService.saveOrUpdateBatch(
                handlerMapping.getHandlerMethods()
                        .values()
                        .stream()
                        .map(this::getResources)
                        .flatMap(Collection::stream)
                        .collect(Collectors.toList())
        );
    }

    /**
     * 获取Resource
     *
     * @param handlerMethod
     * @return
     */
    public List<Resource> getResources(HandlerMethod handlerMethod) {
        Resources res = handlerMethod.getMethodAnnotation(Resources.class);
        if (Objects.isNull(res)) {
            return Collections.emptyList();
        }
        RequestMapping requestMappingAnnotation = handlerMethod.getBeanType().getAnnotation(RequestMapping.class);
        RequestMapping methodMappingAnnotation = handlerMethod.getMethodAnnotation(RequestMapping.class);
        if (Objects.isNull(requestMappingAnnotation) && Objects.isNull(methodMappingAnnotation)) {
            return Collections.emptyList();
        }
        ApiOperation apiOperation = handlerMethod.getMethodAnnotation(ApiOperation.class);
        String[] requestMappings = Objects.nonNull(requestMappingAnnotation) ? requestMappingAnnotation.value() : emptyArray;
        String[] methodMappings = Objects.nonNull(methodMappingAnnotation) ? methodMappingAnnotation.path() : emptyArray;
        RequestMethod[] method = Objects.nonNull(methodMappingAnnotation) ? methodMappingAnnotation.method() : new RequestMethod[0];
        requestMappings = ArrayUtils.isEmpty(requestMappings) ? emptyArray : requestMappings;
        methodMappings = ArrayUtils.isEmpty(methodMappings) ? emptyArray : methodMappings;
        Set<String> mappings = new HashSet<>(1);
        for (String reqMapping : requestMappings) {
            for (String methodMapping : methodMappings) {
                mappings.add(reqMapping + methodMapping);
            }
        }
        List<Resource> resources = new ArrayList<>(1);
        for (RequestMethod requestMethod : method) {
            for (String mapping : mappings) {
                //接口描述
                Resource resource = new Resource();
                resource.setResourceName(Objects.nonNull(apiOperation) ? apiOperation.value() : "未命名资源路径");
                resource.setMapping(mapping);
                resource.setMethod(requestMethod.name());
                resource.setAuthType(res.auth());
                resource.setPerm(resourceService.getResourcePermTag(requestMethod.name(), mapping));
                resource.setId(MD5Util.computeMD5(resource.getPerm()));
                resources.add(resource);
            }
        }
        return resources;
    }

}

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
这是一个比较复杂的程序,需要涉及到硬件和软件两方面的知识。下面给出一个大致的实现思路,仅供参考。 1. 硬件部分 要实现RS485接口modbus协议设备数据自动采集,需要使用一块RS485转串口的模块,将其连接到计算机的串口或USB口上。同,还需要将要采集的设备连接到RS485模块上,并正确设置其通讯参数(如波特率、数据位、校验位等)。 2. 软件部分 2.1 采集程序 编写一个采集程序,通过串口读取RS485接口modbus协议设备的数据,并将其存入数据库中。具体实现可以使用Python语言编写,使用第三方库如PyModbus等来实现modbus通讯协议的解析和数据读取。可以使用定器或线程来实现自动采集功能。 2.2 数据库存储 可以使用MySQL等关系型数据库来存储采集的数据。创建一个数据表来存储采集的数据,包括设备ID、采集间、数据值等字段。 2.3 报警和记录 根据设备的实际情况,设置一个阈值来进行报警。当采集到的数据超过了阈值,就触发报警操作。可以通过发送邮件、短信或弹窗等方式进行报警通知。同,将报警信息记录到另一个数据表中,包括设备ID、采集间、报警值等字段。 3. 总体流程 1) 初始化串口和数据库连接。 2) 定执行采集程序,读取设备数据并存入数据库。 3) 对比采集到的数据和阈值,如果超过阈值则触发报警,并记录报警信息数据库。 4) 关闭串口和数据库连接。 以上是一个简单的实现思路,具体实现还需要根据具体需求进行修改和完善。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值