]> git.basschouten.com Git - openhab-addons.git/blob
b8717015a6b31daa01836af0ce88704a2c0fad57
[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.homeconnect.internal.client.model;
14
15 import static org.openhab.binding.homeconnect.internal.client.model.EventType.EVENT;
16 import static org.openhab.binding.homeconnect.internal.client.model.EventType.NOTIFY;
17 import static org.openhab.binding.homeconnect.internal.client.model.EventType.STATUS;
18
19 import java.time.ZonedDateTime;
20
21 import org.eclipse.jdt.annotation.NonNullByDefault;
22 import org.eclipse.jdt.annotation.Nullable;
23
24 /**
25  * Event model
26  *
27  * @author Jonas BrĂ¼stel - Initial contribution
28  *
29  */
30 @NonNullByDefault
31 public class Event {
32
33     private final String haId;
34     // event type
35     private final EventType type;
36     // event key
37     private @Nullable final String key;
38     // user-friendly name of the feature key
39     private @Nullable final String name;
40     // URI of the resource that changed
41     private @Nullable final String uri;
42     // creation time of event
43     private @Nullable final ZonedDateTime creation;
44     // level of the event
45     private @Nullable final EventLevel level;
46     // expected activity
47     private @Nullable final EventHandling handling;
48     // new value, e.g. in case of a status update (string, number or boolean)
49     private @Nullable final String value;
50     // unit string
51     private @Nullable final String unit;
52
53     public Event(final String haId, final EventType type) {
54         this.haId = haId;
55         this.type = type;
56         this.key = null;
57         this.name = null;
58         this.uri = null;
59         this.creation = ZonedDateTime.now();
60         this.level = null;
61         this.handling = null;
62         this.value = null;
63         this.unit = null;
64     }
65
66     public Event(final String haId, final EventType type, @Nullable final String key, @Nullable final String name,
67             @Nullable final String uri, @Nullable final ZonedDateTime creation, @Nullable final EventLevel level,
68             @Nullable final EventHandling handling, @Nullable final String value, @Nullable final String unit) {
69         this.haId = haId;
70         this.type = type;
71         this.key = key;
72         this.name = name;
73         this.uri = uri;
74         this.creation = creation;
75         this.level = level;
76         this.handling = handling;
77         this.value = value;
78         this.unit = unit;
79     }
80
81     public String getHaId() {
82         return haId;
83     }
84
85     public EventType getType() {
86         return type;
87     }
88
89     public @Nullable String getKey() {
90         return key;
91     }
92
93     public @Nullable String getName() {
94         return name;
95     }
96
97     public @Nullable String getUri() {
98         return uri;
99     }
100
101     public @Nullable ZonedDateTime getCreation() {
102         return creation;
103     }
104
105     public @Nullable EventLevel getLevel() {
106         return level;
107     }
108
109     public @Nullable EventHandling getHandling() {
110         return handling;
111     }
112
113     public @Nullable String getValue() {
114         return value;
115     }
116
117     public boolean getValueAsBoolean() {
118         return Boolean.parseBoolean(value);
119     }
120
121     public int getValueAsInt() {
122         String stringValue = value;
123         return stringValue != null ? Float.valueOf(stringValue).intValue() : 0;
124     }
125
126     public @Nullable String getUnit() {
127         return unit;
128     }
129
130     @Override
131     public String toString() {
132         if (STATUS.equals(type) || EVENT.equals(type) || NOTIFY.equals(type)) {
133             return "Event{" + "haId='" + haId + '\'' + ", type=" + type + ", key='" + key + '\'' + ", name='" + name
134                     + '\'' + ", uri='" + uri + '\'' + ", creation=" + creation + ", level=" + level + ", handling="
135                     + handling + ", value='" + value + '\'' + ", unit='" + unit + '\'' + '}';
136         } else {
137             return "Event{" + "haId='" + haId + '\'' + ", type=" + type + '}';
138         }
139     }
140 }