]> git.basschouten.com Git - openhab-addons.git/blob
9bc160bc484322305234f5e0e636681fe9df5b65
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2020 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.io.transport.modbus;
14
15 import java.util.stream.Stream;
16
17 import org.eclipse.jdt.annotation.NonNull;
18
19 import net.wimpi.modbus.Modbus;
20
21 /**
22  * Modbus write function codes supported by this transport
23  *
24  * @author Sami Salonen - Initial contribution
25  */
26 public enum ModbusWriteFunctionCode {
27     WRITE_COIL(Modbus.WRITE_COIL),
28     WRITE_MULTIPLE_COILS(Modbus.WRITE_MULTIPLE_COILS),
29     WRITE_SINGLE_REGISTER(Modbus.WRITE_SINGLE_REGISTER),
30     WRITE_MULTIPLE_REGISTERS(Modbus.WRITE_MULTIPLE_REGISTERS);
31
32     private final int functionCode;
33
34     ModbusWriteFunctionCode(int code) {
35         functionCode = code;
36     }
37
38     /**
39      * Get numeric function code represented by this instance
40      *
41      * @return
42      */
43     public int getFunctionCode() {
44         return functionCode;
45     }
46
47     /**
48      * Construct {@link ModbusWriteFunctionCode} from the numeric function code
49      *
50      * @param functionCode numeric function code
51      * @return {@link ModbusWriteFunctionCode} matching the numeric function code
52      * @throws IllegalArgumentException with unsupported functions
53      */
54     @SuppressWarnings("null")
55     public static @NonNull ModbusWriteFunctionCode fromFunctionCode(int functionCode) throws IllegalArgumentException {
56         return Stream.of(ModbusWriteFunctionCode.values()).filter(v -> v.getFunctionCode() == functionCode).findFirst()
57                 .orElseThrow(() -> new IllegalArgumentException("Invalid functionCode"));
58     }
59 }