Skip to content

Commit

Permalink
Improve attribute type handling
Browse files Browse the repository at this point in the history
  • Loading branch information
tananaev committed Nov 3, 2022
1 parent 49646ec commit 9280355
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 19 deletions.
30 changes: 25 additions & 5 deletions src/main/java/org/traccar/model/ExtendedModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -91,39 +91,59 @@ public void add(Map.Entry<String, Object> entry) {

public String getString(String key) {
if (attributes.containsKey(key)) {
return (String) attributes.get(key);
return attributes.get(key).toString();
} else {
return null;
}
}

public double getDouble(String key) {
if (attributes.containsKey(key)) {
return ((Number) attributes.get(key)).doubleValue();
Object value = attributes.get(key);
if (value instanceof Number) {
return ((Number) attributes.get(key)).doubleValue();
} else {
return Double.parseDouble(value.toString());
}
} else {
return 0.0;
}
}

public boolean getBoolean(String key) {
if (attributes.containsKey(key)) {
return (Boolean) attributes.get(key);
Object value = attributes.get(key);
if (value instanceof Boolean) {
return (Boolean) attributes.get(key);
} else {
return Boolean.parseBoolean(value.toString());
}
} else {
return false;
}
}

public int getInteger(String key) {
if (attributes.containsKey(key)) {
return ((Number) attributes.get(key)).intValue();
Object value = attributes.get(key);
if (value instanceof Number) {
return ((Number) attributes.get(key)).intValue();
} else {
return Integer.parseInt(value.toString());
}
} else {
return 0;
}
}

public long getLong(String key) {
if (attributes.containsKey(key)) {
return ((Number) attributes.get(key)).longValue();
Object value = attributes.get(key);
if (value instanceof Number) {
return ((Number) attributes.get(key)).longValue();
} else {
return Long.parseLong(value.toString());
}
} else {
return 0;
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/traccar/protocol/KhdProtocolEncoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ protected Object encodeCommand(Command command) {
return encodeCommand(MSG_FACTORY_RESET, uniqueId, null);
case Command.TYPE_SET_SPEED_LIMIT:
ByteBuf content = Unpooled.buffer();
content.writeByte(Integer.parseInt(command.getString(Command.KEY_DATA)));
content.writeByte(command.getInteger(Command.KEY_DATA));
return encodeCommand(MSG_RESUME_OIL, uniqueId, content);
case Command.TYPE_SET_ODOMETER:
return encodeCommand(MSG_DELETE_MILEAGE, uniqueId, null);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2015 - 2020 Anton Tananaev ([email protected])
* Copyright 2015 - 2022 Anton Tananaev ([email protected])
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -99,8 +99,8 @@ protected Object encodeCommand(Command command) {
content.writeShort(command.getInteger(Command.KEY_FREQUENCY) / 10);
return encodeContent(command.getDeviceId(), MeiligaoProtocolDecoder.MSG_TRACK_BY_INTERVAL, content);
case Command.TYPE_OUTPUT_CONTROL:
int index = Integer.parseInt(command.getString(Command.KEY_INDEX)) - 1;
int value = Integer.parseInt(command.getString(Command.KEY_DATA));
int index = command.getInteger(Command.KEY_INDEX) - 1;
int value = command.getInteger(Command.KEY_DATA);
return encodeOutputCommand(command.getDeviceId(), index, value);
case Command.TYPE_ENGINE_STOP:
return encodeOutputCommand(command.getDeviceId(), 1, 1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,14 +80,14 @@ protected Object encodeCommand(Command command) {
return encodeContent(RuptelaProtocolDecoder.MSG_FIRMWARE_UPDATE, content);
case Command.TYPE_OUTPUT_CONTROL:
content.writeInt(command.getInteger(Command.KEY_INDEX));
content.writeInt(Integer.parseInt(command.getString(Command.KEY_DATA)));
content.writeInt(command.getInteger(Command.KEY_DATA));
return encodeContent(RuptelaProtocolDecoder.MSG_SET_IO, content);
case Command.TYPE_SET_CONNECTION:
String c = command.getString(Command.KEY_SERVER) + "," + command.getInteger(Command.KEY_PORT) + ",TCP";
content.writeBytes(c.getBytes(StandardCharsets.US_ASCII));
return encodeContent(RuptelaProtocolDecoder.MSG_SET_CONNECTION, content);
case Command.TYPE_SET_ODOMETER:
content.writeInt(Integer.parseInt(command.getString(Command.KEY_DATA)));
content.writeInt(command.getInteger(Command.KEY_DATA));
return encodeContent(RuptelaProtocolDecoder.MSG_SET_ODOMETER, content);
default:
return null;
Expand Down
16 changes: 8 additions & 8 deletions src/main/java/org/traccar/protocol/SuntechProtocolEncoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,23 +59,23 @@ protected Object encodeUniversalCommand(Command command) {
return formatCommand(command, "CMD;%s;03;01\r", Command.KEY_UNIQUE_ID);
case Command.TYPE_OUTPUT_CONTROL:
if (command.getAttributes().get(Command.KEY_DATA).equals("1")) {
switch (command.getString(Command.KEY_INDEX)) {
case "1":
switch (command.getInteger(Command.KEY_INDEX)) {
case 1:
return formatCommand(command, "CMD;%s;04;01\r", Command.KEY_UNIQUE_ID);
case "2":
case 2:
return formatCommand(command, "CMD;%s;04;03\r", Command.KEY_UNIQUE_ID);
case "3":
case 3:
return formatCommand(command, "CMD;%s;04;09\r", Command.KEY_UNIQUE_ID);
default:
return null;
}
} else {
switch (command.getString(Command.KEY_INDEX)) {
case "1":
switch (command.getInteger(Command.KEY_INDEX)) {
case 1:
return formatCommand(command, "CMD;%s;04;02\r", Command.KEY_UNIQUE_ID);
case "2":
case 2:
return formatCommand(command, "CMD;%s;04;04\r", Command.KEY_UNIQUE_ID);
case "3":
case 3:
return formatCommand(command, "CMD;%s;04;10\r", Command.KEY_UNIQUE_ID);
default:
return null;
Expand Down

0 comments on commit 9280355

Please sign in to comment.