diff options
author | Anton Tananaev <anton.tananaev@gmail.com> | 2015-10-06 15:58:35 +1300 |
---|---|---|
committer | Anton Tananaev <anton.tananaev@gmail.com> | 2015-10-06 15:58:35 +1300 |
commit | e60ecc5f2bc8bb6c53e95c4db2e68cbca79c6e54 (patch) | |
tree | ab64a642ff7a3bd66ae9cdb57d5d347bc2b47bf1 /src/org/traccar/protocol | |
parent | c8ad5cc5c45ab47abe35e3ad2e4f607d520e627d (diff) | |
download | trackermap-server-e60ecc5f2bc8bb6c53e95c4db2e68cbca79c6e54.tar.gz trackermap-server-e60ecc5f2bc8bb6c53e95c4db2e68cbca79c6e54.tar.bz2 trackermap-server-e60ecc5f2bc8bb6c53e95c4db2e68cbca79c6e54.zip |
Implement Cityeasy protocol commands
Diffstat (limited to 'src/org/traccar/protocol')
14 files changed, 108 insertions, 30 deletions
diff --git a/src/org/traccar/protocol/ApelProtocolDecoder.java b/src/org/traccar/protocol/ApelProtocolDecoder.java index c3e16a7dc..7b89d374e 100644 --- a/src/org/traccar/protocol/ApelProtocolDecoder.java +++ b/src/org/traccar/protocol/ApelProtocolDecoder.java @@ -26,7 +26,7 @@ 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.Crc; +import org.traccar.helper.Checksum; import org.traccar.helper.Log; import org.traccar.helper.UnitsConverter; import org.traccar.model.Event; @@ -75,7 +75,7 @@ public class ApelProtocolDecoder extends BaseProtocolDecoder { ChannelBuffer request = ChannelBuffers.directBuffer(ByteOrder.LITTLE_ENDIAN, 8); request.writeShort(type); request.writeShort(0); - request.writeInt(Crc.crc32(request.toByteBuffer(0, 4))); + request.writeInt(Checksum.crc32(request.toByteBuffer(0, 4))); channel.write(request); } @@ -88,7 +88,7 @@ public class ApelProtocolDecoder extends BaseProtocolDecoder { request.writeShort(6); request.writeInt((int) lastIndex); request.writeShort(512); - request.writeInt(Crc.crc32(request.toByteBuffer(0, 10))); + request.writeInt(Checksum.crc32(request.toByteBuffer(0, 10))); channel.write(request); } } diff --git a/src/org/traccar/protocol/AplicomProtocolDecoder.java b/src/org/traccar/protocol/AplicomProtocolDecoder.java index a4d177ed0..aa9dd57d1 100644 --- a/src/org/traccar/protocol/AplicomProtocolDecoder.java +++ b/src/org/traccar/protocol/AplicomProtocolDecoder.java @@ -24,7 +24,7 @@ import org.jboss.netty.channel.Channel; import org.traccar.BaseProtocolDecoder; import org.traccar.Context; import org.traccar.helper.ChannelBufferTools; -import org.traccar.helper.Crc; +import org.traccar.helper.Checksum; import org.traccar.helper.UnitsConverter; import org.traccar.model.Event; import org.traccar.model.Position; @@ -40,7 +40,7 @@ public class AplicomProtocolDecoder extends BaseProtocolDecoder { private static final long IMEI_BASE_TC65I_V11 = 0x14143B4000000L; private static boolean validateImei(long imei) { - return Crc.luhnChecksum(imei / 10) == imei % 10; + return Checksum.luhnChecksum(imei / 10) == imei % 10; } private static long imeiFromUnitId(long unitId) { diff --git a/src/org/traccar/protocol/CastelProtocolDecoder.java b/src/org/traccar/protocol/CastelProtocolDecoder.java index ceed8f5ef..f93b9762c 100644 --- a/src/org/traccar/protocol/CastelProtocolDecoder.java +++ b/src/org/traccar/protocol/CastelProtocolDecoder.java @@ -26,7 +26,7 @@ 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.Crc; +import org.traccar.helper.Checksum; import org.traccar.helper.UnitsConverter; import org.traccar.model.Event; import org.traccar.model.Position; @@ -95,7 +95,7 @@ public class CastelProtocolDecoder extends BaseProtocolDecoder { response.writeByte(version); response.writeBytes(id); response.writeShort(ChannelBuffers.swapShort(MSG_HEARTBEAT_RESPONSE)); - response.writeShort(Crc.crc16Ccitt(response.toByteBuffer(0, response.writerIndex()))); + response.writeShort(Checksum.crc16Ccitt(response.toByteBuffer(0, response.writerIndex()))); response.writeByte(0x0D); response.writeByte(0x0A); channel.write(response, remoteAddress); } @@ -123,7 +123,7 @@ public class CastelProtocolDecoder extends BaseProtocolDecoder { response.writeInt(0xFFFFFFFF); response.writeShort(0); response.writeInt((int) (System.currentTimeMillis() / 1000)); - response.writeShort(Crc.crc16Ccitt(response.toByteBuffer(0, response.writerIndex()))); + response.writeShort(Checksum.crc16Ccitt(response.toByteBuffer(0, response.writerIndex()))); response.writeByte(0x0D); response.writeByte(0x0A); channel.write(response, remoteAddress); } diff --git a/src/org/traccar/protocol/CityeasyProtocol.java b/src/org/traccar/protocol/CityeasyProtocol.java index 69f7ad5ac..1049e48eb 100644 --- a/src/org/traccar/protocol/CityeasyProtocol.java +++ b/src/org/traccar/protocol/CityeasyProtocol.java @@ -20,6 +20,7 @@ import org.jboss.netty.channel.ChannelPipeline; import org.jboss.netty.handler.codec.frame.LengthFieldBasedFrameDecoder; import org.traccar.BaseProtocol; import org.traccar.TrackerServer; +import org.traccar.model.Command; import java.util.List; @@ -27,6 +28,11 @@ public class CityeasyProtocol extends BaseProtocol { public CityeasyProtocol() { super("cityeasy"); + setSupportedCommands( + Command.TYPE_POSITION_SINGLE, + Command.TYPE_POSITION_PERIODIC, + Command.TYPE_POSITION_STOP, + Command.TYPE_SET_TIMEZONE); } @Override @@ -36,6 +42,7 @@ public class CityeasyProtocol extends BaseProtocol { protected void addSpecificHandlers(ChannelPipeline pipeline) { pipeline.addLast("frameDecoder", new LengthFieldBasedFrameDecoder(1024, 2, 2, -4, 0)); pipeline.addLast("objectDecoder", new CityeasyProtocolDecoder(CityeasyProtocol.this)); + pipeline.addLast("objectEncoder", new CityeasyProtocolEncoder()); } }); } diff --git a/src/org/traccar/protocol/CityeasyProtocolDecoder.java b/src/org/traccar/protocol/CityeasyProtocolDecoder.java index 4cb0562a6..457ba9c5d 100644 --- a/src/org/traccar/protocol/CityeasyProtocolDecoder.java +++ b/src/org/traccar/protocol/CityeasyProtocolDecoder.java @@ -25,7 +25,7 @@ import org.jboss.netty.buffer.ChannelBuffer; import org.jboss.netty.channel.Channel; import org.traccar.BaseProtocolDecoder; import org.traccar.helper.ChannelBufferTools; -import org.traccar.helper.Crc; +import org.traccar.helper.Checksum; import org.traccar.model.Event; import org.traccar.model.Position; @@ -51,8 +51,14 @@ public class CityeasyProtocolDecoder extends BaseProtocolDecoder { "(\\d+)" + // Cell ".*"); - private static final int MSG_LOCATION_REPORT = 0x0003; - private static final int MSG_LOCATION_INQUIRY_RESPONSE = 0x0004; + public static final int MSG_ADDRESS_REQUEST = 0x0001; + public static final int MSG_STATUS = 0x0002; + public static final int MSG_LOCATION_REPORT = 0x0003; + public static final int MSG_LOCATION_REQUEST = 0x0004; + public static final int MSG_LOCATION_INTERVAL = 0x0005; + public static final int MSG_PHONE_NUMBER = 0x0006; + public static final int MSG_MONITORING = 0x0007; + public static final int MSG_TIMEZONE = 0x0008; @Override protected Object decode( @@ -66,14 +72,14 @@ public class CityeasyProtocolDecoder extends BaseProtocolDecoder { String imei = ChannelBufferTools.readHexString(buf, 14); if (!identify(imei, channel, null, false)) { - if (!identify(imei + Crc.luhnChecksum(Long.parseLong(imei)), channel)) { + if (!identify(imei + Checksum.luhnChecksum(Long.parseLong(imei)), channel)) { return null; } } int type = buf.readUnsignedShort(); - if (type == MSG_LOCATION_REPORT || type == MSG_LOCATION_INQUIRY_RESPONSE) { + if (type == MSG_LOCATION_REPORT || type == MSG_LOCATION_REQUEST) { String sentence = buf.toString(buf.readerIndex(), buf.readableBytes() - 8, Charset.defaultCharset()); Matcher parser = pattern.matcher(sentence); diff --git a/src/org/traccar/protocol/CityeasyProtocolEncoder.java b/src/org/traccar/protocol/CityeasyProtocolEncoder.java new file mode 100644 index 000000000..9d15e9d5d --- /dev/null +++ b/src/org/traccar/protocol/CityeasyProtocolEncoder.java @@ -0,0 +1,65 @@ +/* + * Copyright 2015 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.traccar.BaseProtocolEncoder; +import org.traccar.helper.Checksum; +import org.traccar.model.Command; + +public class CityeasyProtocolEncoder extends BaseProtocolEncoder { + + private ChannelBuffer encodeContent(int type, ChannelBuffer content) { + + ChannelBuffer buf = ChannelBuffers.dynamicBuffer(); + + buf.writeByte('S'); + buf.writeByte('S'); + buf.writeShort(2 + 2 + 2 + content.readableBytes() + 4 + 2 + 2); + buf.writeShort(type); + buf.writeBytes(content); + buf.writeInt(0x0B); + buf.writeShort(Checksum.crc16Ccitt(buf.toByteBuffer(), 0, 0)); + buf.writeByte('\r'); + buf.writeByte('\n'); + + return buf; + } + + @Override + protected Object encodeCommand(Command command) { + + ChannelBuffer content = ChannelBuffers.dynamicBuffer(); + + switch (command.getType()) { + case Command.TYPE_POSITION_SINGLE: + return encodeContent(CityeasyProtocolDecoder.MSG_LOCATION_REQUEST, content); + case Command.TYPE_POSITION_PERIODIC: + case Command.TYPE_POSITION_STOP: + content.writeShort(((Number) command.getAttributes().getOrDefault(Command.KEY_FREQUENCY, 0)).intValue()); + return encodeContent(CityeasyProtocolDecoder.MSG_LOCATION_INTERVAL, content); + case Command.TYPE_SET_TIMEZONE: + int timezone = ((Number) command.getAttributes().getOrDefault(Command.KEY_TIMEZONE, 0)).intValue(); + content.writeByte(timezone < 0 ? 1 : 0); + content.writeShort(Math.abs(timezone) / 60); + return encodeContent(CityeasyProtocolDecoder.MSG_TIMEZONE, content); + } + + return null; + } + +} diff --git a/src/org/traccar/protocol/GpsGateProtocolDecoder.java b/src/org/traccar/protocol/GpsGateProtocolDecoder.java index 9e583aebd..3994587bf 100644 --- a/src/org/traccar/protocol/GpsGateProtocolDecoder.java +++ b/src/org/traccar/protocol/GpsGateProtocolDecoder.java @@ -24,7 +24,7 @@ import java.util.regex.Pattern; import org.jboss.netty.channel.Channel; import org.traccar.BaseProtocolDecoder; -import org.traccar.helper.Crc; +import org.traccar.helper.Checksum; import org.traccar.model.Position; public class GpsGateProtocolDecoder extends BaseProtocolDecoder { @@ -48,7 +48,7 @@ public class GpsGateProtocolDecoder extends BaseProtocolDecoder { private void send(Channel channel, String message) { if (channel != null) { - channel.write(message + Crc.nmeaChecksum(message) + "\r\n"); + channel.write(message + Checksum.nmeaChecksum(message) + "\r\n"); } } diff --git a/src/org/traccar/protocol/Gt06ProtocolDecoder.java b/src/org/traccar/protocol/Gt06ProtocolDecoder.java index e6acc5e1b..ab5c80cf6 100644 --- a/src/org/traccar/protocol/Gt06ProtocolDecoder.java +++ b/src/org/traccar/protocol/Gt06ProtocolDecoder.java @@ -23,7 +23,7 @@ import org.jboss.netty.buffer.ChannelBuffers; import org.jboss.netty.channel.Channel; import org.traccar.BaseProtocolDecoder; import org.traccar.Context; -import org.traccar.helper.Crc; +import org.traccar.helper.Checksum; import org.traccar.helper.UnitsConverter; import org.traccar.model.Event; import org.traccar.model.Position; @@ -81,7 +81,7 @@ public class Gt06ProtocolDecoder extends BaseProtocolDecoder { response.writeByte(0x05); // size response.writeByte(type); response.writeShort(index); - response.writeShort(Crc.crc16Ccitt(response.toByteBuffer(2, 4))); + response.writeShort(Checksum.crc16Ccitt(response.toByteBuffer(2, 4))); response.writeByte(0x0D); response.writeByte(0x0A); // ending channel.write(response); } diff --git a/src/org/traccar/protocol/Gt06ProtocolEncoder.java b/src/org/traccar/protocol/Gt06ProtocolEncoder.java index e842c3a69..9a1bb4571 100644 --- a/src/org/traccar/protocol/Gt06ProtocolEncoder.java +++ b/src/org/traccar/protocol/Gt06ProtocolEncoder.java @@ -18,7 +18,7 @@ package org.traccar.protocol; import org.jboss.netty.buffer.ChannelBuffer; import org.jboss.netty.buffer.ChannelBuffers; import org.traccar.BaseProtocolEncoder; -import org.traccar.helper.Crc; +import org.traccar.helper.Checksum; import org.traccar.model.Command; public class Gt06ProtocolEncoder extends BaseProtocolEncoder { @@ -40,7 +40,7 @@ public class Gt06ProtocolEncoder extends BaseProtocolEncoder { buf.writeShort(0); // message index - buf.writeShort(Crc.crc16Ccitt(buf.toByteBuffer(2, buf.writerIndex() - 2))); + buf.writeShort(Checksum.crc16Ccitt(buf.toByteBuffer(2, buf.writerIndex() - 2))); buf.writeByte('\r'); buf.writeByte('\n'); diff --git a/src/org/traccar/protocol/KhdProtocolDecoder.java b/src/org/traccar/protocol/KhdProtocolDecoder.java index 6d634e744..3ad6761cd 100644 --- a/src/org/traccar/protocol/KhdProtocolDecoder.java +++ b/src/org/traccar/protocol/KhdProtocolDecoder.java @@ -25,7 +25,7 @@ import org.jboss.netty.channel.Channel; import org.traccar.BaseProtocolDecoder; import org.traccar.helper.ChannelBufferTools; -import org.traccar.helper.Crc; +import org.traccar.helper.Checksum; import org.traccar.helper.UnitsConverter; import org.traccar.model.Event; import org.traccar.model.Position; @@ -136,7 +136,7 @@ public class KhdProtocolDecoder extends BaseProtocolDecoder { response.writeByte(buf.readUnsignedByte()); response.writeByte(type); response.writeByte(0); // reserved - response.writeByte(Crc.xorChecksum(response.toByteBuffer())); + response.writeByte(Checksum.xorChecksum(response.toByteBuffer())); response.writeByte(0x0D); // ending channel.write(response); diff --git a/src/org/traccar/protocol/KhdProtocolEncoder.java b/src/org/traccar/protocol/KhdProtocolEncoder.java index 68196d65c..ca2407743 100644 --- a/src/org/traccar/protocol/KhdProtocolEncoder.java +++ b/src/org/traccar/protocol/KhdProtocolEncoder.java @@ -18,7 +18,7 @@ package org.traccar.protocol; import org.jboss.netty.buffer.ChannelBuffer; import org.jboss.netty.buffer.ChannelBuffers; import org.traccar.BaseProtocolEncoder; -import org.traccar.helper.Crc; +import org.traccar.helper.Checksum; import org.traccar.model.Command; public class KhdProtocolEncoder extends BaseProtocolEncoder { @@ -38,7 +38,7 @@ public class KhdProtocolEncoder extends BaseProtocolEncoder { buf.writeInt(0); // terminal id - buf.writeByte(Crc.xorChecksum(buf.toByteBuffer())); + buf.writeByte(Checksum.xorChecksum(buf.toByteBuffer())); buf.writeByte(0x0D); // ending return buf; diff --git a/src/org/traccar/protocol/LaipacProtocolDecoder.java b/src/org/traccar/protocol/LaipacProtocolDecoder.java index f3884650d..57dd797eb 100644 --- a/src/org/traccar/protocol/LaipacProtocolDecoder.java +++ b/src/org/traccar/protocol/LaipacProtocolDecoder.java @@ -24,7 +24,7 @@ import java.util.regex.Pattern; import org.jboss.netty.channel.Channel; import org.traccar.BaseProtocolDecoder; -import org.traccar.helper.Crc; +import org.traccar.helper.Checksum; import org.traccar.model.Position; public class LaipacProtocolDecoder extends BaseProtocolDecoder { @@ -121,7 +121,7 @@ public class LaipacProtocolDecoder extends BaseProtocolDecoder { if (type.equals("0") && Character.isLowerCase(status.charAt(0))) { response = "$EAVACK,0," + checksum; - response += Crc.nmeaChecksum(response); + response += Checksum.nmeaChecksum(response); } else if (type.equals("S") || type.equals("T")) { response = "$AVCFG,00000000,t*21"; } else if (type.equals("3")) { diff --git a/src/org/traccar/protocol/MeiligaoProtocolDecoder.java b/src/org/traccar/protocol/MeiligaoProtocolDecoder.java index e50b4f60b..51587f3b5 100644 --- a/src/org/traccar/protocol/MeiligaoProtocolDecoder.java +++ b/src/org/traccar/protocol/MeiligaoProtocolDecoder.java @@ -27,7 +27,7 @@ import org.jboss.netty.buffer.ChannelBuffers; import org.jboss.netty.channel.Channel; import org.traccar.BaseProtocolDecoder; import org.traccar.Context; -import org.traccar.helper.Crc; +import org.traccar.helper.Checksum; import org.traccar.model.Event; import org.traccar.model.Position; @@ -100,7 +100,7 @@ public class MeiligaoProtocolDecoder extends BaseProtocolDecoder { if (id.length() == 14) { // Try to recreate full IMEI number // Sometimes first digit is cut, so this won't work - if (identify(id + Crc.luhnChecksum(Long.valueOf(id)), channel, null, false)) { + if (identify(id + Checksum.luhnChecksum(Long.valueOf(id)), channel, null, false)) { return true; } } @@ -121,7 +121,7 @@ public class MeiligaoProtocolDecoder extends BaseProtocolDecoder { buf.writeBytes(id); buf.writeShort(type); buf.writeBytes(msg); - buf.writeShort(Crc.crc16X25Ccitt(buf.toByteBuffer())); + buf.writeShort(Checksum.crc16X25Ccitt(buf.toByteBuffer())); buf.writeByte('\r'); buf.writeByte('\n'); diff --git a/src/org/traccar/protocol/NavigilProtocolDecoder.java b/src/org/traccar/protocol/NavigilProtocolDecoder.java index 686c8a691..24c6618bb 100644 --- a/src/org/traccar/protocol/NavigilProtocolDecoder.java +++ b/src/org/traccar/protocol/NavigilProtocolDecoder.java @@ -22,7 +22,7 @@ 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.Crc; +import org.traccar.helper.Checksum; import org.traccar.helper.UnitsConverter; import org.traccar.model.Event; import org.traccar.model.Position; @@ -68,7 +68,7 @@ public class NavigilProtocolDecoder extends BaseProtocolDecoder { header.writeShort(MESSAGE_ACKNOWLEDGEMENT); header.writeShort(header.capacity() + data.capacity()); header.writeShort(0); - header.writeShort(Crc.crc16X25Ccitt(data.toByteBuffer())); + header.writeShort(Checksum.crc16X25Ccitt(data.toByteBuffer())); header.writeInt(0); header.writeInt((int) (System.currentTimeMillis() / 1000) + LEAP_SECONDS_DELTA); |