From 1262a26ffd8be27609eccc23d4c652c2b0083b46 Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Fri, 17 Jul 2015 19:35:30 +1200 Subject: Remove enum json conversion --- test/org/traccar/web/JsonConverterTest.java | 187 +++++++--------------------- 1 file changed, 46 insertions(+), 141 deletions(-) (limited to 'test/org/traccar') diff --git a/test/org/traccar/web/JsonConverterTest.java b/test/org/traccar/web/JsonConverterTest.java index 07d70d3f2..b02ec2d40 100644 --- a/test/org/traccar/web/JsonConverterTest.java +++ b/test/org/traccar/web/JsonConverterTest.java @@ -1,170 +1,75 @@ package org.traccar.web; -import org.junit.Test; -import org.traccar.model.Factory; - -import java.io.Reader; import java.io.StringReader; import java.text.ParseException; import java.util.Calendar; import java.util.Date; - import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; +import org.junit.Test; +import org.traccar.model.Factory; public class JsonConverterTest { - private T convert(String jsonString, T prototype) throws ParseException { - Reader r = new StringReader( - jsonString); - - return JsonConverter.objectFromJson(r, prototype); - } - @Test public void primitiveConversion() throws ParseException { - AllPrimitives o = convert("{" + - "\"aBoolean\": true, " + - "\"anInt\": 42, " + - "\"aDouble\": 41.99, " + - "\"aString\": \"discworld\", " + - "\"aDate\":\"2015-07-09T19:02:17\"" + - "}", - new AllPrimitives()); - - assertEquals(true, o.getaBoolean()); - assertEquals(42, o.getAnInt()); - assertEquals(41.99, o.getaDouble(), 0.001); - assertEquals("discworld", o.getaString()); + Primitives o = JsonConverter.objectFromJson(new StringReader( + "{" + + "\"boolean\": true, " + + "\"int\": 42, " + + "\"double\": 41.99, " + + "\"string\": \"discworld\", " + + "\"date\":\"2015-07-09T19:02:17\"" + + "}"), + new Primitives()); + + assertEquals(true, o.getBoolean()); + assertEquals(42, o.getInt()); + assertEquals(41.99, o.getDouble(), 0.001); + assertEquals("discworld", o.getString()); Calendar c = Calendar.getInstance(); - c.setTime(o.getaDate()); + c.setTime(o.getDate()); assertEquals(2015, c.get(Calendar.YEAR)); assertEquals(Calendar.JULY, c.get(Calendar.MONTH)); assertEquals(9, c.get(Calendar.DAY_OF_MONTH)); assertEquals(19, c.get(Calendar.HOUR_OF_DAY)); assertEquals(2, c.get(Calendar.MINUTE)); assertEquals(17, c.get(Calendar.SECOND)); - } - - public static class AllPrimitives implements Factory { - - private boolean aBoolean; - private int anInt; - private double aDouble; - private String aString; - private Date aDate; - - - @Override - public Object create() { - return new AllPrimitives(); - } - - public boolean getaBoolean() { - return aBoolean; - } - - public void setaBoolean(boolean aBoolean) { - this.aBoolean = aBoolean; - } - - public int getAnInt() { - return anInt; - } - - public void setAnInt(int anInt) { - this.anInt = anInt; - } - - public double getaDouble() { - return aDouble; - } - - public void setaDouble(double aDouble) { - this.aDouble = aDouble; - } - - public String getaString() { - return aString; - } - - public void setaString(String aString) { - this.aString = aString; - } - public Date getaDate() { - return aDate; - } - - public void setaDate(Date aDate) { - this.aDate = aDate; - } - } - - - @Test - public void enumConversion() throws ParseException { - ObjectWithEnum o = convert("{\"anEnum\": \"VALUE2\"}", new ObjectWithEnum()); - assertEquals(TestEnum.VALUE2, o.getAnEnum()); - } - - - public enum TestEnum { - VALUE1, VALUE2 } - public static class ObjectWithEnum implements Factory { - private TestEnum anEnum; - - public TestEnum getAnEnum() { - return anEnum; - } - - public void setAnEnum(TestEnum anEnum) { - this.anEnum = anEnum; - } - + public static class Primitives implements Factory { + @Override - public Object create() { - return new ObjectWithEnum(); - } - } - + public Primitives create() { + return new Primitives(); + } + + private boolean b; + public boolean getBoolean() { return b; } + public void setBoolean(boolean b) { this.b = b; } + + private int i; + public int getInt() { return i; } + public void setInt(int i) { this.i = i; } + + private long l; + public long getLong() { return l; } + public void setLong(long l) { this.l = l; } + + private double d; + public double getDouble() { return d; } + public void setDouble(double d) { this.d = d; } + + private String s; + public String getString() { return s; } + public void setString(String s) { this.s = s; } + + private Date t; + public Date getDate() { return t; } + public void setDate(Date t) { this.t = t; } - @Test - public void nestedObjectsConversion() throws ParseException { - NestedObjects o = convert("{\"name\": \"Rincewind\", \"nestedObject\": {\"anEnum\":\"VALUE1\"}}", new NestedObjects()); - assertEquals("Rincewind", o.getName()); - assertNotNull("The nested object should be populated", o.getNestedObject()); - assertEquals(TestEnum.VALUE1, o.getNestedObject().getAnEnum()); } - public static class NestedObjects implements Factory { - - private String name; - private ObjectWithEnum nestedObject; - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } - - public ObjectWithEnum getNestedObject() { - return nestedObject; - } - - public void setNestedObject(ObjectWithEnum nestedObject) { - this.nestedObject = nestedObject; - } - - @Override - public Object create() { - return new NestedObjects(); - } - } } -- cgit v1.2.3 From faaf63abdd81d1d4b6e7c7a8ef36e049fdc8000a Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Fri, 17 Jul 2015 22:41:03 +1200 Subject: Finish with GPS103 commands --- src/org/traccar/BaseProtocol.java | 2 +- src/org/traccar/model/Command.java | 1 + .../traccar/protocol/Gps103ProtocolEncoder.java | 41 +++++++++++----------- test/org/traccar/helper/TestIdentityManager.java | 13 ++++--- .../protocol/Gps103ProtocolEncoderTest.java | 29 +++++++++++++++ 5 files changed, 61 insertions(+), 25 deletions(-) create mode 100644 test/org/traccar/protocol/Gps103ProtocolEncoderTest.java (limited to 'test/org/traccar') diff --git a/src/org/traccar/BaseProtocol.java b/src/org/traccar/BaseProtocol.java index 31f278739..aa98d776c 100644 --- a/src/org/traccar/BaseProtocol.java +++ b/src/org/traccar/BaseProtocol.java @@ -42,7 +42,7 @@ public abstract class BaseProtocol implements Protocol { @Override public void sendCommand(ActiveDevice activeDevice, Command command) { if (!supportedCommands.contains(command.getType())) { - throw new RuntimeException("Command " + command + " is not supported in protocol " + this.getName()); + throw new RuntimeException("Command " + command + " is not supported in protocol " + getName()); } activeDevice.write(command); } diff --git a/src/org/traccar/model/Command.java b/src/org/traccar/model/Command.java index 58aa61a7d..f7702dbd0 100644 --- a/src/org/traccar/model/Command.java +++ b/src/org/traccar/model/Command.java @@ -27,6 +27,7 @@ public class Command implements Factory { public static final String TYPE_ENGINE_STOP = "engineStop"; public static final String TYPE_ENGINE_RESUME = "engineResume"; + public static final String KEY_UNIQUE_ID = "uniqueId"; public static final String KEY_FREQUENCY = "frequency"; } diff --git a/src/org/traccar/protocol/Gps103ProtocolEncoder.java b/src/org/traccar/protocol/Gps103ProtocolEncoder.java index d7cddd1ba..27fea8237 100644 --- a/src/org/traccar/protocol/Gps103ProtocolEncoder.java +++ b/src/org/traccar/protocol/Gps103ProtocolEncoder.java @@ -21,42 +21,43 @@ import org.traccar.model.Command; public class Gps103ProtocolEncoder extends BaseProtocolEncoder { - /*@Override - protected void initCommandsTemplates(Map templates) { - templates.put(CommandType.FIX_POSITIONING, new StringCommandTemplate("**,imei:[%s],C,[%s]", Command.UNIQUE_ID, FixPositioningCommand.FREQUENCY) - .addConverter(Duration.class, new CommandValueConversion() { - @Override - public String convert(Duration value) { - return String.format("%02d%s", value.getValue(), value.getUnit().getCommandFormat()); - } - })); - }*/ - - private String formatCommand(String format, Command command) { + private String formatCommand(Command command, String format, String... keys) { - String result = format; + String result = String.format(format, (Object[]) keys); - result = result.replaceAll("\\{uniqueId}", getUniqueId(command.getDeviceId())); + result = result.replaceAll("\\{" + Command.KEY_UNIQUE_ID + "}", getUniqueId(command.getDeviceId())); for (Map.Entry entry : command.getOther().entrySet()) { - result = result.replaceAll("\\{" + entry.getKey() + "}", entry.getValue().toString()); + String value; + if (entry.getKey().equals(Command.KEY_FREQUENCY)) { + long frequency = (Long) entry.getValue(); + if (frequency / 60 / 60 > 0) { + value = String.format("%02dh", frequency / 60 / 60); + } else if (frequency / 60 > 0) { + value = String.format("%02dm", frequency / 60); + } else { + value = String.format("%02ds", frequency); + } + } else { + value = entry.getValue().toString(); + } + result = result.replaceAll("\\{" + entry.getKey() + "}", value); } return result; } - @Override protected Object encodeCommand(Command command) { switch (command.getType()) { case Command.TYPE_POSITION_STOP: - return formatCommand("**,imei:{uniqueId},A", command); + return formatCommand(command, "**,imei:{%s},A", Command.KEY_UNIQUE_ID); case Command.TYPE_POSITION_FIX: - return formatCommand("**,imei:{uniqueId},C,{time}", command); // TODO + return formatCommand(command, "**,imei:{%s},C,{%s}", Command.KEY_UNIQUE_ID, Command.KEY_FREQUENCY); case Command.TYPE_ENGINE_STOP: - return formatCommand("**,imei:{uniqueId},K", command); + return formatCommand(command, "**,imei:{%s},K", Command.KEY_UNIQUE_ID); case Command.TYPE_ENGINE_RESUME: - return formatCommand("**,imei:{uniqueId},J", command); + return formatCommand(command, "**,imei:{%s},J", Command.KEY_UNIQUE_ID); } return null; diff --git a/test/org/traccar/helper/TestIdentityManager.java b/test/org/traccar/helper/TestIdentityManager.java index a4ddeeb09..d279241e4 100644 --- a/test/org/traccar/helper/TestIdentityManager.java +++ b/test/org/traccar/helper/TestIdentityManager.java @@ -4,17 +4,22 @@ import org.traccar.database.IdentityManager; import org.traccar.model.Device; public class TestIdentityManager implements IdentityManager { + + private final Device device; + + public TestIdentityManager() { + device = new Device(); + device.setId(1); + device.setUniqueId("123456789012345"); + } @Override public Device getDeviceById(long id) { - return null; + return device; } @Override public Device getDeviceByUniqueId(String imei) { - Device device = new Device(); - device.setId(new Long(1)); - device.setUniqueId("123456789012345"); return device; } diff --git a/test/org/traccar/protocol/Gps103ProtocolEncoderTest.java b/test/org/traccar/protocol/Gps103ProtocolEncoderTest.java new file mode 100644 index 000000000..74091d0e0 --- /dev/null +++ b/test/org/traccar/protocol/Gps103ProtocolEncoderTest.java @@ -0,0 +1,29 @@ +package org.traccar.protocol; + +import java.util.HashMap; +import java.util.Map; +import org.junit.Assert; +import org.junit.Test; +import org.traccar.model.Command; + +public class Gps103ProtocolEncoderTest { + + @Test + public void testDecode() throws Exception { + + Gps103ProtocolEncoder encoder = new Gps103ProtocolEncoder(); + + Command command = new Command(); + command.setDeviceId(1); + command.setType(Command.TYPE_POSITION_FIX); + + Map other = new HashMap<>(); + other.put(Command.KEY_FREQUENCY, 300l); + + command.setOther(other); + + Assert.assertEquals("**,imei:123456789012345,C,05m", encoder.encodeCommand(command)); + + } + +} -- cgit v1.2.3