private static String generateCSVFileContent(List<VirtualSensor> listOfSensors) { StringBuilder file = new StringBuilder(""); for (VirtualSensor sensor : listOfSensors) { // header Iterator<Map.Entry<String, String>> metadataIterator = sensor.getMetadata().entrySet().iterator(); while (metadataIterator.hasNext()) { Map.Entry<String, String> elem = metadataIterator.next(); file.append("# " + elem.getKey() + ":" + elem.getValue() + "\n"); } // fields, units, types StringBuilder field_names = new StringBuilder("# " + RequestHandler.getConst("FIELDS") + ":"); StringBuilder field_units = new StringBuilder("# " + RequestHandler.getConst("UNITS") + ":"); StringBuilder field_types = new StringBuilder("# " + RequestHandler.getConst("TYPES") + ":"); for (DataField field : sensor.getFields()) { String unit = field.getUnit(); if (unit == null || unit.trim().length() == 0) unit = ""; if (field != sensor.getFields().get(sensor.getFields().size() - 1)) { field_names.append(field.getName().toLowerCase() + ","); field_units.append(unit + ","); field_types.append(field.getType().toLowerCase() + ","); } else { field_names.append(field.getName().toLowerCase() + "\n"); field_units.append(unit + "\n"); field_types.append(field.getType().toLowerCase() + "\n"); } } file.append(field_names.toString() + field_units.toString() + field_types.toString()); // values List<Vector<Double>> values = sensor.getValues(); Vector<Long> timestamps = sensor.getTimestamps(); for (int i = 0; i < timestamps.size(); i++) { file.append( (new java.text.SimpleDateFormat(RequestHandler.getConst("ISO_FORMAT")) .format(new java.util.Date(timestamps.get(i)))) .toString() .replace('T', ' ') + "," + timestamps.get(i)); boolean first = true; for (Object value : values.get(i)) { if (first) { first = false; } else { file.append(","); } file.append("," + value); } file.append("\n"); } // latest values List<Double> latestValues = sensor.getLatestValues(); if (latestValues.size() > 0) { for (Object value : latestValues) { file.append("," + value); } file.append("\n"); } } return file.toString(); }
private static String generateGeoJSONFileContent(List<VirtualSensor> listOfSensors) { JSONObject featureCollection = new JSONObject(); featureCollection.put( RequestHandler.getConst("TYPE_GEOJSON"), RequestHandler.getConst("FEATURE_COLLECTION")); JSONArray features = new JSONArray(); for (VirtualSensor sensor : listOfSensors) { JSONObject feature = new JSONObject(); feature.put(RequestHandler.getConst("TYPE_GEOJSON"), RequestHandler.getConst("FEATURE")); JSONObject geometry = new JSONObject(); geometry.put(RequestHandler.getConst("TYPE_GEOJSON"), RequestHandler.getConst("POINT")); JSONArray coordinates = new JSONArray(); JSONObject properties = new JSONObject(); double longitude = 0; double latitude = 0; double altitude = -1; Iterator<Map.Entry<String, String>> metadataIterator = sensor.getMetadata().entrySet().iterator(); while (metadataIterator.hasNext()) { Map.Entry<String, String> elem = metadataIterator.next(); if (RequestHandler.getConst("LONGITUDE").equalsIgnoreCase(elem.getKey())) longitude = Double.parseDouble(elem.getValue()); else if (RequestHandler.getConst("LATITUDE").equalsIgnoreCase(elem.getKey())) latitude = Double.parseDouble(elem.getValue()); else if (RequestHandler.getConst("ALTITUDE").equalsIgnoreCase(elem.getKey())) altitude = Double.parseDouble(elem.getValue()); else properties.put(elem.getKey(), elem.getValue()); } coordinates.add(longitude); coordinates.add(latitude); if (altitude != -1) coordinates.add(altitude); // fields, units, types JSONArray setOfFields = new JSONArray(); for (DataField field : sensor.getFields()) { String unit = field.getUnit(); if (unit == null || unit.trim().length() == 0) unit = ""; JSONObject fieldInfo = new JSONObject(); fieldInfo.put(RequestHandler.getConst("NAME"), field.getName().toLowerCase()); fieldInfo.put(RequestHandler.getConst("UNIT"), unit); fieldInfo.put(RequestHandler.getConst("TYPE"), field.getType().toLowerCase()); setOfFields.add(fieldInfo); } properties.put(RequestHandler.getConst("FIELDS"), setOfFields); // values Vector<Long> timestamps = sensor.getTimestamps(); if (timestamps.size() > 0) { JSONArray setOfValues = new JSONArray(); List<Vector<Double>> values = sensor.getValues(); for (int i = 0; i < timestamps.size(); i++) { JSONArray valuesForOneField = new JSONArray(); valuesForOneField.add( (new java.text.SimpleDateFormat(RequestHandler.getConst("ISO_FORMAT")) .format(new java.util.Date(timestamps.get(i)))) .toString() .replace('T', ' ')); valuesForOneField.add(timestamps.get(i)); for (Object value : values.get(i)) { valuesForOneField.add(value); } setOfValues.add(valuesForOneField); } properties.put(RequestHandler.getConst("VALUES"), setOfValues); } geometry.put(RequestHandler.getConst("COORDINATES"), coordinates); feature.put(RequestHandler.getConst("GEOMETRY"), geometry); feature.put(RequestHandler.getConst("PROPERTIES"), properties); features.add(feature); // latest values List<Double> latestValues = sensor.getLatestValues(); if (latestValues.size() > 0) { JSONArray setOfValues = new JSONArray(); JSONArray valueForOneField = new JSONArray(); for (Object value : latestValues) { valueForOneField.add(value); } setOfValues.add(valueForOneField); properties.put(RequestHandler.getConst("VALUES"), setOfValues); } } featureCollection.put(RequestHandler.getConst("FEATURES"), features); return featureCollection.toJSONString(); }
private static String generateJSONFileContent(List<VirtualSensor> listOfSensors) { JSONArray sensorsInfo = new JSONArray(); for (VirtualSensor sensor : listOfSensors) { JSONObject sensorInfo = new JSONObject(); // header Iterator<Map.Entry<String, String>> metadataIterator = sensor.getMetadata().entrySet().iterator(); while (metadataIterator.hasNext()) { Map.Entry<String, String> elem = metadataIterator.next(); sensorInfo.put(elem.getKey(), elem.getValue()); } // fields, units, types JSONArray setOfFields = new JSONArray(); for (DataField field : sensor.getFields()) { String unit = field.getUnit(); if (unit == null || unit.trim().length() == 0) unit = ""; JSONObject fieldInfo = new JSONObject(); fieldInfo.put(RequestHandler.getConst("NAME"), field.getName().toLowerCase()); fieldInfo.put(RequestHandler.getConst("UNIT"), unit); fieldInfo.put(RequestHandler.getConst("TYPE"), field.getType().toLowerCase()); setOfFields.add(fieldInfo); } sensorInfo.put(RequestHandler.getConst("FIELDS"), setOfFields); // values Vector<Long> timestamps = sensor.getTimestamps(); if (timestamps.size() > 0) { JSONArray setOfValues = new JSONArray(); List<Vector<Double>> values = sensor.getValues(); for (int i = 0; i < timestamps.size(); i++) { JSONArray valuesForOneField = new JSONArray(); valuesForOneField.add( (new java.text.SimpleDateFormat(RequestHandler.getConst("ISO_FORMAT")) .format(new java.util.Date(timestamps.get(i)))) .toString() .replace('T', ' ')); valuesForOneField.add(timestamps.get(i)); for (Object value : values.get(i)) { valuesForOneField.add(value); } setOfValues.add(valuesForOneField); } sensorInfo.put(RequestHandler.getConst("VALUES"), setOfValues); } // latest values List<Double> latestValues = sensor.getLatestValues(); if (latestValues.size() > 0) { JSONArray setOfValues = new JSONArray(); JSONArray valueForOneField = new JSONArray(); for (Object value : latestValues) { valueForOneField.add(value); } setOfValues.add(valueForOneField); sensorInfo.put(RequestHandler.getConst("VALUES"), setOfValues); } sensorsInfo.add(sensorInfo); } return sensorsInfo.toJSONString(); }