diff --git a/src/main/java/org/traccar/web/client/view/PositionInfoPopup.java b/src/main/java/org/traccar/web/client/view/PositionInfoPopup.java index 550075d4..69b44fa1 100644 --- a/src/main/java/org/traccar/web/client/view/PositionInfoPopup.java +++ b/src/main/java/org/traccar/web/client/view/PositionInfoPopup.java @@ -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; @@ -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 intervals = SensorsEditor.intervals(sensor); - if (!intervals.isEmpty()) { - valueText = intervalText(value, intervals); + Map sensorData = new HashMap(); + + // 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 += "" + parameterName + "" + valueText + ""; + } catch (Exception error) { + } + } + + // write values + for (Map.Entry 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 intervals = SensorsEditor.intervals(sensor); + if (!intervals.isEmpty()) { + valueText = intervalText(doubleValue, intervals); + } } + } else if (parameterName.equals("protocol")) { + parameterName = i18n.protocol(); + } + if (!valueText.isEmpty()) { + body += "" + parameterName + "" + valueText + ""; } - } catch (Exception error) { } } diff --git a/src/main/webapp/m/js/traccar-mobile.js b/src/main/webapp/m/js/traccar-mobile.js index 32a735f1..d65a46aa 100644 --- a/src/main/webapp/m/js/traccar-mobile.js +++ b/src/main/webapp/m/js/traccar-mobile.js @@ -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; }