Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
84 changes: 84 additions & 0 deletions src/main/java/de/doubleslash/usb_led_matrix/view/Co2View.java
Original file line number Diff line number Diff line change
@@ -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();
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -404,15 +404,15 @@ 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();
}
currentlyRunningThread = new Thread(runnable);
currentlyRunningThread.setName(name);
currentlyRunningThread.start();
}
}

void reconnect() {
usbAdapter.connect();
Expand Down Expand Up @@ -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());
Expand Down
162 changes: 162 additions & 0 deletions src/main/resources/layouts/Co2.fxml
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.paint.Color?>
<?import javafx.scene.paint.LinearGradient?>
<?import javafx.scene.paint.Stop?>
<?import javafx.scene.shape.Line?>
<?import javafx.scene.text.Font?>

<VBox fx:id="vBox" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="274.0" prefWidth="585.0" style="-fx-background-color: black;" styleClass="backgroundColor" xmlns="http://javafx.com/javafx/21" xmlns:fx="http://javafx.com/fxml/1" fx:controller="de.doubleslash.usb_led_matrix.view.Co2View">
<children>
<HBox alignment="CENTER_LEFT" prefHeight="77.0" prefWidth="585.0" styleClass="backgroundColor">
<children>
<AnchorPane prefHeight="47.0" prefWidth="207.0" />
<Label prefHeight="81.0" prefWidth="252.0" text="Sensor Values">
<font>
<Font size="32.0" />
</font>
<textFill>
<LinearGradient endX="1.0" endY="0.004761904761904745" startX="0.4238095238095238" startY="0.7285714285714285">
<stops>
<Stop>
<color>
<Color red="0.12156862765550613" green="0.8078431487083435" blue="1.0" />
</color>
</Stop>
<Stop offset="1.0">
<color>
<Color red="0.4745098054409027" green="0.7882353067398071" blue="0.40784314274787903" />
</color>
</Stop>
</stops>
</LinearGradient>
</textFill>
</Label>
</children>
</HBox>
<VBox prefHeight="270.0" prefWidth="308.0" styleClass="backgroundColor">
<children>
<Line endX="480.878662109375" endY="-2.1213202476501465" startX="-100.0" strokeWidth="3.0">
<stroke>
<LinearGradient endX="1.0" endY="1.0">
<stops>
<Stop>
<color>
<Color red="0.08235294371843338" green="0.18431372940540314" blue="0.29019609093666077" />
</color>
</Stop>
<Stop offset="1.0">
<color>
<Color red="0.019607843831181526" green="0.27450981736183167" blue="0.19607843458652496" />
</color>
</Stop>
</stops>
</LinearGradient>
</stroke>
</Line>
<HBox prefHeight="16.0" prefWidth="586.0">
<children>
<Label prefHeight="36.0" prefWidth="124.0" text="Values:" textFill="#79c968">
<font>
<Font size="25.0" />
</font>
<padding>
<Insets left="35.0" />
</padding>
</Label>
</children>
</HBox>
<HBox prefHeight="60.0" prefWidth="586.0">
<children>
<Label prefHeight="26.0" prefWidth="212.0" styleClass="labelText" text="Ip Addrese input:" textFill="WHITE">
<font>
<Font name="System Bold" size="17.0" />
</font>
<padding>
<Insets left="34.0" />
</padding>
</Label>
<TextField fx:id="IPEingabe" prefHeight="26.0" prefWidth="227.0" promptText="Ip address " />
<AnchorPane prefHeight="18.0" prefWidth="11.0" />
<Button mnemonicParsing="false" onAction="#IPButtonSave" prefHeight="30.0" prefWidth="68.0" style="-fx-background-radius: 5;" text="Save" />
</children>
</HBox>
<Line endX="480.878662109375" endY="-2.1213202476501465" startX="-100.0" strokeWidth="3.0">
<stroke>
<LinearGradient endX="1.0" endY="1.0">
<stops>
<Stop>
<color>
<Color red="0.08235294371843338" green="0.18431372940540314" blue="0.29019609093666077" />
</color>
</Stop>
<Stop offset="1.0">
<color>
<Color red="0.019607843831181526" green="0.27450981736183167" blue="0.19607843458652496" />
</color>
</Stop>
</stops>
</LinearGradient>
</stroke>
</Line>
<HBox prefHeight="115.0" prefWidth="298.0" styleClass="backgroundColor">
<children>
<VBox prefHeight="105.0" prefWidth="209.0">
<children>
<Label styleClass="labelText" text="C02:" textFill="WHITE">
<font>
<Font name="System Bold" size="17.0" />
</font>
<padding>
<Insets left="34.0" />
</padding>
</Label>
<Label prefHeight="26.0" prefWidth="171.0" styleClass="labelText" text="Humidity:" textFill="WHITE">
<font>
<Font name="System Bold" size="17.0" />
</font>
<padding>
<Insets left="34.0" />
</padding>
</Label>
<Label prefHeight="26.0" prefWidth="171.0" styleClass="labelText" text="Temperature:" textFill="WHITE">
<font>
<Font name="System Bold" size="17.0" />
</font>
<padding>
<Insets left="34.0" />
</padding>
</Label>
</children>
</VBox>
<AnchorPane prefHeight="85.0" prefWidth="43.0" />
<VBox prefHeight="110.0" prefWidth="333.0">
<children>
<Label fx:id="co2Value" prefHeight="26.0" prefWidth="333.0" styleClass="labelText" text="N/A" textFill="WHITE">
<font>
<Font size="17.0" />
</font></Label>
<Label fx:id="feuchtigkeitValue" prefHeight="26.0" prefWidth="340.0" styleClass="labelText" text="N/A" textFill="WHITE">
<font>
<Font size="17.0" />
</font></Label>
<Label fx:id="temperaturValue" prefHeight="26.0" prefWidth="330.0" styleClass="labelText" text="N/A" textFill="WHITE">
<font>
<Font size="17.0" />
</font>
</Label>
</children>
</VBox>
</children>
</HBox>
</children>
</VBox>
</children>
</VBox>
5 changes: 5 additions & 0 deletions src/main/resources/layouts/configuration.fxml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@
<MenuItem mnemonicParsing="false" onAction="#showInfoView" text="About" />
</items>
</Menu>
<Menu mnemonicParsing="false" text="Sensor">
<items>
<MenuItem mnemonicParsing="false" onAction="#showCo2View" text="CO2-Sensor Daten" />
</items>
</Menu>
</menus>
</MenuBar>
<HBox alignment="TOP_CENTER" prefHeight="64.0" prefWidth="600.0" styleClass="backgroundColor">
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import static org.junit.jupiter.api.Assertions.*;
class Co2ViewTest {

}
Loading