2 * Copyright (c) 2010-2024 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.smartmeter.internal;
15 import java.util.function.Supplier;
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;
25 * Factory to get the correct device reader for a specific {@link ProtocolMode}
27 * @author Matthias Steigenberger - Initial contribution
31 public class MeterDeviceFactory {
34 * Gets a concrete {@link MeterDevice} for given values.
36 * @param serialPortManagerSupplier The Supplier of a {@link SerialPortManager}
37 * @param mode The {@link ProtocolMode}.
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
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.
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) {
52 return new Iec62056_21MeterReader(serialPortManagerSupplier, deviceId, serialPort, initMessage,
53 baudrate, baudrateChangeDelay, protocolMode);
55 return SmlMeterReader.createInstance(serialPortManagerSupplier, deviceId, serialPort, initMessage,
56 baudrate, baudrateChangeDelay);