]> git.basschouten.com Git - openhab-addons.git/blob
2fa5086ac8abc05346fb378ab69466fa30da2bea
[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.rfxcom.internal.connector;
14
15 import java.io.IOException;
16 import java.io.InputStream;
17 import java.util.ArrayList;
18 import java.util.List;
19
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
22
23 /**
24  * RFXCOM connector for serial port communication.
25  *
26  * @author James Hewitt-Thomas - Initial contribution
27  */
28 public abstract class RFXComBaseConnector implements RFXComConnectorInterface {
29     private final Logger logger = LoggerFactory.getLogger(RFXComBaseConnector.class);
30
31     private List<RFXComEventListener> listeners = new ArrayList<>();
32     protected InputStream in;
33
34     @Override
35     public synchronized void addEventListener(RFXComEventListener rfxComEventListener) {
36         if (!listeners.contains(rfxComEventListener)) {
37             listeners.add(rfxComEventListener);
38         }
39     }
40
41     @Override
42     public synchronized void removeEventListener(RFXComEventListener listener) {
43         listeners.remove(listener);
44     }
45
46     void sendMsgToListeners(byte[] msg) {
47         try {
48             for (RFXComEventListener listener : listeners) {
49                 listener.packetReceived(msg);
50             }
51         } catch (Exception e) {
52             logger.error("Event listener invoking error", e);
53         }
54     }
55
56     void sendErrorToListeners(String error) {
57         try {
58             for (RFXComEventListener listener : listeners) {
59                 listener.errorOccurred(error);
60             }
61         } catch (Exception e) {
62             logger.error("Event listener invoking error", e);
63         }
64     }
65
66     int read(byte[] buffer, int offset, int length) throws IOException {
67         return in.read(buffer, offset, length);
68     }
69 }