From 51ebdc5e806edbb5fe978e42a380edcf4ce6fffc Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Sun, 3 Mar 2013 12:13:43 +1300 Subject: Add MTA6CAN protocol --- src/org/traccar/ServerManager.java | 4 +- src/org/traccar/protocol/Mta6ProtocolDecoder.java | 115 ++++++++++++++++++---- 2 files changed, 100 insertions(+), 19 deletions(-) (limited to 'src/org') diff --git a/src/org/traccar/ServerManager.java b/src/org/traccar/ServerManager.java index 0d55b4e0d..0c7995313 100644 --- a/src/org/traccar/ServerManager.java +++ b/src/org/traccar/ServerManager.java @@ -649,7 +649,7 @@ public class ServerManager { protected void addSpecificHandlers(ChannelPipeline pipeline) { pipeline.addLast("httpDecoder", new HttpRequestDecoder()); pipeline.addLast("httpEncoder", new HttpResponseEncoder()); - pipeline.addLast("objectDecoder", new Mta6ProtocolDecoder(ServerManager.this)); + pipeline.addLast("objectDecoder", new Mta6ProtocolDecoder(ServerManager.this, false)); } }); } @@ -662,7 +662,7 @@ public class ServerManager { protected void addSpecificHandlers(ChannelPipeline pipeline) { pipeline.addLast("httpDecoder", new HttpRequestDecoder()); pipeline.addLast("httpEncoder", new HttpResponseEncoder()); - pipeline.addLast("objectDecoder", new Mta6ProtocolDecoder(ServerManager.this)); + pipeline.addLast("objectDecoder", new Mta6ProtocolDecoder(ServerManager.this, true)); } }); } diff --git a/src/org/traccar/protocol/Mta6ProtocolDecoder.java b/src/org/traccar/protocol/Mta6ProtocolDecoder.java index 7645fb7be..749c01393 100644 --- a/src/org/traccar/protocol/Mta6ProtocolDecoder.java +++ b/src/org/traccar/protocol/Mta6ProtocolDecoder.java @@ -37,9 +37,12 @@ import org.traccar.helper.Log; import org.traccar.model.Position; public class Mta6ProtocolDecoder extends BaseProtocolDecoder { + + private boolean simple; - public Mta6ProtocolDecoder(ServerManager serverManager) { + public Mta6ProtocolDecoder(ServerManager serverManager, boolean simple) { super(serverManager); + this.simple = simple; } private void sendContinue(Channel channel) { @@ -113,21 +116,7 @@ public class Mta6ProtocolDecoder extends BaseProtocolDecoder { } } - - private Date readTime(ChannelBuffer buf, FloatReader timeReader) { - long weekTime = (long) (timeReader.readFloat(buf) * 1000); - long weekNumber = buf.readUnsignedShort(); - - Calendar time = Calendar.getInstance(TimeZone.getTimeZone("UTC")); - time.clear(); - time.set(Calendar.YEAR, 1980); - time.set(Calendar.MONTH, 0); - time.set(Calendar.DAY_OF_MONTH, 6); - long offset = time.getTimeInMillis(); - - return new Date(offset + weekNumber * 7 * 24 * 60 * 60 * 1000 + weekTime); - } - + private List parseFormatA(ChannelBuffer buf, long deviceId) { List positions = new LinkedList(); @@ -227,6 +216,94 @@ public class Mta6ProtocolDecoder extends BaseProtocolDecoder { return positions; } + + private Position parseFormatA1(ChannelBuffer buf, long deviceId) { + Position position = new Position(); + position.setDeviceId(deviceId); + StringBuilder extendedInfo = new StringBuilder("mta6can"); + + short flags = buf.readUnsignedByte(); + + // Skip events + short event = buf.readUnsignedByte(); + if (checkBit(event, 7)) { + if (checkBit(event, 6)) { + buf.skipBytes(8); + } else { + while (checkBit(event, 7)) { + event = buf.readUnsignedByte(); + } + } + } + + position.setLatitude(new FloatReader().readFloat(buf) / Math.PI * 180); + position.setLongitude(new FloatReader().readFloat(buf) / Math.PI * 180); + position.setTime(new TimeReader().readTime(buf)); + + buf.readUnsignedByte(); // status + + if (checkBit(flags, 0)) { + position.setAltitude((double) buf.readUnsignedShort()); + position.setSpeed((double) buf.readUnsignedByte()); + position.setCourse((double) buf.readByte()); + extendedInfo.append(""); + extendedInfo.append(new FloatReader().readFloat(buf)); + extendedInfo.append(""); + } + + if (checkBit(flags, 1)) { + new FloatReader().readFloat(buf); // fuel consumtion + extendedInfo.append("").append(new FloatReader().readFloat(buf)).append(""); + extendedInfo.append("").append(buf.readUnsignedByte() * 0.4).append(""); + } + + if (checkBit(flags, 2)) { + extendedInfo.append("").append(buf.readUnsignedShort() * 0.125).append(""); + extendedInfo.append("").append(buf.readUnsignedByte()).append(""); + extendedInfo.append("").append(buf.readUnsignedByte() - 40).append(""); + buf.readUnsignedShort(); // service milage + } + + if (checkBit(flags, 3)) { + extendedInfo.append("").append(buf.readUnsignedShort()).append(""); + extendedInfo.append("").append(buf.readUnsignedShort()).append(""); + extendedInfo.append("").append(buf.readUnsignedShort()).append(""); + extendedInfo.append("").append(buf.readUnsignedShort()).append(""); + } + + if (checkBit(flags, 4)) { + extendedInfo.append(""); + extendedInfo.append(buf.readByte()); + extendedInfo.append(""); + buf.getUnsignedByte(buf.readerIndex()); // control (>> 4) + extendedInfo.append(""); + extendedInfo.append(buf.readUnsignedShort() & 0x0fff); + extendedInfo.append(""); + buf.readUnsignedShort(); // old sensor state (& 0x0fff) + } + + if (checkBit(flags, 5)) { + extendedInfo.append(""); + extendedInfo.append(buf.getUnsignedByte(buf.readerIndex()) >> 2); + extendedInfo.append(""); + position.setPower((double) (buf.readUnsignedShort() & 0x03ff)); + buf.readByte(); // microcontroller temperature + + extendedInfo.append(""); + extendedInfo.append(buf.getUnsignedByte(buf.readerIndex()) >> 5); + extendedInfo.append(""); + + int satellites = buf.readUnsignedByte() & 0x1f; + position.setValid(satellites >= 3); + extendedInfo.append("").append(satellites).append(""); + } + + // TODO: process other data + + position.setExtendedInfo(extendedInfo.toString()); + + return position; + } @Override protected Object decode( @@ -268,7 +345,11 @@ public class Mta6ProtocolDecoder extends BaseProtocolDecoder { // Parse data if (packetId == 0x31 || packetId == 0x32 || packetId == 0x36) { - return parseFormatA(buf, deviceId); + if (simple) { + return parseFormatA1(buf, deviceId); + } else { + return parseFormatA(buf, deviceId); + } } //else if (0x34 0x38 0x4F 0x59) return null; -- cgit v1.2.3