Added some Docs

This commit is contained in:
2018-08-23 20:35:33 +02:00
parent 09d8b7781f
commit 8feb04990f
14 changed files with 323 additions and 177 deletions

View File

@@ -0,0 +1,9 @@
package device;
public class DialogFlow {
public DialogFlow() {
}
}

View File

@@ -1,97 +0,0 @@
package device;
import java.io.IOException;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;
import device.FITBITData.HeartRate;
import device.FITBITData.Sleep;
import device.FITBITData.Steps;
import oauth.AuthFITBIT;
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 */
public int getSteps() throws IOException {
if(shouldUpdateFor(Steps.class)) {
long currentMillisec = System.currentTimeMillis();
steps = auth.run(BASIC_URL + "1" + USER + "activities/steps/date/today/1w.json", Steps.class);
latestRequest.put(steps.getClass(), currentMillisec);
}
return steps.getSteps();
}
/* 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);
}

View File

@@ -0,0 +1,170 @@
package device;
import java.io.IOException;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;
import device.fitbitdata.HeartRate;
import device.fitbitdata.Sleep;
import device.fitbitdata.Steps;
import oauth.AuthFitbit;
/**
* Classe che permette di ricevere i dati di un particolare account FitBit
*/
public class Fitbit {
/**
* Url da dove si possono prendere i dati dai vari dispositivi fitbit
*/
public static final String BASIC_URL = "https://api.fitbit.com/";
/**
* Utente del fitbit<br>
* In questo caso e' universale e prende l'utente che e' attualmente loggato
*/
public static final String USER = "/user/-/";
/**
* Un minuto in millisecondi
*/
private static final long MINUTE = 60000; /* 5 minutes in millisec */
/**
* L'oauth per l'account fitbit
*/
private final AuthFitbit auth;
/**
* Una mappa contenente le ultime classi usate nelle richieste effettuate<br>
* Una sorta di cache
*/
private final Map<Class<?>, Long> latestRequest = new HashMap<>();
/**
* Un calendario in modo da sapere la data per i dati
*/
private final Calendar calendar = Calendar.getInstance();
/**
* La classe per sapere i dati sul battito cardiaco
*/
private HeartRate heart = null;
/**
* La classe per sapere i dati sul sonno
*/
private Sleep sleep = null;
/**
* La classe per sapere i dati sui passi effettuati
*/
private Steps steps = null;
/**
* Crea una istanza di fitbit<br>
* Se l'utente non ha ancora accettato i dati richiesti o l'utente non e'
* loggato,<br>
* verra' aperto il browser in modo che si possano inserire i dati
*
* @throws Exception Nel caso qualunque cosa andasse storta (vedi messaggio)
*/
public Fitbit() throws Exception {
this.auth = new AuthFitbit();
}
/**
* Ricevi i passi che l'utente ha effettuato nell'ultimo giorno
*
* @return un intero rappresentante i passi effettuati
* @throws IOException nel caso la richiesta non vada a buon fine
*/
public int getSteps() throws IOException {
if (shouldUpdateFor(Steps.class))
steps = auth.run(BASIC_URL + "1" + USER + "activities/steps/date/today/1w.json", Steps.class);
return steps.getSteps();
}
/**
* Ricevi il battito cardiaco dell'utente<br>
* Il risultato e' una media del battito che l'utente ha avuto negli ultimi 15 minuti
*
* @return un intero rappresentante la media del battito cardiaco degli ultimi 15 minuti
* @throws IOException nel caso la richiesta non vada a buon fine
*/
public double getHeartRate() throws IOException {
return getHeartRate(15);
}
/**
* Ricevi il battito cardiaco dell'utente<br>
* Il risultato e' una media del battito che l'utente ha avuto negli ultimi minuti
*
* @param lastMinutes fino a quanti minuti bisogna tenere conto (positivi se no ritorno -1)
* @return un intero rappresentante la media del battito cardiaco degli ultimi minuti specificati
* @throws IOException nel caso la richiesta non vada a buon fine
*/
public double getHeartRate(int lastMinutes) throws IOException {
if(lastMinutes<=0)
return -1;
if (shouldUpdateFor(HeartRate.class)) {
long currentMillisec = System.currentTimeMillis();
String now = getHourMinutes(currentMillisec);
String ago = getHourMinutes(currentMillisec - (MINUTE * lastMinutes));
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);
}
return heart.getAverage();
}
/**
* Ricevi le ore di sonno che l'utente ha fatto nell'ultimo giorno
*
* @return un intero rappresentante le ore passate a dormire
* @throws IOException nel caso la richiesta non vada a buon fine
*/
public Object getHoursSleep() throws IOException {
if (shouldUpdateFor(Sleep.class))
sleep = auth.run(BASIC_URL + "1.2" + USER + "sleep/date/today.json", Sleep.class);
return sleep.getMinutesAsleep();
}
/**
* Semplice classe che controlla che si possa fare l'update o meno di una specifica classe<br>
* Se e' possibile fare l'update inserisce la classe nella mappa<br>
* In questo modo se questa funzione viene chiamata una seconda volta con lo stesso parametro restituira' falso<br>
* a meno che non si aspetti 5 minuti
*
* @param type la classe da fare l'update
* @return vero se si puo' fare l'update
*/
private boolean shouldUpdateFor(Class<?> type) {
try {
long current = System.currentTimeMillis();
long latest = latestRequest.get(type);
if(current - latest < MINUTE * 5)
return false;
} catch (NullPointerException e) {
}
latestRequest.put(heart.getClass(), System.currentTimeMillis());
return true;
}
/**
* Funzione che transforma i millisecondi nel formato "hh:mm"
*
* @param milliseconds millisecondi da trasformare
* @return una stringa nel formato "hh:mm"
*/
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);
}

