diff options
author | Anton Tananaev <anton.tananaev@gmail.com> | 2018-09-08 16:50:41 +1200 |
---|---|---|
committer | Anton Tananaev <anton.tananaev@gmail.com> | 2018-09-08 16:50:41 +1200 |
commit | b27ab90540402836b64e2eb634c37dcab7aa6608 (patch) | |
tree | 22da3a9f8dec35bd267bf3b02cc66550bea81a01 | |
parent | 1fde64481bc86b570ebd250784a0844fc4e77043 (diff) | |
download | traccar-server-b27ab90540402836b64e2eb634c37dcab7aa6608.tar.gz traccar-server-b27ab90540402836b64e2eb634c37dcab7aa6608.tar.bz2 traccar-server-b27ab90540402836b64e2eb634c37dcab7aa6608.zip |
Fix issues in Freematics decoder
3 files changed, 112 insertions, 19 deletions
diff --git a/src/org/traccar/protocol/FreematicsProtocol.java b/src/org/traccar/protocol/FreematicsProtocol.java index 3bcd5bfd1..6e7dfa15d 100644 --- a/src/org/traccar/protocol/FreematicsProtocol.java +++ b/src/org/traccar/protocol/FreematicsProtocol.java @@ -16,6 +16,7 @@ package org.traccar.protocol; import io.netty.handler.codec.string.StringDecoder; +import io.netty.handler.codec.string.StringEncoder; import org.traccar.BaseProtocol; import org.traccar.PipelineBuilder; import org.traccar.TrackerServer; @@ -33,6 +34,7 @@ public class FreematicsProtocol extends BaseProtocol { serverList.add(new TrackerServer(true, getName()) { @Override protected void addProtocolHandlers(PipelineBuilder pipeline) { + pipeline.addLast("stringEncoder", new StringEncoder()); pipeline.addLast("stringDecoder", new StringDecoder()); pipeline.addLast("objectDecoder", new FreematicsProtocolDecoder(FreematicsProtocol.this)); } diff --git a/src/org/traccar/protocol/FreematicsProtocolDecoder.java b/src/org/traccar/protocol/FreematicsProtocolDecoder.java index 2cab45189..8dce49195 100644 --- a/src/org/traccar/protocol/FreematicsProtocolDecoder.java +++ b/src/org/traccar/protocol/FreematicsProtocolDecoder.java @@ -18,12 +18,16 @@ package org.traccar.protocol; import io.netty.channel.Channel; import org.traccar.BaseProtocolDecoder; import org.traccar.DeviceSession; +import org.traccar.NetworkMessage; +import org.traccar.helper.Checksum; import org.traccar.helper.DateBuilder; import org.traccar.helper.UnitsConverter; import org.traccar.model.Position; import java.net.SocketAddress; import java.util.Date; +import java.util.LinkedList; +import java.util.List; public class FreematicsProtocolDecoder extends BaseProtocolDecoder { @@ -31,30 +35,67 @@ public class FreematicsProtocolDecoder extends BaseProtocolDecoder { super(protocol); } - @Override - protected Object decode( - Channel channel, SocketAddress remoteAddress, Object msg) throws Exception { + private Object decodeEvent( + Channel channel, SocketAddress remoteAddress, String sentence) throws Exception { - String sentence = (String) msg; - int startIndex = sentence.indexOf('#'); - int endIndex = sentence.indexOf('*'); + DeviceSession deviceSession = null; + String time = null; + + for (String pair : sentence.split(",")) { + String[] data = pair.split("="); + String key = data[0]; + String value = data[1]; + switch (key) { + case "ID": + case "VIN": + if (deviceSession == null) { + deviceSession = getDeviceSession(channel, remoteAddress, value); + } + break; + case "TS": + time = value; + break; + default: + break; + } + } + + if (channel != null && deviceSession != null && time != null) { + String message = "1#EV=1,RX=1,TS=" + time; + message += '*' + Checksum.sum(message); + channel.writeAndFlush(new NetworkMessage(message, remoteAddress)); + } + + return null; + } + + private Object decodePosition( + Channel channel, SocketAddress remoteAddress, String sentence) throws Exception { - DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, sentence.substring(0, startIndex)); + DeviceSession deviceSession = getDeviceSession(channel, remoteAddress); if (deviceSession == null) { return null; } - Position position = new Position(getProtocolName()); - position.setDeviceId(deviceSession.getDeviceId()); - position.setValid(true); + List<Position> positions = new LinkedList<>(); + Position position = null; + DateBuilder dateBuilder = null; - DateBuilder dateBuilder = new DateBuilder(new Date()); - - for (String pair : sentence.substring(startIndex + 1, endIndex).split(",")) { - String[] data = pair.split("="); + for (String pair : sentence.split(",")) { + String[] data = pair.split("[=:]"); int key = Integer.parseInt(data[0], 16); String value = data[1]; switch (key) { + case 0x0: + if (position != null) { + position.setTime(dateBuilder.getDate()); + positions.add(position); + } + position = new Position(getProtocolName()); + position.setDeviceId(deviceSession.getDeviceId()); + position.setValid(true); + dateBuilder = new DateBuilder(new Date()); + break; case 0x11: value = ("000000" + value).substring(value.length()); dateBuilder.setDateReverse( @@ -77,10 +118,10 @@ public class FreematicsProtocolDecoder extends BaseProtocolDecoder { position.setLongitude(Double.parseDouble(value)); break; case 0xC: - position.setAltitude(Integer.parseInt(value)); + position.setAltitude(Double.parseDouble(value)); break; case 0xD: - position.setLatitude(UnitsConverter.knotsFromKph(Integer.parseInt(value))); + position.setSpeed(UnitsConverter.knotsFromKph(Double.parseDouble(value))); break; case 0xE: position.setCourse(Integer.parseInt(value)); @@ -88,16 +129,51 @@ public class FreematicsProtocolDecoder extends BaseProtocolDecoder { case 0xF: position.set(Position.KEY_SATELLITES, Integer.parseInt(value)); break; + case 0x20: + position.set(Position.KEY_ACCELERATION, value); + break; + case 0x24: + position.set(Position.KEY_BATTERY, Integer.parseInt(value) * 0.01); + break; + case 0x81: + position.set(Position.KEY_RSSI, Integer.parseInt(value)); + break; + case 0x82: + position.set(Position.KEY_DEVICE_TEMP, Integer.parseInt(value) * 0.1); + break; default: position.set(data[0], value); break; } + } + if (position != null) { + position.setTime(dateBuilder.getDate()); + positions.add(position); } - position.setTime(dateBuilder.getDate()); + return positions; + } + + @Override + protected Object decode( + Channel channel, SocketAddress remoteAddress, Object msg) throws Exception { + + String sentence = (String) msg; + int startIndex = sentence.indexOf('#'); + int endIndex = sentence.indexOf('*'); + + if (startIndex > 0 && endIndex > 0) { + sentence = sentence.substring(startIndex + 1, endIndex); + + if (sentence.startsWith("EV")) { + return decodeEvent(channel, remoteAddress, sentence); + } else { + return decodePosition(channel, remoteAddress, sentence); + } + } - return position; + return null; } } diff --git a/test/org/traccar/protocol/FreematicsProtocolDecoderTest.java b/test/org/traccar/protocol/FreematicsProtocolDecoderTest.java index f629f68df..90150d76d 100644 --- a/test/org/traccar/protocol/FreematicsProtocolDecoderTest.java +++ b/test/org/traccar/protocol/FreematicsProtocolDecoderTest.java @@ -10,7 +10,22 @@ public class FreematicsProtocolDecoderTest extends ProtocolTest { FreematicsProtocolDecoder decoder = new FreematicsProtocolDecoder(new FreematicsProtocol()); - verifyPosition(decoder, text( + verifyNull(decoder, text( + "0#EV=1,TS=23930,ID=ID1C6606C40A24,SK=TEST_SERVER_KEY*49")); + + verifyPositions(decoder, text( + "1#0:102560,20:0;0;0,24:425,10:4285580,A:-35.803696,B:175.748413,C:0.22,D:0.41,F:5,0:103174,20:0;0;0,24:423,10:4285660,A:-35.803696,B:175.748413,C:0.22,D:0.41,F:5,30:88193792*21")); + + verifyPositions(decoder, text( + "1#0:49244,20:0;0;0,24:423,0:50779,20:0;0;0,24:425,30:32924444*38")); + + verifyNotNull(decoder, text( + "1#0:47607,20:0;0;0,24:423,0:48732,20:0;0;0,24:428,10:4280140,A:0.000000,B:0.000000,C:0.00,D:18520000.00,F:2,30:32924444*BA")); + + verifyPositions(decoder, text( + "1#0:68338,10D:79,30:1010,105:199,10C:4375,104:56,111:62,20:0;-1;95,10:6454200,A:-32.727482,B:150.150301,C:159,D:0,F:5,24:1250*7A")); + + verifyPositions(decoder, text( "1#0=68338,10D=79,30=1010,105=199,10C=4375,104=56,111=62,20=0;-1;95,10=6454200,A=-32.727482,B=150.150301,C=159,D=0,F=5,24=1250*7A")); } |