]> git.basschouten.com Git - openhab-addons.git/blob
da11dd8328bdbb4efb744958d56b625e3c02af90
[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.openthermgateway.internal;
14
15 import javax.measure.Unit;
16
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.eclipse.jdt.annotation.Nullable;
19 import org.openhab.core.library.types.DecimalType;
20 import org.openhab.core.library.types.QuantityType;
21 import org.openhab.core.types.State;
22
23 /**
24  * The {@link FloatDataItem} represents a 2-byte float value in a 2's complement format.
25  *
26  * @author Arjen Korevaar - Initial contribution
27  */
28 @NonNullByDefault
29 public class FloatDataItem extends DataItem {
30
31     private @Nullable Unit<?> unit;
32
33     public @Nullable Unit<?> getUnit() {
34         return unit;
35     }
36
37     public FloatDataItem(Msg msg, String subject) {
38         this(msg, subject, null, null);
39     }
40
41     public FloatDataItem(Msg msg, String subject, Unit<?> unit) {
42         this(msg, subject, unit, null);
43     }
44
45     public FloatDataItem(Msg msg, String subject, CodeType codetype) {
46         this(msg, subject, null, codetype);
47     }
48
49     public FloatDataItem(Msg msg, String subject, @Nullable Unit<?> unit, @Nullable CodeType codetype) {
50         super(msg, ByteType.BOTH, subject, codetype);
51
52         this.unit = unit;
53     }
54
55     @Override
56     public State createState(Message message) {
57         @Nullable
58         Unit<?> unit = this.getUnit();
59         float value = message.getFloat();
60         return (unit == null) ? new DecimalType(value) : new QuantityType<>(value, unit);
61     }
62 }