diff --git a/bundles/org.openhab.binding.enocean/src/main/java/org/openhab/binding/enocean/internal/transceiver/EnOceanTransceiver.java b/bundles/org.openhab.binding.enocean/src/main/java/org/openhab/binding/enocean/internal/transceiver/EnOceanTransceiver.java index 55be4988b..625798e97 100644 --- a/bundles/org.openhab.binding.enocean/src/main/java/org/openhab/binding/enocean/internal/transceiver/EnOceanTransceiver.java +++ b/bundles/org.openhab.binding.enocean/src/main/java/org/openhab/binding/enocean/internal/transceiver/EnOceanTransceiver.java @@ -220,6 +220,7 @@ public abstract class EnOceanTransceiver implements SerialPortEventListener { readingTask = null; timeOut = null; listeners.clear(); + eventListeners.clear(); teachInListener = null; errorListener = null; @@ -302,9 +303,11 @@ public abstract class EnOceanTransceiver implements SerialPortEventListener { } long s = Long.parseLong(HexUtils.bytesToHex(senderId), 16); - HashSet pl = listeners.get(s); - if (pl != null) { - pl.forEach(l -> l.packetReceived(msg)); + synchronized (this) { + HashSet pl = listeners.get(s); + if (pl != null) { + pl.forEach(l -> l.packetReceived(msg)); + } } } } else { @@ -331,7 +334,9 @@ public abstract class EnOceanTransceiver implements SerialPortEventListener { } } - eventListeners.forEach(l -> l.eventReceived(event)); + synchronized (this) { + eventListeners.forEach(l -> l.eventReceived(event)); + } } } catch (Exception e) { logger.error("Exception in informListeners", e); @@ -374,13 +379,13 @@ public abstract class EnOceanTransceiver implements SerialPortEventListener { protected abstract byte[] serializePacket(BasePacket packet) throws EnOceanException; - public void addPacketListener(PacketListener listener, long senderIdToListenTo) { + public synchronized void addPacketListener(PacketListener listener, long senderIdToListenTo) { if (listeners.computeIfAbsent(senderIdToListenTo, k -> new HashSet<>()).add(listener)) { logger.debug("Listener added: {}", senderIdToListenTo); } } - public void removePacketListener(PacketListener listener, long senderIdToListenTo) { + public synchronized void removePacketListener(PacketListener listener, long senderIdToListenTo) { HashSet pl = listeners.get(senderIdToListenTo); if (pl != null) { pl.remove(listener); @@ -390,11 +395,11 @@ public abstract class EnOceanTransceiver implements SerialPortEventListener { } } - public void addEventMessageListener(EventListener listener) { + public synchronized void addEventMessageListener(EventListener listener) { eventListeners.add(listener); } - public void removeEventMessageListener(EventListener listener) { + public synchronized void removeEventMessageListener(EventListener listener) { eventListeners.remove(listener); }