CommandStrategy.java
1.36 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
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());
}
}