diff options
-rw-r--r-- | src/org/traccar/protocol/Tk103ProtocolEncoder.java | 121 | ||||
-rw-r--r-- | test/org/traccar/BaseTest.java | 2 | ||||
-rw-r--r-- | test/org/traccar/FilterHandlerTest.java | 2 | ||||
-rw-r--r-- | test/org/traccar/protocol/Tk103ProtocolDecoderTest.java | 16 | ||||
-rw-r--r-- | test/org/traccar/protocol/Tk103ProtocolEncoderTest.java | 183 |
5 files changed, 88 insertions, 236 deletions
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)); } |