]> git.basschouten.com Git - openhab-addons.git/blob
ed097402f1a9dccda2e72212d37a08d1b76e3a7e
[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.comfoair.internal.datatypes;
14
15 import org.eclipse.jdt.annotation.NonNullByDefault;
16 import org.eclipse.jdt.annotation.Nullable;
17 import org.openhab.binding.comfoair.internal.ComfoAirCommandType;
18 import org.openhab.core.library.types.DecimalType;
19 import org.openhab.core.library.types.QuantityType;
20 import org.openhab.core.library.unit.Units;
21 import org.openhab.core.types.State;
22 import org.openhab.core.types.UnDefType;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25
26 /**
27  * Class to handle revolutions per minute values
28  *
29  * @author Grzegorz Miasko - Initial Contribution
30  * @author Hans Böhm - Refactoring
31  */
32 @NonNullByDefault
33 public class DataTypeRPM implements ComfoAirDataType {
34     private static final DataTypeRPM SINGLETON_INSTANCE = new DataTypeRPM();
35
36     private DataTypeRPM() {
37     }
38
39     private final Logger logger = LoggerFactory.getLogger(DataTypeRPM.class);
40
41     public static DataTypeRPM getInstance() {
42         return SINGLETON_INSTANCE;
43     }
44
45     @Override
46     public State convertToState(int @Nullable [] data, ComfoAirCommandType commandType) {
47         if (data == null) {
48             logger.trace("\"DataTypeRPM\" class \"convertToState\" method parameter: null");
49             return UnDefType.NULL;
50         } else {
51             int value = calculateNumberValue(data, commandType);
52
53             if (value < 0) {
54                 return UnDefType.NULL;
55             }
56             // transferred value is (1875000 / rpm) per protocol
57             return new QuantityType<>((int) (1875000.0 / value), Units.RPM);
58         }
59     }
60
61     @Override
62     public int @Nullable [] convertFromState(State value, ComfoAirCommandType commandType) {
63         int[] template = commandType.getChangeDataTemplate();
64         float rpm;
65
66         if (value instanceof QuantityType<?> qt) {
67             QuantityType<?> qtRpm = ((QuantityType<?>) value).toUnit(Units.RPM);
68
69             if (qtRpm != null) {
70                 rpm = qtRpm.floatValue();
71             } else {
72                 return null;
73             }
74         } else if (value instanceof DecimalType dt) {
75             rpm = dt.floatValue();
76         } else {
77             logger.trace("\"DataTypeRPM\" class \"convertFromState\" undefined state");
78             return null;
79         }
80
81         // transferred value is (1875000 / rpm) per protocol
82         template[commandType.getChangeDataPos()] = (int) (1875000 / rpm);
83         return template;
84     }
85 }