Newer
Older
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package Vue;
import Bluetooth.BluetoothManager;
import Bluetooth.BluetoothThread;
import Interfaces.Observable;
import Interfaces.Observer;
import java.time.LocalDate;
import java.time.Period;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.chart.NumberAxis;
import javafx.scene.chart.XYChart;
import javafx.scene.control.Label;
import javafx.scene.layout.GridPane;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
import javax.mail.*;
import javax.mail.internet.*;
import java.util.*;
/**
* FXML Controller class
*
* @author invite
*/
public class FXMLVueController implements Initializable, Observer {
private Label humidityPlante;
@FXML
private Label humiditySeuil;
@FXML
private HBox planteHydrate;
@FXML
private HBox urgentArrosage;
@FXML
private GridPane configuration;
@FXML
@FXML
private Label seuilError;
@FXML
private Button buttonBluetooth;
@FXML
private Label activationMessage;
@FXML
private NumberAxis axisYGraph;
@FXML
private CategoryAxis axisXGraph;
@FXML
private RadioButton dayGranulation;
@FXML
private RadioButton weekGranulation;
@FXML
private RadioButton monthGranulation;
@FXML
private Button buttonAccueil;
@FXML
private Button buttonConfig;
private GridPane currentGridPane;
private Button currentButton;
private boolean alert;
private boolean isConnected = false;
private BluetoothManager bluetoothManager;
/**
* Initializes the controller class.
*/
@Override
public void initialize(URL url, ResourceBundle rb) {
//Put the gridPane Accueil visible
currentGridPane = accueil;
//TODO initialize lastMailDay with database
lastMailDay = LocalDate.of(2018, 12, 12);
seuil.valueProperty().addListener(new ChangeListener<Number>() {
public void changed(ObservableValue<? extends Number> obs, Number oldVal, Number newVal) {
int intVal = ((int) Math.round(newVal.doubleValue())/10)*10;
seuil.setValue(intVal);
humiditySeuil.setText(""+intVal);
if(humidityPlante.getText() != null && intVal <= Integer.parseInt(humidityPlante.getText()) && intOld > Integer.parseInt(humidityPlante.getText())){ // STOP
while(tmp){
try{
System.out.println("STOP");
workingThread.sendInstruction("STOP");
tmp = false;
} catch(IOException e){
e.printStackTrace();
//Bind the label for the senewValueuil in Accueil and the value in configurations
//humiditySeuil.textProperty().bind(Bindings.convert(seuil.valueProperty()));
humidityPlante.textProperty().addListener(new ChangeListener<String>() {
@Override
public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue) {
if (humiditySeuil.getText() != null && Integer.parseInt(newValue) < Integer.parseInt(humiditySeuil.getText())) {
if(Integer.parseInt(oldValue) > Integer.parseInt(humiditySeuil.getText()) || Integer.parseInt(oldValue) == -1){ // start or change
if(alert){
changeImagePlante(false);
}
boolean tmp = true;
while(tmp){
try{
System.out.println("STOP");
workingThread.sendInstruction("STOP");
tmp = false;
} catch(IOException e){
e.printStackTrace();
//Graph
axisYGraph.setLabel("données");
axisXGraph.setLabel("temps");
XYChart.Series serieLine = new XYChart.Series();
serieLine.getData().add(new XYChart.Data<>(14+"", 12));
graph.getData().add(serieLine);
graph.setLegendVisible(false);
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
public static void initDB(String name) {
String url = "jdbc:sqlite:C:/sqlite/" + name;
try (Connection conn = DriverManager.getConnection(url)) {
if (conn != null) {
DatabaseMetaData meta = conn.getMetaData();
createNewTable(url);
}
} catch (SQLException e) {
System.out.println(e.getMessage());
}
}
/**
* Create a new table in the test database
*
*/
public static void createNewTable(String url) {
// SQLite connection string
// SQL statement for creating a new table
String sql = "CREATE TABLE IF NOT EXISTS data (\n"
+ " id integer PRIMARY KEY AUTOINCREMENT,\n"
+ " month integer NOT NULL CHECK(month <= 12 and month > 0),\n"
+ " day integer NOT NULL CHECK(day <= 31 and day > 0),\n"
+ " hour integer NOT NULL CHECK(hour <24 and hour >= 0),\n"
+ " min integer NOT NULL CHECK(min <60 and min >= 0),\n"
+ " intensity integer,\n"
+ " temperature integer,\n"
+ " humidityAir integer,\n"
+ " humidityPlant integer\n"
+ ");";
try (Connection conn = DriverManager.getConnection(url);
Statement stmt = conn.createStatement()) {
// create a new table
stmt.execute(sql);
} catch (SQLException e) {
System.out.println("ewi" + e.getMessage());
}
}
private Connection connect() {
// SQLite connection string
String url = "jdbc:sqlite:C://sqlite/plant.db";
Connection conn = null;
try {
conn = DriverManager.getConnection(url);
} catch (SQLException e) {
System.out.println(e.getMessage());
}
return conn;
}
public void insert(int month, int day, int hour, int min, int it, int temp, int humiA, int humiT) {
String sql = "INSERT INTO data(month,day,hour,min,intensity,temperature,humidityAir,humidityPlant) VALUES(?,?,?,?,?,?,?,?)";
try (Connection conn = this.connect();
PreparedStatement pstmt = conn.prepareStatement(sql)) {
pstmt.setInt(1, month);
pstmt.setInt(2, day);
pstmt.setInt(3, hour);
pstmt.setInt(4, min);
pstmt.setInt(5, it);
pstmt.setInt(6, temp);
pstmt.setInt(7, humiA);
pstmt.setInt(8, humiT);
pstmt.executeUpdate();
} catch (SQLException e) {
System.out.println(e.getMessage());
}
}
/**
* Change the text for activate or desactivate according to the checkbox
* @param event
*/
@FXML
private void clickAuto(ActionEvent event) {
if(arrosageAuto.isSelected()){
arrosageAuto.setText("Activé");
}
else arrosageAuto.setText("Désactivé");
/**
* Button to activate the bluetooth
* @param event
*/
@FXML
private void clickActivationBluetooth(ActionEvent event) {
isConnected = bluetoothManager.launch();
}
if(isConnected){
try {
bluetoothManager.startThread();
this.activationMessage.setText("Connection bluetooth activée");
this.activationMessage.setStyle("-fx-background-color: #7fca5eff");
this.buttonBluetooth.setDisable(true);
} catch (Exception ex) {
if(obs instanceof BluetoothThread && ((BluetoothThread) obs).equals(this.workingThread)){
this.humidityAir.setText(values[0].replaceFirst("^0+(?!$)", ""));
this.intensity.setText(values[1].replaceFirst("^0+(?!$)", ""));
this.temperature.setText(values[2].replaceFirst("^0+(?!$)", ""));
this.humidityPlante.setText(values[3].replaceFirst("^0+(?!$)", ""));
* @param alertM
*/
public void changeImagePlante(boolean alertM){
if(alertM){
this.planteHydrate.setVisible(false);
this.urgentArrosage.setVisible(true);
}
else{
this.urgentArrosage.setVisible(false);
this.planteHydrate.setVisible(true);
}
}
* Change the page for the grah page when clicking on data button
* @param event
*/
@FXML
private void clickData(ActionEvent event) {
openPage(data, buttonData);
}
/**
* Change the page for the accueil page when clicking on accueil button
* @param event
*/
@FXML
private void clickAccueil(ActionEvent event) {
openPage(accueil, buttonAccueil);
}
/**
* Change the page for the configuration page when clicking on configuration button
* @param event
*/
@FXML
private void clickConfig(ActionEvent event) {
openPage(configuration, buttonConfig);
}
private void openPage(GridPane open, Button justClicked){
currentButton.setStyle("-fx-background-color: #d5d5d5ff");
currentButton.setStyle("-fx-background-color: #7fca5eff");
}
System.out.println("is business day");
System.out.println("is not business day");
LocalDate today = LocalDate.now();
Period difference = Period.between(lastMailDay, today);
if(difference.getDays() > 1){
// send mail
MailUtil.sendDistressMail();
lastMailDay = today;
}
}
}
public void computeGravity(){
int humidity = Integer.parseInt(humidityPlante.getText().replaceFirst("^0+(?!$)", ""));
int seuil = Integer.parseInt(humiditySeuil.getText().replaceFirst("^0+(?!$)", ""));
System.out.println("humi " + humidity);
System.out.println("seuil : " + seuil);
if(humidity < (seuil / 2)) { // la moitié du seuil = critique, sound
boolean tmp = true;
while(tmp){
try{
this.workingThread.sendInstruction("SOUND");
System.out.println("SOUND");
tmp = false;
} catch(IOException e){
e.printStackTrace();
}
}
} else { // light
boolean tmp = true;
while(tmp){
try{
this.workingThread.sendInstruction("LIGHT");
System.out.println("LIGHT");
tmp = false;
} catch(IOException e){
e.printStackTrace();
}
}
}