From 1fb4b23ee69f10f04c69ba2ca1d528737259a5b1 Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Tue, 16 May 2017 07:46:05 +1200 Subject: Implement Alematics protocol --- .../traccar/protocol/AlematicsFrameDecoder.java | 50 +++++++ src/org/traccar/protocol/AlematicsProtocol.java | 46 +++++++ .../traccar/protocol/AlematicsProtocolDecoder.java | 146 +++++++++++++++++++++ 3 files changed, 242 insertions(+) create mode 100644 src/org/traccar/protocol/AlematicsFrameDecoder.java create mode 100644 src/org/traccar/protocol/AlematicsProtocol.java create mode 100644 src/org/traccar/protocol/AlematicsProtocolDecoder.java (limited to 'src/org') 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 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; + } + +} -- cgit v1.2.3