Skip to content

Commit

Permalink
For #266 - implemented parsing of JSON in 'other' column
Browse files Browse the repository at this point in the history
  • Loading branch information
vitalidze committed Sep 15, 2015
1 parent 4577d1e commit df8479d
Show file tree
Hide file tree
Showing 2 changed files with 125 additions and 71 deletions.
81 changes: 59 additions & 22 deletions src/main/java/org/traccar/web/client/view/PositionInfoPopup.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package org.traccar.web.client.view;

import com.google.gwt.core.client.GWT;
import com.google.gwt.json.client.JSONObject;
import com.google.gwt.json.client.JSONParser;
import com.google.gwt.json.client.JSONValue;
import com.google.gwt.xml.client.Node;
Expand Down Expand Up @@ -68,33 +69,69 @@ public void show(int x, int y, final Position position) {
sensors.put(sensor.getParameterName(), sensor);
}

try {
NodeList nodes = XMLParser.parse(other).getFirstChild().getChildNodes();
for (int i = 0; i < nodes.getLength(); i++) {
Node node = nodes.item(i);
String parameterName = node.getNodeName();
String valueText = node.getFirstChild().getNodeValue();
Sensor sensor = sensors.get(parameterName);
if (sensor != null) {
if (!sensor.isVisible()) {
continue;
}
parameterName = sensor.getName();
if (valueText.matches("^[-+]?\\d+(\\.\\d+)?$")) {
double value = Double.parseDouble(valueText);
List<SensorInterval> intervals = SensorsEditor.intervals(sensor);
if (!intervals.isEmpty()) {
valueText = intervalText(value, intervals);
Map<String, Object> sensorData = new HashMap<String, Object>();

// XML
if (other.trim().startsWith("<")) {
try {
NodeList nodes = XMLParser.parse(other).getFirstChild().getChildNodes();
for (int i = 0; i < nodes.getLength(); i++) {
Node node = nodes.item(i);
String parameterName = node.getNodeName();
String valueText = node.getFirstChild().getNodeValue();
sensorData.put(parameterName, valueText);
}
} catch (Exception error) {
}
} else { // JSON
try {
JSONValue parsed = JSONParser.parseStrict(other);
JSONObject object = parsed.isObject();
if (object != null) {
for (String parameterName : object.keySet()) {
JSONValue value = object.get(parameterName);
if (value.isNumber() != null) {
sensorData.put(parameterName, value.isNumber().doubleValue());
} else if (value.isBoolean() != null) {
sensorData.put(parameterName, value.isBoolean().booleanValue());
} else if (value.isString() != null) {
sensorData.put(parameterName, value.isString().stringValue());
}
}
} else if (parameterName.equals("protocol")) {
parameterName = i18n.protocol();
}
if (!valueText.isEmpty()) {
body += "<tr><td style=\"padding: 3px 0px 3px 0px;\">" + parameterName + "</td><td>" + valueText + "</td></tr>";
} catch (Exception error) {
}
}

// write values
for (Map.Entry<String, Object> entry : sensorData.entrySet()) {
String parameterName = entry.getKey();
Object value = entry.getValue();
String valueText = value.toString();
Sensor sensor = sensors.get(parameterName);
if (sensor != null) {
if (!sensor.isVisible()) {
continue;
}
parameterName = sensor.getName();
if (value instanceof Number || value.toString().matches("^[-+]?\\d+(\\.\\d+)?$")) {
double doubleValue;
if (value instanceof Number) {
doubleValue = ((Number) value).doubleValue();
} else {
doubleValue = Double.parseDouble(valueText);
}
List<SensorInterval> intervals = SensorsEditor.intervals(sensor);
if (!intervals.isEmpty()) {
valueText = intervalText(doubleValue, intervals);
}
}
} else if (parameterName.equals("protocol")) {
parameterName = i18n.protocol();
}
if (!valueText.isEmpty()) {
body += "<tr><td style=\"padding: 3px 0px 3px 0px;\">" + parameterName + "</td><td>" + valueText + "</td></tr>";
}
} catch (Exception error) {
}
}

Expand Down
115 changes: 66 additions & 49 deletions src/main/webapp/m/js/traccar-mobile.js
Original file line number Diff line number Diff line change
Expand Up @@ -430,70 +430,87 @@ function loadDevices() {
}

function parseOther(position) {
// parse 'other' field
var xmlDoc;
if (window.DOMParser) {
parser = new DOMParser();
xmlDoc = parser.parseFromString(position.other, "text/xml");
// Internet Explorer
} else {
xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async = false;
xmlDoc.loadXML(position.other);
if (position.other == null) {
return null;
}

// parse 'other' field
var result;
if (xmlDoc.documentElement == null) {
result = null;
} else {
var device;
for (var i = 0; i < appState.devices.length; i++) {
if (appState.devices[i].id == position.device.id) {
device = appState.devices[i];
break;
}
if (position.other.indexOf("<") == 0) {
var xmlDoc;
if (window.DOMParser) {
parser = new DOMParser();
xmlDoc = parser.parseFromString(position.other, "text/xml");
// Internet Explorer
} else {
xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async = false;
xmlDoc.loadXML(position.other);
}

result = {};
var nodes = xmlDoc.documentElement.childNodes;
for (var i = 0; i < nodes.length; i++) {
var name = nodes[i].nodeName;
var valueText = nodes[i].textContent;
if (valueText == null) {
valueText = nodes[i].nodeValue;
if (xmlDoc.documentElement == null) {
return null;
} else {
result = {};
var nodes = xmlDoc.documentElement.childNodes;
for (var i = 0; i < nodes.length; i++) {
var name = nodes[i].nodeName;
var valueText = nodes[i].textContent;
if (valueText == null) {
valueText = nodes[i].nodeValue;
}
result[name] = valueText;
}
var visible = true;
for (var s = 0; s < device.sensors.length; s++) {
var sensor = device.sensors[s];
if (sensor.parameterName == name) {
visible = sensor.visible;
name = sensor.name;
if (sensor.intervals != null && !isNaN(valueText)) {
var intervals = JSON.parse(sensor.intervals);
if (intervals.length > 0) {
var value = valueText;
var valueText = null;
for (var j = 0; j < intervals.length; j++) {
if (valueText == null) {
valueText = intervals[j].text;
}
if (value < intervals[j].value) {
break;
}
}
} else {
result = JSON.parse(position.other);
}

var device;
for (var i = 0; i < appState.devices.length; i++) {
if (appState.devices[i].id == position.device.id) {
device = appState.devices[i];
break;
}
}

for (var originalName in result) {
var name = originalName;
var valueText = result[originalName];
var visible = true;
var changed = false;
for (var s = 0; s < device.sensors.length; s++) {
var sensor = device.sensors[s];
if (sensor.parameterName == name) {
visible = sensor.visible;
name = sensor.name;
changed = true;
if (sensor.intervals != null && !isNaN(valueText)) {
var intervals = JSON.parse(sensor.intervals);
if (intervals.length > 0) {
var value = valueText;
var valueText = null;
for (var j = 0; j < intervals.length; j++) {
if (valueText == null) {
valueText = intervals[j].text;
}
if (value < intervals[j].value) {
break;
}
valueText = intervals[j].text;
}
}
break;
}
break;
}
if (!visible) {
continue;
}
}
if (!visible || changed) {
delete result[originalName];
}
if (visible) {
result[name] = valueText;
}
}

return result;
}

Expand Down

0 comments on commit df8479d

Please sign in to comment.