Message81Cmd.java
3.15 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
package com.bsth.socket.protocol;
import com.bsth.util.ConvertUtil;
import org.joda.time.DateTime;
import java.nio.ByteBuffer;
import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Function;
import java.util.stream.Collectors;
/**
* @author Hill
* 参数设置
*/
public class Message81Cmd implements IMessageBody {
/**
* byte[6]
* 数据采集时间
*/
private long timestamp;
/**
* 参数总数
* n
*/
private byte paramNum;
/**
* 参数具体数据
*/
private Map<Byte, Object> params = new LinkedHashMap<>();
private static Map<Byte, Byte> sizes;
static {
AtomicInteger atomicInteger = new AtomicInteger(1);
List<Byte> byteList = Arrays.asList((byte) 2, (byte) 2, (byte) 2, (byte) 1, (byte) 0, (byte) 2, (byte) 5, (byte) 5, (byte) 1, (byte) 2, (byte) 2, (byte) 1, (byte) 1, (byte) 0, (byte) 2, (byte) 1);
sizes = byteList.stream().collect(Collectors.toMap(val -> (byte) atomicInteger.getAndIncrement(), val -> val));
}
public static void main(String[] args) {
System.out.println("aaa");
}
@Override
public void read(byte[] bytes) {
// TODO Auto-generated method stub
}
@Override
public byte[] write() {
// TODO Auto-generated method stub
int len = 0;
for (Map.Entry<Byte, Object> entry : params.entrySet()) {
Byte key = entry.getKey();
Byte size = sizes.get(entry.getKey());
if (size == 0) {
size = (Byte) params.get((byte) (key - 1));
if (size == null) {
throw new RuntimeException("参数设置中参数不符合协议规范");
}
}
len += size;
}
ByteBuffer buf = ByteBuffer.allocate(7 + paramNum + len);
buf.put(ConvertUtil.timestamp2bytes(timestamp));
buf.put(paramNum);
for (Map.Entry<Byte, Object> entry : params.entrySet()) {
generateParam(buf, entry.getKey(), entry.getValue());
}
return buf.array();
}
private void generateParam(ByteBuffer buf, Byte type, Object value) {
buf.put(type);
switch (type) {
case 0x01:
case 0x02:
case 0x03:
case 0x06:
case 0x0A:
case 0x0B:
case 0x0F:
buf.putShort((Short) value);
break;
case 0x04:
case 0x09:
case 0x0C:
case 0x0D:
case 0x10:
buf.put((Byte) value);
break;
case 0x05:
case 0x0E:
String val = (String) value;
buf.put(ConvertUtil.string2ascii(val, val.length()));
break;
case 0x07:
case 0x08:
buf.put(ConvertUtil.string2ascii((String) value, 5));
break;
default:
break;
}
}
public long getTimestamp() {
return timestamp;
}
@Override
public void setTimestamp(long timestamp) {
this.timestamp = timestamp;
}
public byte getParamNum() {
return paramNum;
}
public void setParamNum(byte paramNum) {
this.paramNum = paramNum;
}
public Map<Byte, Object> getParams() {
return params;
}
public void setParams(Map<Byte, Object> params) {
this.params = params;
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append(" 数据采集时间:").append(new DateTime(timestamp).toString("yyyy-MM-dd HH:mm:ss"))
.append(" 参数数量:").append(paramNum)
.append(" 参数命令IDS:").append(params.size());
return sb.toString();
}
}