2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.digitalstrom.internal.lib.structure.devices.deviceparameters.impl;
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;
20 * The {@link DeviceStateUpdateImpl} is the implementation of the {@link DeviceStateUpdate}.
22 * @author Michael Ochel - Initial contribution
23 * @author Matthias Siegele - Initial contribution
26 public class DeviceStateUpdateImpl implements DeviceStateUpdate {
28 private final String updateType;
29 private final Object value;
32 * Creates a new {@link DeviceStateUpdateImpl} with the given updateType and a value as {@link Object}.
34 * @param updateType must not be null
35 * @param value must not be null
37 public DeviceStateUpdateImpl(String updateType, Object value) {
38 this.updateType = updateType;
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.
46 * @param updateDeviceBinary must not be null
47 * @param value must not be null
49 public DeviceStateUpdateImpl(DeviceBinarayInputEnum updateDeviceBinary, Object value) {
50 this.updateType = DeviceStateUpdate.BINARY_INPUT + updateDeviceBinary.getBinaryInputType();
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.
58 * @param updateSensorType must not be null
59 * @param value must not be null
61 public DeviceStateUpdateImpl(SensorEnum updateSensorType, Object value) {
62 this.updateType = DeviceStateUpdate.UPDATE_DEVICE_SENSOR + updateSensorType.getSensorType();
67 public Object getValue() {
72 public String getType() {
77 public Integer getValueAsInteger() {
79 if (value instanceof Integer) {
80 return (Integer) value;
82 if (value instanceof Float) {
83 return ((Float) value).intValue();
85 if (value instanceof Short) {
86 return ((Short) value).intValue();
88 if (value instanceof String) {
89 return Integer.parseInt((String) value);
91 } catch (Exception e) {
92 throw new ClassCastException();
94 throw new ClassCastException();
98 public String getValueAsString() {
99 if (value instanceof Integer) {
100 return ((Integer) value).toString();
102 if (value instanceof Float) {
103 return ((Float) value).toString();
105 if (value instanceof Short) {
106 return ((Short) value).toString();
108 if (value instanceof String) {
109 return (String) value;
111 throw new ClassCastException();
115 public Short[] getValueAsShortArray() {
116 return (Short[]) value;
120 public Short getValueAsShort() {
122 if (value instanceof Integer) {
123 return ((Integer) value).shortValue();
125 if (value instanceof Float) {
126 return ((Float) value).shortValue();
128 if (value instanceof Short) {
129 return (Short) value;
131 if (value instanceof String) {
132 return Short.parseShort((String) value);
134 } catch (Exception e) {
135 throw new ClassCastException();
137 throw new ClassCastException();
141 public Float getValueAsFloat() {
143 if (value instanceof Integer) {
144 return ((Integer) value).floatValue();
146 if (value instanceof Float) {
147 return (Float) value;
149 if (value instanceof Short) {
150 return ((Short) value).floatValue();
152 if (value instanceof String) {
153 return Float.parseFloat((String) value);
155 } catch (Exception e) {
156 throw new ClassCastException();
158 throw new ClassCastException();
162 public SensorEnum getTypeAsSensorEnum() {
163 return SensorEnum.getSensor(Short.parseShort(updateType.split("-")[1]));
167 public boolean isSensorUpdateType() {
168 return updateType.startsWith(UPDATE_DEVICE_SENSOR);
172 public DeviceBinarayInputEnum getTypeAsDeviceBinarayInputEnum() {
173 return DeviceBinarayInputEnum.getdeviceBinarayInput(Short.parseShort(updateType.split("-")[1]));
177 public boolean isBinarayInputType() {
178 return updateType.startsWith(BINARY_INPUT);
182 public Short getSceneId() {
183 if (isSceneUpdateType()) {
184 return ((Short[]) value)[0];
190 public Short getScenePriority() {
191 if (isSceneUpdateType()) {
192 return ((Short[]) value)[1];
198 public boolean isSceneUpdateType() {
199 return updateType.equals(UPDATE_SCENE_CONFIG) || updateType.equals(UPDATE_SCENE_OUTPUT);