]> git.basschouten.com Git - openhab-addons.git/blob
71c4a8fd23dd79680e509b60da02452ba3d11195
[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.heos.internal.json.dto;
14
15 import java.util.Map;
16
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.eclipse.jdt.annotation.Nullable;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
21
22 /**
23  * Abstract parent class for the HEOS event/response objects
24  *
25  * @author Martin van Wingerden - Initial contribution
26  */
27 @NonNullByDefault
28 public abstract class HeosObject {
29     private final Logger logger = LoggerFactory.getLogger(HeosObject.class);
30
31     public final String rawCommand;
32     private final Map<String, String> attributes;
33
34     HeosObject(String rawCommand, Map<String, String> attributes) {
35         this.rawCommand = rawCommand;
36         this.attributes = attributes;
37     }
38
39     public boolean getBooleanAttribute(HeosCommunicationAttribute attributeName) {
40         return "on".equals(attributes.get(attributeName.getLabel()));
41     }
42
43     public @Nullable Long getNumericAttribute(HeosCommunicationAttribute attributeName) {
44         @Nullable
45         String attribute = attributes.get(attributeName.getLabel());
46
47         if (attribute == null) {
48             return null;
49         }
50
51         try {
52             return Long.valueOf(attribute);
53         } catch (NumberFormatException e) {
54             logger.debug("Failed to parse number: {}, message: {}", attribute, e.getMessage());
55             return null;
56         }
57     }
58
59     public @Nullable String getAttribute(HeosCommunicationAttribute attributeName) {
60         return attributes.get(attributeName.getLabel());
61     }
62
63     public boolean hasAttribute(HeosCommunicationAttribute attribute) {
64         return attributes.containsKey(attribute.getLabel());
65     }
66
67     @Override
68     public String toString() {
69         return "HeosObject{" + "rawCommand='" + rawCommand + '\'' + ", attributes=" + attributes + '}';
70     }
71 }