]> git.basschouten.com Git - openhab-addons.git/blob
636e9fe6b2a153edc37cd9263b6818daff2bb6b1
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2022 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.dsmr.internal.device;
14
15 import org.eclipse.jdt.annotation.NonNullByDefault;
16 import org.openhab.binding.dsmr.internal.device.connector.DSMRSerialConnector;
17 import org.openhab.binding.dsmr.internal.device.connector.DSMRSerialSettings;
18 import org.openhab.core.io.transport.serial.SerialPortManager;
19
20 /**
21  * Implementation of a DSMRDevice with fixed serial port settings. With fixed port settings the code is much simpler
22  * because no detecting of settings needs to be done and when things fail no redirecting is needed either.
23  *
24  * @author Hilbrand Bouwkamp - Initial contribution
25  */
26 @NonNullByDefault
27 public class DSMRFixedConfigDevice implements DSMRDevice {
28
29     private final DSMRSerialConnector dsmrPort;
30     private final DSMRSerialSettings fixedPortSettings;
31     private final DSMRTelegramListener telegramListener;
32
33     /**
34      * Constructor.
35      *
36      * @param serialPortManager the manager to get a new serial port connecting from
37      * @param serialPortName the port name (e.g. /dev/ttyUSB0 or COM1)
38      * @param fixedPortSettings The serial port connection settings
39      * @param listener the parent {@link DSMREventListener}
40      * @param telegramListener listener to report found telegrams or errors
41      */
42     public DSMRFixedConfigDevice(SerialPortManager serialPortManager, String serialPortName,
43             DSMRSerialSettings fixedPortSettings, DSMREventListener listener, DSMRTelegramListener telegramListener) {
44         this.fixedPortSettings = fixedPortSettings;
45         this.telegramListener = telegramListener;
46         telegramListener.setDsmrEventListener(listener);
47
48         dsmrPort = new DSMRSerialConnector(serialPortManager, serialPortName, telegramListener);
49     }
50
51     @Override
52     public void start() {
53         dsmrPort.open(fixedPortSettings);
54     }
55
56     @Override
57     public void restart() {
58         dsmrPort.restart(fixedPortSettings);
59     }
60
61     @Override
62     public void stop() {
63         dsmrPort.close();
64     }
65
66     @Override
67     public void setLenientMode(boolean lenientMode) {
68         telegramListener.setLenientMode(lenientMode);
69     }
70 }