Beispiel #1
0
  public void sendData(Track track, Saveable saveable) throws Exception {
    if (authenticated()) {
      // Determine API action based on type of savable:
      String action = null;
      if (saveable instanceof NTMeasurement) action = "update";
      else if (saveable instanceof TaggedInterval) action = "taginterval";
      if (action == null) throw new IllegalArgumentException("Unknown savable type");

      // Build URL:
      String url =
          apiBaseURL
              + action
              + "?"
              + saveable.toUrl()
              + "&track="
              + track.getTrackID()
              + "&key="
              + account.getAPIKey();
      // log.debug("Sending data: " + url);

      // Send:
      try {
        if (async) httpClient.getRequestAsync(url, "send data", errorCallback);
        else httpClient.getRequest(url);
      } catch (Exception e) {
        // log.debug("Could not send data: " + e.getMessage());
        throw e;
      }
    } else throw new Exception("Not logged in");
  }
Beispiel #2
0
 public void postLog(String logMessage) throws Exception {
   if (authenticated()) {
     try {
       httpClient.postJSONRequest(
           apiBaseURL + "postlog?key=" + account.getAPIKey(),
           "{log:\"" + JSONUtils.escape(logMessage) + "\"}");
     } catch (Exception e) {
       // log.error(e, "Could not send log message"); //don't this here, could cause endless loop
     }
     log.debug("Log posted");
   } else throw new Exception("Not logged in");
 }
Beispiel #3
0
 public void endTrack(Track track) throws Exception {
   if (authenticated()) {
     String url =
         apiBaseURL + "endsession?key=" + account.getAPIKey() + "&track=" + track.getTrackID();
     // log.debug("Ending track (" + url + ")");
     try {
       httpClient.getRequest(url);
       log.info("Track " + track.getTrackID() + " ended on server");
     } catch (Exception e) {
       // log.debug("Could not end track: " + e.getMessage());
       throw e;
     }
   } else throw new Exception("Not logged in");
 }
Beispiel #4
0
 public void sendBatch(Track track, HttpSaver.Cache cache) throws Exception {
   if (cache.getSize() > 0) {
     if (authenticated()) {
       if (!track.isTrackIDSet()) { // this is a new track
         startTrack(track); // throws SaveException if failed
       }
       // Assemble JSON:
       StringBuffer jsonBff = new StringBuffer();
       //	First the measurements:
       jsonBff.append("{measures:[");
       Enumeration mEnum = cache.getMeasurements().elements();
       while (mEnum.hasMoreElements()) {
         jsonBff.append(((NTMeasurement) mEnum.nextElement()).toJSON());
         if (mEnum.hasMoreElements()) jsonBff.append(",");
       }
       jsonBff.append("],");
       //	Then the tagged intervals:
       jsonBff.append("taggedIntervals:[");
       Enumeration tiEnum = cache.getTaggedIntervals().elements();
       while (tiEnum.hasMoreElements()) {
         jsonBff.append(((TaggedInterval) tiEnum.nextElement()).toJSON());
         if (tiEnum.hasMoreElements()) jsonBff.append(",");
       }
       jsonBff.append("]}");
       // send the JSON:
       log.debug(
           "Sending a batch of "
               + cache.getSize()
               + " measurements and tagged intervals of track "
               + track.getTrackID());
       try {
         httpClient.postJSONRequest(
             apiBaseURL + "upload?key=" + account.getAPIKey() + "&track=" + track.getTrackID(),
             jsonBff.toString());
       } catch (Exception e) {
         // log.debug("Could not send batch: " + e.getMessage());
         throw e;
       }
       cache.clear(); // only if sending was successful
     } else throw new Exception("Not logged in");
   }
 }
Beispiel #5
0
 public void startTrack(Track track) throws Exception {
   if (authenticated()) {
     String url =
         apiBaseURL
             + "newsession?key="
             + account.getAPIKey()
             + "&"
             + track.getMetaDataString("=", "&", true, new URLUTF8Encoder.URLStringEncoder());
     // log.debug("Starting new track (" + url + ")");
     String response;
     try {
       response = httpClient.getRequest(url);
       if (!response.substring(0, 2).equalsIgnoreCase("ok"))
         throw new Exception("Server response: " + response);
       track.setTrackID(
           Integer.parseInt(response.substring(3, response.length()))); // set the trackID!!!
       log.info("New track started (ID in NoiseTube database: " + track.getTrackID() + ")");
     } catch (Exception e) {
       // log.debug("Could not start track: " + e.getMessage());
       throw e;
     }
   } else throw new Exception("Not logged in");
 }