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(-) 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