]> git.basschouten.com Git - openhab-addons.git/blob
8606c26bf97df16bcdb9d0a2fe2866601fc7dc48
[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.digitalstrom.internal.lib.event.types;
14
15 import java.util.HashMap;
16 import java.util.Map;
17 import java.util.Map.Entry;
18 import java.util.Set;
19
20 import org.openhab.binding.digitalstrom.internal.lib.event.constants.EventResponseEnum;
21 import org.openhab.binding.digitalstrom.internal.lib.serverconnection.constants.JSONApiResponseKeysEnum;
22
23 import com.google.gson.JsonElement;
24 import com.google.gson.JsonObject;
25
26 /**
27  * The {@link EventItemImpl} is the implementation of the {@link EventItem}.
28  *
29  * @author Michael Ochel - Initial contribution
30  * @author Mathias Siegele - Initial contribution
31  */
32 public class EventItemImpl implements EventItem {
33
34     private final String name;
35     private Map<EventResponseEnum, String> properties;
36     private Map<EventResponseEnum, String> source;
37
38     /**
39      * Creates a new {@link EventItemImpl} from the given digitalSTROM-Event-Item {@link JsonObject}.
40      *
41      * @param jsonEventItem must not be null
42      */
43     public EventItemImpl(JsonObject jsonEventItem) {
44         name = jsonEventItem.get(JSONApiResponseKeysEnum.NAME.getKey()).getAsString();
45
46         if (jsonEventItem.get(JSONApiResponseKeysEnum.PROPERTIES.getKey()).isJsonObject()) {
47             Set<Entry<String, JsonElement>> propObjEntrySet = jsonEventItem
48                     .get(JSONApiResponseKeysEnum.PROPERTIES.getKey()).getAsJsonObject().entrySet();
49             properties = new HashMap<>(propObjEntrySet.size());
50             for (Entry<String, JsonElement> entry : propObjEntrySet) {
51                 if (EventResponseEnum.containsId(entry.getKey())) {
52                     addProperty(EventResponseEnum.getProperty(entry.getKey()), entry.getValue().getAsString());
53                 }
54             }
55         }
56         if (jsonEventItem.get(JSONApiResponseKeysEnum.SOURCE.getKey()).isJsonObject()) {
57             Set<Entry<String, JsonElement>> sourceObjEntrySet = jsonEventItem
58                     .get(JSONApiResponseKeysEnum.SOURCE.getKey()).getAsJsonObject().entrySet();
59             source = new HashMap<>(sourceObjEntrySet.size());
60             for (Entry<String, JsonElement> entry : sourceObjEntrySet) {
61                 if (EventResponseEnum.containsId(entry.getKey())) {
62                     addSource(EventResponseEnum.getProperty(entry.getKey()), entry.getValue().getAsString());
63                 }
64             }
65         }
66     }
67
68     private void addProperty(EventResponseEnum propertieKey, String value) {
69         properties.put(propertieKey, value);
70     }
71
72     private void addSource(EventResponseEnum sourceKey, String value) {
73         source.put(sourceKey, value);
74     }
75
76     @Override
77     public String getName() {
78         return name;
79     }
80
81     @Override
82     public Map<EventResponseEnum, String> getProperties() {
83         return properties;
84     }
85
86     @Override
87     public Map<EventResponseEnum, String> getSource() {
88         return source;
89     }
90
91     /*
92      * (non-Javadoc)
93      *
94      * @see java.lang.Object#toString()
95      */
96     @Override
97     public String toString() {
98         return "EventItemImpl [name=" + name + ", properties=" + properties + ", source=" + source + "]";
99     }
100 }