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
|
/*
* Copyright 2020 Anton Tananaev (anton@traccar.org)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.traccar.protocol;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufUtil;
import io.netty.channel.Channel;
import io.netty.handler.codec.mqtt.MqttConnAckMessage;
import io.netty.handler.codec.mqtt.MqttConnectMessage;
import io.netty.handler.codec.mqtt.MqttConnectReturnCode;
import io.netty.handler.codec.mqtt.MqttMessageBuilders;
import io.netty.handler.codec.mqtt.MqttPublishMessage;
import org.traccar.BaseProtocolDecoder;
import org.traccar.DeviceSession;
import org.traccar.NetworkMessage;
import org.traccar.Protocol;
import org.traccar.model.Position;
import java.net.SocketAddress;
import java.nio.charset.StandardCharsets;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
public class IotmProtocolDecoder extends BaseProtocolDecoder {
public IotmProtocolDecoder(Protocol protocol) {
super(protocol);
}
private Object readValue(ByteBuf buf, int sensorType) {
switch (sensorType) {
case 0:
return false;
case 1:
return true;
case 3:
return 0;
case 4:
return buf.readUnsignedByte();
case 5:
return buf.readUnsignedShortLE();
case 6:
return buf.readUnsignedIntLE();
case 7:
case 11:
return buf.readLongLE();
case 8:
return buf.readByte();
case 9:
return buf.readShortLE();
case 10:
return buf.readIntLE();
case 12:
return buf.readFloatLE();
case 13:
return buf.readDoubleLE();
case 32:
return buf.readCharSequence(buf.readUnsignedByte(), StandardCharsets.US_ASCII).toString();
case 33:
return ByteBufUtil.hexDump(buf.readSlice(buf.readUnsignedByte()));
case 64:
return buf.readCharSequence(buf.readUnsignedShortLE(), StandardCharsets.US_ASCII).toString();
case 65:
return ByteBufUtil.hexDump(buf.readSlice(buf.readUnsignedShortLE()));
case 2:
default:
return null;
}
}
private String getKey(int sensorId) {
switch (sensorId) {
case 0x300C:
return Position.KEY_RPM;
case 0x4003:
return Position.KEY_ODOMETER;
default:
return null;
}
}
@Override
protected Object decode(
Channel channel, SocketAddress remoteAddress, Object msg) throws Exception {
if (msg instanceof MqttConnectMessage) {
MqttConnectMessage message = (MqttConnectMessage) msg;
DeviceSession deviceSession = getDeviceSession(
channel, remoteAddress, message.payload().clientIdentifier());
MqttConnectReturnCode returnCode = deviceSession != null
? MqttConnectReturnCode.CONNECTION_ACCEPTED
: MqttConnectReturnCode.CONNECTION_REFUSED_IDENTIFIER_REJECTED;
MqttConnAckMessage response = MqttMessageBuilders.connAck().returnCode(returnCode).build();
if (channel != null) {
channel.writeAndFlush(new NetworkMessage(response, remoteAddress));
}
} else if (msg instanceof MqttPublishMessage) {
DeviceSession deviceSession = getDeviceSession(channel, remoteAddress);
if (deviceSession == null) {
return null;
}
List<Position> positions = new LinkedList<>();
MqttPublishMessage message = (MqttPublishMessage) msg;
ByteBuf buf = message.payload();
buf.readUnsignedByte(); // structure version
while (buf.readableBytes() > 1) {
int type = buf.readUnsignedByte();
int length = buf.readUnsignedShortLE();
if (type == 1) {
ByteBuf record = buf.readSlice(length);
Position position = new Position(getProtocolName());
position.setDeviceId(deviceSession.getDeviceId());
position.setTime(new Date(record.readUnsignedIntLE()));
while (record.readableBytes() > 0) {
int sensorType = record.readUnsignedByte();
int sensorId = record.readUnsignedShortLE();
if (sensorType == 14) {
position.setValid(true);
position.setLatitude(record.readFloatLE());
position.setLongitude(record.readFloatLE());
position.setSpeed(record.readUnsignedShortLE());
position.set(Position.KEY_HDOP, record.readUnsignedByte());
position.set(Position.KEY_SATELLITES, record.readUnsignedByte());
position.setCourse(record.readUnsignedShortLE());
position.setAltitude(record.readShortLE());
} else {
String key = getKey(sensorId);
Object value = readValue(record, sensorType);
if (key != null && value != null) {
position.getAttributes().put(key, value);
}
}
}
positions.add(position);
} else {
buf.skipBytes(length);
}
}
buf.readUnsignedByte(); // checksum
return positions.isEmpty() ? null : positions;
}
return null;
}
}
|