diff options
author | Anton Tananaev <anton.tananaev@gmail.com> | 2016-07-07 00:11:19 +1200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2016-07-07 00:11:19 +1200 |
commit | 81cac93e54e336b78c80e3a099b72d6bb8f3e1b4 (patch) | |
tree | 39e2e1730d8711c7512b4e870fcd380e1b62a822 /src/org/traccar | |
parent | 21a8fb81a0aa88a7bc1cb6804da034ecc9a79bcd (diff) | |
parent | f8f21ce131556ca5c1ea44ede496ace9d810c844 (diff) | |
download | traccar-server-81cac93e54e336b78c80e3a099b72d6bb8f3e1b4.tar.gz traccar-server-81cac93e54e336b78c80e3a099b72d6bb8f3e1b4.tar.bz2 traccar-server-81cac93e54e336b78c80e3a099b72d6bb8f3e1b4.zip |
Merge pull request #2081 from Abyss777/master
Granit protocol implementation
Diffstat (limited to 'src/org/traccar')
-rw-r--r-- | src/org/traccar/model/Command.java | 1 | ||||
-rw-r--r-- | src/org/traccar/protocol/GranitProtocol.java | 52 | ||||
-rw-r--r-- | src/org/traccar/protocol/GranitProtocolDecoder.java | 212 | ||||
-rw-r--r-- | src/org/traccar/protocol/GranitProtocolEncoder.java | 55 |
4 files changed, 320 insertions, 0 deletions
diff --git a/src/org/traccar/model/Command.java b/src/org/traccar/model/Command.java index 0a1a62216..d15eb9e70 100644 --- a/src/org/traccar/model/Command.java +++ b/src/org/traccar/model/Command.java @@ -21,6 +21,7 @@ import com.fasterxml.jackson.annotation.JsonIgnoreProperties; public class Command extends Message { public static final String TYPE_CUSTOM = "custom"; + public static final String TYPE_IDENTIFICATION = "deviceIdentification"; public static final String TYPE_POSITION_SINGLE = "positionSingle"; public static final String TYPE_POSITION_PERIODIC = "positionPeriodic"; public static final String TYPE_POSITION_STOP = "positionStop"; diff --git a/src/org/traccar/protocol/GranitProtocol.java b/src/org/traccar/protocol/GranitProtocol.java new file mode 100644 index 000000000..a33c43d20 --- /dev/null +++ b/src/org/traccar/protocol/GranitProtocol.java @@ -0,0 +1,52 @@ +/* + * Copyright 2016 Anton Tananaev (anton.tananaev@gmail.com) + * + * 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.frame.LineBasedFrameDecoder; +import org.traccar.BaseProtocol; +import org.traccar.TrackerServer; +import org.traccar.model.Command; + +import java.nio.ByteOrder; +import java.util.List; + +public class GranitProtocol extends BaseProtocol { + + public GranitProtocol() { + super("granit"); + setSupportedCommands( + Command.TYPE_IDENTIFICATION, + Command.TYPE_REBOOT_DEVICE, + Command.TYPE_POSITION_SINGLE); + } + + @Override + public void initTrackerServers(List<TrackerServer> serverList) { + TrackerServer server = new TrackerServer(new ServerBootstrap(), this.getName()) { + @Override + protected void addSpecificHandlers(ChannelPipeline pipeline) { + pipeline.addLast("frameDecoder", new LineBasedFrameDecoder(1024)); + pipeline.addLast("objectEncoder", new GranitProtocolEncoder()); + pipeline.addLast("objectDecoder", new GranitProtocolDecoder(GranitProtocol.this)); + } + }; + server.setEndianness(ByteOrder.LITTLE_ENDIAN); + serverList.add(server); + } + +} diff --git a/src/org/traccar/protocol/GranitProtocolDecoder.java b/src/org/traccar/protocol/GranitProtocolDecoder.java new file mode 100644 index 000000000..b92bc72e2 --- /dev/null +++ b/src/org/traccar/protocol/GranitProtocolDecoder.java @@ -0,0 +1,212 @@ +/* + * Copyright 2016 Anton Tananaev (anton.tananaev@gmail.com) + * + * 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.buffer.ChannelBuffers; +import org.jboss.netty.channel.Channel; +import org.traccar.BaseProtocolDecoder; +import org.traccar.helper.BitUtil; +import org.traccar.helper.Checksum; +import org.traccar.model.Position; + +import java.net.SocketAddress; +import java.nio.ByteOrder; +import java.nio.charset.StandardCharsets; +import java.util.ArrayList; +import java.util.Date; +import java.util.List; + +public class GranitProtocolDecoder extends BaseProtocolDecoder { + + private static final int HEADER_LENGTH = 6; + + public GranitProtocolDecoder(GranitProtocol protocol) { + super(protocol); + } + + public static void appendChecksum(ChannelBuffer buffer, int length) { + buffer.writeByte('*'); + int checksum = Checksum.xor(buffer.toByteBuffer(0, length)) & 0xFF; + String checksumString = String.format("%02X", checksum); + buffer.writeBytes(checksumString.getBytes(StandardCharsets.US_ASCII)); + buffer.writeByte('\r'); buffer.writeByte('\n'); + } + + private static void sendResponseCurrent(Channel channel, int deviceId, long time) { + ChannelBuffer response = ChannelBuffers.dynamicBuffer(ByteOrder.LITTLE_ENDIAN, 0); + response.writeBytes("BB+UGRC~".getBytes(StandardCharsets.US_ASCII)); + response.writeShort(6); //binary length + response.writeInt((int) time); + response.writeShort(deviceId); + appendChecksum(response, 16); + channel.write(response); + } + + private static void sendResponseArchive(Channel channel, int deviceId, int packNum) { + ChannelBuffer response = ChannelBuffers.dynamicBuffer(ByteOrder.LITTLE_ENDIAN, 0); + response.writeBytes("BB+ARCF~".getBytes(StandardCharsets.US_ASCII)); + response.writeShort(4); //binary length + response.writeShort(packNum); + response.writeShort(deviceId); + appendChecksum(response, 14); + channel.write(response); + } + + private static void decodeStructure(ChannelBuffer buf, Position position) { + short flags = buf.readUnsignedByte(); + position.setValid(BitUtil.check(flags, 7)); + position.set(Position.KEY_ALARM, BitUtil.check(flags, 1)); + + short satDel = buf.readUnsignedByte(); + position.set(Position.KEY_SATELLITES, BitUtil.from(satDel, 4)); + + int pdop = BitUtil.to(satDel, 4); + position.set("pdop", pdop); + + int lonDegrees = buf.readUnsignedByte(); + int latDegrees = buf.readUnsignedByte(); + int lonMinutes = buf.readUnsignedShort(); + int latMinutes = buf.readUnsignedShort(); + double latitude = latDegrees + latMinutes / 60000.0; + double longitude = lonDegrees + lonMinutes / 60000.0; + if (!BitUtil.check(flags, 4)) { + latitude = -latDegrees; + } + if (!BitUtil.check(flags, 5)) { + longitude = -longitude; + } + position.setLongitude(longitude); + position.setLatitude(latitude); + + position.setSpeed(buf.readUnsignedByte()); + + int course = buf.readUnsignedByte(); + if (BitUtil.check(flags, 6)) { + course = course | 0x100; + } + position.setCourse(course); + + position.set(Position.KEY_DISTANCE, buf.readShort()); + + int analogIn1 = buf.readUnsignedByte(); + int analogIn2 = buf.readUnsignedByte(); + int analogIn3 = buf.readUnsignedByte(); + int analogIn4 = buf.readUnsignedByte(); + + int analogInHi = buf.readUnsignedByte(); + + analogIn1 = analogInHi << 8 & 0x300 | analogIn1; + analogIn2 = analogInHi << 6 & 0x300 | analogIn2; + analogIn3 = analogInHi << 4 & 0x300 | analogIn3; + analogIn4 = analogInHi << 2 & 0x300 | analogIn4; + + position.set(Position.PREFIX_ADC + 1, analogIn1); + position.set(Position.PREFIX_ADC + 2, analogIn2); + position.set(Position.PREFIX_ADC + 3, analogIn3); + position.set(Position.PREFIX_ADC + 4, analogIn4); + + position.setAltitude(buf.readUnsignedByte() * 10); + + short diOut = buf.readUnsignedByte(); + for (int i = 0; i < 8; i++) { + position.set(Position.PREFIX_IO + (i + 1), BitUtil.check(diOut, i)); + } + buf.skipBytes(1); //StatMess + } + + @Override + protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception { + + ChannelBuffer buf = (ChannelBuffer) msg; + String bufString = buf.toString(StandardCharsets.US_ASCII); + if (hasDeviceId() && bufString.contains("OK") || bufString.startsWith("ERROR") + || bufString.startsWith("+PR") || bufString.startsWith("+IDNT") + || bufString.startsWith("+BBMD")) { + Position position = new Position(); + position.setProtocol(getProtocolName()); + position.setDeviceId(getDeviceId()); + + position.setTime(new Date()); + getLastLocation(position, new Date()); + position.setValid(false); + position.set(Position.KEY_RESULT, bufString); + + return position; + } + + if (buf.readableBytes() < HEADER_LENGTH) { + return null; + } + buf.skipBytes(HEADER_LENGTH); + + if (bufString.startsWith("+RRCB~")) { + buf.skipBytes(2); //binary length 26 + int deviceId = buf.readUnsignedShort(); + if (!identify(String.valueOf(deviceId), channel, remoteAddress)) { + return null; + } + long unixTime = buf.readUnsignedInt(); + if (channel != null) { + sendResponseCurrent(channel, deviceId, unixTime); + } + Position position = new Position(); + position.setProtocol(getProtocolName()); + position.setDeviceId(getDeviceId()); + + position.setTime(new Date(unixTime * 1000)); + + decodeStructure(buf, position); + return position; + + } else if (bufString.startsWith("+DDAT~")) { + buf.skipBytes(2); //binary length + int deviceId = buf.readUnsignedShort(); + if (!identify(String.valueOf(deviceId), channel, remoteAddress)) { + return null; + } + byte format = buf.readByte(); + if (format != 4) { + return null; + } + byte nblocks = buf.readByte(); + int packNum = buf.readUnsignedShort(); + if (channel != null) { + sendResponseArchive(channel, deviceId, packNum); + } + List<Position> positions = new ArrayList<>(); + while (nblocks > 0) { + nblocks--; + long unixTime = buf.readUnsignedInt(); + for (int i = 0; i < 6; i++) { + if (buf.getUnsignedByte(buf.readerIndex()) != 0xFE) { + Position position = new Position(); + position.setProtocol(getProtocolName()); + position.setDeviceId(getDeviceId()); + position.setTime(new Date(unixTime * 1000)); + decodeStructure(buf, position); + positions.add(position); + } else { + buf.skipBytes(20); // skip filled 0xFE structure + } + } + buf.skipBytes(2); // increment + } + return positions; + } + return null; + } +} diff --git a/src/org/traccar/protocol/GranitProtocolEncoder.java b/src/org/traccar/protocol/GranitProtocolEncoder.java new file mode 100644 index 000000000..961f183f5 --- /dev/null +++ b/src/org/traccar/protocol/GranitProtocolEncoder.java @@ -0,0 +1,55 @@ +/* + * Copyright 2016 Anton Tananaev (anton.tananaev@gmail.com) + * + * 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 java.nio.charset.StandardCharsets; + +import org.jboss.netty.buffer.ChannelBuffer; +import org.jboss.netty.buffer.ChannelBuffers; +import org.traccar.BaseProtocolEncoder; +import org.traccar.helper.Log; +import org.traccar.model.Command; + +public class GranitProtocolEncoder extends BaseProtocolEncoder { + @Override + protected Object encodeCommand(Command command) { + + String commandString = ""; + + switch (command.getType()) { + case Command.TYPE_IDENTIFICATION: + commandString = "BB+IDNT"; + break; + case Command.TYPE_REBOOT_DEVICE: + commandString = "BB+RESET"; + break; + case Command.TYPE_POSITION_SINGLE: + commandString = "BB+RRCD"; + break; + default: + Log.warning(new UnsupportedOperationException(command.getType())); + return null; + } + if (!commandString.isEmpty()) { + ChannelBuffer commandBuf = ChannelBuffers.dynamicBuffer(); + commandBuf.writeBytes(commandString.getBytes(StandardCharsets.US_ASCII)); + GranitProtocolDecoder.appendChecksum(commandBuf, commandString.length()); + return commandBuf; + } + return null; + } + +} |