]> git.basschouten.com Git - openhab-addons.git/blob
1bc873dc2d4382cef89a5da2ec496ae86e0f9f1c
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2022 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.mqtt.generic.values;
14
15 import java.util.stream.Collectors;
16 import java.util.stream.Stream;
17
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19 import org.eclipse.jdt.annotation.Nullable;
20 import org.openhab.core.library.CoreItemFactory;
21 import org.openhab.core.library.types.OpenClosedType;
22 import org.openhab.core.library.types.StringType;
23 import org.openhab.core.types.Command;
24
25 /**
26  * Implements an open/close boolean value.
27  *
28  * @author David Graeff - Initial contribution
29  */
30 @NonNullByDefault
31 public class OpenCloseValue extends Value {
32     private final String openString;
33     private final String closeString;
34
35     /**
36      * Creates a contact Open/Close type.
37      */
38     public OpenCloseValue() {
39         super(CoreItemFactory.CONTACT, Stream.of(OpenClosedType.class, StringType.class).collect(Collectors.toList()));
40         this.openString = OpenClosedType.OPEN.name();
41         this.closeString = OpenClosedType.CLOSED.name();
42     }
43
44     /**
45      * Creates a new contact Open/Close value.
46      *
47      * @param openValue The ON value string. This will be compared to MQTT messages.
48      * @param closeValue The OFF value string. This will be compared to MQTT messages.
49      */
50     public OpenCloseValue(@Nullable String openValue, @Nullable String closeValue) {
51         super(CoreItemFactory.CONTACT, Stream.of(OpenClosedType.class, StringType.class).collect(Collectors.toList()));
52         this.openString = openValue == null ? OpenClosedType.OPEN.name() : openValue;
53         this.closeString = closeValue == null ? OpenClosedType.CLOSED.name() : closeValue;
54     }
55
56     @Override
57     public void update(Command command) throws IllegalArgumentException {
58         if (command instanceof OpenClosedType) {
59             state = (OpenClosedType) command;
60         } else {
61             final String updatedValue = command.toString();
62             if (openString.equals(updatedValue)) {
63                 state = OpenClosedType.OPEN;
64             } else if (closeString.equals(updatedValue)) {
65                 state = OpenClosedType.CLOSED;
66             } else {
67                 state = OpenClosedType.valueOf(updatedValue);
68             }
69         }
70     }
71
72     @Override
73     public String getMQTTpublishValue(@Nullable String pattern) {
74         String formatPattern = pattern;
75         if (formatPattern == null) {
76             formatPattern = "%s";
77         }
78
79         return String.format(formatPattern, state == OpenClosedType.OPEN ? openString : closeString);
80     }
81 }