private JSONObject reformLastestTrafficData(String sensorURL, JSONArray filterArr) { JSONObject result = new JSONObject(); try { SensorManager sensorManager = new SensorManager(); Sensor sensor = sensorManager.getSpecifiedSensorWithSensorId(sensorURL); if (sensor == null) return result; Observation obs = sensorManager.getNewestObservationForOneSensor(sensorURL); JSONObject metaJson = new JSONObject(); metaJson.put("name", sensor.getName()); metaJson.put("source", sensor.getSource()); metaJson.put("sourceType", sensor.getSourceType()); metaJson.put("city", sensor.getPlace().getCity()); metaJson.put("country", sensor.getPlace().getCountry()); OutputStream out = null; out = DatabaseUtilities.getNewestSensorData(sensorURL); JSONObject json = (JSONObject) JSONSerializer.toJSON(out.toString()); json.put("updated", DateUtil.date2StandardString(new Date())); json.put("ntriples", JSONUtil.JSONToNTriple(json.getJSONObject("results"))); json.put("error", "false"); json.put("meta", metaJson); System.out.println(json); result = json; } catch (Exception e) { e.printStackTrace(); JSONObject jsonResult = new JSONObject(); jsonResult.put("error", "true"); jsonResult.put("ntriples", ""); result = jsonResult; } return result; }
private String getOverViewData( String location, String lat, String lng, String radius, String keywords) { JSONObject json = new JSONObject(); SensorManager sensorManager = new SensorManager(); ArrayList<Sensor> sensors = sensorManager.getAllSpecifiedSensorAroundPlace("traffic", lng, lat, radius); ArrayList<String> arr = new ArrayList<>(); for (Sensor sensor : sensors) { arr.add(sensor.getSource()); } json.put("traffic", arr); sensors = sensorManager.getAllSpecifiedSensorAroundPlace("airport", lng, lat, "20"); arr = new ArrayList<>(); for (Sensor sensor : sensors) { arr.add(sensor.getSource()); } json.put("airport", arr); sensors = sensorManager.getAllSpecifiedSensorAroundPlace("railwaystation", lng, lat, radius); arr = new ArrayList<>(); for (Sensor sensor : sensors) { arr.add(sensor.getSource()); } json.put("railwaystation", arr); String sindice = DatabaseUtilities.getSindiceSearch(keywords); JSONObject jsonSindice = (JSONObject) JSONSerializer.toJSON(sindice); String[] aryStrings = {"Title", "Formats", "Updated"}; jsonSindice.put("vars", aryStrings); json.put("sindice", jsonSindice); return json.toString(); }
private JSONObject reformLastestAirportData(String airportURL) { SensorManager sensorManager = new SensorManager(); Sensor sensor = sensorManager.getSpecifiedSensorWithSensorId(airportURL); JSONObject j = new JSONObject(); if (sensor == null) return j; JSONObject metaJson = new JSONObject(); metaJson.put("name", sensor.getName()); metaJson.put("source", sensor.getSource()); metaJson.put("sourceType", sensor.getSourceType()); metaJson.put("city", sensor.getPlace().getCity()); metaJson.put("country", sensor.getPlace().getCountry()); String airportCode = sensor .getName() .substring(sensor.getName().lastIndexOf("(") + 1, sensor.getName().lastIndexOf(")")); ArrayList<ArrayList> depatures = sensorManager.getAllFlightsWithSpecifiedDeparture(airportCode); ArrayList<ArrayList> arrivals = sensorManager.getAllFlightsWithSpecifiedDestination(airportCode); ArrayList<List> radarFlights = FlightAroundXMLParser.getFlightAroundInformation( sensor.getPlace().getLat(), sensor.getPlace().getLng()); JSONObject depaturesJson = new JSONObject(); String[] aryStrings = {"CallSign", "FlightNumber", "Route"}; depaturesJson.put("vars", aryStrings); depaturesJson.put("data", depatures); JSONObject arrivalsJson = new JSONObject(); arrivalsJson.put("vars", aryStrings); arrivalsJson.put("data", arrivals); JSONObject radarFlightsJson = new JSONObject(); String[] flightStrings = { "Registration", "Model", "CallSign", "Route", "Latitude", "Longitude", "Distance(km)" }; radarFlightsJson.put("vars", flightStrings); radarFlightsJson.put("data", radarFlights); OutputStream result = null; result = DatabaseUtilities.getSensorMetadata(airportURL); JSONObject json = (JSONObject) JSONSerializer.toJSON(result.toString()); json.put("updated", DateUtil.date2StandardString(new Date())); json.put("ntriples", JSONUtil.JSONToNTriple(json.getJSONObject("results"))); json.put("error", "false"); json.put("meta", metaJson); json.put("departures", depaturesJson); json.put("arrivals", arrivalsJson); json.put("radars", radarFlightsJson); System.out.println(json); return json; }
public String feed(String sensorId) { String nt = ""; try { Sensor sensor = sensorManager.getSpecifiedSensorWithSensorId(sensorId); if (sensor == null) return null; String xml = WebServiceURLRetriever.RetrieveFromURL(sensor.getSource()); System.setProperty( "javax.xml.transform.TransformerFactory", "net.sf.saxon.TransformerFactoryImpl"); TransformerFactory tFactory = TransformerFactory.newInstance(); String xsltPath = XSLTMapFile.sensordata2xslt.get(SourceType.getSourceType(sensor.getSourceType())); xsltPath = MyServlet.realPath + xsltPath; try { Transformer transformer = tFactory.newTransformer(new StreamSource(new File(xsltPath))); String id = sensor.getId().substring(sensor.getId().lastIndexOf("/") + 1); String foi = Double.toString(sensor.getPlace().getLat()).replace(".", "").replace("-", "") + Double.toString(sensor.getPlace().getLng()).replace(".", "").replace("-", ""); transformer.setParameter("sensorId", id); transformer.setParameter("sourceType", sensor.getSourceType()); transformer.setParameter("sensorType", sensor.getSensorType()); transformer.setParameter("sourceURL", sensor.getSource()); transformer.setParameter("foi", foi); InputStream inputStream = new ByteArrayInputStream(xml.getBytes("UTF-8")); Writer outWriter = new StringWriter(); StreamResult result = new StreamResult(outWriter); transformer.transform(new StreamSource(inputStream), result); nt = outWriter.toString().trim(); // System.out.println(nt); sensorManager.insertTriplesToGraph(VirtuosoConstantUtil.sensormasherDataGraphURI, nt); } catch (Exception e) { e.printStackTrace(); } } catch (Exception e) { e.printStackTrace(); } return nt; }
private JSONObject reformLastestRaiwayStationData(String sensorURL) { JSONObject result = new JSONObject(); try { SensorManager sensorManager = new SensorManager(); Sensor sensor = sensorManager.getSpecifiedSensorWithSensorId(sensorURL); if (sensor == null) return result; PredictionDetailedXMLParser preParser = new PredictionDetailedXMLParser(); preParser.getPredictionDetailedForOneStation(sensor); Observation obs = sensorManager.getNewestObservationForOneSensor(sensorURL); JSONObject metaJson = new JSONObject(); metaJson.put("name", sensor.getName()); metaJson.put("source", sensor.getSource()); metaJson.put("sourceType", sensor.getSourceType()); metaJson.put("city", sensor.getPlace().getCity()); metaJson.put("country", sensor.getPlace().getCountry()); ArrayList<String> signs = new ArrayList<String>(); String sign; List<String> observations = sensorManager.getObservationsWithTimeCriteria( sensor.getId(), "=", obs.getTimes(), null); ArrayList readArr = new ArrayList<>(); boolean isFirst = true; for (String obsStr : observations) { List<ArrayList> readings = sensorManager.getReadingDataOfObservation(obsStr); Map m1 = new LinkedHashMap(); for (int i = 0; i < readings.size(); i++) { ArrayList reading = readings.get(i); sign = reading.get(0).toString().substring(reading.get(0).toString().lastIndexOf("#") + 1); if (isFirst) signs.add(sign); String unit = null; String content = null; try { content = reading.get(1).toString(); unit = reading.get(2).toString(); } catch (Exception e) { } if (unit == null) { unit = "no"; } Map m2 = new LinkedHashMap(); m2.put("value", content + ""); m2.put("unit", unit); m1.put(sign, m2); } isFirst = false; readArr.add(m1); } JSONObject readingJson = new JSONObject(); readingJson.put("vars", signs); readingJson.put("readings", readArr); OutputStream out = null; out = DatabaseUtilities.getNewestSensorData(sensorURL); JSONObject json = (JSONObject) JSONSerializer.toJSON(out.toString()); json.put("updated", DateUtil.date2StandardString(new Date())); json.put("ntriples", JSONUtil.JSONToNTriple(json.getJSONObject("results"))); json.put("error", "false"); json.put("meta", metaJson); json.put("data", readingJson); System.out.println(json); result = json; } catch (Exception e) { e.printStackTrace(); JSONObject jsonResult = new JSONObject(); jsonResult.put("error", "true"); result = jsonResult; } return result; }
private JSONObject reformLastestBikehireData(String sensorURL, JSONArray filterArr) { JSONObject result = new JSONObject(); try { SensorManager sensorManager = new SensorManager(); Sensor sensor = sensorManager.getSpecifiedSensorWithSensorId(sensorURL); if (sensor == null) return result; Observation obs = null; List<String> observations = new ArrayList(); if (filterArr.size() == 0) { obs = sensorManager.getNewestObservationForOneSensor(sensorURL); if (obs != null) observations.add(obs.getId()); } else { Date fromTime = DateUtil.string2Date(filterArr.getString(2), "MM/dd/yyyy HH:mm"); String operator = filterArr.getString(1); observations = sensorManager.getObservationsWithTimeCriteria(sensorURL, operator, fromTime, null); } JSONObject metaJson = new JSONObject(); metaJson.put("name", sensor.getName()); metaJson.put("source", sensor.getSource()); metaJson.put("sourceType", sensor.getSourceType()); metaJson.put("city", sensor.getPlace().getCity()); metaJson.put("country", sensor.getPlace().getCountry()); Calendar today = Calendar.getInstance(); today.add(Calendar.DATE, -15); Date fromTime = new Date(today.getTimeInMillis()); ArrayList historicalJson = sensorManager.getSensorHistoricalData(sensorURL, fromTime); List signs = new ArrayList<String>(); String sign; Map m1 = new LinkedHashMap(); ArrayList readArr = new ArrayList<>(); for (String obsId : observations) { List<ArrayList> readings = sensorManager.getReadingDataOfObservation(obsId); for (ArrayList reading : readings) { sign = reading.get(0).toString().substring(reading.get(0).toString().lastIndexOf("#") + 1); signs.add(sign); String unit = null; String content = null; try { content = reading.get(1).toString(); unit = reading.get(2).toString(); } catch (Exception e) { } if (unit == null) { unit = "no"; } Map m2 = new LinkedHashMap(); m2.put("value", content); m2.put("unit", unit); m1.put(sign, m2); } readArr.add(m1); } JSONObject readingJson = new JSONObject(); readingJson.put("vars", signs); readingJson.put("readings", readArr); OutputStream out = null; out = DatabaseUtilities.getNewestSensorData(sensorURL); JSONObject json = (JSONObject) JSONSerializer.toJSON(out.toString()); json.put("updated", DateUtil.date2StandardString(new Date())); json.put("ntriples", JSONUtil.JSONToNTriple(json.getJSONObject("results"))); json.put("error", "false"); json.put("meta", metaJson); json.put("data", readingJson); json.put("history", historicalJson); System.out.println(json); result = json; } catch (Exception e) { e.printStackTrace(); JSONObject jsonResult = new JSONObject(); jsonResult.put("error", "true"); result = jsonResult; } return result; }