]> git.basschouten.com Git - openhab-addons.git/blob
fc7e8258b575bb2e8e91e89283997484a59fbd77
[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 UIntDataItem} represents an 8 or 16 bit unsigned integer.
25  *
26  * @author Arjen Korevaar - Initial contribution
27  */
28 @NonNullByDefault
29 public class UIntDataItem extends DataItem {
30
31     private @Nullable Unit<?> unit;
32
33     public @Nullable Unit<?> getUnit() {
34         return unit;
35     }
36
37     public UIntDataItem(Msg msg, ByteType byteType, String subject) {
38         this(msg, byteType, subject, null);
39     }
40
41     public UIntDataItem(Msg msg, ByteType byteType, String subject, @Nullable Unit<?> unit) {
42         super(msg, byteType, subject, null);
43
44         this.unit = unit;
45     }
46
47     @Override
48     public State createState(Message message) {
49         @Nullable
50         Unit<?> unit = getUnit();
51         int value = message.getUInt(super.getByteType());
52         return (unit == null) ? new DecimalType(value) : new QuantityType<>(value, unit);
53     }
54 }