From 437503927cdc7100fd31db5e9313d9f61bd24874 Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Mon, 27 May 2019 07:56:50 -0700 Subject: Implement new Mictrack protocol --- setup/default.xml | 1 + .../org/traccar/protocol/MictrackProtocol.java | 39 ++++++ .../traccar/protocol/MictrackProtocolDecoder.java | 154 +++++++++++++++++++++ .../protocol/MictrackProtocolDecoderTest.java | 37 +++++ 4 files changed, 231 insertions(+) create mode 100644 src/main/java/org/traccar/protocol/MictrackProtocol.java create mode 100644 src/main/java/org/traccar/protocol/MictrackProtocolDecoder.java create mode 100644 src/test/java/org/traccar/protocol/MictrackProtocolDecoderTest.java diff --git a/setup/default.xml b/setup/default.xml index 7aee416e5..f2063c5ed 100644 --- a/setup/default.xml +++ b/setup/default.xml @@ -267,5 +267,6 @@ 5188 5189 5190 + 5191 diff --git a/src/main/java/org/traccar/protocol/MictrackProtocol.java b/src/main/java/org/traccar/protocol/MictrackProtocol.java new file mode 100644 index 000000000..0960d6bc8 --- /dev/null +++ b/src/main/java/org/traccar/protocol/MictrackProtocol.java @@ -0,0 +1,39 @@ +/* + * Copyright 2019 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.LineBasedFrameDecoder; +import io.netty.handler.codec.string.StringDecoder; +import io.netty.handler.codec.string.StringEncoder; +import org.traccar.BaseProtocol; +import org.traccar.PipelineBuilder; +import org.traccar.TrackerServer; + +public class MictrackProtocol extends BaseProtocol { + + public MictrackProtocol() { + addServer(new TrackerServer(false, getName()) { + @Override + protected void addProtocolHandlers(PipelineBuilder pipeline) { + pipeline.addLast(new LineBasedFrameDecoder(1024)); + pipeline.addLast(new StringEncoder()); + pipeline.addLast(new StringDecoder()); + pipeline.addLast(new Ardi01ProtocolDecoder(MictrackProtocol.this)); + } + }); + } + +} diff --git a/src/main/java/org/traccar/protocol/MictrackProtocolDecoder.java b/src/main/java/org/traccar/protocol/MictrackProtocolDecoder.java new file mode 100644 index 000000000..c11c97df3 --- /dev/null +++ b/src/main/java/org/traccar/protocol/MictrackProtocolDecoder.java @@ -0,0 +1,154 @@ +/* + * Copyright 2019 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.channel.Channel; +import org.traccar.BaseProtocolDecoder; +import org.traccar.DeviceSession; +import org.traccar.Protocol; +import org.traccar.helper.UnitsConverter; +import org.traccar.model.CellTower; +import org.traccar.model.Network; +import org.traccar.model.Position; +import org.traccar.model.WifiAccessPoint; + +import java.net.SocketAddress; +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.Date; + +public class MictrackProtocolDecoder extends BaseProtocolDecoder { + + public MictrackProtocolDecoder(Protocol protocol) { + super(protocol); + } + + private Date decodeTime(String data) throws ParseException { + return new SimpleDateFormat("yyMMddHHmmss").parse(data); + } + + private void decodeLocation(Position position, String data) throws ParseException { + int index = 0; + String[] values = data.split("\\+"); + + position.set(Position.KEY_SATELLITES, Integer.parseInt(values[index++])); + + position.setValid(true); + position.setTime(decodeTime(values[index++])); + position.setLatitude(Double.parseDouble(values[index++])); + position.setLongitude(Double.parseDouble(values[index++])); + position.setSpeed(UnitsConverter.knotsFromMps(Double.parseDouble(values[index++]))); + position.setCourse(Integer.parseInt(values[index++])); + + position.set(Position.KEY_EVENT, Integer.parseInt(values[index++])); + position.set(Position.KEY_BATTERY, Integer.parseInt(values[index++]) * 0.001); + } + + private void decodeCell(Network network, String data) { + String[] values = data.split(","); + int length = values.length % 5 == 0 ? 5 : 4; + for (int i = 0; i < values.length / length; i++) { + int mnc = Integer.parseInt(values[i * length]); + int lac = Integer.parseInt(values[i * length + 1]); + int cid = Integer.parseInt(values[i * length + 2]); + int mcc = Integer.parseInt(values[i * length + 3]); + network.addCellTower(CellTower.from(mcc, mnc, lac, cid)); + } + } + + private void decodeWifi(Network network, String data) { + String[] values = data.split(","); + for (int i = 0; i < values.length / 2; i++) { + network.addWifiAccessPoint(WifiAccessPoint.from(values[i * 2], Integer.parseInt(values[i * 2 + 1]))); + } + } + + private void decodeNetwork(Position position, String data, boolean hasWifi, boolean hasCell) throws ParseException { + int index = 0; + String[] values = data.split("\\+"); + + getLastLocation(position, decodeTime(values[index++])); + + Network network = new Network(); + + if (hasWifi) { + decodeWifi(network, values[index++]); + } + + if (hasCell) { + decodeCell(network, values[index++]); + } + + position.setNetwork(network); + + position.set(Position.KEY_EVENT, Integer.parseInt(values[index++])); + position.set(Position.KEY_BATTERY, Integer.parseInt(values[index++]) * 0.001); + } + + private void decodeStatus(Position position, String data) throws ParseException { + int index = 0; + String[] values = data.split("\\+"); + + position.set(Position.KEY_SATELLITES, Integer.parseInt(values[index++])); + + getLastLocation(position, decodeTime(values[index++])); + + index += 4; // fix values + + position.set(Position.KEY_EVENT, Integer.parseInt(values[index++])); + position.set(Position.KEY_BATTERY, Integer.parseInt(values[index++]) * 0.001); + } + + @Override + protected Object decode( + Channel channel, SocketAddress remoteAddress, Object msg) throws Exception { + + String[] fragments = ((String) msg).split(";"); + + DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, fragments[2]); + if (deviceSession == null) { + return null; + } + + Position position = new Position(getProtocolName()); + position.setDeviceId(deviceSession.getDeviceId()); + + switch (fragments[3]) { + case "R0": + decodeLocation(position, fragments[4]); + break; + case "R1": + decodeNetwork(position, fragments[4], true, false); + break; + case "R2": + case "R3": + decodeNetwork(position, fragments[4], false, true); + break; + case "R12": + case "R13": + decodeNetwork(position, fragments[4], true, true); + break; + case "RH": + decodeStatus(position, fragments[4]); + break; + default: + return null; + } + + return position; + } + +} diff --git a/src/test/java/org/traccar/protocol/MictrackProtocolDecoderTest.java b/src/test/java/org/traccar/protocol/MictrackProtocolDecoderTest.java new file mode 100644 index 000000000..d388d935b --- /dev/null +++ b/src/test/java/org/traccar/protocol/MictrackProtocolDecoderTest.java @@ -0,0 +1,37 @@ +package org.traccar.protocol; + +import org.junit.Test; +import org.traccar.ProtocolTest; + +public class MictrackProtocolDecoderTest extends ProtocolTest { + + @Test + public void testDecode() throws Exception { + + MictrackProtocolDecoder decoder = new MictrackProtocolDecoder(null); + + verifyPosition(decoder, text( + "MT;6;866425031361423;R0;10+190109091803+22.63827+114.02922+2.14+69+2+3744+113"), + position("2019-01-09 17:18:03.000", true, 22.63827, 114.02922)); + + verifyAttributes(decoder, text( + "MT;6;866425031377981;R1;190108024848+6a:db:54:5a:79:6d,-91,00:9a:cd:a2:e6:21,-94+3+3831+0")); + + verifyAttributes(decoder, text( + "MT;1;866425031379169;R2;181129081017+0,21681,20616,460+4+3976+0")); + + verifyAttributes(decoder, text( + "MT;1;866425031379169;R3;181129081017+0,1030,168267587,460,406+4+3976+0")); + + verifyAttributes(decoder, text( + "MT;6;866425031377981;R12;190108024848+6a:db:54:5a:79:6d,-91,00:9a:cd:a2:e6:21,-94+0,21681,20616,460+3+3831+0")); + + verifyAttributes(decoder, text( + "MT;6;866425031377981;R13;190108024848+6a:db:54:5a:79:6d,-91,00:9a:cd:a2:e6:21,-94+0,1030,168267587,460,406+3+3831+0")); + + verifyAttributes(decoder, text( + "MT;5;866425031379169;RH;5+190116112648+0+0+0+0+11+3954+1")); + + } + +} -- cgit v1.2.3