]> git.basschouten.com Git - openhab-addons.git/blob
255d59094f4264225f00248f028cc9b20e58b25f
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2024 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.smartmeter.internal;
14
15 import java.util.function.Supplier;
16
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.eclipse.jdt.annotation.Nullable;
19 import org.openhab.binding.smartmeter.internal.helper.ProtocolMode;
20 import org.openhab.binding.smartmeter.internal.iec62056.Iec62056_21MeterReader;
21 import org.openhab.binding.smartmeter.internal.sml.SmlMeterReader;
22 import org.openhab.core.io.transport.serial.SerialPortManager;
23
24 /**
25  * Factory to get the correct device reader for a specific {@link ProtocolMode}
26  *
27  * @author Matthias Steigenberger - Initial contribution
28  *
29  */
30 @NonNullByDefault
31 public class MeterDeviceFactory {
32
33     /**
34      * Gets a concrete {@link MeterDevice} for given values.
35      * 
36      * @param serialPortManagerSupplier The Supplier of a {@link SerialPortManager}
37      * @param mode The {@link ProtocolMode}.
38      * @param deviceId
39      * @param serialPort The serial port identifier to connect ot.
40      * @param initMessage The message which shall be sent before reading values (or to actually make the meter sent
41      *            values).
42      * @param baudrate The baudrate to set before communication.
43      * @param baudrateChangeDelay The change delay before changing the baudrate (used only for specific protocols).
44      * @return The new {@link MeterDevice} or null.
45      */
46     public static @Nullable MeterDevice<?> getDevice(Supplier<SerialPortManager> serialPortManagerSupplier, String mode,
47             String deviceId, String serialPort, byte @Nullable [] initMessage, int baudrate, int baudrateChangeDelay) {
48         ProtocolMode protocolMode = ProtocolMode.valueOf(mode.toUpperCase());
49         switch (protocolMode) {
50             case D:
51             case ABC:
52                 return new Iec62056_21MeterReader(serialPortManagerSupplier, deviceId, serialPort, initMessage,
53                         baudrate, baudrateChangeDelay, protocolMode);
54             case SML:
55                 return SmlMeterReader.createInstance(serialPortManagerSupplier, deviceId, serialPort, initMessage,
56                         baudrate, baudrateChangeDelay);
57             default:
58                 return null;
59         }
60     }
61 }