ProtocolServiceImplTest.java
6.43 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
package com.fh.party;
import org.junit.Before;
import org.junit.Test;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.huawei.m2m.cig.tup.modules.protocol_adapter.IProtocolAdapter;
/**
* Unit test for simple App.
*/
public class ProtocolServiceImplTest {
private IProtocolAdapter protocolAdapter;
@Before
public void setProtocolAdapter() {
this.protocolAdapter = new ProtocolAdapterImpl();
}
/**
* 测试用例1:设备向平台上报数据。
* <p>
* <pre>
* 设备上报数据:AA72000032088D0320623399
* </pre>
*
* @throws Exception
*/
@Test
public void testDecodeDeviceReportData() throws Exception {
byte[] deviceReqByte = getArray();
ObjectNode objectNode = protocolAdapter.decode(deviceReqByte);
String str = objectNode.toString();
System.out.println(str);
}
/**
* 测试用例2:平台向设备下发控制命令:
* <p>
* <pre>
* {
* //"identifier": "123",
* "msgType": "cloudReq",
* "cmd": "SET_DEVICE_LEVEL",
* "mid": 2016,
* "paras": { "value": "10" },
* "hasMore": 0
* }
* </pre>
*/
//@Test
public byte[] testEncodeIoTSendCommand() throws Exception {
ObjectNode CloudReqObjectNode = initCloudReqObjectNode();
byte[] outputByte = protocolAdapter.encode(CloudReqObjectNode);
System.out.println("cloudReq output:" + parseByte2HexStr(outputByte));
return outputByte;
}
/**
* 测试用例3:设备对平台命令的应答消息 有命令短id
* <p>
* <pre>
* 设备应答消息:AA7201000107E0
*
* <pre>
*
* @throws Exception
*/
//@Test
public void testDecodeDeviceResponseIoT() throws Exception {
byte[] deviceRspByte = initDeviceRspByte();
ObjectNode objectNode = protocolAdapter.decode(deviceRspByte);
String str = objectNode.toString();
System.out.println(str);
}
/**
* 测试用例4:平台收到设备的上报数据后对设备的应答,如果不需要应答则返回null即可
* <pre>
* {
* "identifier": "0",
* "msgType": "cloudRsp",
* "request": [AA,72,00,00,32,08,8D,03,20,62,33,99],
* "errcode": 0,
* "hasMore": 0
* }
*
* <pre>
*
* @throws Exception
*/
//@Test
public void testEncodeIoTResponseDevice() throws Exception {
byte[] deviceReqByte = initDeviceReqByte();
ObjectNode cloudRspObjectNode = initCloudRspObjectNode(deviceReqByte);
byte[] outputByte2 = protocolAdapter.encode(cloudRspObjectNode);
System.out.println("cloudRsp output:" + parseByte2HexStr(outputByte2));
}
public static String parseByte2HexStr(byte[] buf) {
if (null == buf) {
return null;
}
StringBuffer sb = new StringBuffer();
for (int i = 0; i < buf.length; i++) {
String hex = Integer.toHexString(buf[i] & 0xFF);
if (hex.length() == 1) {
hex = '0' + hex;
}
sb.append(hex.toUpperCase());
}
return sb.toString();
}
/*
* 初始化:设备数据上报码流
*/
private static byte[] initDeviceReqByte() {
/**
* 本例入参: AA 72 00 00 32 08 8D 03 20 62 33 99
*/
byte[] byteDeviceReq = new byte[12];
byteDeviceReq[0] = (byte) 0xAA;
byteDeviceReq[1] = (byte) 0x72;
byteDeviceReq[2] = (byte) 0x00;
byteDeviceReq[3] = (byte) 0x00;
byteDeviceReq[4] = (byte) 0x32;
byteDeviceReq[5] = (byte) 0x08;
byteDeviceReq[6] = (byte) 0x8D;
byteDeviceReq[7] = (byte) 0x03;
byteDeviceReq[8] = (byte) 0x20;
byteDeviceReq[9] = (byte) 0x62;
byteDeviceReq[10] = (byte) 0x33;
byteDeviceReq[11] = (byte) 0x99;
System.out.println(byteDeviceReq[5]);
return byteDeviceReq;
}
/*
* 初始化:平台向设备命令下发数据
*/
private static ObjectNode initCloudReqObjectNode() {
ObjectMapper mapper = new ObjectMapper();
ObjectNode cloudReqObjectNode = mapper.createObjectNode();
ObjectNode paras = mapper.createObjectNode();
paras.put("value", "10");
cloudReqObjectNode.put("identifier", "123");
cloudReqObjectNode.put("msgType", "cloudReq");
cloudReqObjectNode.put("cmd", "SET_DEVICE_LEVEL");
cloudReqObjectNode.put("paras", paras);
cloudReqObjectNode.put("hasMore", 0);
cloudReqObjectNode.put("mid", 2016);
return cloudReqObjectNode;
}
private static byte[] getArray() {
byte[] input = new byte[9];
input[0] = 0x01;
input[1] = 0x27;
input[2] = 0x02;
input[3] = 0x12;
input[4] = 0x01;
input[5] = 0x31;
input[6] = 0x44;
input[7] = 0x55;
input[8] = 0x01;
/*cloudReqObjectNode.put("cmdid", (byte)0x01);
cloudReqObjectNode.put("pid", (byte) 0xAA);
cloudReqObjectNode.put("tid", (byte) 0x01);
cloudReqObjectNode.put("eventcnt", (byte) 0x01);
cloudReqObjectNode.put("verno", (byte) 0x01);
cloudReqObjectNode.put("eventstate", (byte) 0xE0);*/
return input;
}
/*
* 初始化:设备对平台的响应码流
*/
private static byte[] initDeviceRspByte() {
/*
* 测试用例:有命令短mid 设备应答消息:AA7201000107E0
*/
byte[] byteDeviceRsp = new byte[12];
byteDeviceRsp[0] = (byte) 0xAA;
byteDeviceRsp[1] = (byte) 0x72;
byteDeviceRsp[2] = (byte) 0x01;
byteDeviceRsp[3] = (byte) 0x00;
byteDeviceRsp[4] = (byte) 0x01;
byteDeviceRsp[5] = (byte) 0x07;
byteDeviceRsp[6] = (byte) 0xE0;
return byteDeviceRsp;
}
/*
* 初始化:平台对设备的应答数据
*/
private static ObjectNode initCloudRspObjectNode(byte[] device2CloudByte) {
ObjectMapper mapper = new ObjectMapper();
ObjectNode cloudRspObjectNode = mapper.createObjectNode();
cloudRspObjectNode.put("identifier", "123");
cloudRspObjectNode.put("msgType", "cloudRsp");
// 设备上报的码流
cloudRspObjectNode.put("request", device2CloudByte);
cloudRspObjectNode.put("errcode", 0);
cloudRspObjectNode.put("hasMore", 0);
return cloudRspObjectNode;
}
}