-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
IUT BLAGNAC
committed
Dec 3, 2024
1 parent
7521952
commit ebc74ad
Showing
7 changed files
with
1,522 additions
and
1,627 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
223 changes: 90 additions & 133 deletions
223
Application/app/src/main/java/sae/view/AfficherGraphiqueControllerSolar.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,154 +1,111 @@ | ||
package sae.view; | ||
|
||
import java.io.File; | ||
import java.io.FileReader; | ||
import java.text.SimpleDateFormat; | ||
import java.util.List; | ||
import java.util.Date; | ||
|
||
import org.json.simple.JSONObject; | ||
import org.json.simple.parser.JSONParser; | ||
|
||
import javafx.fxml.FXML; | ||
import javafx.application.Platform; | ||
import javafx.scene.Scene; | ||
import javafx.scene.chart.LineChart; | ||
import javafx.scene.chart.NumberAxis; | ||
import javafx.scene.chart.XYChart; | ||
import javafx.scene.control.Button; | ||
import javafx.stage.Stage; | ||
import sae.App; | ||
import javafx.util.StringConverter; | ||
import org.json.simple.JSONObject; | ||
import java.text.SimpleDateFormat; | ||
import java.util.Date; | ||
|
||
public class AfficherGraphiqueControllerSolar { | ||
|
||
@SuppressWarnings("unused") | ||
private Stage fenetrePrincipale; | ||
|
||
private App application; | ||
|
||
@FXML | ||
private LineChart<Number, Number> lineChart; | ||
private JSONObject solarData; | ||
|
||
@FXML | ||
private Button butRetour; | ||
|
||
private JSONObject solarData; // Champ pour stocker les données JSON | ||
|
||
/** | ||
* Configure les données de la fenêtre principale et de l'application | ||
*/ | ||
public void setDatas(Stage fenetre, App app) { | ||
this.application = app; | ||
this.fenetrePrincipale = fenetre; | ||
// Constructeur qui prend un JSONObject contenant les données | ||
public AfficherGraphiqueControllerSolar(JSONObject solarData) { | ||
this.solarData = solarData; | ||
} | ||
|
||
/** | ||
* Appelé lorsque le bouton "Retour" est cliqué | ||
*/ | ||
@FXML | ||
private void actionRetour() { | ||
Stage stage = (Stage) butRetour.getScene().getWindow(); | ||
stage.close(); | ||
} | ||
|
||
/** | ||
* Charger les données JSON et afficher le graphique | ||
*/ | ||
@FXML | ||
private void actionAfficherGraphique() { | ||
// Charger les données JSON | ||
chargerFichierSolar(); | ||
|
||
if (solarData == null) { | ||
System.out.println("Aucune donnée chargée depuis le fichier JSON !"); | ||
return; | ||
} | ||
|
||
// Afficher le graphique basé sur currentPower et lastUpdateTime | ||
afficherGraphique(solarData); | ||
} | ||
// Méthode pour afficher le graphique dans une nouvelle fenêtre | ||
public void afficherGraphique() { | ||
// Exécuter la création du graphique sur le thread JavaFX | ||
Platform.runLater(() -> { | ||
Stage stage = new Stage(); | ||
stage.setTitle("Graphique : Current Power vs Time"); | ||
|
||
// Création des axes | ||
NumberAxis xAxis = new NumberAxis(); | ||
xAxis.setLabel("Heure"); | ||
|
||
// Limiter la plage de l'axe des X à 24 heures (24 * 60 * 60 * 1000 ms) | ||
xAxis.setLowerBound(0); // Plage minimum en millisecondes (00:00:00) | ||
xAxis.setUpperBound(24 * 60 * 60 * 1000); // Plage maximum en millisecondes (23:59:59) | ||
xAxis.setTickUnit(60 * 60 * 1000); // Intervalle de ticks : 1 heure (60 min * 60 s * 1000 ms) | ||
|
||
// Ajouter un StringConverter pour formater l'axe des X en heures et minutes | ||
xAxis.setTickLabelFormatter(new StringConverter<Number>() { | ||
private SimpleDateFormat sdf = new SimpleDateFormat("HH:mm"); | ||
|
||
@Override | ||
public String toString(Number value) { | ||
Date date = new Date(value.longValue()); // Convertir la valeur en Date | ||
return sdf.format(date); // Formater l'heure | ||
} | ||
|
||
/** | ||
* Affiche le graphique en utilisant lastUpdateTime comme abscisse et currentPower comme ordonnée | ||
*/ | ||
private void afficherGraphique(JSONObject solarData) { | ||
// Effacer les données précédentes du graphique | ||
lineChart.getData().clear(); | ||
|
||
JSONObject solar = (JSONObject) solarData.get("solar"); | ||
if (solar == null) { | ||
System.out.println("Aucune donnée 'solar' trouvée dans le JSON."); | ||
return; | ||
} | ||
|
||
// Créer une série de données pour le graphique | ||
XYChart.Series<Number, Number> series = new XYChart.Series<>(); | ||
series.setName("Current Power"); | ||
|
||
// Définir le format de la date pour afficher correctement les dates sur l'axe X | ||
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); | ||
|
||
// Itérer sur les clés de l'objet JSON | ||
int index = 1; // Utiliser un index pour l'axe des X | ||
for (Object key : solar.keySet()) { | ||
JSONObject solarEntry = (JSONObject) solar.get(key); | ||
|
||
// Vérifier si l'attribut 'currentPower' existe et a une valeur | ||
if (solarEntry.containsKey("currentPower")) { | ||
JSONObject currentPower = (JSONObject) solarEntry.get("currentPower"); | ||
if (currentPower.containsKey("power")) { | ||
Double powerValue = (Double) currentPower.get("power"); | ||
|
||
// Vérifier si 'lastUpdateTime' existe et le convertir en long (timestamp) | ||
if (solarEntry.containsKey("lastUpdateTime")) { | ||
String lastUpdateTimeStr = (String) solarEntry.get("lastUpdateTime"); | ||
|
||
try { | ||
// Convertir la chaîne de date en un objet Date et récupérer le timestamp | ||
Date lastUpdateTime = dateFormat.parse(lastUpdateTimeStr); | ||
long timestamp = lastUpdateTime.getTime(); // Obtenir le timestamp en millisecondes | ||
|
||
// Ajouter un point à la série | ||
series.getData().add(new XYChart.Data<>(timestamp, powerValue)); | ||
} catch (Exception e) { | ||
System.out.println("Erreur lors de l'analyse de la date lastUpdateTime pour la clé " + key); | ||
@Override | ||
public Number fromString(String string) { | ||
return null; // Cette méthode n'est pas nécessaire ici | ||
} | ||
}); | ||
|
||
// Créer l'axe Y (Current Power) | ||
NumberAxis yAxis = new NumberAxis(); | ||
yAxis.setLabel("Current Power"); | ||
|
||
// Création du graphique | ||
LineChart<Number, Number> lineChart = new LineChart<>(xAxis, yAxis); | ||
lineChart.setTitle("Évolution de Current Power"); | ||
|
||
// Ajouter des données au graphique | ||
XYChart.Series<Number, Number> series = new XYChart.Series<>(); | ||
series.setName("Current Power"); | ||
|
||
// Récupération des données à partir du JSON | ||
JSONObject solar = (JSONObject) solarData.get("solar"); | ||
if (solar != null) { | ||
long firstTimestamp = -1; // Utilisé pour calculer l'écart relatif | ||
for (Object key : solar.keySet()) { | ||
JSONObject entry = (JSONObject) solar.get(key); | ||
try { | ||
// Extraire les données : lastUpdateTime et currentPower | ||
String lastUpdateTimeStr = (String) entry.get("lastUpdateTime"); | ||
JSONObject currentPowerObj = (JSONObject) entry.get("currentPower"); | ||
|
||
if (lastUpdateTimeStr != null && currentPowerObj != null) { | ||
// Convertir la date en timestamp (millisecondes) | ||
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); | ||
Date date = sdf.parse(lastUpdateTimeStr); | ||
long timestamp = date.getTime(); // timestamp en millisecondes | ||
|
||
// Normalisation du timestamp par rapport à la première date | ||
if (firstTimestamp == -1) { | ||
firstTimestamp = timestamp; | ||
} | ||
long normalizedTimestamp = timestamp - firstTimestamp; // Conversion en nombre de millisecondes depuis le premier point | ||
|
||
// Extraire la puissance actuelle | ||
double currentPower = ((Number) currentPowerObj.get("power")).doubleValue(); | ||
|
||
// Ajouter les données au graphique | ||
series.getData().add(new XYChart.Data<>(normalizedTimestamp, currentPower)); | ||
} | ||
} catch (Exception e) { | ||
System.out.println("Erreur lors du traitement des données pour l'entrée " + key + ": " + e.getMessage()); | ||
} | ||
} | ||
} | ||
} | ||
|
||
// Ajouter la série au graphique | ||
lineChart.getData().add(series); | ||
} | ||
|
||
/** | ||
* Charge le fichier JSON et stocke les données dans le champ solarData | ||
*/ | ||
public void chargerFichierSolar() { | ||
JSONParser parser = new JSONParser(); | ||
|
||
try { | ||
// Définir le chemin du fichier solar.json à la racine du projet | ||
File file = new File("Iot/solar.json"); | ||
|
||
if (!file.exists()) { | ||
System.out.println("Le fichier solar.json est introuvable à la racine du projet."); | ||
return; | ||
} | ||
|
||
// Lire et analyser le fichier JSON | ||
FileReader reader = new FileReader(file); | ||
JSONObject json = (JSONObject) parser.parse(reader); | ||
|
||
// Stocker les données dans le champ solarData | ||
this.solarData = json; | ||
|
||
System.out.println("Données extraites du fichier solar.json :"); | ||
System.out.println(json.toJSONString()); | ||
reader.close(); | ||
// Ajouter la série de données au graphique | ||
lineChart.getData().add(series); | ||
|
||
} catch (Exception e) { | ||
System.out.println("Erreur lors du chargement du fichier solar.json : " + e.getMessage()); | ||
e.printStackTrace(); | ||
} | ||
// Créer une scène et afficher le graphique | ||
Scene scene = new Scene(lineChart, 800, 600); | ||
stage.setScene(scene); | ||
stage.show(); | ||
}); | ||
} | ||
} |
Oops, something went wrong.