From 37edf48d13ee048c411fa3411cff852bdc0a6b6f Mon Sep 17 00:00:00 2001 From: Kilian Mair Date: Tue, 14 May 2024 09:43:59 +0200 Subject: [PATCH 1/4] include a new CO2 View --- .../usb_led_matrix/resources/Resources.java | 3 + .../usb_led_matrix/view/Co2View.java | 62 ++++++++++ .../view/ConfigurationView.java | 22 ++++ src/main/resources/layouts/Co2.fxml | 106 ++++++++++++++++++ src/main/resources/layouts/configuration.fxml | 5 + 5 files changed, 198 insertions(+) create mode 100644 src/main/java/de/doubleslash/usb_led_matrix/view/Co2View.java create mode 100644 src/main/resources/layouts/Co2.fxml diff --git a/src/main/java/de/doubleslash/usb_led_matrix/resources/Resources.java b/src/main/java/de/doubleslash/usb_led_matrix/resources/Resources.java index ed16167..21d9283 100644 --- a/src/main/java/de/doubleslash/usb_led_matrix/resources/Resources.java +++ b/src/main/java/de/doubleslash/usb_led_matrix/resources/Resources.java @@ -7,8 +7,11 @@ public enum Resources { CONFIGURATION_VIEW("/layouts/configuration.fxml"), AUTHENTICATION_VIEW("/layouts/authentication.fxml"), INFO_VIEW("/layouts/version.fxml"), + + CO2_VIEW("/layouts/Co2.fxml"), SETTINGS_VIEW("/layouts/settings.fxml"); + private final String location; private Resources(final String location) { diff --git a/src/main/java/de/doubleslash/usb_led_matrix/view/Co2View.java b/src/main/java/de/doubleslash/usb_led_matrix/view/Co2View.java new file mode 100644 index 0000000..b2c4d3a --- /dev/null +++ b/src/main/java/de/doubleslash/usb_led_matrix/view/Co2View.java @@ -0,0 +1,62 @@ +package de.doubleslash.usb_led_matrix.view; + +import javafx.fxml.FXML; +import javafx.scene.control.Label; +import javafx.scene.layout.VBox; + +import java.io.BufferedReader; +import java.io.InputStreamReader; +import java.net.HttpURLConnection; +import java.net.URL; + +public class Co2View { + + @FXML + private Label co2Value; + + @FXML + private Label feuchtigkeitValue; + + @FXML + private Label temperaturValue; + + @FXML + private VBox vBox; + + public void initialize() { + String[] urls = { + "http://172.19.70.169/temperatur", + "http://172.19.70.169/feuchtigkeit", + "http://172.19.70.169/co2" + }; + + for (String url : urls) { + try { + URL apiUrl = new URL(url); + HttpURLConnection connection = (HttpURLConnection) apiUrl.openConnection(); + connection.setRequestMethod("GET"); + + int responseCode = connection.getResponseCode(); + if (responseCode == HttpURLConnection.HTTP_OK) { + BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream())); + String response = reader.readLine(); + reader.close(); + + if (url.contains("co2")) { + co2Value.setText(response); + } else if (url.contains("feuchtigkeit")) { + feuchtigkeitValue.setText(response); + } else if (url.contains("temperatur")) { + temperaturValue.setText(response); + } + } else { + System.out.println("HTTP error response code: " + responseCode + " for URL: " + url); + } + + connection.disconnect(); + } catch (Exception e) { + e.printStackTrace(); + } + } + } +} diff --git a/src/main/java/de/doubleslash/usb_led_matrix/view/ConfigurationView.java b/src/main/java/de/doubleslash/usb_led_matrix/view/ConfigurationView.java index 82b5267..765b1ca 100644 --- a/src/main/java/de/doubleslash/usb_led_matrix/view/ConfigurationView.java +++ b/src/main/java/de/doubleslash/usb_led_matrix/view/ConfigurationView.java @@ -477,6 +477,28 @@ void showInfoView() { } } + @FXML + void showCo2View() { + final FXMLLoader fxmlLoader = new FXMLLoader(Resources.CO2_VIEW.getResource()); + try { + Stage stage = new Stage(); + final Parent root = fxmlLoader.load(); + final Scene scene = new Scene(root); + + stage.setTitle("Co2 Sensordaten"); + stage.setScene(scene); + stage.setResizable(false); + stage.initModality(Modality.APPLICATION_MODAL); + stage.show(); + } catch (final IOException e) { + LOG.error("Could not load view '{}'.", Resources.CO2_VIEW, e); + } + } + + + + + @FXML void showSettingsView() { final FXMLLoader fxmlLoader = new FXMLLoader(Resources.SETTINGS_VIEW.getResource()); diff --git a/src/main/resources/layouts/Co2.fxml b/src/main/resources/layouts/Co2.fxml new file mode 100644 index 0000000..3b7d3cc --- /dev/null +++ b/src/main/resources/layouts/Co2.fxml @@ -0,0 +1,106 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/main/resources/layouts/configuration.fxml b/src/main/resources/layouts/configuration.fxml index 8f28f26..8d6915c 100644 --- a/src/main/resources/layouts/configuration.fxml +++ b/src/main/resources/layouts/configuration.fxml @@ -35,6 +35,11 @@ + + + + + From 63cf747e51bce83a81c66b506bd9a582e85a9c8e Mon Sep 17 00:00:00 2001 From: Kilian Mair Date: Thu, 16 May 2024 07:53:40 +0200 Subject: [PATCH 2/4] Zwischen Speicher --- .../usb_led_matrix/view/Co2View.java | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/src/main/java/de/doubleslash/usb_led_matrix/view/Co2View.java b/src/main/java/de/doubleslash/usb_led_matrix/view/Co2View.java index b2c4d3a..c4d9e4b 100644 --- a/src/main/java/de/doubleslash/usb_led_matrix/view/Co2View.java +++ b/src/main/java/de/doubleslash/usb_led_matrix/view/Co2View.java @@ -1,8 +1,11 @@ package de.doubleslash.usb_led_matrix.view; +import javafx.animation.KeyFrame; +import javafx.animation.Timeline; import javafx.fxml.FXML; import javafx.scene.control.Label; import javafx.scene.layout.VBox; +import javafx.util.Duration; import java.io.BufferedReader; import java.io.InputStreamReader; @@ -20,13 +23,17 @@ public class Co2View { @FXML private Label temperaturValue; - @FXML - private VBox vBox; + private Timeline timeline; public void initialize() { - String[] urls = { - "http://172.19.70.169/temperatur", - "http://172.19.70.169/feuchtigkeit", + timeline = new Timeline(new KeyFrame(Duration.seconds(5), event -> updateValue())); + timeline.setCycleCount(Timeline.INDEFINITE); + timeline.play(); + updateValue(); + } + + public void updateValue() { + String[] urls = { "http://172.19.70.169/temperatur", "http://172.19.70.169/feuchtigkeit", "http://172.19.70.169/co2" }; From c05ea8e41d3417681a8e8bc81d8737593236979c Mon Sep 17 00:00:00 2001 From: Kilian Mair Date: Thu, 16 May 2024 10:11:38 +0200 Subject: [PATCH 3/4] Variable IP Addres eingabe --- .../usb_led_matrix/view/Co2View.java | 35 +++++--- .../view/ConfigurationView.java | 4 +- src/main/resources/layouts/Co2.fxml | 79 ++++++++++++++++--- 3 files changed, 94 insertions(+), 24 deletions(-) diff --git a/src/main/java/de/doubleslash/usb_led_matrix/view/Co2View.java b/src/main/java/de/doubleslash/usb_led_matrix/view/Co2View.java index c4d9e4b..f84916d 100644 --- a/src/main/java/de/doubleslash/usb_led_matrix/view/Co2View.java +++ b/src/main/java/de/doubleslash/usb_led_matrix/view/Co2View.java @@ -1,16 +1,18 @@ package de.doubleslash.usb_led_matrix.view; +import de.doubleslash.usb_led_matrix.usb_adapter.UsbAdapter; import javafx.animation.KeyFrame; import javafx.animation.Timeline; import javafx.fxml.FXML; import javafx.scene.control.Label; -import javafx.scene.layout.VBox; +import javafx.scene.paint.Color; import javafx.util.Duration; import java.io.BufferedReader; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; +import javafx.scene.control.TextField; public class Co2View { @@ -24,22 +26,34 @@ public class Co2View { private Label temperaturValue; private Timeline timeline; + private UsbAdapter usbAdapter; + + @FXML + private TextField IPEingabe; + + private static final String DEFAULT_BASE_URL = "http://example.com"; // Beispiel-URL als Fallback public void initialize() { timeline = new Timeline(new KeyFrame(Duration.seconds(5), event -> updateValue())); timeline.setCycleCount(Timeline.INDEFINITE); timeline.play(); + } + + @FXML + void IPButtonSave() { updateValue(); } public void updateValue() { - String[] urls = { "http://172.19.70.169/temperatur", "http://172.19.70.169/feuchtigkeit", - "http://172.19.70.169/co2" - }; + String baseUrl = IPEingabe.getText().trim(); + if (baseUrl.isEmpty() || !baseUrl.startsWith("http://") && !baseUrl.startsWith("https://")) { + baseUrl = DEFAULT_BASE_URL; + } + String[] endpoints = { "/temperatur", "/feuchtigkeit", "/co2" }; - for (String url : urls) { + for (String endpoint : endpoints) { try { - URL apiUrl = new URL(url); + URL apiUrl = new URL(baseUrl + endpoint); HttpURLConnection connection = (HttpURLConnection) apiUrl.openConnection(); connection.setRequestMethod("GET"); @@ -49,19 +63,20 @@ public void updateValue() { String response = reader.readLine(); reader.close(); - if (url.contains("co2")) { + if (endpoint.contains("co2")) { co2Value.setText(response); - } else if (url.contains("feuchtigkeit")) { + } else if (endpoint.contains("feuchtigkeit")) { feuchtigkeitValue.setText(response); - } else if (url.contains("temperatur")) { + } else if (endpoint.contains("temperatur")) { temperaturValue.setText(response); } } else { - System.out.println("HTTP error response code: " + responseCode + " for URL: " + url); + System.out.println("HTTP error response code: " + responseCode + " for URL: " + baseUrl + endpoint); } connection.disconnect(); } catch (Exception e) { + System.err.println("Error occurred while fetching data from URL: " + baseUrl + endpoint); e.printStackTrace(); } } diff --git a/src/main/java/de/doubleslash/usb_led_matrix/view/ConfigurationView.java b/src/main/java/de/doubleslash/usb_led_matrix/view/ConfigurationView.java index 765b1ca..7513cc7 100644 --- a/src/main/java/de/doubleslash/usb_led_matrix/view/ConfigurationView.java +++ b/src/main/java/de/doubleslash/usb_led_matrix/view/ConfigurationView.java @@ -404,7 +404,7 @@ private void handleFailure() { usbAdapter.closePort(); startNamedThreadWithRunnable(reconnectionCheckRunnable, "reconnectionThread"); } - +Co2View co2View; private void startNamedThreadWithRunnable(final Runnable runnable, final String name) { if (currentlyRunningThread != null && currentlyRunningThread.isAlive()) { currentlyRunningThread.interrupt(); @@ -412,7 +412,7 @@ private void startNamedThreadWithRunnable(final Runnable runnable, final String currentlyRunningThread = new Thread(runnable); currentlyRunningThread.setName(name); currentlyRunningThread.start(); - } + } void reconnect() { usbAdapter.connect(); diff --git a/src/main/resources/layouts/Co2.fxml b/src/main/resources/layouts/Co2.fxml index 3b7d3cc..2fe9262 100644 --- a/src/main/resources/layouts/Co2.fxml +++ b/src/main/resources/layouts/Co2.fxml @@ -1,19 +1,23 @@ + + + - + + - + + + + + + + + + + + + + + + + + + + + + + + + + + + +