]> git.basschouten.com Git - openhab-addons.git/blob
21151283318b88eab02dabc9c2e2a985efa5040a
[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.structure.devices.deviceparameters.DeviceStateUpdate;
16 import org.openhab.binding.digitalstrom.internal.lib.structure.devices.deviceparameters.constants.DeviceBinarayInputEnum;
17 import org.openhab.binding.digitalstrom.internal.lib.structure.devices.deviceparameters.constants.SensorEnum;
18
19 /**
20  * The {@link DeviceStateUpdateImpl} is the implementation of the {@link DeviceStateUpdate}.
21  *
22  * @author Michael Ochel - Initial contribution
23  * @author Matthias Siegele - Initial contribution
24  *
25  */
26 public class DeviceStateUpdateImpl implements DeviceStateUpdate {
27
28     private final String updateType;
29     private final Object value;
30
31     /**
32      * Creates a new {@link DeviceStateUpdateImpl} with the given updateType and a value as {@link Object}.
33      *
34      * @param updateType must not be null
35      * @param value must not be null
36      */
37     public DeviceStateUpdateImpl(String updateType, Object value) {
38         this.updateType = updateType;
39         this.value = value;
40     }
41
42     /**
43      * Creates a new {@link DeviceStateUpdateImpl} through the given {@link DeviceBinaryInput} and value as
44      * {@link Short}. The updateType as {@link String} will be automatically create.
45      *
46      * @param updateDeviceBinary must not be null
47      * @param value must not be null
48      */
49     public DeviceStateUpdateImpl(DeviceBinarayInputEnum updateDeviceBinary, Object value) {
50         this.updateType = DeviceStateUpdate.BINARY_INPUT + updateDeviceBinary.getBinaryInputType();
51         this.value = value;
52     }
53
54     /**
55      * Creates a new {@link DeviceStateUpdateImpl} through the given {@link SensorEnum} and value as
56      * {@link Integer}. The updateType as {@link String} will be automatically create.
57      *
58      * @param updateSensorType must not be null
59      * @param value must not be null
60      */
61     public DeviceStateUpdateImpl(SensorEnum updateSensorType, Object value) {
62         this.updateType = DeviceStateUpdate.UPDATE_DEVICE_SENSOR + updateSensorType.getSensorType();
63         this.value = value;
64     }
65
66     @Override
67     public Object getValue() {
68         return value;
69     }
70
71     @Override
72     public String getType() {
73         return updateType;
74     }
75
76     @Override
77     public Integer getValueAsInteger() {
78         try {
79             if (value instanceof Integer) {
80                 return (Integer) value;
81             }
82             if (value instanceof Float) {
83                 return ((Float) value).intValue();
84             }
85             if (value instanceof Short) {
86                 return ((Short) value).intValue();
87             }
88             if (value instanceof String string) {
89                 return Integer.parseInt(string);
90             }
91         } catch (Exception e) {
92             throw new ClassCastException();
93         }
94         throw new ClassCastException();
95     }
96
97     @Override
98     public String getValueAsString() {
99         if (value instanceof Integer) {
100             return ((Integer) value).toString();
101         }
102         if (value instanceof Float) {
103             return ((Float) value).toString();
104         }
105         if (value instanceof Short) {
106             return ((Short) value).toString();
107         }
108         if (value instanceof String string) {
109             return string;
110         }
111         throw new ClassCastException();
112     }
113
114     @Override
115     public Short[] getValueAsShortArray() {
116         return (Short[]) value;
117     }
118
119     @Override
120     public Short getValueAsShort() {
121         try {
122             if (value instanceof Integer) {
123                 return ((Integer) value).shortValue();
124             }
125             if (value instanceof Float) {
126                 return ((Float) value).shortValue();
127             }
128             if (value instanceof Short) {
129                 return (Short) value;
130             }
131             if (value instanceof String string) {
132                 return Short.parseShort(string);
133             }
134         } catch (Exception e) {
135             throw new ClassCastException();
136         }
137         throw new ClassCastException();
138     }
139
140     @Override
141     public Float getValueAsFloat() {
142         try {
143             if (value instanceof Integer) {
144                 return ((Integer) value).floatValue();
145             }
146             if (value instanceof Float) {
147                 return (Float) value;
148             }
149             if (value instanceof Short) {
150                 return ((Short) value).floatValue();
151             }
152             if (value instanceof String string) {
153                 return Float.parseFloat(string);
154             }
155         } catch (Exception e) {
156             throw new ClassCastException();
157         }
158         throw new ClassCastException();
159     }
160
161     @Override
162     public SensorEnum getTypeAsSensorEnum() {
163         return SensorEnum.getSensor(Short.parseShort(updateType.split("-")[1]));
164     }
165
166     @Override
167     public boolean isSensorUpdateType() {
168         return updateType.startsWith(UPDATE_DEVICE_SENSOR);
169     }
170
171     @Override
172     public DeviceBinarayInputEnum getTypeAsDeviceBinarayInputEnum() {
173         return DeviceBinarayInputEnum.getdeviceBinarayInput(Short.parseShort(updateType.split("-")[1]));
174     }
175
176     @Override
177     public boolean isBinarayInputType() {
178         return updateType.startsWith(BINARY_INPUT);
179     }
180
181     @Override
182     public Short getSceneId() {
183         if (isSceneUpdateType()) {
184             return ((Short[]) value)[0];
185         }
186         return -1;
187     }
188
189     @Override
190     public Short getScenePriority() {
191         if (isSceneUpdateType()) {
192             return ((Short[]) value)[1];
193         }
194         return -1;
195     }
196
197     @Override
198     public boolean isSceneUpdateType() {
199         return updateType.equals(UPDATE_SCENE_CONFIG) || updateType.equals(UPDATE_SCENE_OUTPUT);
200     }
201 }