Example #1
0
    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;
    }
Example #2
0
    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;
    }