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