2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
7 * This program and the accompanying materials are made available under the
8 * terms of the Eclipse Public License 2.0 which is available at
9 * http://www.eclipse.org/legal/epl-2.0
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.plugwise.internal;
15 import java.io.IOException;
16 import java.util.function.Supplier;
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19 import org.openhab.binding.plugwise.internal.config.PlugwiseStickConfig;
20 import org.openhab.binding.plugwise.internal.listener.PlugwiseMessageListener;
21 import org.openhab.binding.plugwise.internal.protocol.Message;
22 import org.openhab.binding.plugwise.internal.protocol.field.MACAddress;
23 import org.openhab.core.io.transport.serial.SerialPortManager;
24 import org.openhab.core.thing.ThingUID;
27 * The {@link PlugwiseCommunicationHandler} handles all serial communication with the Plugwise Stick.
29 * @author Wouter Born, Karel Goderis - Initial contribution
32 public class PlugwiseCommunicationHandler {
34 private final PlugwiseCommunicationContext context;
35 private final PlugwiseMessageProcessor messageProcessor;
36 private final PlugwiseMessageSender messageSender;
38 private boolean initialized = false;
40 public PlugwiseCommunicationHandler(ThingUID bridgeUID, Supplier<PlugwiseStickConfig> configurationSupplier,
41 SerialPortManager serialPortManager) {
42 context = new PlugwiseCommunicationContext(bridgeUID, configurationSupplier, serialPortManager);
43 messageProcessor = new PlugwiseMessageProcessor(context);
44 messageSender = new PlugwiseMessageSender(context);
47 public void addMessageListener(PlugwiseMessageListener listener) {
48 context.getFilteredListeners().addListener(listener);
51 public void addMessageListener(PlugwiseMessageListener listener, MACAddress macAddress) {
52 context.getFilteredListeners().addListener(listener, macAddress);
55 public void removeMessageListener(PlugwiseMessageListener listener) {
56 context.getFilteredListeners().removeListener(listener);
59 public void sendMessage(Message message, PlugwiseMessagePriority priority) throws IOException {
61 messageSender.sendMessage(message, priority);
65 public void start() throws PlugwiseInitializationException {
67 context.clearQueues();
68 context.initializeSerialPort();
69 messageSender.start();
70 messageProcessor.start();
72 } catch (PlugwiseInitializationException e) {
80 messageProcessor.stop();
81 context.closeSerialPort();