]> git.basschouten.com Git - openhab-addons.git/blob
b6ab3369c089045e015684814e414486e4ddad13
[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.structure.devices.deviceparameters.impl;
14
15 import org.openhab.binding.digitalstrom.internal.lib.serverconnection.constants.JSONApiResponseKeysEnum;
16
17 import com.google.gson.JsonObject;
18
19 /**
20  * The {@link DeviceBinaryInput} contains all information of a device binary input, e.g. binary input type id (see
21  * {@link DeviceBinarayInputEnum}, state and so on.
22  *
23  * @author Michael Ochel - initial contributer
24  * @author Matthias Siegele - initial contributer
25  *
26  */
27 public class DeviceBinaryInput {
28
29     private final Short targetGroupType;
30     private final Short targetGroup;
31     private final Short inputType;
32     private final Short inputId;
33     private Short stateValue;
34
35     /**
36      * Creates a new {@link DeviceBinarayInputEnum} through the {@link JsonObject} of the binary inputs at json response
37      * from digitalSTROM JSON-API or property-tree. Will be automatically added to a {@link DeviceImpl}, if binary
38      * inputs exists.
39      *
40      * @param jsonObject must not be null
41      */
42     public DeviceBinaryInput(JsonObject jsonObject) {
43         if (jsonObject.get(JSONApiResponseKeysEnum.TARGET_GROUP_TYPE.getKey()) != null) {
44             targetGroupType = jsonObject.get(JSONApiResponseKeysEnum.TARGET_GROUP_TYPE.getKey()).getAsShort();
45         } else {
46             targetGroupType = null;
47         }
48         if (jsonObject.get(JSONApiResponseKeysEnum.TARGET_GROUP.getKey()) != null) {
49             targetGroup = jsonObject.get(JSONApiResponseKeysEnum.TARGET_GROUP.getKey()).getAsShort();
50         } else {
51             targetGroup = null;
52         }
53         if (jsonObject.get(JSONApiResponseKeysEnum.INPUT_TYPE.getKey()) != null) {
54             inputType = jsonObject.get(JSONApiResponseKeysEnum.INPUT_TYPE.getKey()).getAsShort();
55         } else {
56             inputType = null;
57         }
58         if (jsonObject.get(JSONApiResponseKeysEnum.INPUT_ID.getKey()) != null) {
59             inputId = jsonObject.get(JSONApiResponseKeysEnum.INPUT_ID.getKey()).getAsShort();
60         } else {
61             inputId = null;
62         }
63         if (jsonObject.get(JSONApiResponseKeysEnum.STATE_VALUE.getKey()) != null) {
64             stateValue = jsonObject.get(JSONApiResponseKeysEnum.STATE_VALUE.getKey()).getAsShort();
65         }
66         if (stateValue == null && jsonObject.get(JSONApiResponseKeysEnum.STATE.getKey()) != null) {
67             stateValue = jsonObject.get(JSONApiResponseKeysEnum.STATE.getKey()).getAsShort();
68         } else {
69             stateValue = null;
70         }
71     }
72
73     /**
74      * Returns the current state of this {@link DeviceBinaryInput}.
75      *
76      * @return the state
77      */
78     public Short getState() {
79         return stateValue;
80     }
81
82     /**
83      * Sets the state of this {@link DeviceBinaryInput}.
84      *
85      * @param state the state to set
86      */
87     public void setState(Short state) {
88         this.stateValue = state;
89     }
90
91     /**
92      * Returns the target group type id of this {@link DeviceBinaryInput}.
93      *
94      * @return the targetGroupType
95      */
96     public Short getTargetGroupType() {
97         return targetGroupType;
98     }
99
100     /**
101      * Returns the target group id of this {@link DeviceBinaryInput}.
102      *
103      * @return the targetGroup
104      */
105     public Short getTargetGroup() {
106         return targetGroup;
107     }
108
109     /**
110      * Returns the input type id of this {@link DeviceBinaryInput}. Available input types see
111      * {@link DeviceBinarayInputEnum}.
112      *
113      * @return the inputType
114      */
115     public Short getInputType() {
116         return inputType;
117     }
118
119     /**
120      * Returns the input id of this {@link DeviceBinaryInput}.
121      *
122      * @return the inputId
123      */
124     public Short getInputId() {
125         return inputId;
126     }
127
128     /*
129      * (non-Javadoc)
130      *
131      * @see java.lang.Object#hashCode()
132      */
133     @Override
134     public int hashCode() {
135         final int prime = 31;
136         int result = 1;
137         result = prime * result + ((inputType == null) ? 0 : inputType.hashCode());
138         return result;
139     }
140
141     /*
142      * (non-Javadoc)
143      *
144      * @see java.lang.Object#equals(java.lang.Object)
145      */
146     @Override
147     public boolean equals(Object obj) {
148         if (this == obj) {
149             return true;
150         }
151         if (obj == null) {
152             return false;
153         }
154         if (!(obj instanceof DeviceBinaryInput)) {
155             return false;
156         }
157         DeviceBinaryInput other = (DeviceBinaryInput) obj;
158         if (inputType == null) {
159             if (other.inputType != null) {
160                 return false;
161             }
162         } else if (!inputType.equals(other.inputType)) {
163             return false;
164         }
165         return true;
166     }
167
168     /*
169      * (non-Javadoc)
170      *
171      * @see java.lang.Object#toString()
172      */
173     @Override
174     public String toString() {
175         return "DeviceBinaryInput [targetGroupType=" + targetGroupType + ", targetGroup=" + targetGroup + ", inputType="
176                 + inputType + ", inputId=" + inputId + ", state=" + stateValue + "]";
177     }
178 }