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"); }
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"); }
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"); } }
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"); }
public void update(Measurement newMeasurement, Track track) { // When we haven't stored the last X measurements since we chose to tag some of them... if (needsNewTrack == true) { // ... we need to store them and make sure no new ones will be stored. needsNewTrack = false; Enumeration enumerator = track.getMeasurementsNewestFirst(); int mnumber = 0; if (enumerator.hasMoreElements()) // We skip the newest element, since this element is the first measurement recorded when we // where in the // current screen. This element should not be part of the tagging, since the user didn't saw // this element // yet when he chose to tag an interval. enumerator.nextElement(); while (mnumber++ < MAX_TAGGABLE_MEASUREMENTS_SERIES && enumerator.hasMoreElements()) { tagQueue.offer(enumerator.nextElement()); } } }