FITBIT is almost finished
This commit is contained in:
@@ -5,7 +5,6 @@
|
||||
<content url="file://$MODULE_DIR$">
|
||||
<excludeFolder url="file://$MODULE_DIR$/.gradle" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/build" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/out" />
|
||||
</content>
|
||||
<orderEntry type="inheritedJdk" />
|
||||
<orderEntry type="sourceFolder" forTests="false" />
|
||||
|
||||
@@ -5,11 +5,10 @@ public class Main {
|
||||
public static void main(String[] args) throws Exception {
|
||||
AuthFITBIT fitbit = new AuthFITBIT();
|
||||
|
||||
// HeartRate h = fitbit.run("https://api.fitbit.com/1/user/-/activities/heart/date/today/1d.json", HeartRate.class, false); // 1sec/time/00:00/00:01.json
|
||||
// Sleep s = fitbit.run("https://api.fitbit.com/1.2/user/-/sleep/date/today.json", Sleep.class, false);
|
||||
HeartRate h = fitbit.run("https://api.fitbit.com/1/user/-/activities/heart/date/today/1d/1sec/time/11:00/11:45.json", HeartRate.class);
|
||||
//Sleep s = fitbit.run("https://api.fitbit.com/1.2/user/-/sleep/date/today.json", Sleep.class);
|
||||
Device dev = fitbit.run("https://api.fitbit.com/1/user/-/devices.json", Device.class);
|
||||
|
||||
Device dev = fitbit.run("https://api.fitbit.com/1/user/-/devices.json", Device.class, true);
|
||||
//System.out.println(dev);
|
||||
//System.out.println(h.dateTime + " " + h.average);
|
||||
System.out.println(h.dateTime + " " + h.average);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -14,16 +14,11 @@ import com.google.api.client.json.JsonObjectParser;
|
||||
import com.google.api.client.json.jackson2.JacksonFactory;
|
||||
import com.google.api.client.util.store.DataStoreFactory;
|
||||
import com.google.api.client.util.store.FileDataStoreFactory;
|
||||
import com.sun.xml.internal.xsom.impl.scd.Iterators;
|
||||
import manage.FITBITData.Device;
|
||||
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.*;
|
||||
|
||||
|
||||
public class AuthFITBIT {
|
||||
@@ -57,7 +52,7 @@ public class AuthFITBIT {
|
||||
private static FileDataStoreFactory DATA_STORE_FACTORY;
|
||||
|
||||
/** OAuth 2 scope. */
|
||||
private static final String SCOPE[] = new String[]{"activity","heartrate","location","sleep","settings"};
|
||||
private static final String SCOPE[] = new String[]{"activity","heartrate","sleep","settings"};
|
||||
/** Global instance of the HTTP transport. */
|
||||
private static final HttpTransport HTTP_TRANSPORT = new NetHttpTransport();
|
||||
|
||||
@@ -75,7 +70,7 @@ public class AuthFITBIT {
|
||||
HTTP_TRANSPORT,
|
||||
JSON_FACTORY,
|
||||
new GenericUrl(TOKEN_SERVER_URL),
|
||||
new BasicAuthentication(
|
||||
new BasicAuthentication (
|
||||
OAuth2ClientCredentials.API_KEY, OAuth2ClientCredentials.API_SECRET),
|
||||
OAuth2ClientCredentials.API_KEY,
|
||||
AUTHORIZATION_SERVER_URL).setScopes(Arrays.asList(SCOPE))
|
||||
@@ -87,34 +82,28 @@ public class AuthFITBIT {
|
||||
return new AuthorizationCodeInstalledApp(flow, receiver).authorize( "user" );
|
||||
}
|
||||
|
||||
public <O> O run(String url, Class<O> classe, boolean isDev) throws IOException {
|
||||
public <O> O run(String url, Class<O> classe) throws IOException {
|
||||
FITBITUrl fitbitUrl = new FITBITUrl(url);
|
||||
// url.setFields("activity,heartrate,location,sleep");
|
||||
fitbitUrl.setFields("");
|
||||
GenericJson json;
|
||||
|
||||
HttpRequest request = requestFactory.buildGetRequest(fitbitUrl);
|
||||
HttpResponse response = request.execute();
|
||||
O ret = null;
|
||||
|
||||
if (isDev){
|
||||
if (classe.equals(Device.class)) {
|
||||
List<Map<String, String>> arr = response.parseAs(List.class);
|
||||
Device dev = new Device();
|
||||
dev.getLastSyncTime(response.parseAs(Iterators.Array<Map<String, String>>.class));
|
||||
response.disconnect();
|
||||
dev.getLastSyncTime(arr);
|
||||
|
||||
return mapper.readValue(map.toString(), classe);
|
||||
ret = (O)dev;
|
||||
}
|
||||
else {
|
||||
json = response.parseAs(GenericJson.class);
|
||||
ret = mapper.readValue(json.toString(), classe);
|
||||
}
|
||||
|
||||
response.disconnect();
|
||||
|
||||
// System.out.println("--------------------");
|
||||
//System.out.println(classe.getSimpleName());
|
||||
//System.out.println(json.toPrettyString());
|
||||
|
||||
return mapper.readValue(json.toString(), classe);
|
||||
|
||||
// System.out.println(json.toPrettyString());
|
||||
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +0,0 @@
|
||||
package manage.FITBITData;
|
||||
|
||||
public class Activity {
|
||||
}
|
||||
@@ -2,6 +2,7 @@ package manage.FITBITData;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
@JsonIgnoreProperties(ignoreUnknown = true)
|
||||
@@ -10,13 +11,12 @@ public class Device {
|
||||
public String lastSyncTime;
|
||||
|
||||
// @JsonProperty ("lastSyncTime")
|
||||
public String getLastSyncTime(Map<String,String>[] sync){
|
||||
public String getLastSyncTime(List<Map<String,String>> sync){
|
||||
lastSyncTime = null;
|
||||
for(Map<String, String > d: sync) {
|
||||
String temp = d.get("lastSyncTime");
|
||||
if ((lastSyncTime == null) || (lastSyncTime.compareTo(temp) < 0))
|
||||
lastSyncTime = temp;
|
||||
|
||||
}
|
||||
return lastSyncTime;
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ package manage.FITBITData;
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
@JsonIgnoreProperties(ignoreUnknown = true)
|
||||
@@ -17,12 +18,12 @@ public class HeartRate {
|
||||
}
|
||||
|
||||
@JsonProperty("activities-heart-intraday")
|
||||
public void qualcosAltro(Map<String, Map<String, String>[]> map) {
|
||||
Map<String, String>[] data = map.get("dataset");
|
||||
public void qualcosAltro(Map<String, Object> map) {
|
||||
List<Map> data = (List) map.get("dataset");
|
||||
|
||||
int sum = 0;
|
||||
for(Map<String, String> dat: data)
|
||||
sum += Integer.parseInt(dat.get("value"));
|
||||
average = ((double)sum)/data.length;
|
||||
for(Map<String, Object> dat: data)
|
||||
sum += (int)dat.get("value");
|
||||
average = ((double)sum)/data.size();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +0,0 @@
|
||||
package manage.FITBITData;
|
||||
|
||||
public class Location {
|
||||
}
|
||||
Reference in New Issue
Block a user