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..f84916d --- /dev/null +++ b/src/main/java/de/doubleslash/usb_led_matrix/view/Co2View.java @@ -0,0 +1,84 @@ +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.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 { + + @FXML + private Label co2Value; + + @FXML + private Label feuchtigkeitValue; + + @FXML + 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 baseUrl = IPEingabe.getText().trim(); + if (baseUrl.isEmpty() || !baseUrl.startsWith("http://") && !baseUrl.startsWith("https://")) { + baseUrl = DEFAULT_BASE_URL; + } + String[] endpoints = { "/temperatur", "/feuchtigkeit", "/co2" }; + + for (String endpoint : endpoints) { + try { + URL apiUrl = new URL(baseUrl + endpoint); + 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 (endpoint.contains("co2")) { + co2Value.setText(response); + } else if (endpoint.contains("feuchtigkeit")) { + feuchtigkeitValue.setText(response); + } else if (endpoint.contains("temperatur")) { + temperaturValue.setText(response); + } + } else { + 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 82b5267..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(); @@ -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..71e5bd4 --- /dev/null +++ b/src/main/resources/layouts/Co2.fxml @@ -0,0 +1,162 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +