CommandStrategy.java 1.36 KB
package com.ruoyi.system.strategy;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.protocol.ThinkraceUtil;
import com.ruoyi.system.protocol.adapter.ICommandAdapter;
import io.netty.channel.ChannelHandlerContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import static com.ruoyi.system.protocol.ThinkraceUtil.REQ.LOGIN_REQ;

/**
 * 命令观察者模式
 *
 * @author 20412
 */
public class CommandStrategy {
    private static final Logger log = LoggerFactory.getLogger(CommandStrategy.class);

    public CommandStrategy() {
    }

    public void handler(String command, ChannelHandlerContext ctx) {
        processor(command, ctx);
    }

    private void processor(String command, ChannelHandlerContext ctx) {
        if (StringUtils.isNotEmpty(command) && command.length() > 6) {
            String key = command.substring(0, 6);
            // 未上传登录包无法使用对应指令
            ICommandAdapter adapter = ThinkraceUtil.getAdapter(key);
            if (filterCommand(key, ctx) && adapter != null) {
                adapter.resolveCommand(command, ctx);
                return;
            }
            log.error("设备传来未知指令:{}", command);
        }
    }

    private boolean filterCommand(String key, ChannelHandlerContext ctx) {
        return LOGIN_REQ.equals(key) || ThinkraceUtil.hasDevice(ctx.channel());
    }
}