View File

@@ -5,30 +5,58 @@ import java.util.Set;
import support.Rest;
/**
* Classe che permette di controllare le luci Philips Hue
*/
public class Hue {
//private String baseURL = "192.168.0.2";
//private String username = "C0vPwqjJZo5Jt9Oe5HgO6sBFFMxgoR532IxFoGmx";
private String lightsURL;// = baseURL+"/api/"+username+"/lights/";
/**
* L'url in cui si possono trovare le luci
*/
private String lightsURL; // = baseURL+"/api/"+username+"/lights/";
/**
* Tutte le luci che sono state registrate dall'url
*/
private Map<String, ?> allLights;
public Hue () {
this("http://172.30.1.138/api/C0vPwqjJZo5Jt9Oe5HgO6sBFFMxgoR532IxFoGmx/lights/");
}
/**
* Inizializza la classe cercando tutte le luci all'indirizzo url specificato
*
* @param url l'indirizzo da inserire delle luci Hue
*/
public Hue (String url) {
lightsURL = url;
allLights = Rest.get(lightsURL);
}
/**
* Ritorna un insieme contenente tutti i nomi delle luci che si sono trovate
*
* @return l'insieme dei nomi delle luci
*/
public Set<String> getNameLights() {
return allLights.keySet();
}
/**
* Rimuove dal controllo tutte le luci che hanno il nome uguale ad uno contenuto nell'insieme passato
*
* @param toRemove le luci da rimuovere
*/
public void removeLights(Set<String> toRemove) {
for(String string : toRemove)
allLights.remove(string);
}
/**
* Accende tutte le luci controllate
*/
public void turnOn() {
for (String light : allLights.keySet()) {
String callURL = lightsURL + light + "/state";
@@ -37,9 +65,9 @@ public class Hue {
}
}
public int getCurrentLuminiscence() {
return 0;
}
/**
* Spegne tutte le luci controllate
*/
public void turnOff() {
for (String light : allLights.keySet()) {
String callURL = lightsURL + light + "/state";
@@ -47,8 +75,22 @@ public class Hue {
Rest.put(callURL, body, "application/json");
}
}
/**
* Ritorna la liminosita' attuale delle luci controllate
* @return
*/
public int getCurrentBrightness() {
return 0;
}
/**
* Modifica la luminosita' delle luci a seconda del valore inserito
* @param num la luminosita' che si vuole
*/
public void setBrightness(int num) {
if (num<0)
num=0;
for (String light : allLights.keySet()) {
String callURL = lightsURL + light + "/state";
String body = "{ \"bri\" : "+num+" }";
@@ -56,14 +98,9 @@ public class Hue {
}
}
/*public void setAttribute(String attribute, String value){
for (String light : allLights.keySet()) {
String callURL = lightsURL + light + "/state";
String body = "{ \""+attribute+"\" : "+value+" }";
Rest.put(callURL, body, "application/json");
}
}*/
/**
* Modifica il colore delle luci in modo da fare un bel effetto arcobaleno continuo
*/
public void colorLoop() {
for (String light : allLights.keySet()) {
String callURL = lightsURL + light + "/state";
@@ -72,5 +109,14 @@ public class Hue {
}
}
/**
* Funzione generale per poter utilizzare qualunque valore, ma non funziona
*/
/*public void setAttribute(String attribute, String value){
for (String light : allLights.keySet()) {
String callURL = lightsURL + light + "/state";
String body = "{ \""+attribute+"\" : "+value+" }";
Rest.put(callURL, body, "application/json");
}
}*/
}

View File

@@ -9,8 +9,14 @@ import support.ZWaySimpleCallback;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Sensore che permette di registrare vari dati dell'ambiente
*/
public class Sensor {
// init logger
/**
* Logger?
*/
Logger logger = LoggerFactory.getLogger(Sensor.class);
// sample RaZberry IP address
@@ -24,6 +30,9 @@ public class Sensor {
private DeviceList allZWaveDevices;
private DeviceList devices;
/**
* Crea un sensore contenente tutti i nodi
*/
public Sensor() {
this(null);
}

View File

@@ -1,4 +1,4 @@
package device.FITBITData;
package device.fitbitdata;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

View File

@@ -1,4 +1,4 @@
package device.FITBITData;
package device.fitbitdata;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

View File

@@ -1,4 +1,4 @@
package device.FITBITData;
package device.fitbitdata;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;

View File

@@ -1,4 +1,4 @@
package device.FITBITData;
package device.fitbitdata;
import java.text.SimpleDateFormat;
import java.util.Date;
@@ -20,7 +20,7 @@ public class Steps {
for(Map<String, String> map : array)
if(map.get("dateTime").equals(strDate))
steps = Integer.parseInt(map.get("value"))+1;
steps = Integer.parseInt(map.get("value"));
}
public int getSteps() {

View File

@@ -24,11 +24,11 @@ import com.google.api.client.util.store.DataStoreFactory;
import com.google.api.client.util.store.FileDataStoreFactory;
public class AuthFITBIT {
public class AuthFitbit {
private final HttpRequestFactory requestFactory;
public AuthFITBIT() throws Exception {
public AuthFitbit() throws Exception {
DATA_STORE_FACTORY = new FileDataStoreFactory(DATA_STORE_DIR);
final Credential credential = authorize();

View File

@@ -1,12 +1,12 @@
import org.junit.Test;
import device.FitBit;
import device.Fitbit;
public class TestFitbit {
@Test
public void test01() throws Exception {
FitBit fitBit = new FitBit();
Fitbit fitBit = new Fitbit();
fitBit.getHoursSleep();
System.out.println("Today's average heart-rate: "+fitBit.getHeartRate());

View File

@@ -29,7 +29,7 @@ import java.util.Set;
while(i<999999) {
if (sensor.luminiscenceLevel() < 200) {
lights.getCurrentLuminiscence();
lights.getCurrentBrightness();
lights.setBrightness(200);
}