From 2f98dd6c9f151f48d31ecd09ee06ccbcd4430b97 Mon Sep 17 00:00:00 2001 From: Valerii Vyshniak Date: Thu, 16 Nov 2017 00:07:04 +0100 Subject: T580W: parse altitude and satellites num in GPS data packet --- test/org/traccar/protocol/Tk103ProtocolDecoderTest.java | 10 ++++++++++ 1 file changed, 10 insertions(+) (limited to 'test/org/traccar') diff --git a/test/org/traccar/protocol/Tk103ProtocolDecoderTest.java b/test/org/traccar/protocol/Tk103ProtocolDecoderTest.java index a69ff8856..3c17d2d11 100644 --- a/test/org/traccar/protocol/Tk103ProtocolDecoderTest.java +++ b/test/org/traccar/protocol/Tk103ProtocolDecoderTest.java @@ -159,6 +159,16 @@ public class Tk103ProtocolDecoderTest extends ProtocolTest { verifyPosition(decoder, text( "(094050000111BP05000094050000111150808A3804.2418N04616.7468E000.0201447133.3501000011L0028019DT000)")); + //T580W + verifyPosition(decoder, text( + "(864555555555555,DW3B,131117,A,5544.02870N,01315.08194E,1.597,223707,291.65,-0.10,4)")); + + verifyPosition(decoder, text( + "(864555555555555,DW3B,131117,A,5544.02870N,01315.08194E,1.597,223707,291.65,0.10,8)")); + + verifyPosition(decoder, text( + "(013632651491,ZC07,040613,A,2234.0297N,11405.9101E,000.0,040137,178.48)")); + } } -- cgit v1.2.3 From f26eb9c61bc1270707af0a7589ae6ee56531e905 Mon Sep 17 00:00:00 2001 From: Valerii Vyshniak Date: Thu, 16 Nov 2017 00:31:27 +0100 Subject: T580W: parse battery level --- src/org/traccar/protocol/Tk103ProtocolDecoder.java | 24 +++++++++++++++++++++- .../traccar/protocol/Tk103ProtocolDecoderTest.java | 3 +++ 2 files changed, 26 insertions(+), 1 deletion(-) (limited to 'test/org/traccar') diff --git a/src/org/traccar/protocol/Tk103ProtocolDecoder.java b/src/org/traccar/protocol/Tk103ProtocolDecoder.java index 1a5ada81b..b62ee12ec 100644 --- a/src/org/traccar/protocol/Tk103ProtocolDecoder.java +++ b/src/org/traccar/protocol/Tk103ProtocolDecoder.java @@ -75,7 +75,7 @@ public class Tk103ProtocolDecoder extends BaseProtocolDecoder { .text("ZC20,") .number("(dd)(dd)(dd),") // date (ddmmyy) .number("(dd)(dd)(dd),") // time (hhmmss) - .number("d+,") // battery level + .number("(d+),") // battery level .number("(d+),") // battery voltage .number("(d+),") // power voltage .number("d+") // installed @@ -147,6 +147,23 @@ public class Tk103ProtocolDecoder extends BaseProtocolDecoder { } } + private Integer decodeBattery(int value) { + switch (value) { + case 6: + return 100; + case 5: + return 80; + case 4: + return 60; + case 3: + return 20; + case 2: + return 10; + default: + return null; + } + } + private Position decodeBattery(Channel channel, SocketAddress remoteAddress, String sentence) { Parser parser = new Parser(PATTERN_BATTERY, sentence); if (!parser.matches()) { @@ -164,6 +181,11 @@ public class Tk103ProtocolDecoder extends BaseProtocolDecoder { getLastLocation(position, parser.nextDateTime(Parser.DateTimeFormat.DMY_HMS)); + int batterylevel = parser.nextInt(0); + if (batterylevel != 255) { + position.set(Position.KEY_BATTERY_LEVEL, decodeBattery(batterylevel)); + } + int battery = parser.nextInt(0); if (battery != 65535) { position.set(Position.KEY_BATTERY, battery * 0.01); diff --git a/test/org/traccar/protocol/Tk103ProtocolDecoderTest.java b/test/org/traccar/protocol/Tk103ProtocolDecoderTest.java index 3c17d2d11..4f8ee2858 100644 --- a/test/org/traccar/protocol/Tk103ProtocolDecoderTest.java +++ b/test/org/traccar/protocol/Tk103ProtocolDecoderTest.java @@ -169,6 +169,9 @@ public class Tk103ProtocolDecoderTest extends ProtocolTest { verifyPosition(decoder, text( "(013632651491,ZC07,040613,A,2234.0297N,11405.9101E,000.0,040137,178.48)")); + verifyAttributes(decoder, text( + "(013632651491,ZC20,040613,040137,6,42,112,0")); + } } -- cgit v1.2.3 From fe5f4f69155f5242da357bee37335dac3ff5f9c1 Mon Sep 17 00:00:00 2001 From: Valerii Vyshniak Date: Thu, 16 Nov 2017 02:14:58 +0100 Subject: T580W: parse LBS and WiFi data packet --- src/org/traccar/protocol/Tk103ProtocolDecoder.java | 78 ++++++++++++++++++++++ .../traccar/protocol/Tk103ProtocolDecoderTest.java | 12 ++++ 2 files changed, 90 insertions(+) (limited to 'test/org/traccar') diff --git a/src/org/traccar/protocol/Tk103ProtocolDecoder.java b/src/org/traccar/protocol/Tk103ProtocolDecoder.java index b62ee12ec..c275de6b7 100644 --- a/src/org/traccar/protocol/Tk103ProtocolDecoder.java +++ b/src/org/traccar/protocol/Tk103ProtocolDecoder.java @@ -26,6 +26,7 @@ import org.traccar.helper.PatternBuilder; import org.traccar.model.CellTower; import org.traccar.model.Network; import org.traccar.model.Position; +import org.traccar.model.WifiAccessPoint; import java.net.SocketAddress; import java.util.regex.Pattern; @@ -91,6 +92,20 @@ public class Tk103ProtocolDecoder extends BaseProtocolDecoder { .any() .compile(); + private static final Pattern PATTERN_LBSWIFI = new PatternBuilder() + .number("(d+),") // device id + .expression("(.{4}),") // command + .number("(d+),") // mcc + .number("(d+),") // mnc + .number("(d+),") // lac + .number("(d+),") // cid + .number("(d+),") // number of wifi macs + .expression("((?:(?:[0-9A-Fa-f]{2}:){5}(?:[0-9A-Fa-f]{2})\\*[-+]?\\d{1,2}\\*\\d{1,2},)*)") + .number("(dd)(dd)(dd),") // date (ddmmyy) + .number("(dd)(dd)(dd)") // time (hhmmss) + .any() + .compile(); + private String decodeAlarm(int value) { switch (value) { case 1: @@ -221,6 +236,64 @@ public class Tk103ProtocolDecoder extends BaseProtocolDecoder { return position; } + + private Position decodeLbsWifi(Channel channel, SocketAddress remoteAddress, String sentence) { + Parser parser = new Parser(PATTERN_LBSWIFI, sentence); + if (!parser.matches()) { + return null; + } + + DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, parser.next()); + if (deviceSession == null) { + return null; + } + + Position position = new Position(); + position.setProtocol(getProtocolName()); + position.setDeviceId(deviceSession.getDeviceId()); + + decodeType(position, parser.next(), "0"); + + getLastLocation(position, null); + + Network network = new Network(); + + // Parse LBS + network.addCellTower(CellTower.from( + parser.nextInt(), parser.nextInt(), parser.nextInt(), parser.nextInt())); + + // Parse WiFi macs number and mac addresses itself. + int wifiCount = parser.nextInt(); + if (parser.hasNext()) { + String[] wifimacs = parser.next().split(","); + if (wifimacs.length == wifiCount) { + for (int i = 0; i < wifiCount; i++) { + // Sample wifi string: “00:80:E1:7F:86:97*-55*6” (mac*rssi*channel_number) + String[] wifiinfo = wifimacs[i].split("\\*"); + network.addWifiAccessPoint(WifiAccessPoint.from( + wifiinfo[0], Integer.parseInt(wifiinfo[1]), Integer.parseInt(wifiinfo[2]))); + } + } + } + + if (network.getCellTowers() != null || network.getWifiAccessPoints() != null) { + position.setNetwork(network); + } + + // Parse date-time + DateBuilder dateBuilder = new DateBuilder() + .setDateReverse(parser.nextInt(), parser.nextInt(), parser.nextInt()) + .setTime(parser.nextInt(), parser.nextInt(), parser.nextInt()); + // Set both DeviceTime and FixTime here, as some alarm messages like SOS at device start may be + // sent without valid LBS/WiFi position, so FixTime from getLastLocation will be used as time + // for this SOS alarm, which can be very old. And traccar UI only display FixTime. + // Valid flag will be "false" anyway, so it is possible to check should the position and fixTime + // be trusted or not. + position.setTime(dateBuilder.getDate()); + + return position; + } + @Override protected Object decode( Channel channel, SocketAddress remoteAddress, Object msg) throws Exception { @@ -249,6 +322,11 @@ public class Tk103ProtocolDecoder extends BaseProtocolDecoder { return decodeNetwork(channel, remoteAddress, sentence); } + Position positionWifi = decodeLbsWifi(channel, remoteAddress, sentence); + if (positionWifi != null) { + return positionWifi; + } + Parser parser = new Parser(PATTERN, sentence); if (!parser.matches()) { return null; diff --git a/test/org/traccar/protocol/Tk103ProtocolDecoderTest.java b/test/org/traccar/protocol/Tk103ProtocolDecoderTest.java index 4f8ee2858..f10912d68 100644 --- a/test/org/traccar/protocol/Tk103ProtocolDecoderTest.java +++ b/test/org/traccar/protocol/Tk103ProtocolDecoderTest.java @@ -172,6 +172,18 @@ public class Tk103ProtocolDecoderTest extends ProtocolTest { verifyAttributes(decoder, text( "(013632651491,ZC20,040613,040137,6,42,112,0")); + verifyNotNull(decoder, text( + "(864555555555555,DW51,200,1,3215,43370,2,58:F3:BB:3B:AA:82*-65*1,1C:6A:BB:AA:81:95*-78*1,151117,154419")); + + verifyNotNull(decoder, text( + "(864555555555555,DW5B,210,6,5995,47701,5,30:EE:CC:E7:86:DD*-59*11,4C:60:CC:EA:BB:EE*-68*1,42:AA:DE:EA:BB:00*-69*1,32:CD:BB:C3:4F:CC*-86*3,10:00:43:BA:22:15*-88*1,151117,163722")); + + verifyNotNull(decoder, text( + "(013632651491,DW50,460,0,0,6,2,aa:bb:cc:dd:ee:ff*-8*0,aa:bb:cc:dd:ee:ff*-8*0,040613,040137")); + + verifyNotNull(decoder, text( + "(013632651491,DW50,460,0,0,6,0,040613,040137")); + } } -- cgit v1.2.3 From 3ac49cd8e224887e9a5f8b7a1787183fbdb0e73f Mon Sep 17 00:00:00 2001 From: Valerii Vyshniak Date: Sat, 18 Nov 2017 00:56:52 +0100 Subject: T580W: Commands --- src/org/traccar/protocol/Tk103Protocol.java | 17 ++++++- src/org/traccar/protocol/Tk103ProtocolEncoder.java | 34 ++++++++++++++ .../traccar/protocol/Tk103ProtocolEncoderTest.java | 52 ++++++++++++++++++++++ 3 files changed, 102 insertions(+), 1 deletion(-) (limited to 'test/org/traccar') diff --git a/src/org/traccar/protocol/Tk103Protocol.java b/src/org/traccar/protocol/Tk103Protocol.java index 07a68e2d8..b205e75f1 100644 --- a/src/org/traccar/protocol/Tk103Protocol.java +++ b/src/org/traccar/protocol/Tk103Protocol.java @@ -40,7 +40,22 @@ public class Tk103Protocol extends BaseProtocol { Command.TYPE_REBOOT_DEVICE, Command.TYPE_SET_ODOMETER, Command.TYPE_ENGINE_STOP, - Command.TYPE_ENGINE_RESUME); + Command.TYPE_ENGINE_RESUME, + //T580W commands: + "T580W_positionSingle", + "T580W_positionRealtime", + "T580W_positionRealtimeStop", + "T580W_modeDeepSleepInterval1Hour", + "T580W_modeDeepSleepInterval2Hour", + "T580W_modeDeepSleepInterval3Hour", + "T580W_modeDeepSleepInterval4Hour", + "T580W_modeDeepSleepMotion", + "T580W_modeDeepSleepOff", + "T580W_alarmSosOn", + "T580W_alarmSosOff", + "T580W_multiControllerOn", + "T580W_multiControllerOff", + "T580W_rebootDevice"); } @Override diff --git a/src/org/traccar/protocol/Tk103ProtocolEncoder.java b/src/org/traccar/protocol/Tk103ProtocolEncoder.java index 9e49b6ff1..dc9fc41a1 100644 --- a/src/org/traccar/protocol/Tk103ProtocolEncoder.java +++ b/src/org/traccar/protocol/Tk103ProtocolEncoder.java @@ -22,6 +22,11 @@ import org.traccar.model.Command; public class Tk103ProtocolEncoder extends StringProtocolEncoder { + protected String t580WFormatCommand(Command command, String format, String... keys) { + String content = super.formatCommand(command, format, keys); + return String.format("[begin]sms2,%s,[end]", content); + } + @Override protected Object encodeCommand(Command command) { @@ -43,6 +48,35 @@ public class Tk103ProtocolEncoder extends StringProtocolEncoder { return formatCommand(command, "({%s}AV011)", Command.KEY_UNIQUE_ID); case Command.TYPE_ENGINE_RESUME: return formatCommand(command, "({%s}AV010)", Command.KEY_UNIQUE_ID); + //T580W commands: + case "T580W_positionSingle": + return t580WFormatCommand(command, "*getposl*"); + case "T580W_positionRealtime": + return t580WFormatCommand(command, "*routetrack*99*"); + case "T580W_positionRealtimeStop": + return t580WFormatCommand(command, "*routetrackoff*"); + case "T580W_modeDeepSleepInterval1Hour": + return t580WFormatCommand(command, "*sleep*1*"); + case "T580W_modeDeepSleepInterval2Hour": + return t580WFormatCommand(command, "*sleep*2*"); + case "T580W_modeDeepSleepInterval3Hour": + return t580WFormatCommand(command, "*sleep*3*"); + case "T580W_modeDeepSleepInterval4Hour": + return t580WFormatCommand(command, "*sleep*4*"); + case "T580W_modeDeepSleepMotion": + return t580WFormatCommand(command, "*sleepv*"); + case "T580W_modeDeepSleepOff": + return t580WFormatCommand(command, "*sleepoff*"); + case "T580W_alarmSosOn": + return t580WFormatCommand(command, "*soson*"); + case "T580W_alarmSosOff": + return t580WFormatCommand(command, "*sosoff*"); + case "T580W_multiControllerOn": + return t580WFormatCommand(command, "*multiquery*"); + case "T580W_multiControllerOff": + return t580WFormatCommand(command, "*multiqueryoff*"); + case "T580W_rebootDevice": + return t580WFormatCommand(command, "88888888"); default: Log.warning(new UnsupportedOperationException(command.getType())); break; diff --git a/test/org/traccar/protocol/Tk103ProtocolEncoderTest.java b/test/org/traccar/protocol/Tk103ProtocolEncoderTest.java index afc3b2387..8439f8d48 100644 --- a/test/org/traccar/protocol/Tk103ProtocolEncoderTest.java +++ b/test/org/traccar/protocol/Tk103ProtocolEncoderTest.java @@ -99,4 +99,56 @@ public class Tk103ProtocolEncoderTest extends ProtocolTest { } + @Test + public void testEncodeT580WRebootDevice() throws Exception { + + Tk103ProtocolEncoder encoder = new Tk103ProtocolEncoder(); + + Command command = new Command(); + command.setDeviceId(1); + command.setType("T580W_rebootDevice"); + + Assert.assertEquals("[begin]sms2,88888888,[end]", encoder.encodeCommand(command)); + + } + + @Test + public void testEncodeT580WSosOn() throws Exception { + + Tk103ProtocolEncoder encoder = new Tk103ProtocolEncoder(); + + Command command = new Command(); + command.setDeviceId(1); + command.setType("T580W_alarmSosOn"); + + Assert.assertEquals("[begin]sms2,*soson*,[end]", encoder.encodeCommand(command)); + + } + + @Test + public void testEncodeT580WSosOff() throws Exception { + + Tk103ProtocolEncoder encoder = new Tk103ProtocolEncoder(); + + Command command = new Command(); + command.setDeviceId(1); + command.setType("T580W_alarmSosOff"); + + Assert.assertEquals("[begin]sms2,*sosoff*,[end]", encoder.encodeCommand(command)); + + } + + @Test + public void testEncodeT580WRouteTrack() throws Exception { + + Tk103ProtocolEncoder encoder = new Tk103ProtocolEncoder(); + + Command command = new Command(); + command.setDeviceId(1); + command.setType("T580W_positionRealtime"); + + Assert.assertEquals("[begin]sms2,*routetrack*99*,[end]", encoder.encodeCommand(command)); + + } + } -- cgit v1.2.3 From fd9f7b72c1994508ef26df05f4001ab0fd8449e3 Mon Sep 17 00:00:00 2001 From: Valerii Vyshniak Date: Mon, 20 Nov 2017 02:40:37 +0100 Subject: T580W: review comments fixes --- src/org/traccar/protocol/Tk103Protocol.java | 25 ++-- src/org/traccar/protocol/Tk103ProtocolDecoder.java | 137 ++++++------------ src/org/traccar/protocol/Tk103ProtocolEncoder.java | 119 ++++++++------- .../traccar/protocol/Tk103ProtocolDecoderTest.java | 17 +++ .../traccar/protocol/Tk103ProtocolEncoderTest.java | 160 +++++++++++++++++++-- 5 files changed, 291 insertions(+), 167 deletions(-) (limited to 'test/org/traccar') diff --git a/src/org/traccar/protocol/Tk103Protocol.java b/src/org/traccar/protocol/Tk103Protocol.java index b205e75f1..3360dab32 100644 --- a/src/org/traccar/protocol/Tk103Protocol.java +++ b/src/org/traccar/protocol/Tk103Protocol.java @@ -33,6 +33,14 @@ public class Tk103Protocol extends BaseProtocol { public Tk103Protocol() { super("tk103"); setSupportedDataCommands( + Command.TYPE_CUSTOM, + Command.TYPE_GET_DEVICE_STATUS, + Command.TYPE_IDENTIFICATION, + Command.TYPE_MODE_DEEP_SLEEP, + Command.TYPE_MODE_POWER_SAVING, + Command.TYPE_ALARM_SOS, + Command.TYPE_SET_CONNECTION, + Command.TYPE_SOS_NUMBER, Command.TYPE_POSITION_SINGLE, Command.TYPE_POSITION_PERIODIC, Command.TYPE_POSITION_STOP, @@ -40,22 +48,7 @@ public class Tk103Protocol extends BaseProtocol { Command.TYPE_REBOOT_DEVICE, Command.TYPE_SET_ODOMETER, Command.TYPE_ENGINE_STOP, - Command.TYPE_ENGINE_RESUME, - //T580W commands: - "T580W_positionSingle", - "T580W_positionRealtime", - "T580W_positionRealtimeStop", - "T580W_modeDeepSleepInterval1Hour", - "T580W_modeDeepSleepInterval2Hour", - "T580W_modeDeepSleepInterval3Hour", - "T580W_modeDeepSleepInterval4Hour", - "T580W_modeDeepSleepMotion", - "T580W_modeDeepSleepOff", - "T580W_alarmSosOn", - "T580W_alarmSosOff", - "T580W_multiControllerOn", - "T580W_multiControllerOff", - "T580W_rebootDevice"); + Command.TYPE_ENGINE_RESUME); } @Override diff --git a/src/org/traccar/protocol/Tk103ProtocolDecoder.java b/src/org/traccar/protocol/Tk103ProtocolDecoder.java index e52d60848..2ce7def45 100644 --- a/src/org/traccar/protocol/Tk103ProtocolDecoder.java +++ b/src/org/traccar/protocol/Tk103ProtocolDecoder.java @@ -29,8 +29,6 @@ import org.traccar.model.Position; import org.traccar.model.WifiAccessPoint; import java.net.SocketAddress; -import java.util.HashMap; -import java.util.Map; import java.util.regex.Pattern; public class Tk103ProtocolDecoder extends BaseProtocolDecoder { @@ -67,8 +65,8 @@ public class Tk103ProtocolDecoder extends BaseProtocolDecoder { .any() .number("([+-]ddd.d)?") // temperature .groupBegin() - .number("([+-]?d+.d{1,2}),").optional() // altitude - .number("(d+)$").optional() // number of visible satellites + .number("([+-]?d+.d{1,2}),") // altitude + .number("(d+)$") // number of visible satellites .groupEnd("?") .text(")").optional() .compile(); @@ -102,12 +100,21 @@ public class Tk103ProtocolDecoder extends BaseProtocolDecoder { .number("(d+),") // lac .number("(d+),") // cid .number("(d+),") // number of wifi macs - .expression("((?:(?:[0-9A-Fa-f]{2}:){5}(?:[0-9A-Fa-f]{2})\\*[-+]?\\d{1,2}\\*\\d{1,2},)*)") + .number("((?:(?:xx:){5}(?:xx)\\*[-+]?d+\\*d+,)*)") .number("(dd)(dd)(dd),") // date (ddmmyy) .number("(dd)(dd)(dd)") // time (hhmmss) .any() .compile(); + private static final Pattern PATTERN_COMMAND_RESULT = new PatternBuilder() + .number("(d+),") // device id + .expression(".{4},") // command + .number("(dd)(dd)(dd),") // date (ddmmyy) + .number("(dd)(dd)(dd),") // time (hhmmss) + .expression("\\$([\\s\\S]*?)(?:\\$|$)") // message + .any() + .compile(); + private String decodeAlarm(int value) { switch (value) { case 1: @@ -132,90 +139,61 @@ public class Tk103ProtocolDecoder extends BaseProtocolDecoder { case "BO01": position.set(Position.KEY_ALARM, decodeAlarm(data.charAt(0) - '0')); break; - // Motion Alert with Location case "ZC11": case "DW31": case "DW51": position.set(Position.KEY_ALARM, Position.ALARM_MOVEMENT); break; - // Low Battery Alert with Location case "ZC12": case "DW32": case "DW52": position.set(Position.KEY_ALARM, Position.ALARM_LOW_BATTERY); break; - // Power Cut Alert with Location case "ZC13": case "DW33": case "DW53": position.set(Position.KEY_ALARM, Position.ALARM_POWER_CUT); break; - // ACC On Alert with Location case "ZC15": case "DW35": case "DW55": position.set(Position.KEY_IGNITION, true); break; - // ACC Off Alert with Location case "ZC16": case "DW36": case "DW56": position.set(Position.KEY_IGNITION, false); break; - // Ignition Alert with Location case "ZC29": case "DW42": case "DW62": position.set(Position.KEY_IGNITION, true); break; - // Dismounting Alert with Location case "ZC17": case "DW37": case "DW57": position.set(Position.KEY_ALARM, Position.ALARM_REMOVING); break; - // SOS Alert with Location case "ZC25": case "DW3E": case "DW5E": position.set(Position.KEY_ALARM, Position.ALARM_SOS); break; - // Tampering Alert with Location: case "ZC26": case "DW3F": case "DW5F": position.set(Position.KEY_ALARM, Position.ALARM_TAMPERING); break; - // Low Power Alert with Location case "ZC27": case "DW40": case "DW60": position.set(Position.KEY_ALARM, Position.ALARM_LOW_POWER); break; - // Bad Battery Alert with Location case "ZC28": case "DW41": case "DW61": position.set(Position.KEY_ALARM, "badBattery"); break; - // Fuel Cut On with Location - case "ZC2A": - case "DW43": - case "DW63": - position.set(Position.KEY_FUEL_LEVEL, 0.0); - break; - // Fuel Cut Off with Location: - case "ZC2B": - case "DW44": - case "DW64": - position.set(Position.KEY_FUEL_LEVEL, 1.0); - break; - // Fuel Cut Alert with Location: - case "ZC2C": - case "DW45": - case "DW65": - position.set(Position.KEY_ALARM, "fuelCut"); - break; default: break; } @@ -228,7 +206,7 @@ public class Tk103ProtocolDecoder extends BaseProtocolDecoder { case 5: return 80; case 4: - return 60; + return 50; case 3: return 20; case 2: @@ -238,34 +216,6 @@ public class Tk103ProtocolDecoder extends BaseProtocolDecoder { } } - private static Map batteryInfo = new HashMap(); - - public void setLastBatteryPower(Position positionWithCurrentBatteryInfo) { - batteryInfo.put(positionWithCurrentBatteryInfo.getDeviceId(), positionWithCurrentBatteryInfo); - } - - // It is possible to use "processing.copyAttributes" configuration to have battery level in all events, - // but this will not work if battery events will be filtered somehow and dropped from DB, so it will be no - // event in DB with latest battery level. So, ensure all our events have latest battery level. - public void getLastBatteryPower(Position position) { - if (position != null && position.getDeviceId() != 0) { - Position bi = batteryInfo.get(position.getDeviceId()); - if (bi == null) { - bi = new Position(); - getLastLocation(bi, null); - } - if (bi.getAttributes().containsKey(Position.KEY_BATTERY_LEVEL)) { - position.set(Position.KEY_BATTERY_LEVEL, bi.getDouble(Position.KEY_BATTERY_LEVEL)); - } - if (bi.getAttributes().containsKey(Position.KEY_BATTERY)) { - position.set(Position.KEY_BATTERY, bi.getDouble(Position.KEY_BATTERY)); - } - if (bi.getAttributes().containsKey(Position.KEY_POWER)) { - position.set(Position.KEY_POWER, bi.getDouble(Position.KEY_POWER)); - } - } - } - private Position decodeBattery(Channel channel, SocketAddress remoteAddress, String sentence) { Parser parser = new Parser(PATTERN_BATTERY, sentence); if (!parser.matches()) { @@ -298,8 +248,6 @@ public class Tk103ProtocolDecoder extends BaseProtocolDecoder { position.set(Position.KEY_POWER, power * 0.1); } - setLastBatteryPower(position); - return position; } @@ -320,8 +268,6 @@ public class Tk103ProtocolDecoder extends BaseProtocolDecoder { getLastLocation(position, null); - getLastBatteryPower(position); - position.setNetwork(new Network(CellTower.from( parser.nextInt(0), parser.nextInt(0), parser.nextHexInt(0), parser.nextHexInt(0)))); @@ -347,21 +293,16 @@ public class Tk103ProtocolDecoder extends BaseProtocolDecoder { getLastLocation(position, null); - getLastBatteryPower(position); - Network network = new Network(); - // Parse LBS network.addCellTower(CellTower.from( parser.nextInt(), parser.nextInt(), parser.nextInt(), parser.nextInt())); - // Parse WiFi macs number and mac addresses itself. int wifiCount = parser.nextInt(); if (parser.hasNext()) { String[] wifimacs = parser.next().split(","); if (wifimacs.length == wifiCount) { for (int i = 0; i < wifiCount; i++) { - // Sample wifi string: “00:80:E1:7F:86:97*-55*6” (mac*rssi*channel_number) String[] wifiinfo = wifimacs[i].split("\\*"); network.addWifiAccessPoint(WifiAccessPoint.from( wifiinfo[0], Integer.parseInt(wifiinfo[1]), Integer.parseInt(wifiinfo[2]))); @@ -373,21 +314,35 @@ public class Tk103ProtocolDecoder extends BaseProtocolDecoder { position.setNetwork(network); } - // Parse date-time - DateBuilder dateBuilder = new DateBuilder() - .setDateReverse(parser.nextInt(), parser.nextInt(), parser.nextInt()) - .setTime(parser.nextInt(), parser.nextInt(), parser.nextInt()); - // Set both DeviceTime and FixTime here, as some alarm messages like SOS at device start may be - // sent without valid LBS/WiFi position, so FixTime from getLastLocation will be used as time - // for this SOS alarm, which can be very old. And traccar UI only display FixTime. - // Valid flag will be "false" anyway, so it is possible to check should the position and fixTime - // be trusted or not. - position.setTime(dateBuilder.getDate()); + position.setTime(parser.nextDateTime(Parser.DateTimeFormat.DMY_HMS)); return position; } - @Override + private Position decodeCommandResult(Channel channel, SocketAddress remoteAddress, String sentence) { + Parser parser = new Parser(PATTERN_COMMAND_RESULT, sentence); + if (!parser.matches()) { + return null; + } + + DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, parser.next()); + if (deviceSession == null) { + return null; + } + + Position position = new Position(); + position.setProtocol(getProtocolName()); + position.setDeviceId(deviceSession.getDeviceId()); + + getLastLocation(position, parser.nextDateTime(Parser.DateTimeFormat.DMY_HMS)); + + position.set(Position.KEY_RESULT, parser.next()); + + return position; + + } + +@Override protected Object decode( Channel channel, SocketAddress remoteAddress, Object msg) throws Exception { @@ -398,7 +353,7 @@ public class Tk103ProtocolDecoder extends BaseProtocolDecoder { sentence = sentence.substring(beginIndex + 1); } - if (channel != null) { + if (channel != null && sentence.length() >= 16) { String id = sentence.substring(0, 12); String type = sentence.substring(12, 16); if (type.equals("BP00")) { @@ -413,11 +368,10 @@ public class Tk103ProtocolDecoder extends BaseProtocolDecoder { return decodeBattery(channel, remoteAddress, sentence); } else if (sentence.contains("BZ00")) { return decodeNetwork(channel, remoteAddress, sentence); - } - - Position positionWifi = decodeLbsWifi(channel, remoteAddress, sentence); - if (positionWifi != null) { - return positionWifi; + } else if (sentence.contains("ZC03")) { + return decodeCommandResult(channel, remoteAddress, sentence); + } else if (sentence.contains("DW5")) { + return decodeLbsWifi(channel, remoteAddress, sentence); } Parser parser = new Parser(PATTERN, sentence); @@ -438,8 +392,6 @@ public class Tk103ProtocolDecoder extends BaseProtocolDecoder { decodeType(position, parser.next(), parser.next()); - getLastBatteryPower(position); - DateBuilder dateBuilder = new DateBuilder(); if (alternative) { dateBuilder.setDateReverse(parser.nextInt(0), parser.nextInt(0), parser.nextInt(0)); @@ -503,8 +455,7 @@ public class Tk103ProtocolDecoder extends BaseProtocolDecoder { } if (parser.hasNext()) { - // Store amount of visible satellites as RSSI - position.set(Position.KEY_RSSI, parser.nextInt(0)); + position.set(Position.KEY_SATELLITES_VISIBLE, parser.nextInt(0)); } return position; diff --git a/src/org/traccar/protocol/Tk103ProtocolEncoder.java b/src/org/traccar/protocol/Tk103ProtocolEncoder.java index dc9fc41a1..e43761c7c 100644 --- a/src/org/traccar/protocol/Tk103ProtocolEncoder.java +++ b/src/org/traccar/protocol/Tk103ProtocolEncoder.java @@ -16,6 +16,7 @@ */ package org.traccar.protocol; +import org.traccar.Context; import org.traccar.StringProtocolEncoder; import org.traccar.helper.Log; import org.traccar.model.Command; @@ -27,59 +28,77 @@ public class Tk103ProtocolEncoder extends StringProtocolEncoder { return String.format("[begin]sms2,%s,[end]", content); } + protected String checkEn(Command command, String ifEnabled, String ifDisabled) { + return command.getBoolean(Command.KEY_ENABLE) ? ifEnabled : ifDisabled; + } + @Override protected Object encodeCommand(Command command) { - switch (command.getType()) { - case Command.TYPE_GET_VERSION: - return formatCommand(command, "({%s}AP07)", Command.KEY_UNIQUE_ID); - case Command.TYPE_REBOOT_DEVICE: - return formatCommand(command, "({%s}AT00)", Command.KEY_UNIQUE_ID); - case Command.TYPE_SET_ODOMETER: - return formatCommand(command, "({%s}AX01)", Command.KEY_UNIQUE_ID); - case Command.TYPE_POSITION_SINGLE: - return formatCommand(command, "({%s}AP00)", Command.KEY_UNIQUE_ID); - case Command.TYPE_POSITION_PERIODIC: - return formatCommand(command, "({%s}AR00%s0000)", Command.KEY_UNIQUE_ID, - String.format("%04X", command.getInteger(Command.KEY_FREQUENCY))); - case Command.TYPE_POSITION_STOP: - return formatCommand(command, "({%s}AR0000000000)", Command.KEY_UNIQUE_ID); - case Command.TYPE_ENGINE_STOP: - return formatCommand(command, "({%s}AV011)", Command.KEY_UNIQUE_ID); - case Command.TYPE_ENGINE_RESUME: - return formatCommand(command, "({%s}AV010)", Command.KEY_UNIQUE_ID); - //T580W commands: - case "T580W_positionSingle": - return t580WFormatCommand(command, "*getposl*"); - case "T580W_positionRealtime": - return t580WFormatCommand(command, "*routetrack*99*"); - case "T580W_positionRealtimeStop": - return t580WFormatCommand(command, "*routetrackoff*"); - case "T580W_modeDeepSleepInterval1Hour": - return t580WFormatCommand(command, "*sleep*1*"); - case "T580W_modeDeepSleepInterval2Hour": - return t580WFormatCommand(command, "*sleep*2*"); - case "T580W_modeDeepSleepInterval3Hour": - return t580WFormatCommand(command, "*sleep*3*"); - case "T580W_modeDeepSleepInterval4Hour": - return t580WFormatCommand(command, "*sleep*4*"); - case "T580W_modeDeepSleepMotion": - return t580WFormatCommand(command, "*sleepv*"); - case "T580W_modeDeepSleepOff": - return t580WFormatCommand(command, "*sleepoff*"); - case "T580W_alarmSosOn": - return t580WFormatCommand(command, "*soson*"); - case "T580W_alarmSosOff": - return t580WFormatCommand(command, "*sosoff*"); - case "T580W_multiControllerOn": - return t580WFormatCommand(command, "*multiquery*"); - case "T580W_multiControllerOff": - return t580WFormatCommand(command, "*multiqueryoff*"); - case "T580W_rebootDevice": - return t580WFormatCommand(command, "88888888"); - default: - Log.warning(new UnsupportedOperationException(command.getType())); - break; + boolean deviceT580W = Context.getIdentityManager().lookupAttributeBoolean( + command.getDeviceId(), "tk103.deviceT580W", false, true); + + if (deviceT580W) { + + initDevicePassword(command, "123456"); + + switch (command.getType()) { + case Command.TYPE_CUSTOM: + return t580WFormatCommand(command, "{%s}", Command.KEY_DATA); + case Command.TYPE_GET_DEVICE_STATUS: + return t580WFormatCommand(command, "*status*"); + case Command.TYPE_GET_VERSION: + return t580WFormatCommand(command, "*about*"); + case Command.TYPE_IDENTIFICATION: + return t580WFormatCommand(command, "999999"); + case Command.TYPE_REBOOT_DEVICE: + return t580WFormatCommand(command, "88888888"); + case Command.TYPE_POSITION_SINGLE: + return t580WFormatCommand(command, "*getposl*"); + case Command.TYPE_POSITION_PERIODIC: + return t580WFormatCommand(command, "*routetrack*99*"); + case Command.TYPE_POSITION_STOP: + return t580WFormatCommand(command, "*routetrackoff*"); + case Command.TYPE_MODE_DEEP_SLEEP: + return t580WFormatCommand(command, checkEn(command, "*sleep*2*", "*sleepoff*")); + case Command.TYPE_MODE_POWER_SAVING: + return t580WFormatCommand(command, checkEn(command, "*sleepv*", "*sleepoff*")); + case Command.TYPE_ALARM_SOS: + return t580WFormatCommand(command, checkEn(command, "*soson*", "*sosoff*")); + case Command.TYPE_SET_CONNECTION: + String ipAddress = command.getString(Command.KEY_SERVER); + ipAddress = ipAddress.replace(".", "*"); + return t580WFormatCommand(command, "*setip*%s*{%s}*", ipAddress, Command.KEY_PORT); + case Command.TYPE_SOS_NUMBER: + return t580WFormatCommand(command, "*master*{%s}*{%s}*", + Command.KEY_DEVICE_PASSWORD, Command.KEY_PHONE); + default: + Log.warning(new UnsupportedOperationException(command.getType())); + break; + } + } else { + switch (command.getType()) { + case Command.TYPE_GET_VERSION: + return formatCommand(command, "({%s}AP07)", Command.KEY_UNIQUE_ID); + case Command.TYPE_REBOOT_DEVICE: + return formatCommand(command, "({%s}AT00)", Command.KEY_UNIQUE_ID); + case Command.TYPE_SET_ODOMETER: + return formatCommand(command, "({%s}AX01)", Command.KEY_UNIQUE_ID); + case Command.TYPE_POSITION_SINGLE: + return formatCommand(command, "({%s}AP00)", Command.KEY_UNIQUE_ID); + case Command.TYPE_POSITION_PERIODIC: + return formatCommand(command, "({%s}AR00%s0000)", Command.KEY_UNIQUE_ID, + String.format("%04X", command.getInteger(Command.KEY_FREQUENCY))); + case Command.TYPE_POSITION_STOP: + return formatCommand(command, "({%s}AR0000000000)", Command.KEY_UNIQUE_ID); + case Command.TYPE_ENGINE_STOP: + return formatCommand(command, "({%s}AV011)", Command.KEY_UNIQUE_ID); + case Command.TYPE_ENGINE_RESUME: + return formatCommand(command, "({%s}AV010)", Command.KEY_UNIQUE_ID); + default: + Log.warning(new UnsupportedOperationException(command.getType())); + break; + } } return null; diff --git a/test/org/traccar/protocol/Tk103ProtocolDecoderTest.java b/test/org/traccar/protocol/Tk103ProtocolDecoderTest.java index f10912d68..b8fe872ee 100644 --- a/test/org/traccar/protocol/Tk103ProtocolDecoderTest.java +++ b/test/org/traccar/protocol/Tk103ProtocolDecoderTest.java @@ -184,6 +184,23 @@ public class Tk103ProtocolDecoderTest extends ProtocolTest { verifyNotNull(decoder, text( "(013632651491,DW50,460,0,0,6,0,040613,040137")); + verifyNotNull(decoder, text( + "(864555555555555,ZC03,191117,234207,$Notice: Device version: 1.0$")); + + verifyNotNull(decoder, text( + "(864555555555555,ZC03,191117,234207,$1 .Sensor sensitivity: 1\r\n" + + "2 .Alert status: Off\r\n" + + "3 .Check interval is set to 240 minute(s).\r\n" + + "4 .Checkgsm interval is set to 60 minute(s).\r\n" + + "5 .SOS SMS Alert: On\r\n" + + "6 .SOS Call Alert: On\r\n" + + "7 . Power: 95%$")); + + verifyNotNull(decoder, text( + "(864555555555555,ZC03,191117,234207,$1 .Sensor sensitivity: 1\r\n" + + "2 .Alert status: Off\r\n" + + "3 .Check interval is set to 240 minute(s")); + } } diff --git a/test/org/traccar/protocol/Tk103ProtocolEncoderTest.java b/test/org/traccar/protocol/Tk103ProtocolEncoderTest.java index 8439f8d48..2e5b66572 100644 --- a/test/org/traccar/protocol/Tk103ProtocolEncoderTest.java +++ b/test/org/traccar/protocol/Tk103ProtocolEncoderTest.java @@ -2,11 +2,76 @@ package org.traccar.protocol; import org.junit.Assert; import org.junit.Test; +import org.traccar.Context; import org.traccar.ProtocolTest; +import org.traccar.database.IdentityManager; import org.traccar.model.Command; +import org.traccar.model.Device; +import org.traccar.model.Position; public class Tk103ProtocolEncoderTest extends ProtocolTest { + private static IdentityManager defaultManager; + static { + defaultManager = Context.getIdentityManager(); + } + + private static IdentityManager t580wManager = new IdentityManager() { + + private Device createDevice() { + Device device = new Device(); + device.setId(1); + device.setName("test"); + device.setUniqueId("123456789012345"); + device.set(Command.KEY_DEVICE_PASSWORD, "654321"); + return device; + } + + @Override + public Device getById(long id) { + return createDevice(); + } + + @Override + public Device getByUniqueId(String uniqueId) { + return createDevice(); + } + + @Override + public Position getLastPosition(long deviceId) { + return null; + } + + @Override + public boolean isLatestPosition(Position position) { + return true; + } + + @Override + public boolean lookupAttributeBoolean( + long deviceId, String attributeName, boolean defaultValue, boolean lookupConfig) { + return deviceId == 1 && attributeName == "tk103.deviceT580W" && lookupConfig ? true : defaultValue; + } + + @Override + public String lookupAttributeString( + long deviceId, String attributeName, String defaultValue, boolean lookupConfig) { + return defaultValue; + } + + @Override + public int lookupAttributeInteger( + long deviceId, String attributeName, int defaultValue, boolean lookupConfig) { + return defaultValue; + } + + @Override + public long lookupAttributeLong( + long deviceId, String attributeName, long defaultValue, boolean lookupConfig) { + return defaultValue; + } + }; + @Test public void testEncodeEngineStop() throws Exception { @@ -16,6 +81,8 @@ public class Tk103ProtocolEncoderTest extends ProtocolTest { command.setDeviceId(1); command.setType(Command.TYPE_ENGINE_STOP); + Context.init(defaultManager); + Assert.assertEquals("(123456789012345AV011)", encoder.encodeCommand(command)); } @@ -29,8 +96,14 @@ public class Tk103ProtocolEncoderTest extends ProtocolTest { command.setDeviceId(1); command.setType(Command.TYPE_POSITION_SINGLE); + Context.init(defaultManager); + Assert.assertEquals("(123456789012345AP00)", encoder.encodeCommand(command)); + Context.init(t580wManager); + + Assert.assertEquals("[begin]sms2,*getposl*,[end]", encoder.encodeCommand(command)); + } @Test @@ -43,8 +116,14 @@ public class Tk103ProtocolEncoderTest extends ProtocolTest { command.setType(Command.TYPE_POSITION_PERIODIC); command.set(Command.KEY_FREQUENCY, 60); + Context.init(defaultManager); + Assert.assertEquals("(123456789012345AR00003C0000)", encoder.encodeCommand(command)); + Context.init(t580wManager); + + Assert.assertEquals("[begin]sms2,*routetrack*99*,[end]", encoder.encodeCommand(command)); + } @Test @@ -56,8 +135,14 @@ public class Tk103ProtocolEncoderTest extends ProtocolTest { command.setDeviceId(1); command.setType(Command.TYPE_POSITION_STOP); + Context.init(defaultManager); + Assert.assertEquals("(123456789012345AR0000000000)", encoder.encodeCommand(command)); + Context.init(t580wManager); + + Assert.assertEquals("[begin]sms2,*routetrackoff*,[end]", encoder.encodeCommand(command)); + } @Test @@ -69,8 +154,14 @@ public class Tk103ProtocolEncoderTest extends ProtocolTest { command.setDeviceId(1); command.setType(Command.TYPE_GET_VERSION); + Context.init(defaultManager); + Assert.assertEquals("(123456789012345AP07)", encoder.encodeCommand(command)); + Context.init(t580wManager); + + Assert.assertEquals("[begin]sms2,*about*,[end]", encoder.encodeCommand(command)); + } @Test @@ -82,8 +173,14 @@ public class Tk103ProtocolEncoderTest extends ProtocolTest { command.setDeviceId(1); command.setType(Command.TYPE_REBOOT_DEVICE); + Context.init(defaultManager); + Assert.assertEquals("(123456789012345AT00)", encoder.encodeCommand(command)); + Context.init(t580wManager); + + Assert.assertEquals("[begin]sms2,88888888,[end]", encoder.encodeCommand(command)); + } @Test @@ -95,20 +192,24 @@ public class Tk103ProtocolEncoderTest extends ProtocolTest { command.setDeviceId(1); command.setType(Command.TYPE_SET_ODOMETER); + Context.init(defaultManager); + Assert.assertEquals("(123456789012345AX01)", encoder.encodeCommand(command)); } @Test - public void testEncodeT580WRebootDevice() throws Exception { + public void testEncodeT580WIdentification() throws Exception { Tk103ProtocolEncoder encoder = new Tk103ProtocolEncoder(); Command command = new Command(); command.setDeviceId(1); - command.setType("T580W_rebootDevice"); + command.setType(Command.TYPE_IDENTIFICATION); - Assert.assertEquals("[begin]sms2,88888888,[end]", encoder.encodeCommand(command)); + Context.init(t580wManager); + + Assert.assertEquals("[begin]sms2,999999,[end]", encoder.encodeCommand(command)); } @@ -119,7 +220,10 @@ public class Tk103ProtocolEncoderTest extends ProtocolTest { Command command = new Command(); command.setDeviceId(1); - command.setType("T580W_alarmSosOn"); + command.setType(Command.TYPE_ALARM_SOS); + command.set(Command.KEY_ENABLE, true); + + Context.init(t580wManager); Assert.assertEquals("[begin]sms2,*soson*,[end]", encoder.encodeCommand(command)); @@ -132,22 +236,62 @@ public class Tk103ProtocolEncoderTest extends ProtocolTest { Command command = new Command(); command.setDeviceId(1); - command.setType("T580W_alarmSosOff"); + command.setType(Command.TYPE_ALARM_SOS); + command.set(Command.KEY_ENABLE, false); + + Context.init(t580wManager); Assert.assertEquals("[begin]sms2,*sosoff*,[end]", encoder.encodeCommand(command)); } @Test - public void testEncodeT580WRouteTrack() throws Exception { + public void testEncodeT580WCustom() throws Exception { Tk103ProtocolEncoder encoder = new Tk103ProtocolEncoder(); Command command = new Command(); command.setDeviceId(1); - command.setType("T580W_positionRealtime"); + command.setType(Command.TYPE_CUSTOM); + command.set(Command.KEY_DATA, "any text is ok"); - Assert.assertEquals("[begin]sms2,*routetrack*99*,[end]", encoder.encodeCommand(command)); + Context.init(t580wManager); + + Assert.assertEquals("[begin]sms2,any text is ok,[end]", encoder.encodeCommand(command)); + + } + + @Test + public void testEncodeT580WSetConnection() throws Exception { + + Tk103ProtocolEncoder encoder = new Tk103ProtocolEncoder(); + + Command command = new Command(); + command.setDeviceId(1); + command.setType(Command.TYPE_SET_CONNECTION); + command.set(Command.KEY_SERVER, "1.2.3.4"); + command.set(Command.KEY_PORT, "5555"); + + Context.init(t580wManager); + + Assert.assertEquals("[begin]sms2,*setip*1*2*3*4*5555*,[end]", encoder.encodeCommand(command)); + + } + + @Test + public void testEncodeT580WSosNumber() throws Exception { + + Tk103ProtocolEncoder encoder = new Tk103ProtocolEncoder(); + + Command command = new Command(); + command.setDeviceId(1); + command.setType(Command.TYPE_SOS_NUMBER); + command.set(Command.KEY_INDEX, "0"); + command.set(Command.KEY_PHONE, "+55555555555"); + + Context.init(t580wManager); + + Assert.assertEquals("[begin]sms2,*master*654321*+55555555555*,[end]", encoder.encodeCommand(command)); } -- cgit v1.2.3 From eca7ba9dcedb01c98b6945890f45f2b19bf049a7 Mon Sep 17 00:00:00 2001 From: Valerii Vyshniak Date: Mon, 20 Nov 2017 02:59:50 +0100 Subject: T580W: build fix --- .../traccar/protocol/Tk103ProtocolEncoderTest.java | 96 +++++++++++++++------- 1 file changed, 66 insertions(+), 30 deletions(-) (limited to 'test/org/traccar') diff --git a/test/org/traccar/protocol/Tk103ProtocolEncoderTest.java b/test/org/traccar/protocol/Tk103ProtocolEncoderTest.java index 2e5b66572..4afb7b0e4 100644 --- a/test/org/traccar/protocol/Tk103ProtocolEncoderTest.java +++ b/test/org/traccar/protocol/Tk103ProtocolEncoderTest.java @@ -11,11 +11,6 @@ import org.traccar.model.Position; public class Tk103ProtocolEncoderTest extends ProtocolTest { - private static IdentityManager defaultManager; - static { - defaultManager = Context.getIdentityManager(); - } - private static IdentityManager t580wManager = new IdentityManager() { private Device createDevice() { @@ -81,8 +76,6 @@ public class Tk103ProtocolEncoderTest extends ProtocolTest { command.setDeviceId(1); command.setType(Command.TYPE_ENGINE_STOP); - Context.init(defaultManager); - Assert.assertEquals("(123456789012345AV011)", encoder.encodeCommand(command)); } @@ -96,13 +89,16 @@ public class Tk103ProtocolEncoderTest extends ProtocolTest { command.setDeviceId(1); command.setType(Command.TYPE_POSITION_SINGLE); - Context.init(defaultManager); - Assert.assertEquals("(123456789012345AP00)", encoder.encodeCommand(command)); + IdentityManager old = Context.getIdentityManager(); Context.init(t580wManager); - Assert.assertEquals("[begin]sms2,*getposl*,[end]", encoder.encodeCommand(command)); + try { + Assert.assertEquals("[begin]sms2,*getposl*,[end]", encoder.encodeCommand(command)); + } finally { + Context.init(old); + } } @@ -116,13 +112,16 @@ public class Tk103ProtocolEncoderTest extends ProtocolTest { command.setType(Command.TYPE_POSITION_PERIODIC); command.set(Command.KEY_FREQUENCY, 60); - Context.init(defaultManager); - Assert.assertEquals("(123456789012345AR00003C0000)", encoder.encodeCommand(command)); + IdentityManager old = Context.getIdentityManager(); Context.init(t580wManager); - Assert.assertEquals("[begin]sms2,*routetrack*99*,[end]", encoder.encodeCommand(command)); + try { + Assert.assertEquals("[begin]sms2,*routetrack*99*,[end]", encoder.encodeCommand(command)); + } finally { + Context.init(old); + } } @@ -135,13 +134,16 @@ public class Tk103ProtocolEncoderTest extends ProtocolTest { command.setDeviceId(1); command.setType(Command.TYPE_POSITION_STOP); - Context.init(defaultManager); - Assert.assertEquals("(123456789012345AR0000000000)", encoder.encodeCommand(command)); + IdentityManager old = Context.getIdentityManager(); Context.init(t580wManager); - Assert.assertEquals("[begin]sms2,*routetrackoff*,[end]", encoder.encodeCommand(command)); + try { + Assert.assertEquals("[begin]sms2,*routetrackoff*,[end]", encoder.encodeCommand(command)); + } finally { + Context.init(old); + } } @@ -154,13 +156,16 @@ public class Tk103ProtocolEncoderTest extends ProtocolTest { command.setDeviceId(1); command.setType(Command.TYPE_GET_VERSION); - Context.init(defaultManager); - Assert.assertEquals("(123456789012345AP07)", encoder.encodeCommand(command)); + IdentityManager old = Context.getIdentityManager(); Context.init(t580wManager); - Assert.assertEquals("[begin]sms2,*about*,[end]", encoder.encodeCommand(command)); + try { + Assert.assertEquals("[begin]sms2,*about*,[end]", encoder.encodeCommand(command)); + } finally { + Context.init(old); + } } @@ -173,13 +178,16 @@ public class Tk103ProtocolEncoderTest extends ProtocolTest { command.setDeviceId(1); command.setType(Command.TYPE_REBOOT_DEVICE); - Context.init(defaultManager); - Assert.assertEquals("(123456789012345AT00)", encoder.encodeCommand(command)); + IdentityManager old = Context.getIdentityManager(); Context.init(t580wManager); - Assert.assertEquals("[begin]sms2,88888888,[end]", encoder.encodeCommand(command)); + try { + Assert.assertEquals("[begin]sms2,88888888,[end]", encoder.encodeCommand(command)); + } finally { + Context.init(old); + } } @@ -192,8 +200,6 @@ public class Tk103ProtocolEncoderTest extends ProtocolTest { command.setDeviceId(1); command.setType(Command.TYPE_SET_ODOMETER); - Context.init(defaultManager); - Assert.assertEquals("(123456789012345AX01)", encoder.encodeCommand(command)); } @@ -207,9 +213,14 @@ public class Tk103ProtocolEncoderTest extends ProtocolTest { command.setDeviceId(1); command.setType(Command.TYPE_IDENTIFICATION); + IdentityManager old = Context.getIdentityManager(); Context.init(t580wManager); - Assert.assertEquals("[begin]sms2,999999,[end]", encoder.encodeCommand(command)); + try { + Assert.assertEquals("[begin]sms2,999999,[end]", encoder.encodeCommand(command)); + } finally { + Context.init(old); + } } @@ -223,9 +234,14 @@ public class Tk103ProtocolEncoderTest extends ProtocolTest { command.setType(Command.TYPE_ALARM_SOS); command.set(Command.KEY_ENABLE, true); + IdentityManager old = Context.getIdentityManager(); Context.init(t580wManager); - Assert.assertEquals("[begin]sms2,*soson*,[end]", encoder.encodeCommand(command)); + try { + Assert.assertEquals("[begin]sms2,*soson*,[end]", encoder.encodeCommand(command)); + } finally { + Context.init(old); + } } @@ -239,9 +255,14 @@ public class Tk103ProtocolEncoderTest extends ProtocolTest { command.setType(Command.TYPE_ALARM_SOS); command.set(Command.KEY_ENABLE, false); + IdentityManager old = Context.getIdentityManager(); Context.init(t580wManager); - Assert.assertEquals("[begin]sms2,*sosoff*,[end]", encoder.encodeCommand(command)); + try { + Assert.assertEquals("[begin]sms2,*sosoff*,[end]", encoder.encodeCommand(command)); + } finally { + Context.init(old); + } } @@ -255,9 +276,14 @@ public class Tk103ProtocolEncoderTest extends ProtocolTest { command.setType(Command.TYPE_CUSTOM); command.set(Command.KEY_DATA, "any text is ok"); + IdentityManager old = Context.getIdentityManager(); Context.init(t580wManager); - Assert.assertEquals("[begin]sms2,any text is ok,[end]", encoder.encodeCommand(command)); + try { + Assert.assertEquals("[begin]sms2,any text is ok,[end]", encoder.encodeCommand(command)); + } finally { + Context.init(old); + } } @@ -272,9 +298,14 @@ public class Tk103ProtocolEncoderTest extends ProtocolTest { command.set(Command.KEY_SERVER, "1.2.3.4"); command.set(Command.KEY_PORT, "5555"); + IdentityManager old = Context.getIdentityManager(); Context.init(t580wManager); - Assert.assertEquals("[begin]sms2,*setip*1*2*3*4*5555*,[end]", encoder.encodeCommand(command)); + try { + Assert.assertEquals("[begin]sms2,*setip*1*2*3*4*5555*,[end]", encoder.encodeCommand(command)); + } finally { + Context.init(old); + } } @@ -289,9 +320,14 @@ public class Tk103ProtocolEncoderTest extends ProtocolTest { command.set(Command.KEY_INDEX, "0"); command.set(Command.KEY_PHONE, "+55555555555"); + IdentityManager old = Context.getIdentityManager(); Context.init(t580wManager); - Assert.assertEquals("[begin]sms2,*master*654321*+55555555555*,[end]", encoder.encodeCommand(command)); + try { + Assert.assertEquals("[begin]sms2,*master*654321*+55555555555*,[end]", encoder.encodeCommand(command)); + } finally { + Context.init(old); + } } -- cgit v1.2.3 From 8d8bab8a2df30629fb612b0573b2bd5f46c4b05e Mon Sep 17 00:00:00 2001 From: Valerii Vyshniak Date: Mon, 20 Nov 2017 23:06:09 +0100 Subject: T580W: review comments fixes 2 --- src/org/traccar/protocol/Tk103ProtocolEncoder.java | 121 ++++++-------- test/org/traccar/BaseTest.java | 2 +- test/org/traccar/FilterHandlerTest.java | 2 +- .../traccar/protocol/Tk103ProtocolDecoderTest.java | 16 +- .../traccar/protocol/Tk103ProtocolEncoderTest.java | 183 ++++----------------- 5 files changed, 88 insertions(+), 236 deletions(-) (limited to 'test/org/traccar') diff --git a/src/org/traccar/protocol/Tk103ProtocolEncoder.java b/src/org/traccar/protocol/Tk103ProtocolEncoder.java index e43761c7c..26b966d48 100644 --- a/src/org/traccar/protocol/Tk103ProtocolEncoder.java +++ b/src/org/traccar/protocol/Tk103ProtocolEncoder.java @@ -23,82 +23,61 @@ import org.traccar.model.Command; public class Tk103ProtocolEncoder extends StringProtocolEncoder { - protected String t580WFormatCommand(Command command, String format, String... keys) { - String content = super.formatCommand(command, format, keys); - return String.format("[begin]sms2,%s,[end]", content); - } - - protected String checkEn(Command command, String ifEnabled, String ifDisabled) { - return command.getBoolean(Command.KEY_ENABLE) ? ifEnabled : ifDisabled; - } - @Override protected Object encodeCommand(Command command) { - boolean deviceT580W = Context.getIdentityManager().lookupAttributeBoolean( - command.getDeviceId(), "tk103.deviceT580W", false, true); - - if (deviceT580W) { + boolean alternative = Context.getIdentityManager().lookupAttributeBoolean( + command.getDeviceId(), "tk103.alternative", false, true); - initDevicePassword(command, "123456"); + initDevicePassword(command, "123456"); - switch (command.getType()) { - case Command.TYPE_CUSTOM: - return t580WFormatCommand(command, "{%s}", Command.KEY_DATA); - case Command.TYPE_GET_DEVICE_STATUS: - return t580WFormatCommand(command, "*status*"); - case Command.TYPE_GET_VERSION: - return t580WFormatCommand(command, "*about*"); - case Command.TYPE_IDENTIFICATION: - return t580WFormatCommand(command, "999999"); - case Command.TYPE_REBOOT_DEVICE: - return t580WFormatCommand(command, "88888888"); - case Command.TYPE_POSITION_SINGLE: - return t580WFormatCommand(command, "*getposl*"); - case Command.TYPE_POSITION_PERIODIC: - return t580WFormatCommand(command, "*routetrack*99*"); - case Command.TYPE_POSITION_STOP: - return t580WFormatCommand(command, "*routetrackoff*"); - case Command.TYPE_MODE_DEEP_SLEEP: - return t580WFormatCommand(command, checkEn(command, "*sleep*2*", "*sleepoff*")); - case Command.TYPE_MODE_POWER_SAVING: - return t580WFormatCommand(command, checkEn(command, "*sleepv*", "*sleepoff*")); - case Command.TYPE_ALARM_SOS: - return t580WFormatCommand(command, checkEn(command, "*soson*", "*sosoff*")); - case Command.TYPE_SET_CONNECTION: - String ipAddress = command.getString(Command.KEY_SERVER); - ipAddress = ipAddress.replace(".", "*"); - return t580WFormatCommand(command, "*setip*%s*{%s}*", ipAddress, Command.KEY_PORT); - case Command.TYPE_SOS_NUMBER: - return t580WFormatCommand(command, "*master*{%s}*{%s}*", - Command.KEY_DEVICE_PASSWORD, Command.KEY_PHONE); - default: - Log.warning(new UnsupportedOperationException(command.getType())); - break; - } - } else { - switch (command.getType()) { - case Command.TYPE_GET_VERSION: - return formatCommand(command, "({%s}AP07)", Command.KEY_UNIQUE_ID); - case Command.TYPE_REBOOT_DEVICE: - return formatCommand(command, "({%s}AT00)", Command.KEY_UNIQUE_ID); - case Command.TYPE_SET_ODOMETER: - return formatCommand(command, "({%s}AX01)", Command.KEY_UNIQUE_ID); - case Command.TYPE_POSITION_SINGLE: - return formatCommand(command, "({%s}AP00)", Command.KEY_UNIQUE_ID); - case Command.TYPE_POSITION_PERIODIC: - return formatCommand(command, "({%s}AR00%s0000)", Command.KEY_UNIQUE_ID, - String.format("%04X", command.getInteger(Command.KEY_FREQUENCY))); - case Command.TYPE_POSITION_STOP: - return formatCommand(command, "({%s}AR0000000000)", Command.KEY_UNIQUE_ID); - case Command.TYPE_ENGINE_STOP: - return formatCommand(command, "({%s}AV011)", Command.KEY_UNIQUE_ID); - case Command.TYPE_ENGINE_RESUME: - return formatCommand(command, "({%s}AV010)", Command.KEY_UNIQUE_ID); - default: - Log.warning(new UnsupportedOperationException(command.getType())); - break; - } + switch (command.getType()) { + case Command.TYPE_GET_VERSION: + return alternative ? formatCommand(command, "[begin]sms2,*about*,[end]") + : formatCommand(command, "({%s}AP07)", Command.KEY_UNIQUE_ID); + case Command.TYPE_REBOOT_DEVICE: + return alternative ? formatCommand(command, "[begin]sms2,88888888,[end]") + : formatCommand(command, "({%s}AT00)", Command.KEY_UNIQUE_ID); + case Command.TYPE_SET_ODOMETER: + return formatCommand(command, "({%s}AX01)", Command.KEY_UNIQUE_ID); + case Command.TYPE_POSITION_SINGLE: + return alternative ? formatCommand(command, "[begin]sms2,*getposl*,[end]") + : formatCommand(command, "({%s}AP00)", Command.KEY_UNIQUE_ID); + case Command.TYPE_POSITION_PERIODIC: + return alternative ? formatCommand(command, "[begin]sms2,*routetrack*99*,[end]") + : formatCommand(command, "({%s}AR00%s0000)", Command.KEY_UNIQUE_ID, + String.format("%04X", command.getInteger(Command.KEY_FREQUENCY))); + case Command.TYPE_POSITION_STOP: + return alternative ? formatCommand(command, "[begin]sms2,*routetrackoff*,[end]") + : formatCommand(command, "({%s}AR0000000000)", Command.KEY_UNIQUE_ID); + case Command.TYPE_ENGINE_STOP: + return formatCommand(command, "({%s}AV011)", Command.KEY_UNIQUE_ID); + case Command.TYPE_ENGINE_RESUME: + return formatCommand(command, "({%s}AV010)", Command.KEY_UNIQUE_ID); + case Command.TYPE_CUSTOM: + return formatCommand(command, "[begin]sms2,{%s},[end]", Command.KEY_DATA); + case Command.TYPE_GET_DEVICE_STATUS: + return formatCommand(command, "[begin]sms2,*status*,[end]"); + case Command.TYPE_IDENTIFICATION: + return formatCommand(command, "[begin]sms2,999999,[end]"); + case Command.TYPE_MODE_DEEP_SLEEP: + return formatCommand(command, command.getBoolean(Command.KEY_ENABLE) + ? "[begin]sms2,*sleep*2*,[end]" : "[begin]sms2,*sleepoff*,[end]"); + case Command.TYPE_MODE_POWER_SAVING: + return formatCommand(command, command.getBoolean(Command.KEY_ENABLE) + ? "[begin]sms2,*sleepv*,[end]" : "[begin]sms2,*sleepoff*,[end]"); + case Command.TYPE_ALARM_SOS: + return formatCommand(command, command.getBoolean(Command.KEY_ENABLE) + ? "[begin]sms2,*soson*,[end]" : "[begin]sms2,*sosoff*,[end]"); + case Command.TYPE_SET_CONNECTION: + return formatCommand(command, "[begin]sms2,*setip*%s*{%s}*,[end]", + command.getString(Command.KEY_SERVER).replace(".", "*"), Command.KEY_PORT); + case Command.TYPE_SOS_NUMBER: + return formatCommand(command, "[begin]sms2,*master*{%s}*{%s}*,[end]", + Command.KEY_DEVICE_PASSWORD, Command.KEY_PHONE); + default: + Log.warning(new UnsupportedOperationException(command.getType())); + break; } return null; diff --git a/test/org/traccar/BaseTest.java b/test/org/traccar/BaseTest.java index 4b9ee5451..87ff3842c 100644 --- a/test/org/traccar/BaseTest.java +++ b/test/org/traccar/BaseTest.java @@ -40,7 +40,7 @@ public class BaseTest { @Override public boolean lookupAttributeBoolean( long deviceId, String attributeName, boolean defaultValue, boolean lookupConfig) { - return defaultValue; + return (deviceId == 2 && attributeName == "tk103.alternative" && lookupConfig) ? true : defaultValue; } @Override diff --git a/test/org/traccar/FilterHandlerTest.java b/test/org/traccar/FilterHandlerTest.java index 7ebab3af5..e896aa450 100644 --- a/test/org/traccar/FilterHandlerTest.java +++ b/test/org/traccar/FilterHandlerTest.java @@ -48,7 +48,7 @@ public class FilterHandlerTest { @Override public boolean lookupAttributeBoolean( long deviceId, String attributeName, boolean defaultValue, boolean lookupConfig) { - return defaultValue; + return (deviceId == 2 && attributeName == "tk103.alternative" && lookupConfig) ? true : defaultValue; } @Override diff --git a/test/org/traccar/protocol/Tk103ProtocolDecoderTest.java b/test/org/traccar/protocol/Tk103ProtocolDecoderTest.java index b8fe872ee..74edd10ab 100644 --- a/test/org/traccar/protocol/Tk103ProtocolDecoderTest.java +++ b/test/org/traccar/protocol/Tk103ProtocolDecoderTest.java @@ -189,17 +189,17 @@ public class Tk103ProtocolDecoderTest extends ProtocolTest { verifyNotNull(decoder, text( "(864555555555555,ZC03,191117,234207,$1 .Sensor sensitivity: 1\r\n" + - "2 .Alert status: Off\r\n" + - "3 .Check interval is set to 240 minute(s).\r\n" + - "4 .Checkgsm interval is set to 60 minute(s).\r\n" + - "5 .SOS SMS Alert: On\r\n" + - "6 .SOS Call Alert: On\r\n" + - "7 . Power: 95%$")); + "2 .Alert status: Off\r\n" + + "3 .Check interval is set to 240 minute(s).\r\n" + + "4 .Checkgsm interval is set to 60 minute(s).\r\n" + + "5 .SOS SMS Alert: On\r\n" + + "6 .SOS Call Alert: On\r\n" + + "7 . Power: 95%$")); verifyNotNull(decoder, text( "(864555555555555,ZC03,191117,234207,$1 .Sensor sensitivity: 1\r\n" + - "2 .Alert status: Off\r\n" + - "3 .Check interval is set to 240 minute(s")); + "2 .Alert status: Off\r\n" + + "3 .Check interval is set to 240 minute(s")); } diff --git a/test/org/traccar/protocol/Tk103ProtocolEncoderTest.java b/test/org/traccar/protocol/Tk103ProtocolEncoderTest.java index 4afb7b0e4..f902268ce 100644 --- a/test/org/traccar/protocol/Tk103ProtocolEncoderTest.java +++ b/test/org/traccar/protocol/Tk103ProtocolEncoderTest.java @@ -2,71 +2,11 @@ package org.traccar.protocol; import org.junit.Assert; import org.junit.Test; -import org.traccar.Context; import org.traccar.ProtocolTest; -import org.traccar.database.IdentityManager; import org.traccar.model.Command; -import org.traccar.model.Device; -import org.traccar.model.Position; public class Tk103ProtocolEncoderTest extends ProtocolTest { - private static IdentityManager t580wManager = new IdentityManager() { - - private Device createDevice() { - Device device = new Device(); - device.setId(1); - device.setName("test"); - device.setUniqueId("123456789012345"); - device.set(Command.KEY_DEVICE_PASSWORD, "654321"); - return device; - } - - @Override - public Device getById(long id) { - return createDevice(); - } - - @Override - public Device getByUniqueId(String uniqueId) { - return createDevice(); - } - - @Override - public Position getLastPosition(long deviceId) { - return null; - } - - @Override - public boolean isLatestPosition(Position position) { - return true; - } - - @Override - public boolean lookupAttributeBoolean( - long deviceId, String attributeName, boolean defaultValue, boolean lookupConfig) { - return deviceId == 1 && attributeName == "tk103.deviceT580W" && lookupConfig ? true : defaultValue; - } - - @Override - public String lookupAttributeString( - long deviceId, String attributeName, String defaultValue, boolean lookupConfig) { - return defaultValue; - } - - @Override - public int lookupAttributeInteger( - long deviceId, String attributeName, int defaultValue, boolean lookupConfig) { - return defaultValue; - } - - @Override - public long lookupAttributeLong( - long deviceId, String attributeName, long defaultValue, boolean lookupConfig) { - return defaultValue; - } - }; - @Test public void testEncodeEngineStop() throws Exception { @@ -91,14 +31,9 @@ public class Tk103ProtocolEncoderTest extends ProtocolTest { Assert.assertEquals("(123456789012345AP00)", encoder.encodeCommand(command)); - IdentityManager old = Context.getIdentityManager(); - Context.init(t580wManager); + command.setDeviceId(2); - try { - Assert.assertEquals("[begin]sms2,*getposl*,[end]", encoder.encodeCommand(command)); - } finally { - Context.init(old); - } + Assert.assertEquals("[begin]sms2,*getposl*,[end]", encoder.encodeCommand(command)); } @@ -114,14 +49,9 @@ public class Tk103ProtocolEncoderTest extends ProtocolTest { Assert.assertEquals("(123456789012345AR00003C0000)", encoder.encodeCommand(command)); - IdentityManager old = Context.getIdentityManager(); - Context.init(t580wManager); + command.setDeviceId(2); - try { - Assert.assertEquals("[begin]sms2,*routetrack*99*,[end]", encoder.encodeCommand(command)); - } finally { - Context.init(old); - } + Assert.assertEquals("[begin]sms2,*routetrack*99*,[end]", encoder.encodeCommand(command)); } @@ -136,14 +66,9 @@ public class Tk103ProtocolEncoderTest extends ProtocolTest { Assert.assertEquals("(123456789012345AR0000000000)", encoder.encodeCommand(command)); - IdentityManager old = Context.getIdentityManager(); - Context.init(t580wManager); + command.setDeviceId(2); - try { - Assert.assertEquals("[begin]sms2,*routetrackoff*,[end]", encoder.encodeCommand(command)); - } finally { - Context.init(old); - } + Assert.assertEquals("[begin]sms2,*routetrackoff*,[end]", encoder.encodeCommand(command)); } @@ -158,14 +83,9 @@ public class Tk103ProtocolEncoderTest extends ProtocolTest { Assert.assertEquals("(123456789012345AP07)", encoder.encodeCommand(command)); - IdentityManager old = Context.getIdentityManager(); - Context.init(t580wManager); + command.setDeviceId(2); - try { - Assert.assertEquals("[begin]sms2,*about*,[end]", encoder.encodeCommand(command)); - } finally { - Context.init(old); - } + Assert.assertEquals("[begin]sms2,*about*,[end]", encoder.encodeCommand(command)); } @@ -180,14 +100,9 @@ public class Tk103ProtocolEncoderTest extends ProtocolTest { Assert.assertEquals("(123456789012345AT00)", encoder.encodeCommand(command)); - IdentityManager old = Context.getIdentityManager(); - Context.init(t580wManager); + command.setDeviceId(2); - try { - Assert.assertEquals("[begin]sms2,88888888,[end]", encoder.encodeCommand(command)); - } finally { - Context.init(old); - } + Assert.assertEquals("[begin]sms2,88888888,[end]", encoder.encodeCommand(command)); } @@ -205,129 +120,87 @@ public class Tk103ProtocolEncoderTest extends ProtocolTest { } @Test - public void testEncodeT580WIdentification() throws Exception { + public void testEncodeIdentification() throws Exception { Tk103ProtocolEncoder encoder = new Tk103ProtocolEncoder(); Command command = new Command(); - command.setDeviceId(1); + command.setDeviceId(2); command.setType(Command.TYPE_IDENTIFICATION); - IdentityManager old = Context.getIdentityManager(); - Context.init(t580wManager); - - try { - Assert.assertEquals("[begin]sms2,999999,[end]", encoder.encodeCommand(command)); - } finally { - Context.init(old); - } + Assert.assertEquals("[begin]sms2,999999,[end]", encoder.encodeCommand(command)); } @Test - public void testEncodeT580WSosOn() throws Exception { + public void testEncodeSosOn() throws Exception { Tk103ProtocolEncoder encoder = new Tk103ProtocolEncoder(); Command command = new Command(); - command.setDeviceId(1); + command.setDeviceId(2); command.setType(Command.TYPE_ALARM_SOS); command.set(Command.KEY_ENABLE, true); - IdentityManager old = Context.getIdentityManager(); - Context.init(t580wManager); - - try { - Assert.assertEquals("[begin]sms2,*soson*,[end]", encoder.encodeCommand(command)); - } finally { - Context.init(old); - } + Assert.assertEquals("[begin]sms2,*soson*,[end]", encoder.encodeCommand(command)); } @Test - public void testEncodeT580WSosOff() throws Exception { + public void testEncodeSosOff() throws Exception { Tk103ProtocolEncoder encoder = new Tk103ProtocolEncoder(); Command command = new Command(); - command.setDeviceId(1); + command.setDeviceId(2); command.setType(Command.TYPE_ALARM_SOS); command.set(Command.KEY_ENABLE, false); - IdentityManager old = Context.getIdentityManager(); - Context.init(t580wManager); - - try { - Assert.assertEquals("[begin]sms2,*sosoff*,[end]", encoder.encodeCommand(command)); - } finally { - Context.init(old); - } + Assert.assertEquals("[begin]sms2,*sosoff*,[end]", encoder.encodeCommand(command)); } @Test - public void testEncodeT580WCustom() throws Exception { + public void testEncodeCustom() throws Exception { Tk103ProtocolEncoder encoder = new Tk103ProtocolEncoder(); Command command = new Command(); - command.setDeviceId(1); + command.setDeviceId(2); command.setType(Command.TYPE_CUSTOM); command.set(Command.KEY_DATA, "any text is ok"); - IdentityManager old = Context.getIdentityManager(); - Context.init(t580wManager); - - try { - Assert.assertEquals("[begin]sms2,any text is ok,[end]", encoder.encodeCommand(command)); - } finally { - Context.init(old); - } + Assert.assertEquals("[begin]sms2,any text is ok,[end]", encoder.encodeCommand(command)); } @Test - public void testEncodeT580WSetConnection() throws Exception { + public void testEncodeSetConnection() throws Exception { Tk103ProtocolEncoder encoder = new Tk103ProtocolEncoder(); Command command = new Command(); - command.setDeviceId(1); + command.setDeviceId(2); command.setType(Command.TYPE_SET_CONNECTION); command.set(Command.KEY_SERVER, "1.2.3.4"); command.set(Command.KEY_PORT, "5555"); - IdentityManager old = Context.getIdentityManager(); - Context.init(t580wManager); - - try { - Assert.assertEquals("[begin]sms2,*setip*1*2*3*4*5555*,[end]", encoder.encodeCommand(command)); - } finally { - Context.init(old); - } + Assert.assertEquals("[begin]sms2,*setip*1*2*3*4*5555*,[end]", encoder.encodeCommand(command)); } @Test - public void testEncodeT580WSosNumber() throws Exception { + public void testEncodeSosNumber() throws Exception { Tk103ProtocolEncoder encoder = new Tk103ProtocolEncoder(); Command command = new Command(); - command.setDeviceId(1); + command.setDeviceId(2); command.setType(Command.TYPE_SOS_NUMBER); command.set(Command.KEY_INDEX, "0"); command.set(Command.KEY_PHONE, "+55555555555"); - IdentityManager old = Context.getIdentityManager(); - Context.init(t580wManager); - - try { - Assert.assertEquals("[begin]sms2,*master*654321*+55555555555*,[end]", encoder.encodeCommand(command)); - } finally { - Context.init(old); - } + Assert.assertEquals("[begin]sms2,*master*123456*+55555555555*,[end]", encoder.encodeCommand(command)); } -- cgit v1.2.3 From e5a8be1987aecf3735801165ee19c1bcdd4fcc05 Mon Sep 17 00:00:00 2001 From: Valerii Vyshniak Date: Wed, 22 Nov 2017 02:05:59 +0100 Subject: T580W: review comments fixes 3, re-implemented tk103 frame decoder --- src/org/traccar/protocol/Tk103FrameDecoder.java | 66 ++++++++++++++++++++++ src/org/traccar/protocol/Tk103Protocol.java | 3 +- src/org/traccar/protocol/Tk103ProtocolDecoder.java | 2 +- test/org/traccar/BaseTest.java | 2 +- test/org/traccar/FilterHandlerTest.java | 2 +- .../traccar/protocol/Tk103ProtocolDecoderTest.java | 1 - .../traccar/protocol/Tk103ProtocolEncoderTest.java | 32 ++--------- 7 files changed, 76 insertions(+), 32 deletions(-) create mode 100644 src/org/traccar/protocol/Tk103FrameDecoder.java (limited to 'test/org/traccar') diff --git a/src/org/traccar/protocol/Tk103FrameDecoder.java b/src/org/traccar/protocol/Tk103FrameDecoder.java new file mode 100644 index 000000000..17e791553 --- /dev/null +++ b/src/org/traccar/protocol/Tk103FrameDecoder.java @@ -0,0 +1,66 @@ +/* + * Copyright 2017 Valerii Vyshniak (val@val.one) + * 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.buffer.ChannelBuffers; +import org.jboss.netty.channel.Channel; +import org.jboss.netty.channel.ChannelHandlerContext; +import org.jboss.netty.handler.codec.frame.FrameDecoder; + +import java.nio.ByteOrder; + +public class Tk103FrameDecoder extends FrameDecoder { + + @Override + protected Object decode( + ChannelHandlerContext ctx, + Channel channel, + ChannelBuffer buf) throws Exception { + + if (buf.readableBytes() < 2) { + return null; + } + + int index; + for (index = buf.readerIndex(); true; index++) { + index = buf.indexOf(index, buf.writerIndex(), (byte) ')'); + if (index == -1) { + break; + } + int cnt = 0; + for (int i = buf.readerIndex(); i < index; i++) { + if (buf.getByte(i) == (byte) '$') { + cnt++; + } + } + if (cnt % 2 == 0) { + break; + } + } + + if (index != -1) { + ChannelBuffer result = ChannelBuffers.buffer(ByteOrder.LITTLE_ENDIAN, index + 1 - buf.readerIndex()); + buf.readBytes(result, index + 1 - buf.readerIndex()); + result.writerIndex(result.writerIndex() - 1); + return result; + } + + return null; + } + +} diff --git a/src/org/traccar/protocol/Tk103Protocol.java b/src/org/traccar/protocol/Tk103Protocol.java index 3360dab32..6ef9c0a56 100644 --- a/src/org/traccar/protocol/Tk103Protocol.java +++ b/src/org/traccar/protocol/Tk103Protocol.java @@ -22,7 +22,6 @@ 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.CharacterDelimiterFrameDecoder; import org.traccar.TrackerServer; import org.traccar.model.Command; @@ -56,7 +55,7 @@ public class Tk103Protocol extends BaseProtocol { serverList.add(new TrackerServer(new ServerBootstrap(), getName()) { @Override protected void addSpecificHandlers(ChannelPipeline pipeline) { - pipeline.addLast("frameDecoder", new CharacterDelimiterFrameDecoder(1024, ')')); + pipeline.addLast("frameDecoder", new Tk103FrameDecoder()); pipeline.addLast("stringDecoder", new StringDecoder()); pipeline.addLast("stringEncoder", new StringEncoder()); pipeline.addLast("objectEncoder", new Tk103ProtocolEncoder()); diff --git a/src/org/traccar/protocol/Tk103ProtocolDecoder.java b/src/org/traccar/protocol/Tk103ProtocolDecoder.java index 2ce7def45..2fc6bf8d5 100644 --- a/src/org/traccar/protocol/Tk103ProtocolDecoder.java +++ b/src/org/traccar/protocol/Tk103ProtocolDecoder.java @@ -353,7 +353,7 @@ public class Tk103ProtocolDecoder extends BaseProtocolDecoder { sentence = sentence.substring(beginIndex + 1); } - if (channel != null && sentence.length() >= 16) { + if (channel != null) { String id = sentence.substring(0, 12); String type = sentence.substring(12, 16); if (type.equals("BP00")) { diff --git a/test/org/traccar/BaseTest.java b/test/org/traccar/BaseTest.java index 87ff3842c..4b9ee5451 100644 --- a/test/org/traccar/BaseTest.java +++ b/test/org/traccar/BaseTest.java @@ -40,7 +40,7 @@ public class BaseTest { @Override public boolean lookupAttributeBoolean( long deviceId, String attributeName, boolean defaultValue, boolean lookupConfig) { - return (deviceId == 2 && attributeName == "tk103.alternative" && lookupConfig) ? true : defaultValue; + return defaultValue; } @Override diff --git a/test/org/traccar/FilterHandlerTest.java b/test/org/traccar/FilterHandlerTest.java index e896aa450..7ebab3af5 100644 --- a/test/org/traccar/FilterHandlerTest.java +++ b/test/org/traccar/FilterHandlerTest.java @@ -48,7 +48,7 @@ public class FilterHandlerTest { @Override public boolean lookupAttributeBoolean( long deviceId, String attributeName, boolean defaultValue, boolean lookupConfig) { - return (deviceId == 2 && attributeName == "tk103.alternative" && lookupConfig) ? true : defaultValue; + return defaultValue; } @Override diff --git a/test/org/traccar/protocol/Tk103ProtocolDecoderTest.java b/test/org/traccar/protocol/Tk103ProtocolDecoderTest.java index 74edd10ab..0ceb3404e 100644 --- a/test/org/traccar/protocol/Tk103ProtocolDecoderTest.java +++ b/test/org/traccar/protocol/Tk103ProtocolDecoderTest.java @@ -159,7 +159,6 @@ public class Tk103ProtocolDecoderTest extends ProtocolTest { verifyPosition(decoder, text( "(094050000111BP05000094050000111150808A3804.2418N04616.7468E000.0201447133.3501000011L0028019DT000)")); - //T580W verifyPosition(decoder, text( "(864555555555555,DW3B,131117,A,5544.02870N,01315.08194E,1.597,223707,291.65,-0.10,4)")); diff --git a/test/org/traccar/protocol/Tk103ProtocolEncoderTest.java b/test/org/traccar/protocol/Tk103ProtocolEncoderTest.java index f902268ce..0c16ccee6 100644 --- a/test/org/traccar/protocol/Tk103ProtocolEncoderTest.java +++ b/test/org/traccar/protocol/Tk103ProtocolEncoderTest.java @@ -31,10 +31,6 @@ public class Tk103ProtocolEncoderTest extends ProtocolTest { Assert.assertEquals("(123456789012345AP00)", encoder.encodeCommand(command)); - command.setDeviceId(2); - - Assert.assertEquals("[begin]sms2,*getposl*,[end]", encoder.encodeCommand(command)); - } @Test @@ -49,10 +45,6 @@ public class Tk103ProtocolEncoderTest extends ProtocolTest { Assert.assertEquals("(123456789012345AR00003C0000)", encoder.encodeCommand(command)); - command.setDeviceId(2); - - Assert.assertEquals("[begin]sms2,*routetrack*99*,[end]", encoder.encodeCommand(command)); - } @Test @@ -66,10 +58,6 @@ public class Tk103ProtocolEncoderTest extends ProtocolTest { Assert.assertEquals("(123456789012345AR0000000000)", encoder.encodeCommand(command)); - command.setDeviceId(2); - - Assert.assertEquals("[begin]sms2,*routetrackoff*,[end]", encoder.encodeCommand(command)); - } @Test @@ -83,10 +71,6 @@ public class Tk103ProtocolEncoderTest extends ProtocolTest { Assert.assertEquals("(123456789012345AP07)", encoder.encodeCommand(command)); - command.setDeviceId(2); - - Assert.assertEquals("[begin]sms2,*about*,[end]", encoder.encodeCommand(command)); - } @Test @@ -100,10 +84,6 @@ public class Tk103ProtocolEncoderTest extends ProtocolTest { Assert.assertEquals("(123456789012345AT00)", encoder.encodeCommand(command)); - command.setDeviceId(2); - - Assert.assertEquals("[begin]sms2,88888888,[end]", encoder.encodeCommand(command)); - } @Test @@ -125,7 +105,7 @@ public class Tk103ProtocolEncoderTest extends ProtocolTest { Tk103ProtocolEncoder encoder = new Tk103ProtocolEncoder(); Command command = new Command(); - command.setDeviceId(2); + command.setDeviceId(1); command.setType(Command.TYPE_IDENTIFICATION); Assert.assertEquals("[begin]sms2,999999,[end]", encoder.encodeCommand(command)); @@ -138,7 +118,7 @@ public class Tk103ProtocolEncoderTest extends ProtocolTest { Tk103ProtocolEncoder encoder = new Tk103ProtocolEncoder(); Command command = new Command(); - command.setDeviceId(2); + command.setDeviceId(1); command.setType(Command.TYPE_ALARM_SOS); command.set(Command.KEY_ENABLE, true); @@ -152,7 +132,7 @@ public class Tk103ProtocolEncoderTest extends ProtocolTest { Tk103ProtocolEncoder encoder = new Tk103ProtocolEncoder(); Command command = new Command(); - command.setDeviceId(2); + command.setDeviceId(1); command.setType(Command.TYPE_ALARM_SOS); command.set(Command.KEY_ENABLE, false); @@ -166,7 +146,7 @@ public class Tk103ProtocolEncoderTest extends ProtocolTest { Tk103ProtocolEncoder encoder = new Tk103ProtocolEncoder(); Command command = new Command(); - command.setDeviceId(2); + command.setDeviceId(1); command.setType(Command.TYPE_CUSTOM); command.set(Command.KEY_DATA, "any text is ok"); @@ -180,7 +160,7 @@ public class Tk103ProtocolEncoderTest extends ProtocolTest { Tk103ProtocolEncoder encoder = new Tk103ProtocolEncoder(); Command command = new Command(); - command.setDeviceId(2); + command.setDeviceId(1); command.setType(Command.TYPE_SET_CONNECTION); command.set(Command.KEY_SERVER, "1.2.3.4"); command.set(Command.KEY_PORT, "5555"); @@ -195,7 +175,7 @@ public class Tk103ProtocolEncoderTest extends ProtocolTest { Tk103ProtocolEncoder encoder = new Tk103ProtocolEncoder(); Command command = new Command(); - command.setDeviceId(2); + command.setDeviceId(1); command.setType(Command.TYPE_SOS_NUMBER); command.set(Command.KEY_INDEX, "0"); command.set(Command.KEY_PHONE, "+55555555555"); -- cgit v1.2.3 From 3efb847ba93f5983cb8235d7b14ae6b116417407 Mon Sep 17 00:00:00 2001 From: Valerii Vyshniak Date: Thu, 23 Nov 2017 12:23:05 +0100 Subject: Added possibility to test with different Device attributes --- test/org/traccar/BaseTest.java | 60 +--------- test/org/traccar/FilterHandlerTest.java | 56 +--------- test/org/traccar/TestIdentityManager.java | 122 +++++++++++++++++++++ .../traccar/protocol/Tk103ProtocolEncoderTest.java | 65 +++++++++-- 4 files changed, 182 insertions(+), 121 deletions(-) create mode 100644 test/org/traccar/TestIdentityManager.java (limited to 'test/org/traccar') diff --git a/test/org/traccar/BaseTest.java b/test/org/traccar/BaseTest.java index 4b9ee5451..fb1b5de8f 100644 --- a/test/org/traccar/BaseTest.java +++ b/test/org/traccar/BaseTest.java @@ -1,67 +1,9 @@ package org.traccar; -import org.traccar.database.IdentityManager; -import org.traccar.model.Device; -import org.traccar.model.Position; - public class BaseTest { static { - Context.init(new IdentityManager() { - - private Device createDevice() { - Device device = new Device(); - device.setId(1); - device.setName("test"); - device.setUniqueId("123456789012345"); - return device; - } - - @Override - public Device getById(long id) { - return createDevice(); - } - - @Override - public Device getByUniqueId(String uniqueId) { - return createDevice(); - } - - @Override - public Position getLastPosition(long deviceId) { - return null; - } - - @Override - public boolean isLatestPosition(Position position) { - return true; - } - - @Override - public boolean lookupAttributeBoolean( - long deviceId, String attributeName, boolean defaultValue, boolean lookupConfig) { - return defaultValue; - } - - @Override - public String lookupAttributeString( - long deviceId, String attributeName, String defaultValue, boolean lookupConfig) { - return defaultValue; - } - - @Override - public int lookupAttributeInteger( - long deviceId, String attributeName, int defaultValue, boolean lookupConfig) { - return defaultValue; - } - - @Override - public long lookupAttributeLong( - long deviceId, String attributeName, long defaultValue, boolean lookupConfig) { - return defaultValue; - } - - }); + Context.init(new TestIdentityManager()); } } diff --git a/test/org/traccar/FilterHandlerTest.java b/test/org/traccar/FilterHandlerTest.java index 7ebab3af5..0d488a7fb 100644 --- a/test/org/traccar/FilterHandlerTest.java +++ b/test/org/traccar/FilterHandlerTest.java @@ -15,61 +15,7 @@ import static org.junit.Assert.assertNull; public class FilterHandlerTest { static { - Context.init(new IdentityManager() { - - private Device createDevice() { - Device device = new Device(); - device.setId(1); - device.setName("test"); - device.setUniqueId("123456789012345"); - return device; - } - - @Override - public Device getById(long id) { - return createDevice(); - } - - @Override - public Device getByUniqueId(String uniqueId) { - return createDevice(); - } - - @Override - public Position getLastPosition(long deviceId) { - return null; - } - - @Override - public boolean isLatestPosition(Position position) { - return true; - } - - @Override - public boolean lookupAttributeBoolean( - long deviceId, String attributeName, boolean defaultValue, boolean lookupConfig) { - return defaultValue; - } - - @Override - public String lookupAttributeString( - long deviceId, String attributeName, String defaultValue, boolean lookupConfig) { - return "alarm,result"; - } - - @Override - public int lookupAttributeInteger( - long deviceId, String attributeName, int defaultValue, boolean lookupConfig) { - return defaultValue; - } - - @Override - public long lookupAttributeLong( - long deviceId, String attributeName, long defaultValue, boolean lookupConfig) { - return defaultValue; - } - - }); + Context.init(new TestIdentityManager()); } private FilterHandler filtingHandler; diff --git a/test/org/traccar/TestIdentityManager.java b/test/org/traccar/TestIdentityManager.java new file mode 100644 index 000000000..82bcf5c63 --- /dev/null +++ b/test/org/traccar/TestIdentityManager.java @@ -0,0 +1,122 @@ +package org.traccar; + +import org.traccar.database.*; +import org.traccar.model.*; +import java.util.concurrent.ConcurrentHashMap; + +import java.util.Map; + +public final class TestIdentityManager implements IdentityManager { + + private static Map devicesById = new ConcurrentHashMap<>(); + + private static final long TEST_DEVICE_ID_START = 100; + private static final String TEST_DEVICE_DEFAULT_UNIQUE_ID_PREFIX = "555"; + private static long newDeviceId = TEST_DEVICE_ID_START; + + public TestIdentityManager() { + } + + public static Device createTestDevice() { + Device device = new Device(); + device.setId(newDeviceId); + device.setName("TestDevice" + newDeviceId); + device.setUniqueId(TEST_DEVICE_DEFAULT_UNIQUE_ID_PREFIX + newDeviceId); + devicesById.put(newDeviceId, device); + newDeviceId++; + return device; + } + + private Device createDefaultTestDevice() { + Device device = new Device(); + device.setId(1); + device.setName("test"); + device.setUniqueId("123456789012345"); + return device; + } + + @Override + public Device getById(long id) { + return id < TEST_DEVICE_ID_START ? createDefaultTestDevice() : devicesById.get(id); + } + + @Override + public Device getByUniqueId(String uniqueId) { + if (uniqueId.length() > TEST_DEVICE_DEFAULT_UNIQUE_ID_PREFIX.length() + && uniqueId.substring(0, TEST_DEVICE_DEFAULT_UNIQUE_ID_PREFIX.length()) + .equals(TEST_DEVICE_DEFAULT_UNIQUE_ID_PREFIX)) { + try { + long id = Long.parseLong( + uniqueId.substring(TEST_DEVICE_DEFAULT_UNIQUE_ID_PREFIX.length(), uniqueId.length())); + if (id >= TEST_DEVICE_ID_START) { + return devicesById.get(id); + } + } catch (NumberFormatException e) {} + } + return createDefaultTestDevice(); + } + + @Override + public Position getLastPosition(long deviceId) { + return null; + } + + @Override + public boolean isLatestPosition(Position position) { + return true; + } + + @Override + public boolean lookupAttributeBoolean( + long deviceId, String attributeName, boolean defaultValue, boolean lookupConfig) { + if (deviceId < TEST_DEVICE_ID_START) { + return defaultValue; + } + + Object result = devicesById.get(deviceId).getAttributes().get(attributeName); + if (result != null) { + return result instanceof String ? Boolean.parseBoolean((String) result) : (Boolean) result; + } + return defaultValue; + } + + @Override + public String lookupAttributeString( + long deviceId, String attributeName, String defaultValue, boolean lookupConfig) { + if (deviceId < TEST_DEVICE_ID_START) { + return "alarm,result"; + } + + Object result = devicesById.get(deviceId).getAttributes().get(attributeName); + return result != null ? (String) result : defaultValue; + } + + @Override + public int lookupAttributeInteger( + long deviceId, String attributeName, int defaultValue, boolean lookupConfig) { + if (deviceId < TEST_DEVICE_ID_START) { + return defaultValue; + } + + Object result = devicesById.get(deviceId).getAttributes().get(attributeName); + if (result != null) { + return result instanceof String ? Integer.parseInt((String) result) : ((Number) result).intValue(); + } + return defaultValue; + } + + @Override + public long lookupAttributeLong( + long deviceId, String attributeName, long defaultValue, boolean lookupConfig) { + if (deviceId < TEST_DEVICE_ID_START) { + return defaultValue; + } + + Object result = devicesById.get(deviceId).getAttributes().get(attributeName); + if (result != null) { + return result instanceof String ? Long.parseLong((String) result) : ((Number) result).longValue(); + } + return defaultValue; + } + +} diff --git a/test/org/traccar/protocol/Tk103ProtocolEncoderTest.java b/test/org/traccar/protocol/Tk103ProtocolEncoderTest.java index 0c16ccee6..a58cd9ee1 100644 --- a/test/org/traccar/protocol/Tk103ProtocolEncoderTest.java +++ b/test/org/traccar/protocol/Tk103ProtocolEncoderTest.java @@ -3,7 +3,9 @@ package org.traccar.protocol; import org.junit.Assert; import org.junit.Test; import org.traccar.ProtocolTest; +import org.traccar.TestIdentityManager; import org.traccar.model.Command; +import org.traccar.model.Device; public class Tk103ProtocolEncoderTest extends ProtocolTest { @@ -31,6 +33,12 @@ public class Tk103ProtocolEncoderTest extends ProtocolTest { Assert.assertEquals("(123456789012345AP00)", encoder.encodeCommand(command)); + Device device = TestIdentityManager.createTestDevice(); + device.set("tk103.alternative", true); + command.setDeviceId(device.getId()); + + Assert.assertEquals("[begin]sms2,*getposl*,[end]", encoder.encodeCommand(command)); + } @Test @@ -45,6 +53,12 @@ public class Tk103ProtocolEncoderTest extends ProtocolTest { Assert.assertEquals("(123456789012345AR00003C0000)", encoder.encodeCommand(command)); + Device device = TestIdentityManager.createTestDevice(); + device.set("tk103.alternative", true); + command.setDeviceId(device.getId()); + + Assert.assertEquals("[begin]sms2,*routetrack*99*,[end]", encoder.encodeCommand(command)); + } @Test @@ -58,6 +72,12 @@ public class Tk103ProtocolEncoderTest extends ProtocolTest { Assert.assertEquals("(123456789012345AR0000000000)", encoder.encodeCommand(command)); + Device device = TestIdentityManager.createTestDevice(); + device.set("tk103.alternative", true); + command.setDeviceId(device.getId()); + + Assert.assertEquals("[begin]sms2,*routetrackoff*,[end]", encoder.encodeCommand(command)); + } @Test @@ -71,6 +91,12 @@ public class Tk103ProtocolEncoderTest extends ProtocolTest { Assert.assertEquals("(123456789012345AP07)", encoder.encodeCommand(command)); + Device device = TestIdentityManager.createTestDevice(); + device.set("tk103.alternative", true); + command.setDeviceId(device.getId()); + + Assert.assertEquals("[begin]sms2,*about*,[end]", encoder.encodeCommand(command)); + } @Test @@ -84,6 +110,12 @@ public class Tk103ProtocolEncoderTest extends ProtocolTest { Assert.assertEquals("(123456789012345AT00)", encoder.encodeCommand(command)); + Device device = TestIdentityManager.createTestDevice(); + device.set("tk103.alternative", true); + command.setDeviceId(device.getId()); + + Assert.assertEquals("[begin]sms2,88888888,[end]", encoder.encodeCommand(command)); + } @Test @@ -104,8 +136,11 @@ public class Tk103ProtocolEncoderTest extends ProtocolTest { Tk103ProtocolEncoder encoder = new Tk103ProtocolEncoder(); + Device device = TestIdentityManager.createTestDevice(); + device.set("tk103.alternative", true); + Command command = new Command(); - command.setDeviceId(1); + command.setDeviceId(device.getId()); command.setType(Command.TYPE_IDENTIFICATION); Assert.assertEquals("[begin]sms2,999999,[end]", encoder.encodeCommand(command)); @@ -117,8 +152,11 @@ public class Tk103ProtocolEncoderTest extends ProtocolTest { Tk103ProtocolEncoder encoder = new Tk103ProtocolEncoder(); + Device device = TestIdentityManager.createTestDevice(); + device.set("tk103.alternative", true); + Command command = new Command(); - command.setDeviceId(1); + command.setDeviceId(device.getId()); command.setType(Command.TYPE_ALARM_SOS); command.set(Command.KEY_ENABLE, true); @@ -131,8 +169,11 @@ public class Tk103ProtocolEncoderTest extends ProtocolTest { Tk103ProtocolEncoder encoder = new Tk103ProtocolEncoder(); + Device device = TestIdentityManager.createTestDevice(); + device.set("tk103.alternative", true); + Command command = new Command(); - command.setDeviceId(1); + command.setDeviceId(device.getId()); command.setType(Command.TYPE_ALARM_SOS); command.set(Command.KEY_ENABLE, false); @@ -145,8 +186,11 @@ public class Tk103ProtocolEncoderTest extends ProtocolTest { Tk103ProtocolEncoder encoder = new Tk103ProtocolEncoder(); + Device device = TestIdentityManager.createTestDevice(); + device.set("tk103.alternative", true); + Command command = new Command(); - command.setDeviceId(1); + command.setDeviceId(device.getId()); command.setType(Command.TYPE_CUSTOM); command.set(Command.KEY_DATA, "any text is ok"); @@ -159,8 +203,11 @@ public class Tk103ProtocolEncoderTest extends ProtocolTest { Tk103ProtocolEncoder encoder = new Tk103ProtocolEncoder(); + Device device = TestIdentityManager.createTestDevice(); + device.set("tk103.alternative", true); + Command command = new Command(); - command.setDeviceId(1); + command.setDeviceId(device.getId()); command.setType(Command.TYPE_SET_CONNECTION); command.set(Command.KEY_SERVER, "1.2.3.4"); command.set(Command.KEY_PORT, "5555"); @@ -174,13 +221,17 @@ public class Tk103ProtocolEncoderTest extends ProtocolTest { Tk103ProtocolEncoder encoder = new Tk103ProtocolEncoder(); + Device device = TestIdentityManager.createTestDevice(); + device.set("tk103.alternative", true); + device.set(Command.KEY_DEVICE_PASSWORD, "232323"); + Command command = new Command(); - command.setDeviceId(1); + command.setDeviceId(device.getId()); command.setType(Command.TYPE_SOS_NUMBER); command.set(Command.KEY_INDEX, "0"); command.set(Command.KEY_PHONE, "+55555555555"); - Assert.assertEquals("[begin]sms2,*master*123456*+55555555555*,[end]", encoder.encodeCommand(command)); + Assert.assertEquals("[begin]sms2,*master*232323*+55555555555*,[end]", encoder.encodeCommand(command)); } -- cgit v1.2.3 From d03b20aa07a0edf49ef16e3f7e0c5e0a440b1e5a Mon Sep 17 00:00:00 2001 From: Valerii Vyshniak Date: Thu, 23 Nov 2017 23:06:44 +0100 Subject: Simplify TestIdentityManager to match original from BaseTest --- test/org/traccar/TestIdentityManager.java | 76 +++++++------------------------ 1 file changed, 16 insertions(+), 60 deletions(-) (limited to 'test/org/traccar') diff --git a/test/org/traccar/TestIdentityManager.java b/test/org/traccar/TestIdentityManager.java index 82bcf5c63..c613dcb54 100644 --- a/test/org/traccar/TestIdentityManager.java +++ b/test/org/traccar/TestIdentityManager.java @@ -1,7 +1,8 @@ package org.traccar; -import org.traccar.database.*; -import org.traccar.model.*; +import org.traccar.database.IdentityManager; +import org.traccar.model.Device; +import org.traccar.model.Position; import java.util.concurrent.ConcurrentHashMap; import java.util.Map; @@ -9,51 +10,34 @@ import java.util.Map; public final class TestIdentityManager implements IdentityManager { private static Map devicesById = new ConcurrentHashMap<>(); - - private static final long TEST_DEVICE_ID_START = 100; - private static final String TEST_DEVICE_DEFAULT_UNIQUE_ID_PREFIX = "555"; - private static long newDeviceId = TEST_DEVICE_ID_START; + private static long newDeviceId = 100; public TestIdentityManager() { } - public static Device createTestDevice() { + private static Device createDevice(long id) { Device device = new Device(); - device.setId(newDeviceId); - device.setName("TestDevice" + newDeviceId); - device.setUniqueId(TEST_DEVICE_DEFAULT_UNIQUE_ID_PREFIX + newDeviceId); - devicesById.put(newDeviceId, device); - newDeviceId++; + device.setId(id); + device.setName("test"); + device.setUniqueId("123456789012345"); return device; } - private Device createDefaultTestDevice() { - Device device = new Device(); - device.setId(1); - device.setName("test"); - device.setUniqueId("123456789012345"); + public static Device createTestDevice() { + Device device = createDevice(newDeviceId); + devicesById.put(newDeviceId++, device); return device; } @Override public Device getById(long id) { - return id < TEST_DEVICE_ID_START ? createDefaultTestDevice() : devicesById.get(id); + Device device = devicesById.get(id); + return device != null ? device : createDevice(1); } @Override public Device getByUniqueId(String uniqueId) { - if (uniqueId.length() > TEST_DEVICE_DEFAULT_UNIQUE_ID_PREFIX.length() - && uniqueId.substring(0, TEST_DEVICE_DEFAULT_UNIQUE_ID_PREFIX.length()) - .equals(TEST_DEVICE_DEFAULT_UNIQUE_ID_PREFIX)) { - try { - long id = Long.parseLong( - uniqueId.substring(TEST_DEVICE_DEFAULT_UNIQUE_ID_PREFIX.length(), uniqueId.length())); - if (id >= TEST_DEVICE_ID_START) { - return devicesById.get(id); - } - } catch (NumberFormatException e) {} - } - return createDefaultTestDevice(); + return createDevice(1); } @Override @@ -69,54 +53,26 @@ public final class TestIdentityManager implements IdentityManager { @Override public boolean lookupAttributeBoolean( long deviceId, String attributeName, boolean defaultValue, boolean lookupConfig) { - if (deviceId < TEST_DEVICE_ID_START) { - return defaultValue; - } - - Object result = devicesById.get(deviceId).getAttributes().get(attributeName); - if (result != null) { - return result instanceof String ? Boolean.parseBoolean((String) result) : (Boolean) result; - } - return defaultValue; + Device device = devicesById.get(deviceId); + return device != null ? device.getBoolean(attributeName) : defaultValue; } @Override public String lookupAttributeString( long deviceId, String attributeName, String defaultValue, boolean lookupConfig) { - if (deviceId < TEST_DEVICE_ID_START) { return "alarm,result"; - } - - Object result = devicesById.get(deviceId).getAttributes().get(attributeName); - return result != null ? (String) result : defaultValue; } @Override public int lookupAttributeInteger( long deviceId, String attributeName, int defaultValue, boolean lookupConfig) { - if (deviceId < TEST_DEVICE_ID_START) { return defaultValue; - } - - Object result = devicesById.get(deviceId).getAttributes().get(attributeName); - if (result != null) { - return result instanceof String ? Integer.parseInt((String) result) : ((Number) result).intValue(); - } - return defaultValue; } @Override public long lookupAttributeLong( long deviceId, String attributeName, long defaultValue, boolean lookupConfig) { - if (deviceId < TEST_DEVICE_ID_START) { return defaultValue; - } - - Object result = devicesById.get(deviceId).getAttributes().get(attributeName); - if (result != null) { - return result instanceof String ? Long.parseLong((String) result) : ((Number) result).longValue(); - } - return defaultValue; } } -- cgit v1.2.3 From 3f2edfa6a8adfaee74884612d1c2f0b4bbd4acaf Mon Sep 17 00:00:00 2001 From: Valerii Vyshniak Date: Thu, 23 Nov 2017 23:47:03 +0100 Subject: T580W: move frame start symbol handling from tk103 protocol decoder to frame decoder --- src/org/traccar/protocol/Tk103ProtocolDecoder.java | 5 - .../traccar/protocol/Tk103ProtocolDecoderTest.java | 120 ++++++++++----------- 2 files changed, 60 insertions(+), 65 deletions(-) (limited to 'test/org/traccar') diff --git a/src/org/traccar/protocol/Tk103ProtocolDecoder.java b/src/org/traccar/protocol/Tk103ProtocolDecoder.java index 2fc6bf8d5..d75e019c8 100644 --- a/src/org/traccar/protocol/Tk103ProtocolDecoder.java +++ b/src/org/traccar/protocol/Tk103ProtocolDecoder.java @@ -348,11 +348,6 @@ public class Tk103ProtocolDecoder extends BaseProtocolDecoder { String sentence = (String) msg; - int beginIndex = sentence.indexOf('('); - if (beginIndex != -1) { - sentence = sentence.substring(beginIndex + 1); - } - if (channel != null) { String id = sentence.substring(0, 12); String type = sentence.substring(12, 16); diff --git a/test/org/traccar/protocol/Tk103ProtocolDecoderTest.java b/test/org/traccar/protocol/Tk103ProtocolDecoderTest.java index 0ceb3404e..db5964fa0 100644 --- a/test/org/traccar/protocol/Tk103ProtocolDecoderTest.java +++ b/test/org/traccar/protocol/Tk103ProtocolDecoderTest.java @@ -11,183 +11,183 @@ public class Tk103ProtocolDecoderTest extends ProtocolTest { Tk103ProtocolDecoder decoder = new Tk103ProtocolDecoder(new Tk103Protocol()); verifyPosition(decoder, text( - "(007611121184BR00170816A2401.5217N07447.0788E000.0221352232.340000004FL0030F14F)")); + "007611121184BR00170816A2401.5217N07447.0788E000.0221352232.340000004FL0030F14F")); verifyNull(decoder, text( - "(027044702512BP00027044702512HSO01A4)")); + "027044702512BP00027044702512HSO01A4")); verifyPosition(decoder, text( - "(864768011069660,ZC11,250517,V,0000.0000N,00000.0000E,000.0,114725,000.0,0.00,11)")); + "864768011069660,ZC11,250517,V,0000.0000N,00000.0000E,000.0,114725,000.0,0.00,11")); verifyPosition(decoder, text( - "(864768011069660,ZC17,250517,A,3211.7118N,03452.8086E,0.68,115525,208.19,64.50,9)")); + "864768011069660,ZC17,250517,A,3211.7118N,03452.8086E,0.68,115525,208.19,64.50,9")); verifyNull(decoder, text( - "(357593060760397BP02,G,2,170304A6015.7466N01101.8460E001.609445591.048,7)")); + "357593060760397BP02,G,2,170304A6015.7466N01101.8460E001.609445591.048,7")); verifyPosition(decoder, text( - "(325031693849BR00170228A5750.8012N02700.7476E000.2154529000.0000000200L00000000,170228,194530)")); + "325031693849BR00170228A5750.8012N02700.7476E000.2154529000.0000000200L00000000,170228,194530")); verifyPosition(decoder, text( - "(087073803649BR00170221A6142.0334N02712.2197E000.3203149000.00,00000000L00000000)")); + "087073803649BR00170221A6142.0334N02712.2197E000.3203149000.00,00000000L00000000")); verifyPosition(decoder, text( - "(864768010869060,DW30,050117,A,5135.82713N,00001.17918E,0.089,154745,000.0,43.40,12)")); + "864768010869060,DW30,050117,A,5135.82713N,00001.17918E,0.089,154745,000.0,43.40,12")); verifyNotNull(decoder, text( - "(087073104337BZ00,740,000,3bf7,0425,3bf7,0bf5,3bf7,09e7,3bf7,cbad,3bf7,0dcf,3bf7,c7b2,01000000)")); + "087073104337BZ00,740,000,3bf7,0425,3bf7,0bf5,3bf7,09e7,3bf7,cbad,3bf7,0dcf,3bf7,c7b2,01000000")); verifyNull(decoder, text( - "(087073005534BP00HSO")); + "087073005534BP00HSO")); verifyNull(decoder, text( - "(027028258309BQ86,0,05550c21b10d1d0f431008bd114c0ea5078400010007a100423932,161117005322,01000001)")); + "027028258309BQ86,0,05550c21b10d1d0f431008bd114c0ea5078400010007a100423932,161117005322,01000001")); verifyNull(decoder, text( - "(027028258309BQ86,0,05470c0eb20d040f4410022911360e92077e00010007a1004237c7,161117005232,01000001)")); + "027028258309BQ86,0,05470c0eb20d040f4410022911360e92077e00010007a1004237c7,161117005232,01000001")); verifyPosition(decoder, text( - "(01602009983BR00160830V1855.7022S4817.8731W000.0002729000.0010000000L00000000)")); + "01602009983BR00160830V1855.7022S4817.8731W000.0002729000.0010000000L00000000")); verifyPosition(decoder, text( - "(088046338039BR00160727A3354.7768N03540.7258E000.0140832068.4700000000L00BEB0D4+017.7)")); + "088046338039BR00160727A3354.7768N03540.7258E000.0140832068.4700000000L00BEB0D4+017.7")); verifyPosition(decoder, text( - "(088046338039BP05000088046338039160727A3354.7768N03540.7258E000.0140309065.1000000000L00BEB0D4+017.3)")); + "088046338039BP05000088046338039160727A3354.7768N03540.7258E000.0140309065.1000000000L00BEB0D4+017.3")); verifyAttributes(decoder, text( - "(013632651491,ZC20,180716,144222,6,392,65535,255")); + "013632651491,ZC20,180716,144222,6,392,65535,255")); verifyAttributes(decoder, text( - "(087072009461BR00000007V0000.0000N00000.0000E000.00014039900000000L00000000")); + "087072009461BR00000007V0000.0000N00000.0000E000.00014039900000000L00000000")); verifyPosition(decoder, text( - "(013612345678BO012061830A2934.0133N10627.2544E040.0080331309.6200000000L000770AD")); + "013612345678BO012061830A2934.0133N10627.2544E040.0080331309.6200000000L000770AD")); verifyNotNull(decoder, text( - "(088047194605BZ00,510,010,36e6,932c,43,36e6,766b,36,36e6,7668,32")); + "088047194605BZ00,510,010,36e6,932c,43,36e6,766b,36,36e6,7668,32")); verifyAttributes(decoder, text( - "(013632651491,ZC20,040613,040137,6,421,112,0")); + "013632651491,ZC20,040613,040137,6,421,112,0")); verifyAttributes(decoder, text( - "(864768010159785,ZC20,291015,030413,3,362,65535,255")); + "864768010159785,ZC20,291015,030413,3,362,65535,255")); verifyPosition(decoder, text( - "(088047365460BR00151024A2555.3531S02855.3329E004.7055148276.1701000000L00009AA3)"), + "088047365460BR00151024A2555.3531S02855.3329E004.7055148276.1701000000L00009AA3"), position("2015-10-24 05:51:48.000", true, -25.92255, 28.92222)); verifyPosition(decoder, text( - "(088047365460BP05354188047365460150929A3258.1754S02755.4323E009.4193927301.9000000000L00000000)")); + "088047365460BP05354188047365460150929A3258.1754S02755.4323E009.4193927301.9000000000L00000000")); verifyPosition(decoder, text( - "(088048003342BP05354188048003342150917A1352.9801N10030.9050E000.0103115265.5600010000L000003F9)")); + "088048003342BP05354188048003342150917A1352.9801N10030.9050E000.0103115265.5600010000L000003F9")); verifyPosition(decoder, text( - "(088048003342BR00150917A1352.9801N10030.9050E000.0103224000.0000010000L000003F9)")); + "088048003342BR00150917A1352.9801N10030.9050E000.0103224000.0000010000L000003F9")); verifyPosition(decoder, text( - "(088048003342BR00150807A1352.9871N10030.9084E000.0110718000.0001010000L00000000)")); + "088048003342BR00150807A1352.9871N10030.9084E000.0110718000.0001010000L00000000")); verifyNull(decoder, text( - "(090411121854BP0000001234567890HSO")); + "090411121854BP0000001234567890HSO")); verifyPosition(decoder, text( - "(01029131573BR00150428A3801.6382N02351.0159E000.0080729278.7800000000LEF9ECB9C)")); + "01029131573BR00150428A3801.6382N02351.0159E000.0080729278.7800000000LEF9ECB9C")); verifyPosition(decoder, text( - "(035988863964BP05000035988863964110524A4241.7977N02318.7561E000.0123536356.5100000000L000946BB")); + "035988863964BP05000035988863964110524A4241.7977N02318.7561E000.0123536356.5100000000L000946BB")); verifyPosition(decoder, text( - "(013632782450BP05000013632782450120803V0000.0000N00000.0000E000.0174654000.0000000000L00000000")); + "013632782450BP05000013632782450120803V0000.0000N00000.0000E000.0174654000.0000000000L00000000")); verifyPosition(decoder, text( - "(013666666666BP05000013666666666110925A1234.5678N01234.5678W000.002033490.00000000000L000024DE")); + "013666666666BP05000013666666666110925A1234.5678N01234.5678W000.002033490.00000000000L000024DE")); verifyPosition(decoder, text( - "(013666666666BO012110925A1234.5678N01234.5678W000.0025948118.7200000000L000024DE")); + "013666666666BO012110925A1234.5678N01234.5678W000.0025948118.7200000000L000024DE")); verifyPosition(decoder, text( - "\n\n\n(088045133878BR00130228A5124.5526N00117.7152W000.0233614352.2200000000L01B0CF1C")); + "088045133878BR00130228A5124.5526N00117.7152W000.0233614352.2200000000L01B0CF1C")); verifyPosition(decoder, text( - "(008600410203BP05000008600410203130721A4152.5790N01239.2770E000.0145238173.870100000AL0000000")); + "008600410203BP05000008600410203130721A4152.5790N01239.2770E000.0145238173.870100000AL0000000")); verifyPosition(decoder, text( - "(013012345678BR00130515A4843.9703N01907.6211E000.019232800000000000000L00009239")); + "013012345678BR00130515A4843.9703N01907.6211E000.019232800000000000000L00009239")); verifyPosition(decoder, text( - "(012345678901BP05000012345678901130520A3439.9629S05826.3504W000.1175622323.8700000000L000450AC")); + "012345678901BP05000012345678901130520A3439.9629S05826.3504W000.1175622323.8700000000L000450AC")); verifyPosition(decoder, text( - "(012345678901BR00130520A3439.9629S05826.3504W000.1175622323.8700000000L000450AC")); + "012345678901BR00130520A3439.9629S05826.3504W000.1175622323.8700000000L000450AC")); verifyPosition(decoder, text( - "(352606090042050,BP05,240414,V,0000.0000N,00000.0000E,000.0,193133,000.0")); + "352606090042050,BP05,240414,V,0000.0000N,00000.0000E,000.0,193133,000.0")); verifyPosition(decoder, text( - "(352606090042050,BP05,240414,A,4527.3513N,00909.9758E,4.80,112825,155.49"), + "352606090042050,BP05,240414,A,4527.3513N,00909.9758E,4.80,112825,155.49"), position("2014-04-24 11:28:25.000", true, 45.45586, 9.16626)); verifyPosition(decoder, text( - "(013632782450,BP05,101201,A,2234.0297N,11405.9101E,000.0,040137,178.48,00000000,L00000000")); + "013632782450,BP05,101201,A,2234.0297N,11405.9101E,000.0,040137,178.48,00000000,L00000000")); verifyPosition(decoder, text( - "(864768010009188,BP05,271114,V,4012.19376N,00824.05638E,000.0,154436,000.0")); + "864768010009188,BP05,271114,V,4012.19376N,00824.05638E,000.0,154436,000.0")); verifyPosition(decoder, text( - "(013632651491,BP05,040613,A,2234.0297N,11405.9101E,000.0,040137,178.48)")); + "013632651491,BP05,040613,A,2234.0297N,11405.9101E,000.0,040137,178.48")); verifyPosition(decoder, text( - "(013632651491,ZC07,040613,A,2234.0297N,11405.9101E,000.0,040137,178.48)")); + "013632651491,ZC07,040613,A,2234.0297N,11405.9101E,000.0,040137,178.48")); verifyPosition(decoder, text( - "(013632651491,ZC11,040613,A,2234.0297N,11405.9101E,000.0,040137,178.48)")); + "013632651491,ZC11,040613,A,2234.0297N,11405.9101E,000.0,040137,178.48")); verifyPosition(decoder, text( - "(013632651491,ZC12,040613,A,2234.0297N,11405.9101E,000.0,040137,178.48)")); + "013632651491,ZC12,040613,A,2234.0297N,11405.9101E,000.0,040137,178.48")); verifyPosition(decoder, text( - "(013632651491,ZC13,040613,A,2234.0297N,11405.9101E,000.0,040137,178.48)")); + "013632651491,ZC13,040613,A,2234.0297N,11405.9101E,000.0,040137,178.48")); verifyPosition(decoder, text( - "(013632651491,ZC17,040613,A,2234.0297N,11405.9101E,000.0,040137,178.48)")); + "013632651491,ZC17,040613,A,2234.0297N,11405.9101E,000.0,040137,178.48")); verifyNull(decoder, text( - "(013632651491,ZC20,040613,040137,6,42,112,0)")); + "013632651491,ZC20,040613,040137,6,42,112,0)")); verifyPosition(decoder, text( - "(094050000111BP05000094050000111150808A3804.2418N04616.7468E000.0201447133.3501000011L0028019DT000)")); + "094050000111BP05000094050000111150808A3804.2418N04616.7468E000.0201447133.3501000011L0028019DT000")); verifyPosition(decoder, text( - "(864555555555555,DW3B,131117,A,5544.02870N,01315.08194E,1.597,223707,291.65,-0.10,4)")); + "864555555555555,DW3B,131117,A,5544.02870N,01315.08194E,1.597,223707,291.65,-0.10,4")); verifyPosition(decoder, text( - "(864555555555555,DW3B,131117,A,5544.02870N,01315.08194E,1.597,223707,291.65,0.10,8)")); + "864555555555555,DW3B,131117,A,5544.02870N,01315.08194E,1.597,223707,291.65,0.10,8")); verifyPosition(decoder, text( - "(013632651491,ZC07,040613,A,2234.0297N,11405.9101E,000.0,040137,178.48)")); + "013632651491,ZC07,040613,A,2234.0297N,11405.9101E,000.0,040137,178.48")); verifyAttributes(decoder, text( - "(013632651491,ZC20,040613,040137,6,42,112,0")); + "013632651491,ZC20,040613,040137,6,42,112,0")); verifyNotNull(decoder, text( - "(864555555555555,DW51,200,1,3215,43370,2,58:F3:BB:3B:AA:82*-65*1,1C:6A:BB:AA:81:95*-78*1,151117,154419")); + "864555555555555,DW51,200,1,3215,43370,2,58:F3:BB:3B:AA:82*-65*1,1C:6A:BB:AA:81:95*-78*1,151117,154419")); verifyNotNull(decoder, text( - "(864555555555555,DW5B,210,6,5995,47701,5,30:EE:CC:E7:86:DD*-59*11,4C:60:CC:EA:BB:EE*-68*1,42:AA:DE:EA:BB:00*-69*1,32:CD:BB:C3:4F:CC*-86*3,10:00:43:BA:22:15*-88*1,151117,163722")); + "864555555555555,DW5B,210,6,5995,47701,5,30:EE:CC:E7:86:DD*-59*11,4C:60:CC:EA:BB:EE*-68*1,42:AA:DE:EA:BB:00*-69*1,32:CD:BB:C3:4F:CC*-86*3,10:00:43:BA:22:15*-88*1,151117,163722")); verifyNotNull(decoder, text( - "(013632651491,DW50,460,0,0,6,2,aa:bb:cc:dd:ee:ff*-8*0,aa:bb:cc:dd:ee:ff*-8*0,040613,040137")); + "013632651491,DW50,460,0,0,6,2,aa:bb:cc:dd:ee:ff*-8*0,aa:bb:cc:dd:ee:ff*-8*0,040613,040137")); verifyNotNull(decoder, text( - "(013632651491,DW50,460,0,0,6,0,040613,040137")); + "013632651491,DW50,460,0,0,6,0,040613,040137")); verifyNotNull(decoder, text( - "(864555555555555,ZC03,191117,234207,$Notice: Device version: 1.0$")); + "864555555555555,ZC03,191117,234207,$Notice: Device version: 1.0$")); verifyNotNull(decoder, text( - "(864555555555555,ZC03,191117,234207,$1 .Sensor sensitivity: 1\r\n" + + "864555555555555,ZC03,191117,234207,$1 .Sensor sensitivity: 1\r\n" + "2 .Alert status: Off\r\n" + "3 .Check interval is set to 240 minute(s).\r\n" + "4 .Checkgsm interval is set to 60 minute(s).\r\n" + @@ -196,7 +196,7 @@ public class Tk103ProtocolDecoderTest extends ProtocolTest { "7 . Power: 95%$")); verifyNotNull(decoder, text( - "(864555555555555,ZC03,191117,234207,$1 .Sensor sensitivity: 1\r\n" + + "864555555555555,ZC03,191117,234207,$1 .Sensor sensitivity: 1\r\n" + "2 .Alert status: Off\r\n" + "3 .Check interval is set to 240 minute(s")); -- cgit v1.2.3 From 388b61f289d849848961abb8f454a163b6abc944 Mon Sep 17 00:00:00 2001 From: Valerii Vyshniak Date: Mon, 27 Nov 2017 00:05:12 +0100 Subject: T580W: tk103 frame decoder unit tests --- .../traccar/protocol/Tk103FrameDecoderTest.java | 42 ++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 test/org/traccar/protocol/Tk103FrameDecoderTest.java (limited to 'test/org/traccar') diff --git a/test/org/traccar/protocol/Tk103FrameDecoderTest.java b/test/org/traccar/protocol/Tk103FrameDecoderTest.java new file mode 100644 index 000000000..aae68eb97 --- /dev/null +++ b/test/org/traccar/protocol/Tk103FrameDecoderTest.java @@ -0,0 +1,42 @@ +package org.traccar.protocol; + +import org.jboss.netty.buffer.ChannelBuffer; +import org.junit.Test; +import org.traccar.ProtocolTest; + +public class Tk103FrameDecoderTest extends ProtocolTest { + + @Test + public void testDecode() throws Exception { + + Tk103FrameDecoder decoder = new Tk103FrameDecoder(); + + verifyFrame( + binary("3836343735353535353535353535352C445733422C3133313131372C412C353536322E30323837304E2C30313334382E3038313934452C312E3539372C3232333730372C3239312E36352C2D302E31302C34"), + decoder.decode(null, null, binary("283836343735353535353535353535352C445733422C3133313131372C412C353536322E30323837304E2C30313334382E3038313934452C312E3539372C3232333730372C3239312E36352C2D302E31302C3429283836343735353535353535353535352C5A4332302C3133313131372C3232333730362C362C3339342C36353533352C32353529"))); + + ChannelBuffer buf = binary("283836343535353535353535353535352C445735422C3231302C362C353939352C34373730312C352C33303A45453A43433A45373A38363A44442A2D35392A31312C34433A36303A43433A45413A42423A45452A2D36382A312C34323A41413A44453A45413A42423A30302A2D36392A312C33323A43443A42423A43333A34463A43432A2D38362A332C31303A30303A34333A42413A32323A31352A2D38382A312C3135313131372C31363337323229283836343735353535353535353535352C5A4332302C3133313131372C3232333730362C362C3339342C36353533352C32353529"); + verifyFrame( + binary("3836343535353535353535353535352C445735422C3231302C362C353939352C34373730312C352C33303A45453A43433A45373A38363A44442A2D35392A31312C34433A36303A43433A45413A42423A45452A2D36382A312C34323A41413A44453A45413A42423A30302A2D36392A312C33323A43443A42423A43333A34463A43432A2D38362A332C31303A30303A34333A42413A32323A31352A2D38382A312C3135313131372C313633373232"), + decoder.decode(null, null, buf)); + verifyFrame( + binary("3836343735353535353535353535352C5A4332302C3133313131372C3232333730362C362C3339342C36353533352C323535"), + decoder.decode(null, null, buf)); + + verifyFrame( + binary("3836343735353535353535353535352C445733422C3133313131372C412C353536322E30323837304E2C30313334382E3038313934452C312E3539372C3232333730372C3239312E36352C2D302E31302C34"), + decoder.decode(null, null, binary("676172626167652540232A5E242D2B3C3E3F2429292924242D2D283836343735353535353535353535352C445733422C3133313131372C412C353536322E30323837304E2C30313334382E3038313934452C312E3539372C3232333730372C3239312E36352C2D302E31302C3429283836343735353535353535353535352C5A4332302C3133313131372C3232333730362C362C3339342C36353533352C32353529"))); + + verifyNull(decoder.decode(null, null, binary("67"))); + + verifyNull(decoder.decode(null, null, binary("676172626167652540232a5e242d2b3c3e3f24"))); + + verifyFrame( + binary("38363437353535353535352C5A4330332C3139313131372C3233343432312C24294E6F746963653A0D0A446576696365732073657269616C206E756D6265723A200D0A3538303535353535353535292E0D0A536F6674776172652076657273696F6E3A0D0A56322E3030302C323031362F30382F32332031313A31372924"), + decoder.decode(null, null, binary("610D0A676172626167652540232A5E242D2B3C3E3F2429292924242D2D2838363437353535353535352C5A4330332C3139313131372C3233343432312C24294E6F746963653A0D0A446576696365732073657269616C206E756D6265723A200D0A3538303535353535353535292E0D0A536F6674776172652076657273696F6E3A0D0A56322E3030302C323031362F30382F32332031313A3137292429283836343735353535353535353535352C5A4332302C3133313131372C3232333730362C362C3339342C36353533352C32353529"))); + + verifyNull(decoder.decode(null, null, binary("610D0A676172626167652540232A5E242D2B3C3E3F2429292924242D2D2838363437353535353535352C5A4330332C3139313131372C3233343432312C24294E6F746963653A0D0A446576696365732073657269616C206E756D6265723A200D0A3538303535353535353535292E0D0A536F6674776172652076657273696F6E3A0D0A56322E3030302C323031362F30382F32332031313A31372929283836343735353535353535353535352C5A4332302C3133313131372C3232333730362C362C3339342C36353533352C32353529"))); + + } + +} -- cgit v1.2.3 From f58fe1fcbfcc2be5a0a8113e9e561794eb9b2491 Mon Sep 17 00:00:00 2001 From: Valerii Vyshniak Date: Sun, 3 Dec 2017 03:10:26 +0100 Subject: T580W: start and stop frame symbols in data output from tk103 frame decoder --- src/org/traccar/protocol/Tk103FrameDecoder.java | 4 +- src/org/traccar/protocol/Tk103ProtocolDecoder.java | 10 +- .../traccar/protocol/Tk103FrameDecoderTest.java | 10 +- .../traccar/protocol/Tk103ProtocolDecoderTest.java | 126 ++++++++++----------- 4 files changed, 73 insertions(+), 77 deletions(-) (limited to 'test/org/traccar') diff --git a/src/org/traccar/protocol/Tk103FrameDecoder.java b/src/org/traccar/protocol/Tk103FrameDecoder.java index e6f37981e..e8e0325da 100644 --- a/src/org/traccar/protocol/Tk103FrameDecoder.java +++ b/src/org/traccar/protocol/Tk103FrameDecoder.java @@ -38,7 +38,6 @@ public class Tk103FrameDecoder extends FrameDecoder { buf.clear(); return null; } - frameStartIndex++; int frameEndIndex, freeTextSymbolCounter; for (frameEndIndex = frameStartIndex, freeTextSymbolCounter = 0;; frameEndIndex++) { @@ -71,8 +70,7 @@ public class Tk103FrameDecoder extends FrameDecoder { } buf.readerIndex(frameStartIndex); - ChannelBuffer result = buf.readBytes(frameEndIndex - frameStartIndex); - buf.readerIndex(buf.readerIndex() + 1); + ChannelBuffer result = buf.readBytes(frameEndIndex + 1 - frameStartIndex); return result; diff --git a/src/org/traccar/protocol/Tk103ProtocolDecoder.java b/src/org/traccar/protocol/Tk103ProtocolDecoder.java index d75e019c8..7e2cb06cb 100644 --- a/src/org/traccar/protocol/Tk103ProtocolDecoder.java +++ b/src/org/traccar/protocol/Tk103ProtocolDecoder.java @@ -41,6 +41,7 @@ public class Tk103ProtocolDecoder extends BaseProtocolDecoder { } private static final Pattern PATTERN = new PatternBuilder() + .text("(").optional() .number("(d+)(,)?") // device id .expression("(.{4}),?") // command .number("(d*)") @@ -72,6 +73,7 @@ public class Tk103ProtocolDecoder extends BaseProtocolDecoder { .compile(); private static final Pattern PATTERN_BATTERY = new PatternBuilder() + .text("(").optional() .number("(d+),") // device id .text("ZC20,") .number("(dd)(dd)(dd),") // date (ddmmyy) @@ -80,9 +82,11 @@ public class Tk103ProtocolDecoder extends BaseProtocolDecoder { .number("(d+),") // battery voltage .number("(d+),") // power voltage .number("d+") // installed + .any() .compile(); private static final Pattern PATTERN_NETWORK = new PatternBuilder() + .text("(").optional() .number("(d{12})") // device id .text("BZ00,") .number("(d+),") // mcc @@ -93,6 +97,7 @@ public class Tk103ProtocolDecoder extends BaseProtocolDecoder { .compile(); private static final Pattern PATTERN_LBSWIFI = new PatternBuilder() + .text("(").optional() .number("(d+),") // device id .expression("(.{4}),") // command .number("(d+),") // mcc @@ -107,6 +112,7 @@ public class Tk103ProtocolDecoder extends BaseProtocolDecoder { .compile(); private static final Pattern PATTERN_COMMAND_RESULT = new PatternBuilder() + .text("(").optional() .number("(d+),") // device id .expression(".{4},") // command .number("(dd)(dd)(dd),") // date (ddmmyy) @@ -349,8 +355,8 @@ public class Tk103ProtocolDecoder extends BaseProtocolDecoder { String sentence = (String) msg; if (channel != null) { - String id = sentence.substring(0, 12); - String type = sentence.substring(12, 16); + String id = sentence.substring(1, 13); + String type = sentence.substring(13, 17); if (type.equals("BP00")) { channel.write("(" + id + "AP01HSO)"); return null; diff --git a/test/org/traccar/protocol/Tk103FrameDecoderTest.java b/test/org/traccar/protocol/Tk103FrameDecoderTest.java index aae68eb97..33f7182e0 100644 --- a/test/org/traccar/protocol/Tk103FrameDecoderTest.java +++ b/test/org/traccar/protocol/Tk103FrameDecoderTest.java @@ -12,19 +12,19 @@ public class Tk103FrameDecoderTest extends ProtocolTest { Tk103FrameDecoder decoder = new Tk103FrameDecoder(); verifyFrame( - binary("3836343735353535353535353535352C445733422C3133313131372C412C353536322E30323837304E2C30313334382E3038313934452C312E3539372C3232333730372C3239312E36352C2D302E31302C34"), + binary("283836343735353535353535353535352C445733422C3133313131372C412C353536322E30323837304E2C30313334382E3038313934452C312E3539372C3232333730372C3239312E36352C2D302E31302C3429"), decoder.decode(null, null, binary("283836343735353535353535353535352C445733422C3133313131372C412C353536322E30323837304E2C30313334382E3038313934452C312E3539372C3232333730372C3239312E36352C2D302E31302C3429283836343735353535353535353535352C5A4332302C3133313131372C3232333730362C362C3339342C36353533352C32353529"))); ChannelBuffer buf = binary("283836343535353535353535353535352C445735422C3231302C362C353939352C34373730312C352C33303A45453A43433A45373A38363A44442A2D35392A31312C34433A36303A43433A45413A42423A45452A2D36382A312C34323A41413A44453A45413A42423A30302A2D36392A312C33323A43443A42423A43333A34463A43432A2D38362A332C31303A30303A34333A42413A32323A31352A2D38382A312C3135313131372C31363337323229283836343735353535353535353535352C5A4332302C3133313131372C3232333730362C362C3339342C36353533352C32353529"); verifyFrame( - binary("3836343535353535353535353535352C445735422C3231302C362C353939352C34373730312C352C33303A45453A43433A45373A38363A44442A2D35392A31312C34433A36303A43433A45413A42423A45452A2D36382A312C34323A41413A44453A45413A42423A30302A2D36392A312C33323A43443A42423A43333A34463A43432A2D38362A332C31303A30303A34333A42413A32323A31352A2D38382A312C3135313131372C313633373232"), + binary("283836343535353535353535353535352C445735422C3231302C362C353939352C34373730312C352C33303A45453A43433A45373A38363A44442A2D35392A31312C34433A36303A43433A45413A42423A45452A2D36382A312C34323A41413A44453A45413A42423A30302A2D36392A312C33323A43443A42423A43333A34463A43432A2D38362A332C31303A30303A34333A42413A32323A31352A2D38382A312C3135313131372C31363337323229"), decoder.decode(null, null, buf)); verifyFrame( - binary("3836343735353535353535353535352C5A4332302C3133313131372C3232333730362C362C3339342C36353533352C323535"), + binary("283836343735353535353535353535352C5A4332302C3133313131372C3232333730362C362C3339342C36353533352C32353529"), decoder.decode(null, null, buf)); verifyFrame( - binary("3836343735353535353535353535352C445733422C3133313131372C412C353536322E30323837304E2C30313334382E3038313934452C312E3539372C3232333730372C3239312E36352C2D302E31302C34"), + binary("283836343735353535353535353535352C445733422C3133313131372C412C353536322E30323837304E2C30313334382E3038313934452C312E3539372C3232333730372C3239312E36352C2D302E31302C3429"), decoder.decode(null, null, binary("676172626167652540232A5E242D2B3C3E3F2429292924242D2D283836343735353535353535353535352C445733422C3133313131372C412C353536322E30323837304E2C30313334382E3038313934452C312E3539372C3232333730372C3239312E36352C2D302E31302C3429283836343735353535353535353535352C5A4332302C3133313131372C3232333730362C362C3339342C36353533352C32353529"))); verifyNull(decoder.decode(null, null, binary("67"))); @@ -32,7 +32,7 @@ public class Tk103FrameDecoderTest extends ProtocolTest { verifyNull(decoder.decode(null, null, binary("676172626167652540232a5e242d2b3c3e3f24"))); verifyFrame( - binary("38363437353535353535352C5A4330332C3139313131372C3233343432312C24294E6F746963653A0D0A446576696365732073657269616C206E756D6265723A200D0A3538303535353535353535292E0D0A536F6674776172652076657273696F6E3A0D0A56322E3030302C323031362F30382F32332031313A31372924"), + binary("2838363437353535353535352C5A4330332C3139313131372C3233343432312C24294E6F746963653A0D0A446576696365732073657269616C206E756D6265723A200D0A3538303535353535353535292E0D0A536F6674776172652076657273696F6E3A0D0A56322E3030302C323031362F30382F32332031313A3137292429"), decoder.decode(null, null, binary("610D0A676172626167652540232A5E242D2B3C3E3F2429292924242D2D2838363437353535353535352C5A4330332C3139313131372C3233343432312C24294E6F746963653A0D0A446576696365732073657269616C206E756D6265723A200D0A3538303535353535353535292E0D0A536F6674776172652076657273696F6E3A0D0A56322E3030302C323031362F30382F32332031313A3137292429283836343735353535353535353535352C5A4332302C3133313131372C3232333730362C362C3339342C36353533352C32353529"))); verifyNull(decoder.decode(null, null, binary("610D0A676172626167652540232A5E242D2B3C3E3F2429292924242D2D2838363437353535353535352C5A4330332C3139313131372C3233343432312C24294E6F746963653A0D0A446576696365732073657269616C206E756D6265723A200D0A3538303535353535353535292E0D0A536F6674776172652076657273696F6E3A0D0A56322E3030302C323031362F30382F32332031313A31372929283836343735353535353535353535352C5A4332302C3133313131372C3232333730362C362C3339342C36353533352C32353529"))); diff --git a/test/org/traccar/protocol/Tk103ProtocolDecoderTest.java b/test/org/traccar/protocol/Tk103ProtocolDecoderTest.java index db5964fa0..8b185c104 100644 --- a/test/org/traccar/protocol/Tk103ProtocolDecoderTest.java +++ b/test/org/traccar/protocol/Tk103ProtocolDecoderTest.java @@ -11,194 +11,186 @@ public class Tk103ProtocolDecoderTest extends ProtocolTest { Tk103ProtocolDecoder decoder = new Tk103ProtocolDecoder(new Tk103Protocol()); verifyPosition(decoder, text( - "007611121184BR00170816A2401.5217N07447.0788E000.0221352232.340000004FL0030F14F")); + "(007611121184BR00170816A2401.5217N07447.0788E000.0221352232.340000004FL0030F14F)")); verifyNull(decoder, text( - "027044702512BP00027044702512HSO01A4")); + "(027044702512BP00027044702512HSO01A4)")); verifyPosition(decoder, text( - "864768011069660,ZC11,250517,V,0000.0000N,00000.0000E,000.0,114725,000.0,0.00,11")); + "(864768011069660,ZC11,250517,V,0000.0000N,00000.0000E,000.0,114725,000.0,0.00,11)")); verifyPosition(decoder, text( - "864768011069660,ZC17,250517,A,3211.7118N,03452.8086E,0.68,115525,208.19,64.50,9")); + "(864768011069660,ZC17,250517,A,3211.7118N,03452.8086E,0.68,115525,208.19,64.50,9)")); verifyNull(decoder, text( - "357593060760397BP02,G,2,170304A6015.7466N01101.8460E001.609445591.048,7")); + "(357593060760397BP02,G,2,170304A6015.7466N01101.8460E001.609445591.048,7)")); verifyPosition(decoder, text( - "325031693849BR00170228A5750.8012N02700.7476E000.2154529000.0000000200L00000000,170228,194530")); + "(325031693849BR00170228A5750.8012N02700.7476E000.2154529000.0000000200L00000000,170228,194530)")); verifyPosition(decoder, text( - "087073803649BR00170221A6142.0334N02712.2197E000.3203149000.00,00000000L00000000")); + "(087073803649BR00170221A6142.0334N02712.2197E000.3203149000.00,00000000L00000000)")); verifyPosition(decoder, text( - "864768010869060,DW30,050117,A,5135.82713N,00001.17918E,0.089,154745,000.0,43.40,12")); + "(864768010869060,DW30,050117,A,5135.82713N,00001.17918E,0.089,154745,000.0,43.40,12)")); verifyNotNull(decoder, text( - "087073104337BZ00,740,000,3bf7,0425,3bf7,0bf5,3bf7,09e7,3bf7,cbad,3bf7,0dcf,3bf7,c7b2,01000000")); + "(087073104337BZ00,740,000,3bf7,0425,3bf7,0bf5,3bf7,09e7,3bf7,cbad,3bf7,0dcf,3bf7,c7b2,01000000)")); verifyNull(decoder, text( - "087073005534BP00HSO")); + "(087073005534BP00HSO)")); verifyNull(decoder, text( - "027028258309BQ86,0,05550c21b10d1d0f431008bd114c0ea5078400010007a100423932,161117005322,01000001")); + "(027028258309BQ86,0,05550c21b10d1d0f431008bd114c0ea5078400010007a100423932,161117005322,01000001)")); verifyNull(decoder, text( - "027028258309BQ86,0,05470c0eb20d040f4410022911360e92077e00010007a1004237c7,161117005232,01000001")); + "(027028258309BQ86,0,05470c0eb20d040f4410022911360e92077e00010007a1004237c7,161117005232,01000001)")); verifyPosition(decoder, text( - "01602009983BR00160830V1855.7022S4817.8731W000.0002729000.0010000000L00000000")); + "(01602009983BR00160830V1855.7022S4817.8731W000.0002729000.0010000000L00000000)")); verifyPosition(decoder, text( - "088046338039BR00160727A3354.7768N03540.7258E000.0140832068.4700000000L00BEB0D4+017.7")); + "(088046338039BR00160727A3354.7768N03540.7258E000.0140832068.4700000000L00BEB0D4+017.7)")); verifyPosition(decoder, text( - "088046338039BP05000088046338039160727A3354.7768N03540.7258E000.0140309065.1000000000L00BEB0D4+017.3")); + "(088046338039BP05000088046338039160727A3354.7768N03540.7258E000.0140309065.1000000000L00BEB0D4+017.3)")); verifyAttributes(decoder, text( - "013632651491,ZC20,180716,144222,6,392,65535,255")); + "(013632651491,ZC20,180716,144222,6,392,65535,255)")); verifyAttributes(decoder, text( - "087072009461BR00000007V0000.0000N00000.0000E000.00014039900000000L00000000")); + "(087072009461BR00000007V0000.0000N00000.0000E000.00014039900000000L00000000)")); verifyPosition(decoder, text( - "013612345678BO012061830A2934.0133N10627.2544E040.0080331309.6200000000L000770AD")); + "(013612345678BO012061830A2934.0133N10627.2544E040.0080331309.6200000000L000770AD)")); verifyNotNull(decoder, text( - "088047194605BZ00,510,010,36e6,932c,43,36e6,766b,36,36e6,7668,32")); + "(088047194605BZ00,510,010,36e6,932c,43,36e6,766b,36,36e6,7668,32)")); verifyAttributes(decoder, text( - "013632651491,ZC20,040613,040137,6,421,112,0")); + "(013632651491,ZC20,040613,040137,6,421,112,0)")); verifyAttributes(decoder, text( - "864768010159785,ZC20,291015,030413,3,362,65535,255")); + "(864768010159785,ZC20,291015,030413,3,362,65535,255)")); verifyPosition(decoder, text( - "088047365460BR00151024A2555.3531S02855.3329E004.7055148276.1701000000L00009AA3"), + "(088047365460BR00151024A2555.3531S02855.3329E004.7055148276.1701000000L00009AA3)"), position("2015-10-24 05:51:48.000", true, -25.92255, 28.92222)); verifyPosition(decoder, text( - "088047365460BP05354188047365460150929A3258.1754S02755.4323E009.4193927301.9000000000L00000000")); + "(088047365460BP05354188047365460150929A3258.1754S02755.4323E009.4193927301.9000000000L00000000)")); verifyPosition(decoder, text( - "088048003342BP05354188048003342150917A1352.9801N10030.9050E000.0103115265.5600010000L000003F9")); + "(088048003342BP05354188048003342150917A1352.9801N10030.9050E000.0103115265.5600010000L000003F9)")); verifyPosition(decoder, text( - "088048003342BR00150917A1352.9801N10030.9050E000.0103224000.0000010000L000003F9")); + "(088048003342BR00150917A1352.9801N10030.9050E000.0103224000.0000010000L000003F9)")); verifyPosition(decoder, text( - "088048003342BR00150807A1352.9871N10030.9084E000.0110718000.0001010000L00000000")); + "(088048003342BR00150807A1352.9871N10030.9084E000.0110718000.0001010000L00000000)")); verifyNull(decoder, text( - "090411121854BP0000001234567890HSO")); + "(090411121854BP0000001234567890HSO)")); verifyPosition(decoder, text( - "01029131573BR00150428A3801.6382N02351.0159E000.0080729278.7800000000LEF9ECB9C")); + "(01029131573BR00150428A3801.6382N02351.0159E000.0080729278.7800000000LEF9ECB9C)")); verifyPosition(decoder, text( - "035988863964BP05000035988863964110524A4241.7977N02318.7561E000.0123536356.5100000000L000946BB")); + "(035988863964BP05000035988863964110524A4241.7977N02318.7561E000.0123536356.5100000000L000946BB)")); verifyPosition(decoder, text( - "013632782450BP05000013632782450120803V0000.0000N00000.0000E000.0174654000.0000000000L00000000")); + "(013632782450BP05000013632782450120803V0000.0000N00000.0000E000.0174654000.0000000000L00000000)")); verifyPosition(decoder, text( - "013666666666BP05000013666666666110925A1234.5678N01234.5678W000.002033490.00000000000L000024DE")); + "(013666666666BP05000013666666666110925A1234.5678N01234.5678W000.002033490.00000000000L000024DE)")); verifyPosition(decoder, text( - "013666666666BO012110925A1234.5678N01234.5678W000.0025948118.7200000000L000024DE")); + "(013666666666BO012110925A1234.5678N01234.5678W000.0025948118.7200000000L000024DE)")); verifyPosition(decoder, text( - "088045133878BR00130228A5124.5526N00117.7152W000.0233614352.2200000000L01B0CF1C")); + "(088045133878BR00130228A5124.5526N00117.7152W000.0233614352.2200000000L01B0CF1C)")); verifyPosition(decoder, text( - "008600410203BP05000008600410203130721A4152.5790N01239.2770E000.0145238173.870100000AL0000000")); + "(008600410203BP05000008600410203130721A4152.5790N01239.2770E000.0145238173.870100000AL0000000)")); verifyPosition(decoder, text( - "013012345678BR00130515A4843.9703N01907.6211E000.019232800000000000000L00009239")); + "(013012345678BR00130515A4843.9703N01907.6211E000.019232800000000000000L00009239)")); verifyPosition(decoder, text( - "012345678901BP05000012345678901130520A3439.9629S05826.3504W000.1175622323.8700000000L000450AC")); + "(012345678901BP05000012345678901130520A3439.9629S05826.3504W000.1175622323.8700000000L000450AC)")); verifyPosition(decoder, text( - "012345678901BR00130520A3439.9629S05826.3504W000.1175622323.8700000000L000450AC")); + "(012345678901BR00130520A3439.9629S05826.3504W000.1175622323.8700000000L000450AC)")); verifyPosition(decoder, text( - "352606090042050,BP05,240414,V,0000.0000N,00000.0000E,000.0,193133,000.0")); + "(352606090042050,BP05,240414,V,0000.0000N,00000.0000E,000.0,193133,000.0)")); verifyPosition(decoder, text( - "352606090042050,BP05,240414,A,4527.3513N,00909.9758E,4.80,112825,155.49"), + "(352606090042050,BP05,240414,A,4527.3513N,00909.9758E,4.80,112825,155.49)"), position("2014-04-24 11:28:25.000", true, 45.45586, 9.16626)); verifyPosition(decoder, text( - "013632782450,BP05,101201,A,2234.0297N,11405.9101E,000.0,040137,178.48,00000000,L00000000")); + "(013632782450,BP05,101201,A,2234.0297N,11405.9101E,000.0,040137,178.48,00000000,L00000000)")); verifyPosition(decoder, text( - "864768010009188,BP05,271114,V,4012.19376N,00824.05638E,000.0,154436,000.0")); + "(864768010009188,BP05,271114,V,4012.19376N,00824.05638E,000.0,154436,000.0)")); verifyPosition(decoder, text( - "013632651491,BP05,040613,A,2234.0297N,11405.9101E,000.0,040137,178.48")); + "(013632651491,BP05,040613,A,2234.0297N,11405.9101E,000.0,040137,178.48)")); verifyPosition(decoder, text( - "013632651491,ZC07,040613,A,2234.0297N,11405.9101E,000.0,040137,178.48")); + "(013632651491,ZC07,040613,A,2234.0297N,11405.9101E,000.0,040137,178.48)")); verifyPosition(decoder, text( - "013632651491,ZC11,040613,A,2234.0297N,11405.9101E,000.0,040137,178.48")); + "(013632651491,ZC11,040613,A,2234.0297N,11405.9101E,000.0,040137,178.48)")); verifyPosition(decoder, text( - "013632651491,ZC12,040613,A,2234.0297N,11405.9101E,000.0,040137,178.48")); + "(013632651491,ZC12,040613,A,2234.0297N,11405.9101E,000.0,040137,178.48)")); verifyPosition(decoder, text( - "013632651491,ZC13,040613,A,2234.0297N,11405.9101E,000.0,040137,178.48")); + "(013632651491,ZC13,040613,A,2234.0297N,11405.9101E,000.0,040137,178.48)")); verifyPosition(decoder, text( - "013632651491,ZC17,040613,A,2234.0297N,11405.9101E,000.0,040137,178.48")); - - verifyNull(decoder, text( - "013632651491,ZC20,040613,040137,6,42,112,0)")); + "(013632651491,ZC17,040613,A,2234.0297N,11405.9101E,000.0,040137,178.48)")); verifyPosition(decoder, text( - "094050000111BP05000094050000111150808A3804.2418N04616.7468E000.0201447133.3501000011L0028019DT000")); + "(094050000111BP05000094050000111150808A3804.2418N04616.7468E000.0201447133.3501000011L0028019DT000)")); verifyPosition(decoder, text( - "864555555555555,DW3B,131117,A,5544.02870N,01315.08194E,1.597,223707,291.65,-0.10,4")); + "(864555555555555,DW3B,131117,A,5544.02870N,01315.08194E,1.597,223707,291.65,-0.10,4)")); verifyPosition(decoder, text( - "864555555555555,DW3B,131117,A,5544.02870N,01315.08194E,1.597,223707,291.65,0.10,8")); + "(864555555555555,DW3B,131117,A,5544.02870N,01315.08194E,1.597,223707,291.65,0.10,8)")); verifyPosition(decoder, text( - "013632651491,ZC07,040613,A,2234.0297N,11405.9101E,000.0,040137,178.48")); + "(013632651491,ZC07,040613,A,2234.0297N,11405.9101E,000.0,040137,178.48)")); verifyAttributes(decoder, text( - "013632651491,ZC20,040613,040137,6,42,112,0")); + "(013632651491,ZC20,040613,040137,6,42,112,0)")); verifyNotNull(decoder, text( - "864555555555555,DW51,200,1,3215,43370,2,58:F3:BB:3B:AA:82*-65*1,1C:6A:BB:AA:81:95*-78*1,151117,154419")); + "(864555555555555,DW51,200,1,3215,43370,2,58:F3:BB:3B:AA:82*-65*1,1C:6A:BB:AA:81:95*-78*1,151117,154419)")); verifyNotNull(decoder, text( - "864555555555555,DW5B,210,6,5995,47701,5,30:EE:CC:E7:86:DD*-59*11,4C:60:CC:EA:BB:EE*-68*1,42:AA:DE:EA:BB:00*-69*1,32:CD:BB:C3:4F:CC*-86*3,10:00:43:BA:22:15*-88*1,151117,163722")); + "(864555555555555,DW5B,210,6,5995,47701,5,30:EE:CC:E7:86:DD*-59*11,4C:60:CC:EA:BB:EE*-68*1,42:AA:DE:EA:BB:00*-69*1,32:CD:BB:C3:4F:CC*-86*3,10:00:43:BA:22:15*-88*1,151117,163722)")); verifyNotNull(decoder, text( - "013632651491,DW50,460,0,0,6,2,aa:bb:cc:dd:ee:ff*-8*0,aa:bb:cc:dd:ee:ff*-8*0,040613,040137")); + "(013632651491,DW50,460,0,0,6,2,aa:bb:cc:dd:ee:ff*-8*0,aa:bb:cc:dd:ee:ff*-8*0,040613,040137)")); verifyNotNull(decoder, text( - "013632651491,DW50,460,0,0,6,0,040613,040137")); + "(013632651491,DW50,460,0,0,6,0,040613,040137)")); verifyNotNull(decoder, text( - "864555555555555,ZC03,191117,234207,$Notice: Device version: 1.0$")); + "(864555555555555,ZC03,191117,234207,$Notice: Device version: 1.0$)")); verifyNotNull(decoder, text( - "864555555555555,ZC03,191117,234207,$1 .Sensor sensitivity: 1\r\n" + + "(864555555555555,ZC03,191117,234207,$1 .Sensor sensitivity: 1\r\n" + "2 .Alert status: Off\r\n" + "3 .Check interval is set to 240 minute(s).\r\n" + "4 .Checkgsm interval is set to 60 minute(s).\r\n" + "5 .SOS SMS Alert: On\r\n" + "6 .SOS Call Alert: On\r\n" + - "7 . Power: 95%$")); - - verifyNotNull(decoder, text( - "864555555555555,ZC03,191117,234207,$1 .Sensor sensitivity: 1\r\n" + - "2 .Alert status: Off\r\n" + - "3 .Check interval is set to 240 minute(s")); + "7 . Power: 95%$)")); } -- cgit v1.2.3 From 8e60c4319f7931eabd98c88eeadfa1280c47974e Mon Sep 17 00:00:00 2001 From: Valerii Vyshniak Date: Sun, 3 Dec 2017 15:20:13 +0100 Subject: T580W: tk103 encoder alternative commands in separate class to be able to test them separately --- src/org/traccar/protocol/Tk103ProtocolEncoder.java | 45 ++----- .../protocol/Tk103ProtocolEncoderAlternative.java | 70 ++++++++++ test/org/traccar/TestIdentityManager.java | 33 ++--- .../traccar/protocol/Tk103ProtocolEncoderTest.java | 149 ++++++++++++--------- 4 files changed, 171 insertions(+), 126 deletions(-) create mode 100644 src/org/traccar/protocol/Tk103ProtocolEncoderAlternative.java (limited to 'test/org/traccar') diff --git a/src/org/traccar/protocol/Tk103ProtocolEncoder.java b/src/org/traccar/protocol/Tk103ProtocolEncoder.java index 26b966d48..03b12b296 100644 --- a/src/org/traccar/protocol/Tk103ProtocolEncoder.java +++ b/src/org/traccar/protocol/Tk103ProtocolEncoder.java @@ -26,55 +26,30 @@ public class Tk103ProtocolEncoder extends StringProtocolEncoder { @Override protected Object encodeCommand(Command command) { - boolean alternative = Context.getIdentityManager().lookupAttributeBoolean( - command.getDeviceId(), "tk103.alternative", false, true); - - initDevicePassword(command, "123456"); + if (Context.getIdentityManager().lookupAttributeBoolean( + command.getDeviceId(), "tk103.alternative", false, true)) { + Tk103ProtocolEncoderAlternative alternative = new Tk103ProtocolEncoderAlternative(); + return alternative.encodeCommand(command); + } switch (command.getType()) { case Command.TYPE_GET_VERSION: - return alternative ? formatCommand(command, "[begin]sms2,*about*,[end]") - : formatCommand(command, "({%s}AP07)", Command.KEY_UNIQUE_ID); + return formatCommand(command, "({%s}AP07)", Command.KEY_UNIQUE_ID); case Command.TYPE_REBOOT_DEVICE: - return alternative ? formatCommand(command, "[begin]sms2,88888888,[end]") - : formatCommand(command, "({%s}AT00)", Command.KEY_UNIQUE_ID); + return formatCommand(command, "({%s}AT00)", Command.KEY_UNIQUE_ID); case Command.TYPE_SET_ODOMETER: return formatCommand(command, "({%s}AX01)", Command.KEY_UNIQUE_ID); case Command.TYPE_POSITION_SINGLE: - return alternative ? formatCommand(command, "[begin]sms2,*getposl*,[end]") - : formatCommand(command, "({%s}AP00)", Command.KEY_UNIQUE_ID); + return formatCommand(command, "({%s}AP00)", Command.KEY_UNIQUE_ID); case Command.TYPE_POSITION_PERIODIC: - return alternative ? formatCommand(command, "[begin]sms2,*routetrack*99*,[end]") - : formatCommand(command, "({%s}AR00%s0000)", Command.KEY_UNIQUE_ID, + return formatCommand(command, "({%s}AR00%s0000)", Command.KEY_UNIQUE_ID, String.format("%04X", command.getInteger(Command.KEY_FREQUENCY))); case Command.TYPE_POSITION_STOP: - return alternative ? formatCommand(command, "[begin]sms2,*routetrackoff*,[end]") - : formatCommand(command, "({%s}AR0000000000)", Command.KEY_UNIQUE_ID); + return formatCommand(command, "({%s}AR0000000000)", Command.KEY_UNIQUE_ID); case Command.TYPE_ENGINE_STOP: return formatCommand(command, "({%s}AV011)", Command.KEY_UNIQUE_ID); case Command.TYPE_ENGINE_RESUME: return formatCommand(command, "({%s}AV010)", Command.KEY_UNIQUE_ID); - case Command.TYPE_CUSTOM: - return formatCommand(command, "[begin]sms2,{%s},[end]", Command.KEY_DATA); - case Command.TYPE_GET_DEVICE_STATUS: - return formatCommand(command, "[begin]sms2,*status*,[end]"); - case Command.TYPE_IDENTIFICATION: - return formatCommand(command, "[begin]sms2,999999,[end]"); - case Command.TYPE_MODE_DEEP_SLEEP: - return formatCommand(command, command.getBoolean(Command.KEY_ENABLE) - ? "[begin]sms2,*sleep*2*,[end]" : "[begin]sms2,*sleepoff*,[end]"); - case Command.TYPE_MODE_POWER_SAVING: - return formatCommand(command, command.getBoolean(Command.KEY_ENABLE) - ? "[begin]sms2,*sleepv*,[end]" : "[begin]sms2,*sleepoff*,[end]"); - case Command.TYPE_ALARM_SOS: - return formatCommand(command, command.getBoolean(Command.KEY_ENABLE) - ? "[begin]sms2,*soson*,[end]" : "[begin]sms2,*sosoff*,[end]"); - case Command.TYPE_SET_CONNECTION: - return formatCommand(command, "[begin]sms2,*setip*%s*{%s}*,[end]", - command.getString(Command.KEY_SERVER).replace(".", "*"), Command.KEY_PORT); - case Command.TYPE_SOS_NUMBER: - return formatCommand(command, "[begin]sms2,*master*{%s}*{%s}*,[end]", - Command.KEY_DEVICE_PASSWORD, Command.KEY_PHONE); default: Log.warning(new UnsupportedOperationException(command.getType())); break; diff --git a/src/org/traccar/protocol/Tk103ProtocolEncoderAlternative.java b/src/org/traccar/protocol/Tk103ProtocolEncoderAlternative.java new file mode 100644 index 000000000..68398ae84 --- /dev/null +++ b/src/org/traccar/protocol/Tk103ProtocolEncoderAlternative.java @@ -0,0 +1,70 @@ +/* + * Copyright 2017 Valerii Vyshniak (val@val.one) + * 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.traccar.StringProtocolEncoder; +import org.traccar.helper.Log; +import org.traccar.model.Command; + +public class Tk103ProtocolEncoderAlternative extends StringProtocolEncoder { + + @Override + protected Object encodeCommand(Command command) { + + initDevicePassword(command, "123456"); + + switch (command.getType()) { + case Command.TYPE_CUSTOM: + return formatCommand(command, "[begin]sms2,{%s},[end]", Command.KEY_DATA); + case Command.TYPE_GET_VERSION: + return formatCommand(command, "[begin]sms2,*about*,[end]"); + case Command.TYPE_IDENTIFICATION: + return formatCommand(command, "[begin]sms2,999999,[end]"); + case Command.TYPE_GET_DEVICE_STATUS: + return formatCommand(command, "[begin]sms2,*status*,[end]"); + case Command.TYPE_REBOOT_DEVICE: + return formatCommand(command, "[begin]sms2,88888888,[end]"); + case Command.TYPE_POSITION_SINGLE: + return formatCommand(command, "[begin]sms2,*getposl*,[end]"); + case Command.TYPE_POSITION_PERIODIC: + return formatCommand(command, "[begin]sms2,*routetrack*99*,[end]"); + case Command.TYPE_POSITION_STOP: + return formatCommand(command, "[begin]sms2,*routetrackoff*,[end]"); + case Command.TYPE_MODE_DEEP_SLEEP: + return formatCommand(command, command.getBoolean(Command.KEY_ENABLE) + ? "[begin]sms2,*sleep*2*,[end]" : "[begin]sms2,*sleepoff*,[end]"); + case Command.TYPE_MODE_POWER_SAVING: + return formatCommand(command, command.getBoolean(Command.KEY_ENABLE) + ? "[begin]sms2,*sleepv*,[end]" : "[begin]sms2,*sleepoff*,[end]"); + case Command.TYPE_ALARM_SOS: + return formatCommand(command, command.getBoolean(Command.KEY_ENABLE) + ? "[begin]sms2,*soson*,[end]" : "[begin]sms2,*sosoff*,[end]"); + case Command.TYPE_SET_CONNECTION: + return formatCommand(command, "[begin]sms2,*setip*%s*{%s}*,[end]", + command.getString(Command.KEY_SERVER).replace(".", "*"), Command.KEY_PORT); + case Command.TYPE_SOS_NUMBER: + return formatCommand(command, "[begin]sms2,*master*{%s}*{%s}*,[end]", + Command.KEY_DEVICE_PASSWORD, Command.KEY_PHONE); + default: + Log.warning(new UnsupportedOperationException(command.getType())); + break; + } + + return null; + } + +} diff --git a/test/org/traccar/TestIdentityManager.java b/test/org/traccar/TestIdentityManager.java index c613dcb54..f9d95927d 100644 --- a/test/org/traccar/TestIdentityManager.java +++ b/test/org/traccar/TestIdentityManager.java @@ -3,41 +3,25 @@ package org.traccar; import org.traccar.database.IdentityManager; import org.traccar.model.Device; import org.traccar.model.Position; -import java.util.concurrent.ConcurrentHashMap; - -import java.util.Map; public final class TestIdentityManager implements IdentityManager { - private static Map devicesById = new ConcurrentHashMap<>(); - private static long newDeviceId = 100; - - public TestIdentityManager() { - } - - private static Device createDevice(long id) { + private static Device createDevice() { Device device = new Device(); - device.setId(id); + device.setId(1); device.setName("test"); device.setUniqueId("123456789012345"); return device; } - public static Device createTestDevice() { - Device device = createDevice(newDeviceId); - devicesById.put(newDeviceId++, device); - return device; - } - @Override public Device getById(long id) { - Device device = devicesById.get(id); - return device != null ? device : createDevice(1); + return createDevice(); } @Override public Device getByUniqueId(String uniqueId) { - return createDevice(1); + return createDevice(); } @Override @@ -53,26 +37,25 @@ public final class TestIdentityManager implements IdentityManager { @Override public boolean lookupAttributeBoolean( long deviceId, String attributeName, boolean defaultValue, boolean lookupConfig) { - Device device = devicesById.get(deviceId); - return device != null ? device.getBoolean(attributeName) : defaultValue; + return defaultValue; } @Override public String lookupAttributeString( long deviceId, String attributeName, String defaultValue, boolean lookupConfig) { - return "alarm,result"; + return "alarm,result"; } @Override public int lookupAttributeInteger( long deviceId, String attributeName, int defaultValue, boolean lookupConfig) { - return defaultValue; + return defaultValue; } @Override public long lookupAttributeLong( long deviceId, String attributeName, long defaultValue, boolean lookupConfig) { - return defaultValue; + return defaultValue; } } diff --git a/test/org/traccar/protocol/Tk103ProtocolEncoderTest.java b/test/org/traccar/protocol/Tk103ProtocolEncoderTest.java index a58cd9ee1..c521d4d73 100644 --- a/test/org/traccar/protocol/Tk103ProtocolEncoderTest.java +++ b/test/org/traccar/protocol/Tk103ProtocolEncoderTest.java @@ -33,12 +33,6 @@ public class Tk103ProtocolEncoderTest extends ProtocolTest { Assert.assertEquals("(123456789012345AP00)", encoder.encodeCommand(command)); - Device device = TestIdentityManager.createTestDevice(); - device.set("tk103.alternative", true); - command.setDeviceId(device.getId()); - - Assert.assertEquals("[begin]sms2,*getposl*,[end]", encoder.encodeCommand(command)); - } @Test @@ -53,12 +47,6 @@ public class Tk103ProtocolEncoderTest extends ProtocolTest { Assert.assertEquals("(123456789012345AR00003C0000)", encoder.encodeCommand(command)); - Device device = TestIdentityManager.createTestDevice(); - device.set("tk103.alternative", true); - command.setDeviceId(device.getId()); - - Assert.assertEquals("[begin]sms2,*routetrack*99*,[end]", encoder.encodeCommand(command)); - } @Test @@ -72,12 +60,6 @@ public class Tk103ProtocolEncoderTest extends ProtocolTest { Assert.assertEquals("(123456789012345AR0000000000)", encoder.encodeCommand(command)); - Device device = TestIdentityManager.createTestDevice(); - device.set("tk103.alternative", true); - command.setDeviceId(device.getId()); - - Assert.assertEquals("[begin]sms2,*routetrackoff*,[end]", encoder.encodeCommand(command)); - } @Test @@ -91,12 +73,6 @@ public class Tk103ProtocolEncoderTest extends ProtocolTest { Assert.assertEquals("(123456789012345AP07)", encoder.encodeCommand(command)); - Device device = TestIdentityManager.createTestDevice(); - device.set("tk103.alternative", true); - command.setDeviceId(device.getId()); - - Assert.assertEquals("[begin]sms2,*about*,[end]", encoder.encodeCommand(command)); - } @Test @@ -110,12 +86,6 @@ public class Tk103ProtocolEncoderTest extends ProtocolTest { Assert.assertEquals("(123456789012345AT00)", encoder.encodeCommand(command)); - Device device = TestIdentityManager.createTestDevice(); - device.set("tk103.alternative", true); - command.setDeviceId(device.getId()); - - Assert.assertEquals("[begin]sms2,88888888,[end]", encoder.encodeCommand(command)); - } @Test @@ -132,15 +102,77 @@ public class Tk103ProtocolEncoderTest extends ProtocolTest { } @Test - public void testEncodeIdentification() throws Exception { + public void testEncodePositionSingleAlternative() throws Exception { - Tk103ProtocolEncoder encoder = new Tk103ProtocolEncoder(); + Tk103ProtocolEncoderAlternative encoder = new Tk103ProtocolEncoderAlternative(); + + Command command = new Command(); + command.setDeviceId(1); + command.setType(Command.TYPE_POSITION_SINGLE); + + Assert.assertEquals("[begin]sms2,*getposl*,[end]", encoder.encodeCommand(command)); + + } + + @Test + public void testEncodePositionPeriodicAlternative() throws Exception { + + Tk103ProtocolEncoderAlternative encoder = new Tk103ProtocolEncoderAlternative(); + + Command command = new Command(); + command.setDeviceId(1); + command.setType(Command.TYPE_POSITION_PERIODIC); + + Assert.assertEquals("[begin]sms2,*routetrack*99*,[end]", encoder.encodeCommand(command)); + + } + + @Test + public void testEncodePositionStopAlternative() throws Exception { + + Tk103ProtocolEncoderAlternative encoder = new Tk103ProtocolEncoderAlternative(); + + Command command = new Command(); + command.setDeviceId(1); + command.setType(Command.TYPE_POSITION_STOP); + + Assert.assertEquals("[begin]sms2,*routetrackoff*,[end]", encoder.encodeCommand(command)); + + } + + @Test + public void testEncodeGetVersionAlternative() throws Exception { + + Tk103ProtocolEncoderAlternative encoder = new Tk103ProtocolEncoderAlternative(); + + Command command = new Command(); + command.setDeviceId(1); + command.setType(Command.TYPE_GET_VERSION); + + Assert.assertEquals("[begin]sms2,*about*,[end]", encoder.encodeCommand(command)); + + } - Device device = TestIdentityManager.createTestDevice(); - device.set("tk103.alternative", true); + @Test + public void testEncodeRebootDeviceAlternative() throws Exception { + + Tk103ProtocolEncoderAlternative encoder = new Tk103ProtocolEncoderAlternative(); Command command = new Command(); - command.setDeviceId(device.getId()); + command.setDeviceId(1); + command.setType(Command.TYPE_REBOOT_DEVICE); + + Assert.assertEquals("[begin]sms2,88888888,[end]", encoder.encodeCommand(command)); + + } + + @Test + public void testEncodeIdentificationAlternative() throws Exception { + + Tk103ProtocolEncoderAlternative encoder = new Tk103ProtocolEncoderAlternative(); + + Command command = new Command(); + command.setDeviceId(1); command.setType(Command.TYPE_IDENTIFICATION); Assert.assertEquals("[begin]sms2,999999,[end]", encoder.encodeCommand(command)); @@ -148,15 +180,12 @@ public class Tk103ProtocolEncoderTest extends ProtocolTest { } @Test - public void testEncodeSosOn() throws Exception { + public void testEncodeSosOnAlternative() throws Exception { - Tk103ProtocolEncoder encoder = new Tk103ProtocolEncoder(); - - Device device = TestIdentityManager.createTestDevice(); - device.set("tk103.alternative", true); + Tk103ProtocolEncoderAlternative encoder = new Tk103ProtocolEncoderAlternative(); Command command = new Command(); - command.setDeviceId(device.getId()); + command.setDeviceId(1); command.setType(Command.TYPE_ALARM_SOS); command.set(Command.KEY_ENABLE, true); @@ -165,15 +194,12 @@ public class Tk103ProtocolEncoderTest extends ProtocolTest { } @Test - public void testEncodeSosOff() throws Exception { - - Tk103ProtocolEncoder encoder = new Tk103ProtocolEncoder(); + public void testEncodeSosOffAlternative() throws Exception { - Device device = TestIdentityManager.createTestDevice(); - device.set("tk103.alternative", true); + Tk103ProtocolEncoderAlternative encoder = new Tk103ProtocolEncoderAlternative(); Command command = new Command(); - command.setDeviceId(device.getId()); + command.setDeviceId(1); command.setType(Command.TYPE_ALARM_SOS); command.set(Command.KEY_ENABLE, false); @@ -182,15 +208,12 @@ public class Tk103ProtocolEncoderTest extends ProtocolTest { } @Test - public void testEncodeCustom() throws Exception { - - Tk103ProtocolEncoder encoder = new Tk103ProtocolEncoder(); + public void testEncodeCustomAlternative() throws Exception { - Device device = TestIdentityManager.createTestDevice(); - device.set("tk103.alternative", true); + Tk103ProtocolEncoderAlternative encoder = new Tk103ProtocolEncoderAlternative(); Command command = new Command(); - command.setDeviceId(device.getId()); + command.setDeviceId(1); command.setType(Command.TYPE_CUSTOM); command.set(Command.KEY_DATA, "any text is ok"); @@ -199,15 +222,12 @@ public class Tk103ProtocolEncoderTest extends ProtocolTest { } @Test - public void testEncodeSetConnection() throws Exception { - - Tk103ProtocolEncoder encoder = new Tk103ProtocolEncoder(); + public void testEncodeSetConnectionAlternative() throws Exception { - Device device = TestIdentityManager.createTestDevice(); - device.set("tk103.alternative", true); + Tk103ProtocolEncoderAlternative encoder = new Tk103ProtocolEncoderAlternative(); Command command = new Command(); - command.setDeviceId(device.getId()); + command.setDeviceId(1); command.setType(Command.TYPE_SET_CONNECTION); command.set(Command.KEY_SERVER, "1.2.3.4"); command.set(Command.KEY_PORT, "5555"); @@ -217,19 +237,16 @@ public class Tk103ProtocolEncoderTest extends ProtocolTest { } @Test - public void testEncodeSosNumber() throws Exception { + public void testEncodeSosNumberAlternative() throws Exception { - Tk103ProtocolEncoder encoder = new Tk103ProtocolEncoder(); - - Device device = TestIdentityManager.createTestDevice(); - device.set("tk103.alternative", true); - device.set(Command.KEY_DEVICE_PASSWORD, "232323"); + Tk103ProtocolEncoderAlternative encoder = new Tk103ProtocolEncoderAlternative(); Command command = new Command(); - command.setDeviceId(device.getId()); + command.setDeviceId(1); command.setType(Command.TYPE_SOS_NUMBER); command.set(Command.KEY_INDEX, "0"); command.set(Command.KEY_PHONE, "+55555555555"); + command.set(Command.KEY_DEVICE_PASSWORD, "232323"); Assert.assertEquals("[begin]sms2,*master*232323*+55555555555*,[end]", encoder.encodeCommand(command)); -- cgit v1.2.3 From b9c78ab2c6ac9f2e7598743114864aae04404ac1 Mon Sep 17 00:00:00 2001 From: Valerii Vyshniak Date: Mon, 4 Dec 2017 09:30:45 +0100 Subject: T580W: Parameter in constructor to force alternative tk103 encoder for tests --- src/org/traccar/protocol/Tk103ProtocolEncoder.java | 55 +++++++++++++---- .../protocol/Tk103ProtocolEncoderAlternative.java | 70 ---------------------- .../traccar/protocol/Tk103ProtocolEncoderTest.java | 22 +++---- 3 files changed, 56 insertions(+), 91 deletions(-) delete mode 100644 src/org/traccar/protocol/Tk103ProtocolEncoderAlternative.java (limited to 'test/org/traccar') diff --git a/src/org/traccar/protocol/Tk103ProtocolEncoder.java b/src/org/traccar/protocol/Tk103ProtocolEncoder.java index 03b12b296..d7b17eeb7 100644 --- a/src/org/traccar/protocol/Tk103ProtocolEncoder.java +++ b/src/org/traccar/protocol/Tk103ProtocolEncoder.java @@ -23,33 +23,68 @@ import org.traccar.model.Command; public class Tk103ProtocolEncoder extends StringProtocolEncoder { + private final boolean forceAlternative; + + public Tk103ProtocolEncoder() { + this.forceAlternative = false; + } + + public Tk103ProtocolEncoder(boolean forceAlternative) { + this.forceAlternative = forceAlternative; + } + @Override protected Object encodeCommand(Command command) { - if (Context.getIdentityManager().lookupAttributeBoolean( - command.getDeviceId(), "tk103.alternative", false, true)) { - Tk103ProtocolEncoderAlternative alternative = new Tk103ProtocolEncoderAlternative(); - return alternative.encodeCommand(command); - } + boolean alternative = forceAlternative || Context.getIdentityManager().lookupAttributeBoolean( + command.getDeviceId(), "tk103.alternative", false, true); + + initDevicePassword(command, "123456"); switch (command.getType()) { case Command.TYPE_GET_VERSION: - return formatCommand(command, "({%s}AP07)", Command.KEY_UNIQUE_ID); + return alternative ? formatCommand(command, "[begin]sms2,*about*,[end]") + : formatCommand(command, "({%s}AP07)", Command.KEY_UNIQUE_ID); case Command.TYPE_REBOOT_DEVICE: - return formatCommand(command, "({%s}AT00)", Command.KEY_UNIQUE_ID); + return alternative ? formatCommand(command, "[begin]sms2,88888888,[end]") + : formatCommand(command, "({%s}AT00)", Command.KEY_UNIQUE_ID); case Command.TYPE_SET_ODOMETER: return formatCommand(command, "({%s}AX01)", Command.KEY_UNIQUE_ID); case Command.TYPE_POSITION_SINGLE: - return formatCommand(command, "({%s}AP00)", Command.KEY_UNIQUE_ID); + return alternative ? formatCommand(command, "[begin]sms2,*getposl*,[end]") + : formatCommand(command, "({%s}AP00)", Command.KEY_UNIQUE_ID); case Command.TYPE_POSITION_PERIODIC: - return formatCommand(command, "({%s}AR00%s0000)", Command.KEY_UNIQUE_ID, + return alternative ? formatCommand(command, "[begin]sms2,*routetrack*99*,[end]") + : formatCommand(command, "({%s}AR00%s0000)", Command.KEY_UNIQUE_ID, String.format("%04X", command.getInteger(Command.KEY_FREQUENCY))); case Command.TYPE_POSITION_STOP: - return formatCommand(command, "({%s}AR0000000000)", Command.KEY_UNIQUE_ID); + return alternative ? formatCommand(command, "[begin]sms2,*routetrackoff*,[end]") + : formatCommand(command, "({%s}AR0000000000)", Command.KEY_UNIQUE_ID); case Command.TYPE_ENGINE_STOP: return formatCommand(command, "({%s}AV011)", Command.KEY_UNIQUE_ID); case Command.TYPE_ENGINE_RESUME: return formatCommand(command, "({%s}AV010)", Command.KEY_UNIQUE_ID); + case Command.TYPE_CUSTOM: + return formatCommand(command, "[begin]sms2,{%s},[end]", Command.KEY_DATA); + case Command.TYPE_GET_DEVICE_STATUS: + return formatCommand(command, "[begin]sms2,*status*,[end]"); + case Command.TYPE_IDENTIFICATION: + return formatCommand(command, "[begin]sms2,999999,[end]"); + case Command.TYPE_MODE_DEEP_SLEEP: + return formatCommand(command, command.getBoolean(Command.KEY_ENABLE) + ? "[begin]sms2,*sleep*2*,[end]" : "[begin]sms2,*sleepoff*,[end]"); + case Command.TYPE_MODE_POWER_SAVING: + return formatCommand(command, command.getBoolean(Command.KEY_ENABLE) + ? "[begin]sms2,*sleepv*,[end]" : "[begin]sms2,*sleepoff*,[end]"); + case Command.TYPE_ALARM_SOS: + return formatCommand(command, command.getBoolean(Command.KEY_ENABLE) + ? "[begin]sms2,*soson*,[end]" : "[begin]sms2,*sosoff*,[end]"); + case Command.TYPE_SET_CONNECTION: + return formatCommand(command, "[begin]sms2,*setip*%s*{%s}*,[end]", + command.getString(Command.KEY_SERVER).replace(".", "*"), Command.KEY_PORT); + case Command.TYPE_SOS_NUMBER: + return formatCommand(command, "[begin]sms2,*master*{%s}*{%s}*,[end]", + Command.KEY_DEVICE_PASSWORD, Command.KEY_PHONE); default: Log.warning(new UnsupportedOperationException(command.getType())); break; diff --git a/src/org/traccar/protocol/Tk103ProtocolEncoderAlternative.java b/src/org/traccar/protocol/Tk103ProtocolEncoderAlternative.java deleted file mode 100644 index 68398ae84..000000000 --- a/src/org/traccar/protocol/Tk103ProtocolEncoderAlternative.java +++ /dev/null @@ -1,70 +0,0 @@ -/* - * Copyright 2017 Valerii Vyshniak (val@val.one) - * 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.traccar.StringProtocolEncoder; -import org.traccar.helper.Log; -import org.traccar.model.Command; - -public class Tk103ProtocolEncoderAlternative extends StringProtocolEncoder { - - @Override - protected Object encodeCommand(Command command) { - - initDevicePassword(command, "123456"); - - switch (command.getType()) { - case Command.TYPE_CUSTOM: - return formatCommand(command, "[begin]sms2,{%s},[end]", Command.KEY_DATA); - case Command.TYPE_GET_VERSION: - return formatCommand(command, "[begin]sms2,*about*,[end]"); - case Command.TYPE_IDENTIFICATION: - return formatCommand(command, "[begin]sms2,999999,[end]"); - case Command.TYPE_GET_DEVICE_STATUS: - return formatCommand(command, "[begin]sms2,*status*,[end]"); - case Command.TYPE_REBOOT_DEVICE: - return formatCommand(command, "[begin]sms2,88888888,[end]"); - case Command.TYPE_POSITION_SINGLE: - return formatCommand(command, "[begin]sms2,*getposl*,[end]"); - case Command.TYPE_POSITION_PERIODIC: - return formatCommand(command, "[begin]sms2,*routetrack*99*,[end]"); - case Command.TYPE_POSITION_STOP: - return formatCommand(command, "[begin]sms2,*routetrackoff*,[end]"); - case Command.TYPE_MODE_DEEP_SLEEP: - return formatCommand(command, command.getBoolean(Command.KEY_ENABLE) - ? "[begin]sms2,*sleep*2*,[end]" : "[begin]sms2,*sleepoff*,[end]"); - case Command.TYPE_MODE_POWER_SAVING: - return formatCommand(command, command.getBoolean(Command.KEY_ENABLE) - ? "[begin]sms2,*sleepv*,[end]" : "[begin]sms2,*sleepoff*,[end]"); - case Command.TYPE_ALARM_SOS: - return formatCommand(command, command.getBoolean(Command.KEY_ENABLE) - ? "[begin]sms2,*soson*,[end]" : "[begin]sms2,*sosoff*,[end]"); - case Command.TYPE_SET_CONNECTION: - return formatCommand(command, "[begin]sms2,*setip*%s*{%s}*,[end]", - command.getString(Command.KEY_SERVER).replace(".", "*"), Command.KEY_PORT); - case Command.TYPE_SOS_NUMBER: - return formatCommand(command, "[begin]sms2,*master*{%s}*{%s}*,[end]", - Command.KEY_DEVICE_PASSWORD, Command.KEY_PHONE); - default: - Log.warning(new UnsupportedOperationException(command.getType())); - break; - } - - return null; - } - -} diff --git a/test/org/traccar/protocol/Tk103ProtocolEncoderTest.java b/test/org/traccar/protocol/Tk103ProtocolEncoderTest.java index c521d4d73..a99889e3b 100644 --- a/test/org/traccar/protocol/Tk103ProtocolEncoderTest.java +++ b/test/org/traccar/protocol/Tk103ProtocolEncoderTest.java @@ -104,7 +104,7 @@ public class Tk103ProtocolEncoderTest extends ProtocolTest { @Test public void testEncodePositionSingleAlternative() throws Exception { - Tk103ProtocolEncoderAlternative encoder = new Tk103ProtocolEncoderAlternative(); + Tk103ProtocolEncoder encoder = new Tk103ProtocolEncoder(true); Command command = new Command(); command.setDeviceId(1); @@ -117,7 +117,7 @@ public class Tk103ProtocolEncoderTest extends ProtocolTest { @Test public void testEncodePositionPeriodicAlternative() throws Exception { - Tk103ProtocolEncoderAlternative encoder = new Tk103ProtocolEncoderAlternative(); + Tk103ProtocolEncoder encoder = new Tk103ProtocolEncoder(true); Command command = new Command(); command.setDeviceId(1); @@ -130,7 +130,7 @@ public class Tk103ProtocolEncoderTest extends ProtocolTest { @Test public void testEncodePositionStopAlternative() throws Exception { - Tk103ProtocolEncoderAlternative encoder = new Tk103ProtocolEncoderAlternative(); + Tk103ProtocolEncoder encoder = new Tk103ProtocolEncoder(true); Command command = new Command(); command.setDeviceId(1); @@ -143,7 +143,7 @@ public class Tk103ProtocolEncoderTest extends ProtocolTest { @Test public void testEncodeGetVersionAlternative() throws Exception { - Tk103ProtocolEncoderAlternative encoder = new Tk103ProtocolEncoderAlternative(); + Tk103ProtocolEncoder encoder = new Tk103ProtocolEncoder(true); Command command = new Command(); command.setDeviceId(1); @@ -156,7 +156,7 @@ public class Tk103ProtocolEncoderTest extends ProtocolTest { @Test public void testEncodeRebootDeviceAlternative() throws Exception { - Tk103ProtocolEncoderAlternative encoder = new Tk103ProtocolEncoderAlternative(); + Tk103ProtocolEncoder encoder = new Tk103ProtocolEncoder(true); Command command = new Command(); command.setDeviceId(1); @@ -169,7 +169,7 @@ public class Tk103ProtocolEncoderTest extends ProtocolTest { @Test public void testEncodeIdentificationAlternative() throws Exception { - Tk103ProtocolEncoderAlternative encoder = new Tk103ProtocolEncoderAlternative(); + Tk103ProtocolEncoder encoder = new Tk103ProtocolEncoder(true); Command command = new Command(); command.setDeviceId(1); @@ -182,7 +182,7 @@ public class Tk103ProtocolEncoderTest extends ProtocolTest { @Test public void testEncodeSosOnAlternative() throws Exception { - Tk103ProtocolEncoderAlternative encoder = new Tk103ProtocolEncoderAlternative(); + Tk103ProtocolEncoder encoder = new Tk103ProtocolEncoder(true); Command command = new Command(); command.setDeviceId(1); @@ -196,7 +196,7 @@ public class Tk103ProtocolEncoderTest extends ProtocolTest { @Test public void testEncodeSosOffAlternative() throws Exception { - Tk103ProtocolEncoderAlternative encoder = new Tk103ProtocolEncoderAlternative(); + Tk103ProtocolEncoder encoder = new Tk103ProtocolEncoder(true); Command command = new Command(); command.setDeviceId(1); @@ -210,7 +210,7 @@ public class Tk103ProtocolEncoderTest extends ProtocolTest { @Test public void testEncodeCustomAlternative() throws Exception { - Tk103ProtocolEncoderAlternative encoder = new Tk103ProtocolEncoderAlternative(); + Tk103ProtocolEncoder encoder = new Tk103ProtocolEncoder(true); Command command = new Command(); command.setDeviceId(1); @@ -224,7 +224,7 @@ public class Tk103ProtocolEncoderTest extends ProtocolTest { @Test public void testEncodeSetConnectionAlternative() throws Exception { - Tk103ProtocolEncoderAlternative encoder = new Tk103ProtocolEncoderAlternative(); + Tk103ProtocolEncoder encoder = new Tk103ProtocolEncoder(true); Command command = new Command(); command.setDeviceId(1); @@ -239,7 +239,7 @@ public class Tk103ProtocolEncoderTest extends ProtocolTest { @Test public void testEncodeSosNumberAlternative() throws Exception { - Tk103ProtocolEncoderAlternative encoder = new Tk103ProtocolEncoderAlternative(); + Tk103ProtocolEncoder encoder = new Tk103ProtocolEncoder(true); Command command = new Command(); command.setDeviceId(1); -- cgit v1.2.3