]> git.basschouten.com Git - openhab-addons.git/blob
1fd8f64e891486b8746b0075ed7d9b74958d068e
[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.smartmeter.internal.helper;
14
15 import org.eclipse.jdt.annotation.NonNullByDefault;
16
17 /**
18  *
19  * @author Matthias Steigenberger - Initial contribution
20  *
21  */
22 @NonNullByDefault
23 public enum Baudrate {
24     AUTO(-1) {
25         @Override
26         public String toString() {
27             return name();
28         }
29     },
30     _300(300),
31     _600(600),
32     _1200(1200),
33     _1800(1800),
34     _2400(2400),
35     _4800(4800),
36     _9600(9600),
37     _19200(19200),
38     _38400(38400);
39
40     private int baudrate;
41
42     private Baudrate(int baudrate) {
43         this.baudrate = baudrate;
44     }
45
46     public int getBaudrate() {
47         return baudrate;
48     }
49
50     public static Baudrate fromBaudrate(int baudrate) {
51         for (Baudrate baud : values()) {
52             if (baud.getBaudrate() == baudrate) {
53                 return baud;
54             }
55         }
56         return Baudrate._9600;
57     }
58
59     public static Baudrate fromString(String baudrate) {
60         try {
61             if (baudrate.equalsIgnoreCase(AUTO.name())) {
62                 return Baudrate.AUTO;
63             }
64             return valueOf("_" + baudrate.toUpperCase());
65         } catch (Exception e) {
66             return Baudrate.AUTO;
67         }
68     }
69
70     @Override
71     public String toString() {
72         return getBaudrate() + "bd";
73     }
74 }