]> git.basschouten.com Git - openhab-addons.git/blob
ef0612b2267bd82053edba901e23fc52685f988f
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.plugwise.internal;
14
15 import java.io.IOException;
16 import java.util.function.Supplier;
17
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;
25
26 /**
27  * The {@link PlugwiseCommunicationHandler} handles all serial communication with the Plugwise Stick.
28  *
29  * @author Wouter Born, Karel Goderis - Initial contribution
30  */
31 @NonNullByDefault
32 public class PlugwiseCommunicationHandler {
33
34     private final PlugwiseCommunicationContext context;
35     private final PlugwiseMessageProcessor messageProcessor;
36     private final PlugwiseMessageSender messageSender;
37
38     private boolean initialized = false;
39
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);
45     }
46
47     public void addMessageListener(PlugwiseMessageListener listener) {
48         context.getFilteredListeners().addListener(listener);
49     }
50
51     public void addMessageListener(PlugwiseMessageListener listener, MACAddress macAddress) {
52         context.getFilteredListeners().addListener(listener, macAddress);
53     }
54
55     public void removeMessageListener(PlugwiseMessageListener listener) {
56         context.getFilteredListeners().removeListener(listener);
57     }
58
59     public void sendMessage(Message message, PlugwiseMessagePriority priority) throws IOException {
60         if (initialized) {
61             messageSender.sendMessage(message, priority);
62         }
63     }
64
65     public void start() throws PlugwiseInitializationException {
66         try {
67             context.clearQueues();
68             context.initializeSerialPort();
69             messageSender.start();
70             messageProcessor.start();
71             initialized = true;
72         } catch (PlugwiseInitializationException e) {
73             initialized = false;
74             throw e;
75         }
76     }
77
78     public void stop() {
79         messageSender.stop();
80         messageProcessor.stop();
81         context.closeSerialPort();
82         initialized = false;
83     }
84 }