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 DAO.SQLiteManager;
Puissegur Alexis
committed
import Model.DataModel;
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.*;
import javafx.geometry.Side;
import javafx.scene.control.TextField;
/**
* FXML Controller class
*
* @author invite
*/
public class FXMLVueController implements Initializable, Observer {
Puissegur Alexis
committed
public static final int MONTH = 333;
public static final int WEEK = 666;
public static final int DAY = 999;
private Label humidityPlante;
@FXML
private Label humiditySeuil;
@FXML
private HBox planteHydrate;
@FXML
private HBox urgentArrosage;
@FXML
private GridPane configuration;
@FXML
@FXML
private Button buttonBluetooth;
@FXML
private Label activationMessage;
private LineChart<String, Number> graph;
@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;
private LocalDate lastMailDay;
private SQLiteManager DBManager;
private HashMap<String, ComboboxModel> currentMap;
/**
* 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();
dataGraph.valueProperty().addListener(new ChangeListener<String>() {
@Override public void changed(ObservableValue ov, String t, String t1) {
ComboboxModel selected = currentMap.get(t1);
if(selected != null){
Puissegur Alexis
committed
Puissegur Alexis
committed
if(selected.getDay() == -1 && selected.getWeek() == -1){ // month
drawGraph(selected,MONTH);
} else if(selected.getWeek() == -1){ // day
Puissegur Alexis
committed
drawGraph(selected,DAY);
DBManager.selectDayOfMonthData(selected.getMonth(), selected.getDay());
} else { // week
Puissegur Alexis
committed
drawGraph(selected,WEEK);
DBManager.selectWeekOfMonthData(selected.getMonth(), selected.getWeek());
}
}
}
});
granulation.selectedToggleProperty().addListener(new ChangeListener<Toggle>(){
public void changed(ObservableValue<? extends Toggle> ov,
Toggle old_toggle, Toggle new_toggle) {
RadioButton rb = (RadioButton) new_toggle.getToggleGroup().getSelectedToggle();
if (dayGranulation.equals(rb)) {
dataGraph.getItems().clear();
currentMap = DBManager.selectDayOfMonths();
dataGraph.getItems().addAll(currentMap.keySet());
} else if(weekGranulation.equals(rb)){
dataGraph.getItems().clear();
currentMap = DBManager.selectWeekOfMonth();
dataGraph.getItems().addAll(currentMap.keySet());
} else if(monthGranulation.equals(rb)) {
dataGraph.getItems().clear();
currentMap = DBManager.selectMonth();
dataGraph.getItems().addAll(currentMap.keySet());
Puissegur Alexis
committed
axisYGraph.setLabel("Valeur");
axisXGraph.setLabel("Temps");
axisXGraph.setAnimated(false);
DBManager = new SQLiteManager("plant.db");
Puissegur Alexis
committed
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
public void drawGraph(ComboboxModel selected, int mode){
ArrayList<ArrayList<DataModel>> res = null;
graph.getData().clear();
switch(mode){
case MONTH:
res = DBManager.selectMonthData(selected.getMonth());
graph.setTitle("Visualisation mois : " + SQLiteManager.months[selected.getMonth()]);
break;
case WEEK:
res = DBManager.selectWeekOfMonthData(selected.getMonth(),selected.getWeek());
graph.setTitle("Visualisation " + SQLiteManager.months[selected.getMonth()] + " sem. " + selected.getWeek());
break;
case DAY:
res = DBManager.selectDayOfMonthData(selected.getMonth(),selected.getDay());
graph.setTitle("Visualisation " + selected.getDay() + " " + SQLiteManager.months[selected.getMonth()]);
break;
default:
graph.setTitle("???");
break;
}
LineChart.Series series1 = new LineChart.Series();
LineChart.Series series2 = new LineChart.Series();
LineChart.Series series3 = new LineChart.Series();
LineChart.Series series4 = new LineChart.Series();
series1.setName("Luminosité");
series2.setName("Température");
series3.setName("Humidité air");
series4.setName("Humidité plante");
Puissegur Alexis
committed
for(DataModel dm : res.get(0)){
series1.getData().add(new XYChart.Data<>(dm.getKey()+"",dm.getValue()));
Puissegur Alexis
committed
}
for(DataModel dm : res.get(1)){
series2.getData().add(new XYChart.Data(dm.getKey(),dm.getValue()));
}
for(DataModel dm : res.get(2)){
series3.getData().add(new XYChart.Data(dm.getKey(),dm.getValue()));
}
for(DataModel dm : res.get(3)){
series4.getData().add(new XYChart.Data(dm.getKey(),dm.getValue()));
Puissegur Alexis
committed
graph.getData().addAll(series1,series2,series3,series4);
Puissegur Alexis
committed
}
/**
* 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].trim().replaceFirst("^0+(?!$)", ""));
this.intensity.setText(values[1].trim().replaceFirst("^0+(?!$)", ""));
this.temperature.setText(values[2].trim().replaceFirst("^0+(?!$)", ""));
this.humidityPlante.setText(values[3].trim().replaceFirst("^0+(?!$)", ""));
}
);
if(areValidNumbers(values)){
DBManager.insert(Calendar.getInstance().get(Calendar.YEAR),
Calendar.getInstance().get(Calendar.MONTH),
Calendar.getInstance().get(Calendar.WEEK_OF_MONTH),
Calendar.getInstance().get(Calendar.DAY_OF_MONTH),
Calendar.getInstance().get(Calendar.HOUR_OF_DAY),
Calendar.getInstance().get(Calendar.MINUTE),
Integer.parseInt(values[1].trim().replaceFirst("^0+(?!$)", "")),
Integer.parseInt(values[2].trim().replaceFirst("^0+(?!$)", "")),
Integer.parseInt(values[0].trim().replaceFirst("^0+(?!$)", "")),
Integer.parseInt(values[3].trim().replaceFirst("^0+(?!$)", "")));
} else {
}
}
}
static boolean areValidNumbers(String[] values) {
// Return true if all characters are digits.
for(String value : values){
for (int i = 0; i < value.length(); i++) {
if (!Character.isDigit(value.charAt(i))) {
return false;
* @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");
}
private void work(){
if(CalendarUtil.isBusinessDay(Calendar.getInstance())){
System.out.println("is business day");
computeGravity();
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(this.humidityPlante.getText(), this.humiditySeuil.getText());
int humidity = Integer.parseInt(humidityPlante.getText().trim().replaceFirst("^0+(?!$)", ""));
int seuil = Integer.parseInt(humiditySeuil.getText().trim().replaceFirst("^0+(?!$)", ""));
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();
}
}
}