Added new Class for FitBit and Fixes
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -28,6 +28,7 @@ hs_err_pid*
|
|||||||
.idea/
|
.idea/
|
||||||
.idea/*.xml
|
.idea/*.xml
|
||||||
.gradle/
|
.gradle/
|
||||||
|
.iml
|
||||||
|
|
||||||
# eclipse things #
|
# eclipse things #
|
||||||
.classpath
|
.classpath
|
||||||
|
|||||||
@@ -5,6 +5,7 @@
|
|||||||
<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" />
|
||||||
|
|||||||
@@ -3,12 +3,12 @@ import manage.FITBITData.*;
|
|||||||
|
|
||||||
public class Main {
|
public class Main {
|
||||||
public static void main(String[] args) throws Exception {
|
public static void main(String[] args) throws Exception {
|
||||||
AuthFITBIT fitbit = new AuthFITBIT();
|
FitBit fitBit = new FitBit();
|
||||||
|
fitBit.getHoursSleep();
|
||||||
|
|
||||||
HeartRate h = fitbit.run("https://api.fitbit.com/1/user/-/activities/heart/date/today/1d/1sec/time/11:00/11:45.json", HeartRate.class);
|
System.out.println("Today's average heart-rate: "+fitBit.getHeartRate());
|
||||||
//Sleep s = fitbit.run("https://api.fitbit.com/1.2/user/-/sleep/date/today.json", Sleep.class);
|
System.out.println("Today's hours of sleep: "+fitBit.getHoursSleep());
|
||||||
Device dev = fitbit.run("https://api.fitbit.com/1/user/-/devices.json", Device.class);
|
System.out.println("Today's steps: "+fitBit.getSteps());
|
||||||
|
System.out.println("Fine.");
|
||||||
System.out.println(h.dateTime + " " + h.average);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -82,28 +82,36 @@ 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) throws IOException {
|
public <O> O run(String url, Class<O> returnClass) throws IOException {
|
||||||
|
return run(url, returnClass, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
public <O> O run(String url, Class<O> returnClass, boolean useAsParse) throws IOException {
|
||||||
FITBITUrl fitbitUrl = new FITBITUrl(url);
|
FITBITUrl fitbitUrl = new FITBITUrl(url);
|
||||||
fitbitUrl.setFields("");
|
fitbitUrl.setFields("");
|
||||||
GenericJson json;
|
|
||||||
|
|
||||||
HttpRequest request = requestFactory.buildGetRequest(fitbitUrl);
|
HttpRequest request = requestFactory.buildGetRequest(fitbitUrl);
|
||||||
HttpResponse response = request.execute();
|
HttpResponse response = request.execute();
|
||||||
O ret = null;
|
|
||||||
|
|
||||||
if (classe.equals(Device.class)) {
|
/*
|
||||||
List<Map<String, String>> arr = response.parseAs(List.class);
|
GenericJson json = response.parseAs(GenericJson.class);
|
||||||
Device dev = new Device();
|
response.disconnect();
|
||||||
dev.getLastSyncTime(arr);
|
|
||||||
|
|
||||||
ret = (O)dev;
|
System.out.println(returnClass.getSimpleName());
|
||||||
}
|
System.out.println(url);
|
||||||
else {
|
System.out.println(json.toPrettyString());
|
||||||
json = response.parseAs(GenericJson.class);
|
|
||||||
ret = mapper.readValue(json.toString(), classe);
|
return mapper.readValue(json.toString(), returnClass);
|
||||||
}
|
*/
|
||||||
|
|
||||||
|
/**/
|
||||||
|
O ret = ( useAsParse ?
|
||||||
|
response.parseAs(returnClass) :
|
||||||
|
mapper.readValue(response.parseAs(GenericJson.class).toString(), returnClass)
|
||||||
|
);
|
||||||
|
|
||||||
response.disconnect();
|
response.disconnect();
|
||||||
return ret;
|
return ret;
|
||||||
|
/**/
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
95
src/main/java/manage/FITBITData/FitBit.java
Normal file
95
src/main/java/manage/FITBITData/FitBit.java
Normal file
@@ -0,0 +1,95 @@
|
|||||||
|
package manage.FITBITData;
|
||||||
|
|
||||||
|
import manage.AuthFITBIT;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.Calendar;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
public class FitBit {
|
||||||
|
|
||||||
|
public static final String BASIC_URL = "https://api.fitbit.com/";
|
||||||
|
public static final String USER = "/user/-/";
|
||||||
|
private static final long MINUTE = 60000; /* 5 minutes in millisec */
|
||||||
|
|
||||||
|
private final AuthFITBIT auth;
|
||||||
|
private final Map<Class, Long> latestRequest = new HashMap<>();
|
||||||
|
private final Calendar calendar = Calendar.getInstance();
|
||||||
|
|
||||||
|
private HeartRate heart = null;
|
||||||
|
private Sleep sleep = null;
|
||||||
|
private Steps steps = null;
|
||||||
|
|
||||||
|
public FitBit() throws Exception {
|
||||||
|
this(new AuthFITBIT());
|
||||||
|
}
|
||||||
|
|
||||||
|
public FitBit(AuthFITBIT auth) {
|
||||||
|
if(auth == null)
|
||||||
|
throw new NullPointerException("I must have an Auth for the FitBit");
|
||||||
|
this.auth = auth;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* passi */
|
||||||
|
//https://api.fitbit.com/1/user/-/activities/steps/date/today.json
|
||||||
|
public int getSteps() throws IOException {
|
||||||
|
if(shouldUpdateFor(Steps.class)) {
|
||||||
|
long currentMillisec = System.currentTimeMillis();
|
||||||
|
|
||||||
|
steps = auth.run(BASIC_URL + "1" + USER + "activities/steps/date/today.json", Steps.class);
|
||||||
|
latestRequest.put(steps.getClass(), currentMillisec);
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* battito */
|
||||||
|
public double getHeartRate() throws IOException {
|
||||||
|
if(shouldUpdateFor(HeartRate.class)) {
|
||||||
|
long currentMillisec = System.currentTimeMillis();
|
||||||
|
|
||||||
|
String now = getHourMinutes(currentMillisec);
|
||||||
|
String ago = getHourMinutes(currentMillisec-(MINUTE*15));
|
||||||
|
|
||||||
|
if(now.compareTo(ago) < 0)
|
||||||
|
ago = "00:00";
|
||||||
|
|
||||||
|
heart = auth.run(BASIC_URL + "1" + USER + "activities/heart/date/today/1d/1sec/time/"+ago+"/"+now+".json", HeartRate.class);
|
||||||
|
latestRequest.put(heart.getClass(), currentMillisec);
|
||||||
|
}
|
||||||
|
return heart.getAverage();
|
||||||
|
}
|
||||||
|
|
||||||
|
/* sonno */
|
||||||
|
public Object getHoursSleep() throws IOException {
|
||||||
|
if(shouldUpdateFor(Sleep.class)) {
|
||||||
|
long currentMillisec = System.currentTimeMillis();
|
||||||
|
|
||||||
|
sleep = auth.run(BASIC_URL + "1.2" + USER + "sleep/date/today.json", Sleep.class);
|
||||||
|
latestRequest.put(sleep.getClass(), currentMillisec);
|
||||||
|
}
|
||||||
|
return sleep.getMinutesAsleep();
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean shouldUpdateFor(Class type) {
|
||||||
|
try {
|
||||||
|
long current = System.currentTimeMillis();
|
||||||
|
long latest = latestRequest.get(type);
|
||||||
|
|
||||||
|
if (current - latest > MINUTE * 5)
|
||||||
|
return true;
|
||||||
|
return false;
|
||||||
|
} catch (NullPointerException e) {}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
private String getHourMinutes(long milliseconds) {
|
||||||
|
calendar.setTimeInMillis(milliseconds);
|
||||||
|
|
||||||
|
int hour = calendar.get(Calendar.HOUR_OF_DAY);
|
||||||
|
int minu = calendar.get(Calendar.MINUTE);
|
||||||
|
return String.format("%02d:%02d", hour, minu);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Device dev = auth.run(BASIC_URL + "devices.json", Device.class);
|
||||||
|
}
|
||||||
@@ -9,8 +9,12 @@ import java.util.Map;
|
|||||||
@JsonIgnoreProperties(ignoreUnknown = true)
|
@JsonIgnoreProperties(ignoreUnknown = true)
|
||||||
public class HeartRate {
|
public class HeartRate {
|
||||||
|
|
||||||
public String dateTime;
|
private String dateTime;
|
||||||
public double average;
|
private double average;
|
||||||
|
|
||||||
|
public double getAverage() {
|
||||||
|
return average;
|
||||||
|
}
|
||||||
|
|
||||||
@JsonProperty("activities-heart")
|
@JsonProperty("activities-heart")
|
||||||
public void quelloCheVoglio(Map<String, Object>[] activities){
|
public void quelloCheVoglio(Map<String, Object>[] activities){
|
||||||
@@ -18,12 +22,15 @@ public class HeartRate {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@JsonProperty("activities-heart-intraday")
|
@JsonProperty("activities-heart-intraday")
|
||||||
public void qualcosAltro(Map<String, Object> map) {
|
public void setAverage(Map<String, Object> map) {
|
||||||
List<Map> data = (List) map.get("dataset");
|
List<Map> data = (List) map.get("dataset");
|
||||||
|
|
||||||
int sum = 0;
|
int sum = 0;
|
||||||
for(Map<String, Object> dat: data)
|
for(Map<String, Object> dat: data)
|
||||||
sum += (int)dat.get("value");
|
sum += (int)dat.get("value");
|
||||||
average = ((double)sum)/data.size();
|
average = ((double)sum)/data.size();
|
||||||
|
|
||||||
|
if(data.size() == 0)
|
||||||
|
average = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,10 +3,19 @@ package manage.FITBITData;
|
|||||||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
|
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
|
||||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||||
|
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
@JsonIgnoreProperties(ignoreUnknown = true)
|
@JsonIgnoreProperties(ignoreUnknown = true)
|
||||||
public class Sleep {
|
public class Sleep {
|
||||||
|
|
||||||
public String time;
|
private int minutesAsleep;
|
||||||
|
|
||||||
|
|
||||||
|
public int getMinutesAsleep() {
|
||||||
|
return minutesAsleep;
|
||||||
|
}
|
||||||
|
|
||||||
|
@JsonProperty("summary")
|
||||||
|
public void setMinutesAsleep(Map<String, Object> map) {
|
||||||
|
minutesAsleep = (int) map.get("totalMinutesAsleep");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
5
src/main/java/manage/FITBITData/Steps.java
Normal file
5
src/main/java/manage/FITBITData/Steps.java
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
package manage.FITBITData;
|
||||||
|
|
||||||
|
public class Steps {
|
||||||
|
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user