Init of Database
- don't look in the database, 'cause is a little messy
This commit is contained in:
55
build.gradle
55
build.gradle
@@ -14,33 +14,48 @@ repositories {
|
|||||||
}
|
}
|
||||||
|
|
||||||
dependencies {
|
dependencies {
|
||||||
|
// Tests
|
||||||
testCompile group: 'junit', name: 'junit', version: '4.12'
|
testCompile group: 'junit', name: 'junit', version: '4.12'
|
||||||
compile "com.sparkjava:spark-core:2.7.2"
|
// implementation 'junit:junit:4.12'
|
||||||
compile "com.google.code.gson:gson:2.8.4"
|
|
||||||
// compile "org.xerial:sqlite-jdbc:3.21.0.1" //todo remove commenting because "You need to install an appropriate JDBC (Java Database Connectivity) driver to run your Java database programs."
|
|
||||||
compile 'org.apache.httpcomponents:httpclient:4.5.3'
|
|
||||||
compile 'com.google.api-client:google-api-client:1.23.0'
|
|
||||||
compile group: 'com.google.oauth-client', name: 'google-oauth-client-jetty', version: '1.11.0-beta'
|
|
||||||
|
|
||||||
|
// GSON but works even without it anyway
|
||||||
|
// compile "com.google.code.gson:gson:2.8.4"
|
||||||
|
|
||||||
// z-way-lib and all its dependencies (from https://github.com/pathec/ZWay-library-for-Java)
|
// Database
|
||||||
|
compile "org.xerial:sqlite-jdbc:3.21.0.1"
|
||||||
|
// compile group: 'mysql', name: 'mysql-connector-java', version: '8.0.12'
|
||||||
|
|
||||||
|
// Rest request
|
||||||
|
compile 'org.apache.httpcomponents:httpclient:4.5.6'
|
||||||
|
|
||||||
|
// Z-way
|
||||||
compile files('lib/zway-lib-0.2.9-SNAPSHOT.jar')
|
compile files('lib/zway-lib-0.2.9-SNAPSHOT.jar')
|
||||||
|
|
||||||
compile 'org.apache.commons:commons-lang3:3.4'
|
// Logger
|
||||||
compile 'org.eclipse.jetty:jetty-client:9.4.11.v20180605'
|
compile 'org.slf4j:slf4j-simple:1.7.25'
|
||||||
compile 'org.eclipse.jetty:jetty-http:9.4.11.v20180605'
|
|
||||||
compile 'org.eclipse.jetty:jetty-io:9.4.11.v20180605'
|
|
||||||
compile 'org.eclipse.jetty:jetty-util:9.4.11.v20180605'
|
|
||||||
compile 'org.eclipse.jetty.websocket:websocket-api:9.4.11.v20180605'
|
|
||||||
compile 'org.eclipse.jetty.websocket:websocket-client:9.4.11.v20180605'
|
|
||||||
compile 'org.eclipse.jetty.websocket:websocket-common:9.4.11.v20180605'
|
|
||||||
compile 'org.slf4j:slf4j-simple:1.7.21'
|
|
||||||
|
|
||||||
//DialogFlow
|
// Server Spark
|
||||||
|
compile "com.sparkjava:spark-core:2.7.2"
|
||||||
|
// compile 'org.apache.commons:commons-lang3:3.4'
|
||||||
|
// compile group: 'org.eclipse.jetty', name: 'jetty-webapp', version: '9.4.11.v20180605'
|
||||||
|
// compile 'org.eclipse.jetty:jetty-client:9.4.11.v20180605'
|
||||||
|
// compile 'org.eclipse.jetty.websocket:websocket-server:9.4.11.v20180605'
|
||||||
|
|
||||||
|
// Oauth
|
||||||
|
compile( group: 'com.google.oauth-client', name: 'google-oauth-client-jetty', version: '1.23.0') {
|
||||||
|
// don't pull in an old ancient jetty version
|
||||||
|
// todo it doesn't seems to work
|
||||||
|
exclude(group: 'org.mortbay.jetty', module: 'jetty')
|
||||||
|
exclude(group: 'org.mortbay.jetty', module: 'jetty-util')
|
||||||
|
exclude(group: 'org.mortbay.jetty', module: 'servlet-api')
|
||||||
|
}
|
||||||
|
compile 'com.google.oauth-client:google-oauth-client-jetty:1.23.0'
|
||||||
|
compile 'com.google.api-client:google-api-client:1.23.0'
|
||||||
|
|
||||||
|
// DialogFlow
|
||||||
compile "ai.api:libai:1.6.12"
|
compile "ai.api:libai:1.6.12"
|
||||||
// compile 'com.google.cloud:google-cloud-dialogflow:0.59.0-alpha' // for the v2 of dialogflow
|
// compile 'com.google.cloud:google-cloud-dialogflow:0.59.0-alpha' // for the v2 of dialogflow
|
||||||
|
|
||||||
//for objectMapper
|
// objectMapper for fitbitdata
|
||||||
compile group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: '2.9.5'
|
compile group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: '2.9.5' // maybe duplicate in google-api
|
||||||
implementation 'junit:junit:4.12'
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,6 +9,10 @@ import support.Rest;
|
|||||||
* Classe che permette di controllare le luci Philips Hue
|
* Classe che permette di controllare le luci Philips Hue
|
||||||
*/
|
*/
|
||||||
public class Hue {
|
public class Hue {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* La luminopsita' massima a cui si puo' arrivare
|
||||||
|
*/
|
||||||
public static final int MAX_BRIGHTNESS = 255;
|
public static final int MAX_BRIGHTNESS = 255;
|
||||||
//private String baseURL = "192.168.0.2";
|
//private String baseURL = "192.168.0.2";
|
||||||
//private String username = "C0vPwqjJZo5Jt9Oe5HgO6sBFFMxgoR532IxFoGmx";
|
//private String username = "C0vPwqjJZo5Jt9Oe5HgO6sBFFMxgoR532IxFoGmx";
|
||||||
@@ -22,12 +26,28 @@ public class Hue {
|
|||||||
*/
|
*/
|
||||||
private Map<String, ?> allLights;
|
private Map<String, ?> allLights;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* L'ultima luminosita' impostata
|
||||||
|
*/
|
||||||
private int brightness = 0;
|
private int brightness = 0;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Cerca le luci Philips Hue a ll'indirizzo <a href="http://172.30.1.138/api/C0vPwqjJZo5Jt9Oe5HgO6sBFFMxgoR532IxFoGmx/lights/">http://172.30.1.138/api/C0vPwqjJZo5Jt9Oe5HgO6sBFFMxgoR532IxFoGmx/lights/</a>
|
||||||
|
*/
|
||||||
public Hue () {
|
public Hue () {
|
||||||
this("http://172.30.1.138/api/C0vPwqjJZo5Jt9Oe5HgO6sBFFMxgoR532IxFoGmx/lights/");
|
this("172.30.1.138", "C0vPwqjJZo5Jt9Oe5HgO6sBFFMxgoR532IxFoGmx");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//todo maybe the key is the user, but who knows?
|
||||||
|
/**
|
||||||
|
* Cerca le luci Philips Hue nell'indirizzo specificato e con la chiave specificata
|
||||||
|
* @param ip l'indirizzo IP
|
||||||
|
* @param key la chiuave utililzzata
|
||||||
|
*/
|
||||||
|
public Hue(String ip, String key) {
|
||||||
|
this("http://" + ip + "/api/" + key + "/lights/");
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Inizializza la classe cercando tutte le luci all'indirizzo url specificato
|
* Inizializza la classe cercando tutte le luci all'indirizzo url specificato
|
||||||
*
|
*
|
||||||
@@ -36,7 +56,7 @@ public class Hue {
|
|||||||
public Hue (String url) {
|
public Hue (String url) {
|
||||||
lightsURL = url;
|
lightsURL = url;
|
||||||
allLights = Rest.get(lightsURL);
|
allLights = Rest.get(lightsURL);
|
||||||
// Todo brightness initial
|
// Todo brightness initial, maybe by default 50% or 75% of the total
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -1,18 +1,53 @@
|
|||||||
package main;
|
package main;
|
||||||
|
|
||||||
import device.*;
|
import device.*;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
import support.DBConnect;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Created by 20015159 on 28/08/2018.
|
* Created by 20015159 on 28/08/2018.
|
||||||
*/
|
*/
|
||||||
public class Main {
|
public class Main {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Un Logger per capire meglio quali pezzi vengono eseguiti e quali no.
|
||||||
|
*/
|
||||||
|
private static Logger log = LoggerFactory.getLogger("SeniorAssistant");
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Funzione principale, qui si creano tutte le classi che verranno utilizzate.
|
||||||
|
* @param args per ora nulla, ma forse in futuro si potrebbe mettere roba
|
||||||
|
*/
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
|
log.info("Connecting to hue lights");
|
||||||
|
Hue lights = new Hue();
|
||||||
|
log.info("Connecting to the sensors");
|
||||||
|
Sensor sensor = new Sensor();
|
||||||
|
|
||||||
|
try {
|
||||||
|
log.info("Connecting to Fitbit");
|
||||||
|
Fitbit fitbit = new Fitbit();
|
||||||
|
startInsertData(fitbit); // add here functions associated with fitbit
|
||||||
|
} catch (Exception e) { // in this way the program will continue without fitbit
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
|
startWebhook(lights);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Fa partire il server Webhook per DialogFlow e continua l'esecuzione
|
||||||
|
* @param hue Le luci che vengono modificate a seconda delle richieste dell'utente
|
||||||
|
*/
|
||||||
|
private static void startWebhook(Hue hue) {
|
||||||
|
log.info("Adding actions to Webhook");
|
||||||
DialogFlowWebHook df = new DialogFlowWebHook();
|
DialogFlowWebHook df = new DialogFlowWebHook();
|
||||||
|
|
||||||
df.addOnAction("LightsON", () -> {return "Luci accese";});
|
df.addOnAction("LightsON", () -> {hue.turnOn(); return "Luci accese";});
|
||||||
df.addOnAction("LightsOFF", () -> {return "Luci spente";});
|
df.addOnAction("LightsOFF", () -> {hue.turnOff(); return "Luci spente";});
|
||||||
|
|
||||||
|
log.info("Starting Webhook");
|
||||||
df.startServer();
|
df.startServer();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -24,7 +59,8 @@ public class Main {
|
|||||||
* (magari ci si calcola quando bisogna risvegliarsi e si mette un wait)
|
* (magari ci si calcola quando bisogna risvegliarsi e si mette un wait)
|
||||||
* @param fibit da dove prende i dati
|
* @param fibit da dove prende i dati
|
||||||
*/
|
*/
|
||||||
private void startDb(Fitbit fibit) {
|
private static void startInsertData(Fitbit fibit) {
|
||||||
|
log.info("Connecting to DB to write fitbit data periodically");
|
||||||
/*
|
/*
|
||||||
try {
|
try {
|
||||||
Connection conn = DBConnect.getInstance().getConnection();
|
Connection conn = DBConnect.getInstance().getConnection();
|
||||||
|
|||||||
@@ -1,8 +1,6 @@
|
|||||||
package support;
|
package support;
|
||||||
|
|
||||||
import java.sql.Connection;
|
import java.sql.*;
|
||||||
import java.sql.DriverManager;
|
|
||||||
import java.sql.SQLException;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Handle the connection to the SQLite database that stores our tasks.
|
* Handle the connection to the SQLite database that stores our tasks.
|
||||||
@@ -12,28 +10,31 @@ import java.sql.SQLException;
|
|||||||
public class DBConnect {
|
public class DBConnect {
|
||||||
|
|
||||||
// todo add a db where we put daily (or hourly, but only for heart) updates
|
// todo add a db where we put daily (or hourly, but only for heart) updates
|
||||||
static private final String DB_LOCATION = "jdbc:sqlite:src/main/resources/tasks.db";
|
public static final String DB_LOCATION = "jdbc:sqlite:src/main/resources/";
|
||||||
static private DBConnect instance = null;
|
public static final String DB_NAME = "user_data.db";
|
||||||
|
|
||||||
private DBConnect() {
|
private static DBConnect instance;
|
||||||
instance = this;
|
|
||||||
|
private final Connection conn;
|
||||||
|
|
||||||
|
private DBConnect() throws SQLException {
|
||||||
|
conn = DriverManager.getConnection(DB_LOCATION + DB_NAME);
|
||||||
|
buildTablesIfNotExisting();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static DBConnect getInstance() {
|
public static DBConnect getInstance() {
|
||||||
return (instance == null ? new DBConnect() : instance);
|
try {
|
||||||
|
instance = instance==null? new DBConnect():instance;
|
||||||
|
} catch (SQLException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
return instance;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Connection getConnection() throws SQLException {
|
private void buildTablesIfNotExisting() throws SQLException {
|
||||||
try {
|
Statement statement = conn.createStatement();
|
||||||
/* todo this might work for create the database
|
// todo working, but not quite well
|
||||||
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/");
|
statement.execute("CREATE TABLE IF NOT EXISTS user (user VARCHAR(16) PRIMARY KEY, name VARCHAR(16), birthday DATE);");
|
||||||
Statement s = Conn.createStatement();
|
statement.execute("CREATE TABLE IF NOT EXISTS heart_rate (date DATE, rate DOUBLE, user VARCHAR(16), PRIMARY KEY(date, user));");
|
||||||
int result = s.executeUpdate("CREATE DATABASE databasename");
|
|
||||||
st.close();
|
|
||||||
*/
|
|
||||||
return DriverManager.getConnection(DB_LOCATION);
|
|
||||||
} catch (SQLException e) {
|
|
||||||
throw new SQLException("Cannot get connection to " + DB_LOCATION, e);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
BIN
src/main/resources/user_data.db
Normal file
BIN
src/main/resources/user_data.db
Normal file
Binary file not shown.
Reference in New Issue
Block a user