]> git.basschouten.com Git - openhab-addons.git/blob
7fc2d6339ad5171eac96f3653e3d83786d192d8d
[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.freeathomesystem.internal.valuestateconverter;
14
15 import org.eclipse.jdt.annotation.NonNullByDefault;
16 import org.openhab.core.library.types.OnOffType;
17 import org.openhab.core.types.State;
18
19 /**
20  * The {@link BinaryValueStateConverter} is a value converter for integer values with a specific mask
21  *
22  * @author Andras Uhrin - Initial contribution
23  *
24  */
25 @NonNullByDefault
26 public class BinaryValueStateConverter implements ValueStateConverter {
27
28     private int maskValue;
29
30     public BinaryValueStateConverter(int mask) {
31         maskValue = mask;
32     }
33
34     @Override
35     public State convertToState(String value) {
36         int intValue = Integer.decode(value);
37         int result;
38
39         result = intValue & maskValue;
40
41         if (maskValue == result) {
42             return OnOffType.ON;
43         } else {
44             return OnOffType.OFF;
45         }
46     }
47
48     @Override
49     public String convertToValueString(State state) {
50         if (state.equals(OnOffType.ON)) {
51             return "1";
52         }
53
54         if (state.equals(OnOffType.OFF)) {
55             return "0";
56         }
57
58         return "";
59     }
60 }