swordLock

This commit is contained in:
LionTion committed 2026-10-23 19:42:34 +02:00
1 parent 00685ae103
commit eccb0efca3
1 file changed
+52 -1
@@ -16,6 +16,7 @@ import java.io.IOException;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicReference;
import java.util.logging.Logger;
@Component
@@ -30,6 +31,10 @@ public class MqttConfig extends TextWebSocketHandler {
// Atomic variable tracking whether the sword is online/active
private final AtomicBoolean swordIsOnline = new AtomicBoolean(true);
// Atomic variable tracking if a client already locked in onto the sword
private final AtomicBoolean swordLock = new AtomicBoolean(false);
private final AtomicReference<String> swordLockId = new AtomicReference<String>(null);
private static final String BROKER_URL = "ssl://excaliburs-erben.de:8883";
private static final String MQTT_USER = "exodususer";
private static final String MQTT_PASS = "awesomeexodusoutdooradventures";
@@ -101,7 +106,9 @@ public class MqttConfig extends TextWebSocketHandler {
String eventTopic = (isDev ? "staging/" : "") + "outdoor/king-arthur/sword/event";
String status = swordIsOnline.get() ? "online" : "offline";
String jsonMessage = objectMapper.writeValueAsString(new BridgeMessage(eventTopic, status));
String swordLockMessage = objectMapper.writeValueAsString(new BridgeMessage("system/swordLock", swordLock.get() ? "locked" : "unlocked"));
session.sendMessage(new TextMessage(jsonMessage));
session.sendMessage(new TextMessage(swordLockMessage));
logger.info("Client " + session.getId() + " den Status: " + status + " mitgeteilt. TourCode: " + session.getAttributes().get("tourCode"));
} catch (IOException e) {
logger.severe("Fehler beim Senden des aktuellen Sword-Status an neuen Client: " + e.getMessage());
@@ -120,7 +127,27 @@ public class MqttConfig extends TextWebSocketHandler {
ClientMessage clientMsg = objectMapper.readValue(message.getPayload(), ClientMessage.class);
if (mqttClient != null && mqttClient.isConnected()) {
logger.info(clientMsg.topic + ": " + clientMsg.payload);
if (clientMsg.topic.startsWith("system/")) {
if (clientMsg.topic.equals("system/swordLock")) {
if (clientMsg.payload.equals("takeLock")) {
logger.info(String.format("SID: %s LID: %s", session.getId(), swordLockId.get()));
if (swordLock.compareAndSet(false, true)) {
logger.info("LOCK SET");
swordLockId.set(session.getId());
broadcastToWebSockets("system/swordLock", "locked");
}
} else if (clientMsg.payload.equals("giveLock")) {
logger.info(String.format("SID: %s LID: %s", session.getId(), swordLockId.get()));
if (session.getId().equals(swordLockId.get())) {
logger.info("LOCK UNSET");
swordLock.set(false);
broadcastToWebSockets("system/swordLock", "unlocked");
}
}
}
} else if (mqttClient != null && mqttClient.isConnected()) {
MqttMessage mqttMessage = new MqttMessage(clientMsg.getPayload().getBytes());
mqttMessage.setQos(2);
mqttClient.publish(clientMsg.getTopic(), mqttMessage);
@@ -133,10 +160,34 @@ public class MqttConfig extends TextWebSocketHandler {
@Override
public void afterConnectionClosed(WebSocketSession session, CloseStatus status) {
String swordlockid = swordLockId.get();
String sessionId = session.getId();
logger.info(String.format("CLOSED: %s -> %s", sessionId, swordlockid));
sessions.remove(session);
if (swordlockid != null && swordlockid.equals(sessionId)) {
swordLock.set(false);
swordLockId.set(null);
logger.info("LOCK UNSET");
broadcastToWebSockets("system/swordLock", "unlocked");
}
logger.fine("Client getrennt: " + session.getId());
}
@Override
public void handleTransportError(WebSocketSession session, Throwable exception) throws Exception {
String swordlockid = swordLockId.get();
String sessionId = session.getId();
logger.info(String.format("ERROR: %s -> %s", sessionId, swordlockid));
sessions.remove(session);
if (swordlockid != null && swordlockid.equals(sessionId)) {
swordLock.set(false);
swordLockId.set(null);
logger.info("LOCK UNSET");
broadcastToWebSockets("system/swordLock", "unlocked");
}
}
private void broadcastToMqtt(String topic, String payload) {
}