diff --git a/.gitignore b/.gitignore index 5ff6309..a91c35d 100644 --- a/.gitignore +++ b/.gitignore @@ -35,4 +35,5 @@ build/ .vscode/ ### Mac OS ### -.DS_Store \ No newline at end of file +.DS_Store +/.idea/ diff --git a/.idea/.gitignore b/.idea/.gitignore deleted file mode 100644 index 13566b8..0000000 --- a/.idea/.gitignore +++ /dev/null @@ -1,8 +0,0 @@ -# Default ignored files -/shelf/ -/workspace.xml -# Editor-based HTTP Client requests -/httpRequests/ -# Datasource local storage ignored files -/dataSources/ -/dataSources.local.xml diff --git a/.idea/encodings.xml b/.idea/encodings.xml deleted file mode 100644 index aa00ffa..0000000 --- a/.idea/encodings.xml +++ /dev/null @@ -1,7 +0,0 @@ - - - - - - - \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml deleted file mode 100644 index 17e9c2e..0000000 --- a/.idea/misc.xml +++ /dev/null @@ -1,13 +0,0 @@ - - - - - - - - - \ No newline at end of file diff --git a/.idea/uiDesigner.xml b/.idea/uiDesigner.xml deleted file mode 100644 index 2b63946..0000000 --- a/.idea/uiDesigner.xml +++ /dev/null @@ -1,124 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/lib/pn532/pn532-sdk/1.0.2/pn532-sdk-1.0.2.jar b/lib/pn532/pn532-sdk/1.0.2/pn532-sdk-1.0.2.jar new file mode 100644 index 0000000..3f959a8 Binary files /dev/null and b/lib/pn532/pn532-sdk/1.0.2/pn532-sdk-1.0.2.jar differ diff --git a/lib/pn532/pn532-sdk/1.0.2/pn532-sdk-1.0.2.jar.md5 b/lib/pn532/pn532-sdk/1.0.2/pn532-sdk-1.0.2.jar.md5 new file mode 100644 index 0000000..0ec9815 --- /dev/null +++ b/lib/pn532/pn532-sdk/1.0.2/pn532-sdk-1.0.2.jar.md5 @@ -0,0 +1 @@ +171ab3333078fd908c878b85563a5ccc \ No newline at end of file diff --git a/lib/pn532/pn532-sdk/1.0.2/pn532-sdk-1.0.2.jar.sha1 b/lib/pn532/pn532-sdk/1.0.2/pn532-sdk-1.0.2.jar.sha1 new file mode 100644 index 0000000..8e962bd --- /dev/null +++ b/lib/pn532/pn532-sdk/1.0.2/pn532-sdk-1.0.2.jar.sha1 @@ -0,0 +1 @@ +2bf9f8b279977cb5511cb075f4f2cd316e2bfa62 \ No newline at end of file diff --git a/lib/pn532/pn532-sdk/1.0.2/pn532-sdk-1.0.2.pom b/lib/pn532/pn532-sdk/1.0.2/pn532-sdk-1.0.2.pom new file mode 100644 index 0000000..b37e64a --- /dev/null +++ b/lib/pn532/pn532-sdk/1.0.2/pn532-sdk-1.0.2.pom @@ -0,0 +1,9 @@ + + + 4.0.0 + pn532 + pn532-sdk + 1.0.2 + POM was created from install:install-file + diff --git a/lib/pn532/pn532-sdk/1.0.2/pn532-sdk-1.0.2.pom.md5 b/lib/pn532/pn532-sdk/1.0.2/pn532-sdk-1.0.2.pom.md5 new file mode 100644 index 0000000..6c7d5a3 --- /dev/null +++ b/lib/pn532/pn532-sdk/1.0.2/pn532-sdk-1.0.2.pom.md5 @@ -0,0 +1 @@ +c0332fdac981cf53dc426b4fb6a173df \ No newline at end of file diff --git a/lib/pn532/pn532-sdk/1.0.2/pn532-sdk-1.0.2.pom.sha1 b/lib/pn532/pn532-sdk/1.0.2/pn532-sdk-1.0.2.pom.sha1 new file mode 100644 index 0000000..a62fa61 --- /dev/null +++ b/lib/pn532/pn532-sdk/1.0.2/pn532-sdk-1.0.2.pom.sha1 @@ -0,0 +1 @@ +d6d7adcafc0119db3ec26d1bf8522fff1e78160c \ No newline at end of file diff --git a/lib/pn532/pn532-sdk/maven-metadata-local.xml b/lib/pn532/pn532-sdk/maven-metadata-local.xml new file mode 100644 index 0000000..b178090 --- /dev/null +++ b/lib/pn532/pn532-sdk/maven-metadata-local.xml @@ -0,0 +1,12 @@ + + + pn532 + pn532-sdk + + 1.0.2 + + 1.0.2 + + 20231116111447 + + diff --git a/lib/pn532/pn532-sdk/maven-metadata-local.xml.md5 b/lib/pn532/pn532-sdk/maven-metadata-local.xml.md5 new file mode 100644 index 0000000..cabf41c --- /dev/null +++ b/lib/pn532/pn532-sdk/maven-metadata-local.xml.md5 @@ -0,0 +1 @@ +324d2ea359305a5cdef2fd48169e4c6d \ No newline at end of file diff --git a/lib/pn532/pn532-sdk/maven-metadata-local.xml.sha1 b/lib/pn532/pn532-sdk/maven-metadata-local.xml.sha1 new file mode 100644 index 0000000..7badd75 --- /dev/null +++ b/lib/pn532/pn532-sdk/maven-metadata-local.xml.sha1 @@ -0,0 +1 @@ +68b60db960171920acbf8b33283396ded102e64e \ No newline at end of file diff --git a/pom.xml b/pom.xml index 8afba98..68c3713 100644 --- a/pom.xml +++ b/pom.xml @@ -12,8 +12,76 @@ 21 21 UTF-8 + + 1.7.32 + 2.4.0 + 3.5.1 + + + schafkopf-backend-build + + + maven-jar-plugin + 3.3.0 + + + + org.example.BackendServer + + + + + + + maven-compiler-plugin + 3.11.0 + + true + true + false + + + + + org.apache.maven.plugins + maven-shade-plugin + 3.5.1 + + + + + + + + package + + shade + + + + + + + + + in-project + In Project Repo + file://${project.basedir}/lib + + + + + org.slf4j + slf4j-api + ${slf4j.version} + + + org.slf4j + slf4j-simple + ${slf4j.version} + org.eclipse.jetty.websocket @@ -47,5 +115,31 @@ dotenv-java 3.0.0 + + + + com.pi4j + pi4j-core + ${pi4j.version} + + + + + com.pi4j + pi4j-plugin-raspberrypi + ${pi4j.version} + + + com.pi4j + pi4j-plugin-pigpio + ${pi4j.version} + + + + + pn532 + pn532-sdk + 1.0.2 + \ No newline at end of file diff --git a/src/main/java/org/example/BackendServer.java b/src/main/java/org/example/BackendServer.java index f7c62ee..ef68297 100644 --- a/src/main/java/org/example/BackendServer.java +++ b/src/main/java/org/example/BackendServer.java @@ -13,6 +13,7 @@ import org.eclipse.jetty.websocket.server.config.JettyWebSocketServletContainerI import java.util.ArrayList; import java.util.List; +import java.util.concurrent.CountDownLatch; import org.eclipse.jetty.servlet.FilterHolder; import org.eclipse.jetty.servlets.CrossOriginFilter; @@ -33,17 +34,25 @@ public class BackendServer private final ServerConnector connector; private final Schafkopf schafkopfGame; + private final KartenLeser nfcLeser; + private CountDownLatch nfcLatch = new CountDownLatch(1); + + private Boolean readingMode = false; + private String uidString = ""; public BackendServer() { - Dotenv dotenv = Dotenv.configure().load(); + Dotenv dotenv = Dotenv.configure().directory("./").load(); server = new Server(); InetSocketAddress address = new InetSocketAddress(dotenv.get("VITE_APP_WEBSOCKET_IP"), 8080); connector = new ServerConnector(server); connector.setHost(address.getHostName()); connector.setPort(address.getPort()); server.addConnector(connector); + schafkopfGame = new Schafkopf(this); + nfcLeser = new KartenLeser(this); + // Setup the basic application "context" for this application at "/" // This is also known as the handler tree (in jetty speak) ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS); @@ -133,4 +142,23 @@ public class BackendServer public void showFarbe() { schafkopfGame.showFarbe(); } + public void nfcGelesen(String uidString) { + if(this.uidString.equals(uidString)){ + return; + } + if(!this.readingMode){ + return; + } + + this.uidString = uidString; + nfcLatch.countDown(); + } + + public String waitForCardScan() throws InterruptedException { + this.readingMode = true; + nfcLatch.await(); + this.readingMode = false; + nfcLatch = new CountDownLatch(1); + return this.uidString; + } } \ No newline at end of file diff --git a/src/main/java/org/example/KartenLeser.java b/src/main/java/org/example/KartenLeser.java new file mode 100644 index 0000000..3e13729 --- /dev/null +++ b/src/main/java/org/example/KartenLeser.java @@ -0,0 +1,72 @@ +package org.example; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + +import com.pi4j.Pi4J; +import com.pi4j.io.i2c.I2C; +import mk.hsilomedus.pn532.*; +import mk.hsilomedus.pn532.Pn532SamThread.Pn532SamThreadListener; + +public final class KartenLeser { + + private static BackendServer server; + + public KartenLeser(BackendServer server){ + this.server = server; + + new Thread(() -> { + + new KartenListener().run(); + + }).start(); + } + + + + public static final void main(String[] args) throws IOException { + + } + + private static class KartenListener implements Pn532SamThreadListener { + + @SuppressWarnings("rawtypes") + Pn532SamThread i2cThread = new Pn532SamThread<>(this, new Pn532I2c()); + + + public void run() { + Pn532ContextHelper.initialize(); + i2cThread.start(); + } + + public void close() { + closeThread(i2cThread); + Pn532ContextHelper.shutdown(); + } + + @Override + public void receiveMessage(String message) { + System.out.println(message); + } + + @Override + public void uidReceived(String displayName, byte[] uid) { + server.nfcGelesen(Pn532SamThreadListener.getUidString(uid) ); + } + + @SuppressWarnings("rawtypes") + private void closeThread(Pn532SamThread thread) { + if (thread != null && thread.isAlive()) { + thread.close(); + + try { + thread.join(); + } catch (InterruptedException e) { + System.out.println("Error closing thread: " + e.getMessage()); + Thread.currentThread().interrupt(); + } + } + } + } +} \ No newline at end of file diff --git a/src/main/java/org/example/KartenUtil.java b/src/main/java/org/example/KartenUtil.java index c4bcbb1..ebebed8 100644 --- a/src/main/java/org/example/KartenUtil.java +++ b/src/main/java/org/example/KartenUtil.java @@ -76,6 +76,89 @@ public class KartenUtil { return cardName; } + public static String getIdOfUid(String uid){ + switch (uid){ + case "04A56BB4780000": + return "eichel_7"; + case "04A46BB4780000": + return "eichel_8"; + case "04A26BB4780000": + return "eichel_9"; + case "04A16BB4780000": + return "eichel_x"; + case "049E6BB4780000": + return "eichel_k"; + case "04A86BB4780000": + return "eichel_a"; + case "04A06BB4780000": + return "eichel_u"; + case "049F6BB4780000": + return "eichel_o"; + + case "04F26BB4780000": + return "blatt_7"; + case "04A76BB4780000": + return "blatt_8"; + case "049B6BB4780000": + return "blatt_9"; + case "04996BB4780000": + return "blatt_x"; + case "041CD2C2126F81": + return "blatt_k"; + case "04A96BB4780000": + return "blatt_a"; + case "049A6BB4780000": + return "blatt_u"; + case "049D6BB4780000": + return "blatt_o"; + + case "04936BB4780000": + return "schell_7"; + case "04F697C2126F80": + return "schell_8"; + case "04946BB4780000": + return "schell_9"; + case "04956BB4780000": + return "schell_x"; + case "04986BB4780000": + return "schell_k"; + case "04AA6BB4780000": + return "schell_a"; + case "04966BB4780000": + return "schell_u"; + case "04976BB4780000": + return "schell_o"; + + case "04F36BB4780000": + return "herz_7"; + case "04B06BB4780000": + return "herz_8"; + case "04AF6BB4780000": + return "herz_9"; + case "04AE6BB4780000": + return "herz_x"; + case "04AB6BB4780000": + return "herz_k"; + case "049C6BB4780000": + return "herz_a"; + case "04AD6BB4780000": + return "herz_u"; + case "04AC6BB4780000": + return "herz_o"; + } + return null; + } + + public static Karte getKarteById(String id){ + List kartenList = initializeSchafKopfCardDeck(); + for (Karte karte : kartenList) { + if (karte.getId().equalsIgnoreCase(id)) { + return karte; + } + } + return null; + } + public static void removeCards(List allCards, List cardsToRemove) { allCards.removeAll(cardsToRemove); } diff --git a/src/main/java/org/example/Schafkopf.java b/src/main/java/org/example/Schafkopf.java index 4e04263..e45da80 100644 --- a/src/main/java/org/example/Schafkopf.java +++ b/src/main/java/org/example/Schafkopf.java @@ -78,7 +78,6 @@ public class Schafkopf { String karteJson = gson.toJson(karte); server.sendMessageToAllFrontendEndpoints(karteJson); } - } public void showFarbe() {