Init of Database

- don't look in the database, 'cause is a little messy
This commit is contained in:
2018-08-29 23:35:27 +02:00
parent 7b21345f89
commit 1f4a4b80c0
5 changed files with 118 additions and 46 deletions

View File

@@ -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<String, ?> allLights;
/**
* L'ultima luminosita' impostata
*/
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 () {
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
}
/**

View File

@@ -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();

View File

@@ -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));");
}
}

Binary file not shown.