diff options
-rw-r--r-- | setup/default.xml | 1 | ||||
-rw-r--r-- | src/org/traccar/protocol/AlematicsFrameDecoder.java | 50 | ||||
-rw-r--r-- | src/org/traccar/protocol/AlematicsProtocol.java | 46 | ||||
-rw-r--r-- | src/org/traccar/protocol/AlematicsProtocolDecoder.java | 146 | ||||
-rw-r--r-- | test/org/traccar/protocol/AlematicsProtocolDecoderTest.java | 36 |
5 files changed, 279 insertions, 0 deletions
diff --git a/setup/default.xml b/setup/default.xml index 146cba80b..bdcd9227d 100644 --- a/setup/default.xml +++ b/setup/default.xml @@ -570,5 +570,6 @@ <entry key='dmt.port'>5137</entry> <entry key='xt2400.port'>5138</entry> <entry key='dmthttp.port'>5139</entry> + <entry key='alematics.port'>5140</entry> </properties> diff --git a/src/org/traccar/protocol/AlematicsFrameDecoder.java b/src/org/traccar/protocol/AlematicsFrameDecoder.java new file mode 100644 index 000000000..b8b3e3403 --- /dev/null +++ b/src/org/traccar/protocol/AlematicsFrameDecoder.java @@ -0,0 +1,50 @@ +/* + * Copyright 2017 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 org.jboss.netty.buffer.ChannelBuffer; +import org.jboss.netty.channel.Channel; +import org.jboss.netty.channel.ChannelHandlerContext; +import org.jboss.netty.handler.codec.frame.LineBasedFrameDecoder; + +public class AlematicsFrameDecoder extends LineBasedFrameDecoder { + + private static final int MESSAGE_MINIMUM_LENGTH = 2; + + public AlematicsFrameDecoder(int maxFrameLength) { + super(maxFrameLength); + } + + // example of heartbeat: FA F8 00 07 00 03 15 AD 4E 78 3A D2 + + @Override + protected Object decode(ChannelHandlerContext ctx, Channel channel, ChannelBuffer buf) throws Exception { + + if (buf.readableBytes() < MESSAGE_MINIMUM_LENGTH) { + return null; + } + + if (buf.getUnsignedShort(buf.readerIndex()) == 0xFAF8) { + ChannelBuffer heartbeat = buf.readBytes(12); + if (channel != null) { + channel.write(heartbeat); + } + } + + return super.decode(ctx, channel, buf); + } + +} diff --git a/src/org/traccar/protocol/AlematicsProtocol.java b/src/org/traccar/protocol/AlematicsProtocol.java new file mode 100644 index 000000000..241ce8bf9 --- /dev/null +++ b/src/org/traccar/protocol/AlematicsProtocol.java @@ -0,0 +1,46 @@ +/* + * Copyright 2017 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 org.jboss.netty.bootstrap.ServerBootstrap; +import org.jboss.netty.channel.ChannelPipeline; +import org.jboss.netty.handler.codec.string.StringDecoder; +import org.jboss.netty.handler.codec.string.StringEncoder; +import org.traccar.BaseProtocol; +import org.traccar.TrackerServer; + +import java.util.List; + +public class AlematicsProtocol extends BaseProtocol { + + public AlematicsProtocol() { + super("alematics"); + } + + @Override + public void initTrackerServers(List<TrackerServer> serverList) { + serverList.add(new TrackerServer(new ServerBootstrap(), getName()) { + @Override + protected void addSpecificHandlers(ChannelPipeline pipeline) { + pipeline.addLast("frameDecoder", new AlematicsFrameDecoder(1024)); + pipeline.addLast("stringEncoder", new StringEncoder()); + pipeline.addLast("stringDecoder", new StringDecoder()); + pipeline.addLast("objectDecoder", new AlematicsProtocolDecoder(AlematicsProtocol.this)); + } + }); + } + +} diff --git a/src/org/traccar/protocol/AlematicsProtocolDecoder.java b/src/org/traccar/protocol/AlematicsProtocolDecoder.java new file mode 100644 index 000000000..47d31b7a7 --- /dev/null +++ b/src/org/traccar/protocol/AlematicsProtocolDecoder.java @@ -0,0 +1,146 @@ +/* + * Copyright 2017 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 org.jboss.netty.channel.Channel; +import org.traccar.BaseProtocolDecoder; +import org.traccar.DeviceSession; +import org.traccar.helper.BitUtil; +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.util.regex.Pattern; + +public class AlematicsProtocolDecoder extends BaseProtocolDecoder { + + public AlematicsProtocolDecoder(AlematicsProtocol protocol) { + super(protocol); + } + + private static final Pattern PATTERN = new PatternBuilder() + .text("$T,") + .number("(d+),") // type + .number("(d+),") // index + .number("(d+),") // id + .number("(dddd)(dd)(dd)") // gps date + .number("(dd)(dd)(dd),") // gps time + .number("(dddd)(dd)(dd)") // device date + .number("(dd)(dd)(dd),") // device time + .number("(-?d+.d+),") // latitude + .number("(-?d+.d+),") // longitude + .number("(d+),") // speed + .number("(d+),") // course + .number("(-?d+),") // altitude + .number("(d+.d),") // hdop + .number("(d+),") // satellites + .number("(d+),") // input + .number("(d+),") // output + .number("(d+.d+),") // adc + .number("(d+.d+),") // power + .number("(d+),") // odometer + .number("(d+),") // extra mask + .expression("(.*)") // extra data + .compile(); + + private void decodeExtras(Position position, Parser parser) { + + int mask = parser.nextInt(); + String[] data = parser.next().split(","); + + int index = 0; + + if (BitUtil.check(mask, 0)) { + index++; // pulse counter 3 + } + + if (BitUtil.check(mask, 1)) { + position.set(Position.KEY_POWER, Integer.parseInt(data[index++])); + } + + if (BitUtil.check(mask, 2)) { + position.set(Position.KEY_BATTERY, Integer.parseInt(data[index++])); + } + + if (BitUtil.check(mask, 3)) { + position.set(Position.KEY_OBD_SPEED, Integer.parseInt(data[index++])); + } + + if (BitUtil.check(mask, 4)) { + position.set(Position.KEY_RPM, Integer.parseInt(data[index++])); + } + + if (BitUtil.check(mask, 5)) { + position.set(Position.KEY_RSSI, Integer.parseInt(data[index++])); + } + + if (BitUtil.check(mask, 6)) { + index++; // pulse counter 2 + } + + if (BitUtil.check(mask, 7)) { + index++; // magnetic rotation sensor rpm + } + + } + + @Override + protected Object decode( + Channel channel, SocketAddress remoteAddress, Object msg) throws Exception { + + Parser parser = new Parser(PATTERN, (String) msg); + if (!parser.matches()) { + return null; + } + + Position position = new Position(); + position.setProtocol(getProtocolName()); + + position.set(Position.KEY_TYPE, parser.nextInt()); + position.set(Position.KEY_INDEX, parser.nextInt()); + + DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, parser.next()); + if (deviceSession == null) { + return null; + } + position.setDeviceId(deviceSession.getDeviceId()); + + position.setFixTime(parser.nextDateTime()); + position.setDeviceTime(parser.nextDateTime()); + + position.setValid(true); + position.setLatitude(parser.nextDouble(0)); + position.setLongitude(parser.nextDouble(0)); + position.setSpeed(UnitsConverter.knotsFromKph(parser.nextInt(0))); + position.setCourse(parser.nextInt(0)); + position.setAltitude(parser.nextInt(0)); + + position.set(Position.KEY_HDOP, parser.nextDouble()); + position.set(Position.KEY_SATELLITES, parser.nextInt()); + position.set(Position.KEY_INPUT, parser.nextInt()); + position.set(Position.KEY_OUTPUT, parser.nextInt()); + position.set(Position.PREFIX_ADC + 1, parser.nextDouble()); + position.set(Position.KEY_POWER, parser.nextDouble()); + position.set(Position.KEY_ODOMETER, parser.nextInt()); + + decodeExtras(position, parser); + + return position; + } + +} diff --git a/test/org/traccar/protocol/AlematicsProtocolDecoderTest.java b/test/org/traccar/protocol/AlematicsProtocolDecoderTest.java new file mode 100644 index 000000000..448a48101 --- /dev/null +++ b/test/org/traccar/protocol/AlematicsProtocolDecoderTest.java @@ -0,0 +1,36 @@ +package org.traccar.protocol; + +import org.junit.Test; +import org.traccar.ProtocolTest; + +public class AlematicsProtocolDecoderTest extends ProtocolTest { + + @Test + public void testDecode() throws Exception { + + AlematicsProtocolDecoder decoder = new AlematicsProtocolDecoder(new AlematicsProtocol()); + + verifyPosition(decoder, text( + "$T,2,552,868259020159698,20170515060949,20170515060949,25.035277,121.561986,0,202,78,1.0,8,1,0,0.000,12.768,1629,38,12770,4109,9")); + + verifyPosition(decoder, text( + "$T,2,553,868259020159698,20170515061019,20170515061019,25.035295,121.561981,0,202,79,1.0,8,1,0,0.000,12.768,1629,38,12772,4109,8")); + + verifyPosition(decoder, text( + "$T,4,4,868259020159698,20170515061033,20170515061033,25.035303,121.561975,0,202,81,1.7,6,1,0,0.000,12.770,1629,0,$S,A1,1,,12345.67,88.4,301.5,,2593.25,12.4,89.2,,5999.44,789.572,2345.67,,10763,1024,5,")); + + verifyPosition(decoder, text( + "$T,2,554,868259020159698,20170515061049,20170515061049,25.035309,121.561976,0,202,82,1.1,7,1,0,0.000,12.768,1629,38,12770,4109,9")); + + verifyPosition(decoder, text( + "$T,4,5,868259020159698,20170515061058,20170515061058,25.035308,121.561976,0,202,82,1.2,7,1,0,0.000,12.772,1629,0,$S,A1,1,,12345.67,88.4,301.5,,2593.25,12.4,89.2,,5999.44,789.572,2345.67,,10763,1024,5,")); + + verifyPosition(decoder, text( + "$T,50,592,868259020159698,20170515062915,20170515062915,25.035005,121.561555,0,31,89,3.7,5,1,0,0.000,12.752,1629,38,12752,4203,6")); + + verifyPosition(decoder, text( + "$T,50,594,868259020159698,20170515062928,20170515062928,25.035151,121.561671,0,31,93,1.8,5,0,0,0.000,12.752,1629,38,12756,4205,6")); + + } + +} |