- fixed some conversion
- fixed DB
- audio now use Javax instead of sun
- wav now works (apparently they must be at 16bit)
This commit is contained in:
2018-09-24 17:24:59 +02:00
parent 75ebc025a3
commit abd68e0eb1
18 changed files with 62 additions and 31 deletions

View File

@@ -147,9 +147,9 @@ public class Fitbit {
* *
* @return un intero rappresentante le ore passate a dormire * @return un intero rappresentante le ore passate a dormire
*/ */
public synchronized long getHoursSleep() { public synchronized double getHoursSleep() {
sleep = update(Sleep.class, sleep,"1.2" + USER + "sleep/date/today.json"); sleep = update(Sleep.class, sleep,"1.2" + USER + "sleep/date/today.json");
return sleep.getMinutesAsleep()/60; return (double)sleep.getMinutesAsleep()/60;
} }
/** /**

View File

@@ -16,13 +16,13 @@ import java.util.Map;
@JsonIgnoreProperties(ignoreUnknown = true) @JsonIgnoreProperties(ignoreUnknown = true)
public class Sleep { public class Sleep {
private long minutesAsleep; private int minutesAsleep;
private List<SleepData> datas; private List<SleepData> datas;
@JsonProperty("summary") @JsonProperty("summary")
private void setMinutesAsleep(Map<String, Object> map) { private void setMinutesAsleep(Map<String, Object> map) {
minutesAsleep = (long) map.get("totalMinutesAsleep"); minutesAsleep = (int) map.get("totalMinutesAsleep");
} }
@JsonProperty("sleep") @JsonProperty("sleep")

View File

@@ -70,6 +70,8 @@ public class SeniorAssistant {
LOG.info("Connessione al database..."); LOG.info("Connessione al database...");
Database database = remoteDbUser == null ? new LocalDB() : new RemoteDB(remoteDbUser); Database database = remoteDbUser == null ? new LocalDB() : new RemoteDB(remoteDbUser);
if(remoteDbUser != null && !database.isReachable())
database = new LocalDB();
threads.startInsertData(database, fitbit); threads.startInsertData(database, fitbit);
threads.startHueControlledByHeartBeat(lights, fitbit, database); threads.startHueControlledByHeartBeat(lights, fitbit, database);

View File

@@ -8,7 +8,6 @@ import device.fitbitdata.HeartRate;
import device.fitbitdata.Steps; import device.fitbitdata.Steps;
import support.audio.Audio; import support.audio.Audio;
import support.audio.AudioFile; import support.audio.AudioFile;
import support.audio.Musich;
import support.database.Database; import support.database.Database;
import java.sql.Timestamp; import java.sql.Timestamp;
@@ -102,8 +101,8 @@ public class VariousThreads {
return String.format("Oggi hai percorso circa %.2f kilometri", steps/2000); return String.format("Oggi hai percorso circa %.2f kilometri", steps/2000);
}); });
df.addOnAction("ReqSleep", (params) -> { df.addOnAction("ReqSleep", (params) -> {
long sleep = fitbit.getHoursSleep(); double sleep = fitbit.getHoursSleep();
return String.format("Oggi hai dormito per %.2f ore", (double)sleep/3600000); return String.format("Oggi hai dormito per %.2f ore", sleep);
}); });
df.startServer(); df.startServer();
@@ -172,7 +171,6 @@ public class VariousThreads {
public void startHueControlledByHeartBeat(final Hue lights, final Fitbit fitbit, final Database database) { public void startHueControlledByHeartBeat(final Hue lights, final Fitbit fitbit, final Database database) {
final int minutes = 30; final int minutes = 30;
final int delta = 15; final int delta = 15;
final Audio audio = new AudioFile();
Thread thread = getThreadStartingEach(new Runnable() { Thread thread = getThreadStartingEach(new Runnable() {
@Override @Override
public synchronized void run() { public synchronized void run() {
@@ -198,11 +196,11 @@ public class VariousThreads {
if (Math.abs(rateNow-average) > delta ) { // ALTO if (Math.abs(rateNow-average) > delta ) { // ALTO
lights.decreaseBrightness(); lights.decreaseBrightness();
audio.playRandom("relax"); audio.playRandom("relax");
new AudioFile().play("molti battiti.wav"); new AudioFile("molti battiti.wav");
} }
else if (Math.abs(rateNow-average) < delta) { // BASSO avvisa ma niente musica else if (Math.abs(rateNow-average) < delta) { // BASSO avvisa ma niente musica
lights.increaseBrightness(); lights.increaseBrightness();
audio.play("pochi battiti.wav"); new AudioFile("pochi battiti.wav");
} }
} }
}, minutes, "lights-with-heartbeat"); }, minutes, "lights-with-heartbeat");
@@ -219,7 +217,6 @@ public class VariousThreads {
public void startCheckSteps(final Database database) { public void startCheckSteps(final Database database) {
final int norm = 4500; final int norm = 4500;
final int delta = 1500; // average steps for 60 year old -> 3.500-5.500 or so they say final int delta = 1500; // average steps for 60 year old -> 3.500-5.500 or so they say
final Audio audio = new AudioFile();
Thread thread = getThreadStartingAt(new Runnable() { Thread thread = getThreadStartingAt(new Runnable() {
@Override @Override
@@ -233,9 +230,9 @@ public class VariousThreads {
final long average = size!=0? (long)(sum/size):0; final long average = size!=0? (long)(sum/size):0;
if (Math.abs(norm-average) > delta ) if (Math.abs(norm-average) > delta )
audio.play("molti passi.wav"); new AudioFile("molti passi.wav");
else if (Math.abs(norm-average) < delta) else if (Math.abs(norm-average) < delta)
audio.play("pochi passi.wav"); new AudioFile("pochi passi.wav");
} }
}, 20, "checking-steps"); }, 20, "checking-steps");

View File

@@ -1,11 +1,10 @@
package support.audio; package support.audio;
import sun.audio.AudioPlayer; import javax.sound.sampled.AudioInputStream;
import sun.audio.AudioStream; import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.LineUnavailableException;
import java.io.File; import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.HashMap; import java.util.HashMap;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
@@ -24,7 +23,7 @@ public class AudioFile implements Audio {
/** /**
* L'ultimo audio fatto partire * L'ultimo audio fatto partire
*/ */
private AudioStream lastIn = null; private final Clip clip;
/** /**
* Serve per crearsi una mappa di tutte le canzoni * Serve per crearsi una mappa di tutte le canzoni
@@ -36,6 +35,28 @@ public class AudioFile implements Audio {
*/ */
private final static Map<String, List<File>> dirs = getAllDirs(PATH_AUDIO); private final static Map<String, List<File>> dirs = getAllDirs(PATH_AUDIO);
/**
* Crea un oggetto audio che si puo' poi far riprodurre e stoppare
*/
public AudioFile() {
Clip clip = null;
try {
clip = AudioSystem.getClip();
} catch (LineUnavailableException e) {
e.printStackTrace();
}
this.clip = clip;
}
/**
* Utilizzando questo costruttore si fa partire in automatico il file scelto
* @param file il nome del file scelto da far partire subito
*/
public AudioFile(String file) {
this();
this.play(file);
}
/** /**
* Fa partire una canzone che si trova nella cartella audio o in una delle sue sottocartelle * Fa partire una canzone che si trova nella cartella audio o in una delle sue sottocartelle
* @param name la stringa per far partire la canzone * @param name la stringa per far partire la canzone
@@ -45,10 +66,10 @@ public class AudioFile implements Audio {
stop(); stop();
try { try {
File file = files.get(name); File file = files.get(name);
lastIn = new AudioStream( new FileInputStream(file)); AudioInputStream audioIn = AudioSystem.getAudioInputStream(file);
AudioPlayer.player.start(lastIn); clip.open(audioIn);
clip.start();
} catch (IOException e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }
} }
@@ -66,12 +87,11 @@ public class AudioFile implements Audio {
@Override @Override
public void stop() { public void stop() {
try { try {
AudioPlayer.player.stop(lastIn); clip.stop();
lastIn.close(); clip.close();
lastIn = null; } catch (Exception e) {
} catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} catch (NullPointerException e) {} }
} }
/** /**

View File

@@ -20,7 +20,7 @@ public class LocalDB implements Database {
/** /**
* Il percorso dove trovare il database, strutturato in: &lt;interfaccia&gt;:&lt;implementazione&gt;:&lt;percorso vero e proprio&gt; * Il percorso dove trovare il database, strutturato in: &lt;interfaccia&gt;:&lt;implementazione&gt;:&lt;percorso vero e proprio&gt;
*/ */
public static final String DB_LOCATION = "jdbc:sqlite:"; public static final String DB_LOCATION = "jdbc:sqlite:src/main/resources/";
/** /**
* Il nome del database (aka il nome del file) * Il nome del database (aka il nome del file)

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@@ -1,2 +1,2 @@
org.slf4j.simpleLogger.defaultLogLevel=warn org.slf4j.simpleLogger.defaultLogLevel=info
org.slf4j.simpleLogger.logFile=System.out org.slf4j.simpleLogger.logFile=System.out

Binary file not shown.

View File

@@ -21,7 +21,7 @@ public class TestMusich {
@Test @Test
public void test2() { public void test2() {
AudioFile audio = new AudioFile(); AudioFile audio = new AudioFile();
audio.play("Godzilla.wav"); // apparently it doesn't like some wav audio.play("Godzilla.wav");
waitAndPrint(3); waitAndPrint(3);
audio.play("Tullio.wav"); audio.play("Tullio.wav");
waitAndPrint(10); waitAndPrint(10);
@@ -46,6 +46,18 @@ public class TestMusich {
audio2.stop(); audio2.stop();
} }
@Test
public void test4() {
AudioFile audio = new AudioFile();
new AudioFile("pochi passi.wav");
waitAndPrint(3);
audio.play("molti battiti.wav");
waitAndPrint(3);
audio.stop();
}
public void waitAndPrint(Integer seconds) { public void waitAndPrint(Integer seconds) {
if(seconds != null) synchronized (seconds) { if(seconds != null) synchronized (seconds) {
try { try {