]> git.basschouten.com Git - openhab-addons.git/blob
04107b642631960daa4eccdbea59753cabee81eb
[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.ism8.server;
14
15 import java.nio.ByteBuffer;
16
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.eclipse.jdt.annotation.Nullable;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
21
22 /**
23  * The {@link DataPointLongValue} is the data points for long values
24  *
25  * @author Hans-Reiner Hoffmann - Initial contribution
26  */
27 @NonNullByDefault
28 public class DataPointLongValue extends DataPointBase<@Nullable Double> {
29     private final Logger logger = LoggerFactory.getLogger(DataPointLongValue.class);
30     private final float factor;
31     private final String outputFormat;
32
33     public DataPointLongValue(int id, String knxDataType, String description) {
34         super(id, knxDataType, description);
35
36         if ("13.002".equals(knxDataType)) {
37             this.setUnit("m³/h");
38             this.factor = 0.0001f;
39             this.outputFormat = "%.1f";
40         } else {
41             this.setUnit("");
42             this.factor = 1.0f;
43             this.outputFormat = "%.1f";
44         }
45     }
46
47     @Override
48     public String getValueText() {
49         return String.format(this.outputFormat, this.getValue());
50     }
51
52     @Override
53     public void processData(byte[] data) {
54         if (this.checkProcessData(data)) {
55             if (data[3] != 4 && data.length <= 7) {
56                 logger.debug("DataPoint-ProcessData: Data size wrong for this type({}/4).", data[3]);
57                 return;
58             }
59
60             int rawValue = Byte.toUnsignedInt(data[4]) * 0x1000000 + Byte.toUnsignedInt(data[5]) * 0x10000
61                     + Byte.toUnsignedInt(data[6]) * 0x100 + Byte.toUnsignedInt(data[7]);
62             this.setValue((double) rawValue * this.factor);
63         }
64     }
65
66     @Override
67     protected byte[] convertWriteValue(Object value) {
68         ByteBuffer data = ByteBuffer.allocate(4);
69         double dblVal;
70         try {
71             dblVal = Double.parseDouble(value.toString());
72         } catch (NumberFormatException e) {
73             dblVal = 0.0;
74         }
75
76         int val = (int) (dblVal / this.factor);
77         data.put((byte) (val & 0xFF));
78         val = (val & 0xFF) / 256;
79         data.put((byte) (val & 0xFF));
80         val = (val & 0xFF) / 256;
81         data.put((byte) (val & 0xFF));
82         val = (val & 0xFF) / 256;
83         data.put((byte) (val & 0xFF));
84         return data.array();
85     }
86 }