]> git.basschouten.com Git - openhab-addons.git/blob
38529c6d46e1433fabc6c2e4765f8d19f877c8cf
[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 org.eclipse.jdt.annotation.NonNullByDefault;
16 import org.eclipse.jdt.annotation.Nullable;
17 import org.slf4j.Logger;
18 import org.slf4j.LoggerFactory;
19
20 /**
21  * The {@link DataPointScaling} is the data points for scaling values
22  *
23  * @author Hans-Reiner Hoffmann - Initial contribution
24  */
25 @NonNullByDefault
26 public class DataPointScaling extends DataPointBase<@Nullable Double> {
27     private final Logger logger = LoggerFactory.getLogger(DataPointScaling.class);
28     private String outputFormat = "";
29
30     public DataPointScaling(int id, String knxDataType, String description) {
31         super(id, knxDataType, description);
32         this.setUnit("%");
33         this.outputFormat = "%.1f";
34     }
35
36     @Override
37     public String getValueText() {
38         return String.format(this.outputFormat, this.getValue());
39     }
40
41     @Override
42     public void processData(byte[] data) {
43         if (this.checkProcessData(data)) {
44             if (data[3] != 1 && data.length <= 4) {
45                 logger.debug("DataPoint-ProcessData: Data size wrong for this type({}/1).", data[3]);
46                 return;
47             }
48
49             this.setValue((Byte.toUnsignedInt(data[4]) * 100.0) / 255.0);
50         }
51     }
52
53     @Override
54     protected byte[] convertWriteValue(Object value) {
55         this.setValue(Math.max(Math.min(Double.parseDouble(value.toString()), 100), 0));
56         Object rawVal = this.getValue();
57         double rawValResult = rawVal != null ? (Double) rawVal : 0.0;
58         byte val = (byte) (rawValResult / 100.0 * 255.0);
59         return new byte[] { val };
60     }
61 }