From 1f4a4b80c0e452ecc2b8d04f8d2b621a233a61b9 Mon Sep 17 00:00:00 2001 From: Giacomo Date: Wed, 29 Aug 2018 23:35:27 +0200 Subject: [PATCH] Init of Database - don't look in the database, 'cause is a little messy --- build.gradle | 55 +++++++++++++++++---------- src/main/java/device/Hue.java | 26 +++++++++++-- src/main/java/main/Main.java | 42 ++++++++++++++++++-- src/main/java/support/DBConnect.java | 41 ++++++++++---------- src/main/resources/user_data.db | Bin 0 -> 20480 bytes 5 files changed, 118 insertions(+), 46 deletions(-) create mode 100644 src/main/resources/user_data.db diff --git a/build.gradle b/build.gradle index 1214e34..e9b1313 100644 --- a/build.gradle +++ b/build.gradle @@ -14,33 +14,48 @@ repositories { } dependencies { + // Tests testCompile group: 'junit', name: 'junit', version: '4.12' - compile "com.sparkjava:spark-core:2.7.2" - 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' +// implementation 'junit:junit:4.12' + // 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 'org.apache.commons:commons-lang3:3.4' - compile 'org.eclipse.jetty:jetty-client:9.4.11.v20180605' - 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' + // Logger + compile 'org.slf4j:slf4j-simple:1.7.25' - //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 'com.google.cloud:google-cloud-dialogflow:0.59.0-alpha' // for the v2 of dialogflow - //for objectMapper - compile group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: '2.9.5' - implementation 'junit:junit:4.12' + // objectMapper for fitbitdata + compile group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: '2.9.5' // maybe duplicate in google-api } diff --git a/src/main/java/device/Hue.java b/src/main/java/device/Hue.java index dbb1b7f..8e83c16 100644 --- a/src/main/java/device/Hue.java +++ b/src/main/java/device/Hue.java @@ -9,6 +9,10 @@ import support.Rest; * Classe che permette di controllare le luci Philips Hue */ public class Hue { + + /** + * La luminopsita' massima a cui si puo' arrivare + */ public static final int MAX_BRIGHTNESS = 255; //private String baseURL = "192.168.0.2"; //private String username = "C0vPwqjJZo5Jt9Oe5HgO6sBFFMxgoR532IxFoGmx"; @@ -22,12 +26,28 @@ public class Hue { */ private Map allLights; + /** + * L'ultima luminosita' impostata + */ private int brightness = 0; + /** + * Cerca le luci Philips Hue a ll'indirizzo http://172.30.1.138/api/C0vPwqjJZo5Jt9Oe5HgO6sBFFMxgoR532IxFoGmx/lights/ + */ 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 * @@ -36,7 +56,7 @@ public class Hue { public Hue (String url) { lightsURL = url; allLights = Rest.get(lightsURL); - // Todo brightness initial + // Todo brightness initial, maybe by default 50% or 75% of the total } /** diff --git a/src/main/java/main/Main.java b/src/main/java/main/Main.java index 7ba363e..ac0e471 100644 --- a/src/main/java/main/Main.java +++ b/src/main/java/main/Main.java @@ -1,18 +1,53 @@ package main; import device.*; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import support.DBConnect; /** * Created by 20015159 on 28/08/2018. */ 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) { + 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(); - df.addOnAction("LightsON", () -> {return "Luci accese";}); - df.addOnAction("LightsOFF", () -> {return "Luci spente";}); + df.addOnAction("LightsON", () -> {hue.turnOn(); return "Luci accese";}); + df.addOnAction("LightsOFF", () -> {hue.turnOff(); return "Luci spente";}); + log.info("Starting Webhook"); df.startServer(); } @@ -24,7 +59,8 @@ public class Main { * (magari ci si calcola quando bisogna risvegliarsi e si mette un wait) * @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 { Connection conn = DBConnect.getInstance().getConnection(); diff --git a/src/main/java/support/DBConnect.java b/src/main/java/support/DBConnect.java index 94d58dd..9d67509 100644 --- a/src/main/java/support/DBConnect.java +++ b/src/main/java/support/DBConnect.java @@ -1,8 +1,6 @@ package support; -import java.sql.Connection; -import java.sql.DriverManager; -import java.sql.SQLException; +import java.sql.*; /** * Handle the connection to the SQLite database that stores our tasks. @@ -12,28 +10,31 @@ import java.sql.SQLException; public class DBConnect { // 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"; - static private DBConnect instance = null; + public static final String DB_LOCATION = "jdbc:sqlite:src/main/resources/"; + public static final String DB_NAME = "user_data.db"; - private DBConnect() { - instance = this; + private static DBConnect instance; + + private final Connection conn; + + private DBConnect() throws SQLException { + conn = DriverManager.getConnection(DB_LOCATION + DB_NAME); + buildTablesIfNotExisting(); } 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 { - try { - /* todo this might work for create the database - Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/"); - Statement s = Conn.createStatement(); - 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); - } + private void buildTablesIfNotExisting() throws SQLException { + Statement statement = conn.createStatement(); + // todo working, but not quite well + statement.execute("CREATE TABLE IF NOT EXISTS user (user VARCHAR(16) PRIMARY KEY, name VARCHAR(16), birthday DATE);"); + statement.execute("CREATE TABLE IF NOT EXISTS heart_rate (date DATE, rate DOUBLE, user VARCHAR(16), PRIMARY KEY(date, user));"); } } diff --git a/src/main/resources/user_data.db b/src/main/resources/user_data.db new file mode 100644 index 0000000000000000000000000000000000000000..0e61cba59161ac93b951b9808faba91e601f5410 GIT binary patch literal 20480 zcmeI%&q~8E9Ki8(tA7x-?lS5zJGnto#=wgg>ntix)H;QEDV23@usQ5HQ1G(%@PWMf z9KL}^n~qTxocHn#B>lBX^GiQDg1$@Wtj%^J z|85veCqbxcuIV~<+mUVipyf#QL{^jb#&(;>wp*?5*5rkIa%Q_7dFpg5`QVQN`K{NI zcY`qMcl~F17|&FD$*q}{iZ*K|dHR7LMqcPgK^oH-YBwI2%wkFPiab2Oj+bT4 z*YzXkwmYs)VAMhb0YJ9 zqOFxxbFchY9wq42RjbbR>uT