diff options
author | Anton Tananaev <anton.tananaev@gmail.com> | 2018-08-20 17:32:19 +1200 |
---|---|---|
committer | Anton Tananaev <anton.tananaev@gmail.com> | 2018-08-20 17:32:19 +1200 |
commit | 5848419666fc6eb14c5b0178a9738f19f0be2b61 (patch) | |
tree | 891288be7b13d70cff4cb6f36db3f7aeaa9f5a7c /src | |
parent | 98fd364db65daf847b2b76819cd36602aaef8d25 (diff) | |
download | traccar-server-5848419666fc6eb14c5b0178a9738f19f0be2b61.tar.gz traccar-server-5848419666fc6eb14c5b0178a9738f19f0be2b61.tar.bz2 traccar-server-5848419666fc6eb14c5b0178a9738f19f0be2b61.zip |
Implement wristband tracker protocol
Diffstat (limited to 'src')
-rw-r--r-- | src/org/traccar/BaseProtocolDecoder.java | 9 | ||||
-rw-r--r-- | src/org/traccar/protocol/MeiligaoProtocolDecoder.java | 10 | ||||
-rw-r--r-- | src/org/traccar/protocol/WristbandProtocol.java | 42 | ||||
-rw-r--r-- | src/org/traccar/protocol/WristbandProtocolDecoder.java | 146 |
4 files changed, 197 insertions, 10 deletions
diff --git a/src/org/traccar/BaseProtocolDecoder.java b/src/org/traccar/BaseProtocolDecoder.java index d7ccb6460..d9ab60e3a 100644 --- a/src/org/traccar/BaseProtocolDecoder.java +++ b/src/org/traccar/BaseProtocolDecoder.java @@ -67,6 +67,15 @@ public abstract class BaseProtocolDecoder extends ExtendedObjectDecoder { return protocol.getName(); } + public String getServer(Channel channel) { + String server = Context.getConfig().getString(getProtocolName() + ".server"); + if (server == null && channel != null) { + InetSocketAddress address = (InetSocketAddress) channel.localAddress(); + server = address.getAddress().getHostAddress() + ":" + address.getPort(); + } + return server; + } + protected double convertSpeed(double value, String defaultUnits) { switch (Context.getConfig().getString(getProtocolName() + ".speed", defaultUnits)) { case "kmh": diff --git a/src/org/traccar/protocol/MeiligaoProtocolDecoder.java b/src/org/traccar/protocol/MeiligaoProtocolDecoder.java index 30905076e..9fbd053eb 100644 --- a/src/org/traccar/protocol/MeiligaoProtocolDecoder.java +++ b/src/org/traccar/protocol/MeiligaoProtocolDecoder.java @@ -29,7 +29,6 @@ import org.traccar.helper.Parser; import org.traccar.helper.PatternBuilder; import org.traccar.model.Position; -import java.net.InetSocketAddress; import java.net.SocketAddress; import java.nio.charset.StandardCharsets; import java.util.HashMap; @@ -206,15 +205,6 @@ public class MeiligaoProtocolDecoder extends BaseProtocolDecoder { } } - private String getServer(Channel channel) { - String server = Context.getConfig().getString(getProtocolName() + ".server"); - if (server == null && channel != null) { - InetSocketAddress address = (InetSocketAddress) channel.localAddress(); - server = address.getAddress().getHostAddress() + ":" + address.getPort(); - } - return server; - } - private String decodeAlarm(short value) { switch (value) { case 0x01: diff --git a/src/org/traccar/protocol/WristbandProtocol.java b/src/org/traccar/protocol/WristbandProtocol.java new file mode 100644 index 000000000..02db38f4f --- /dev/null +++ b/src/org/traccar/protocol/WristbandProtocol.java @@ -0,0 +1,42 @@ +/* + * Copyright 2018 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.handler.codec.LengthFieldBasedFrameDecoder; +import org.traccar.BaseProtocol; +import org.traccar.PipelineBuilder; +import org.traccar.TrackerServer; + +import java.util.List; + +public class WristbandProtocol extends BaseProtocol { + + public WristbandProtocol() { + super("wristband"); + } + + @Override + public void initTrackerServers(List<TrackerServer> serverList) { + serverList.add(new TrackerServer(false, getName()) { + @Override + protected void addProtocolHandlers(PipelineBuilder pipeline) { + pipeline.addLast("frameDecoder", new LengthFieldBasedFrameDecoder(1024, 3, 2, 3, 0)); + pipeline.addLast("objectDecoder", new WristbandProtocolDecoder(WristbandProtocol.this)); + } + }); + } + +} diff --git a/src/org/traccar/protocol/WristbandProtocolDecoder.java b/src/org/traccar/protocol/WristbandProtocolDecoder.java new file mode 100644 index 000000000..f62836765 --- /dev/null +++ b/src/org/traccar/protocol/WristbandProtocolDecoder.java @@ -0,0 +1,146 @@ +/* + * Copyright 2018 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.Unpooled; +import io.netty.channel.Channel; +import org.traccar.BaseProtocolDecoder; +import org.traccar.DeviceSession; +import org.traccar.NetworkMessage; +import org.traccar.helper.Parser; +import org.traccar.helper.PatternBuilder; +import org.traccar.helper.UnitsConverter; +import org.traccar.model.Position; + +import java.net.SocketAddress; +import java.nio.charset.StandardCharsets; +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.Date; +import java.util.LinkedList; +import java.util.List; +import java.util.regex.Pattern; + +public class WristbandProtocolDecoder extends BaseProtocolDecoder { + + public WristbandProtocolDecoder(WristbandProtocol protocol) { + super(protocol); + } + + private void sendResponse( + Channel channel, String imei, String version, int type, String data) { + + if (channel != null) { + String sentence = String.format("YX%s|%s|0|{F%d#%s}\r\n", imei, version, type, data); + ByteBuf response = Unpooled.buffer(); + response.writeBytes(new byte[]{0x00, 0x01, 0x02}); + response.writeShort(sentence.length()); + response.writeCharSequence(sentence, StandardCharsets.US_ASCII); + response.writeBytes(new byte[]{(byte) 0xFF, (byte) 0xFE, (byte) 0xFC}); + channel.writeAndFlush(new NetworkMessage(response, channel.remoteAddress())); + } + } + + private static final Pattern PATTERN = new PatternBuilder() + .expression("..") // header + .number("(d+)|") // imei + .number("(vd+.d+)|") // version + .number("d+|") // model + .text("{") + .number("F(d+)") // function + .groupBegin() + .text("#") + .expression("(.*)") // data + .groupEnd("?") + .text("}") + .text("\r\n") + .compile(); + + private Position decodePosition(DeviceSession deviceSession, String sentence) throws ParseException { + + Position position = new Position(getProtocolName()); + position.setDeviceId(deviceSession.getDeviceId()); + + String[] values = sentence.split(","); + + position.setValid(true); + position.setLongitude(Double.parseDouble(values[0])); + position.setLatitude(Double.parseDouble(values[1])); + position.setTime(new SimpleDateFormat("yyyyMMddHHmmss").parse(values[2])); + position.setSpeed(UnitsConverter.knotsFromKph(Double.parseDouble(values[3]))); + + return position; + } + + private List<Position> decodeMessage( + Channel channel, SocketAddress remoteAddress, String sentence) throws ParseException { + + Parser parser = new Parser(PATTERN, sentence); + if (!parser.matches()) { + return null; + } + + String imei = parser.next(); + DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, imei); + if (deviceSession == null) { + return null; + } + + String version = parser.next(); + int type = parser.nextInt(); + + List<Position> positions = new LinkedList<>(); + + switch (type) { + case 90: + sendResponse(channel, imei, version, type, getServer(channel)); + break; + case 91: + String time = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss").format(new Date()); + sendResponse(channel, imei, version, type, time + "|" + getServer(channel)); + break; + case 1: + sendResponse(channel, imei, version, type, "0"); + break; + case 2: + for (String fragment : parser.next().split("\\|")) { + positions.add(decodePosition(deviceSession, fragment)); + } + break; + default: + break; + } + + return positions.isEmpty() ? null : positions; + } + + @Override + protected Object decode( + Channel channel, SocketAddress remoteAddress, Object msg) throws Exception { + + ByteBuf buf = (ByteBuf) msg; + buf.skipBytes(3); // header + buf.readUnsignedShort(); // length + + String sentence = buf.toString(buf.readerIndex(), buf.readableBytes() - 3, StandardCharsets.US_ASCII); + + buf.skipBytes(3); // footer + + return decodeMessage(channel, remoteAddress, sentence); + } + +} |