ICommandAdapter.java
2.63 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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package com.ruoyi.system.protocol.adapter;
import com.ruoyi.system.protocol.ThinkraceUtil;
import com.ruoyi.system.protocol.adapter.device.ILoginPacketAdapter;
import io.netty.channel.ChannelHandlerContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* 命令适配器基类
*
* @author 20412
*/
public abstract class ICommandAdapter {
protected static final Logger log = LoggerFactory.getLogger(ICommandAdapter.class);
/**
* 指令解析
*
* @param command 指令
* @param ctx netty处理器上下文
* @return obj 处理结果
*/
public abstract Object resolveCommand(String command, ChannelHandlerContext ctx);
/**
* 指令下发
*
* @return obj 处理结果
*/
public abstract CommandListen sendCommand(Object params);
/**
* 生成命令序列号
*
* @return commandSn
*/
protected String generatorSn() {
return ThinkraceUtil.generatorSn();
}
/**
* 设置命令
*
* @param sn 命令流水号
* @param imei 设备号
* @return
*/
private CommandListen syncCommandSnAsk(String imei, String sn) {
String key = ThinkraceUtil.commandLabelInit(imei, sn);
return new CommandListenImpl(key);
}
protected CommandListen syncSendCommand(String imei, String sn, String command) {
ThinkraceUtil.sendCommand(imei, command);
return syncCommandSnAsk(imei, sn);
}
public interface CommandListen {
/**
* 监听应答
*
* @return 应答结果
*/
boolean listenAsk();
}
protected class CommandListenImpl implements CommandListen {
private String key;
protected CommandListenImpl(String key) {
this.key = key;
}
@Override
public boolean listenAsk() {
// 循环读取
try {
Thread.sleep(100);
Integer val = ThinkraceUtil.getCommandSn(this.key);
while (val != null) {
if (!val.equals(ThinkraceUtil.COMMAND_LABEL_SURE)) {
return true;
}
Thread.sleep(100);
val = ThinkraceUtil.getCommandSn(this.key);
}
return false;
} catch (Exception e) {
System.out.println("等待命令响应出现异常:" + e.getMessage());
return false;
}
}
}
protected void responseLog(String imei, String response) {
log.info("设备:{},命令解析完毕,返回响应:{}", imei, response);
}
}