]> git.basschouten.com Git - openhab-addons.git/blob
ba26711a0ecf95836a33178439b95060fcf3205e
[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.types.State;
20 import org.openhab.core.types.UnDefType;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
23
24 /**
25  * Class to handle revolutions per minute values
26  *
27  * @author Grzegorz Miasko - Initial Contribution
28  * @author Hans Böhm - Refactoring
29  */
30 @NonNullByDefault
31 public class DataTypeRPM implements ComfoAirDataType {
32     private static final DataTypeRPM SINGLETON_INSTANCE = new DataTypeRPM();
33
34     private DataTypeRPM() {
35     }
36
37     private final Logger logger = LoggerFactory.getLogger(DataTypeRPM.class);
38
39     public static DataTypeRPM getInstance() {
40         return SINGLETON_INSTANCE;
41     }
42
43     @Override
44     public State convertToState(int @Nullable [] data, ComfoAirCommandType commandType) {
45         if (data == null) {
46             logger.trace("\"DataTypeRPM\" class \"convertToState\" method parameter: null");
47             return UnDefType.NULL;
48         } else {
49             int value = calculateNumberValue(data, commandType);
50
51             if (value < 0) {
52                 return UnDefType.NULL;
53             }
54             // transferred value is (1875000 / rpm) per protocol
55             return new DecimalType((int) (1875000.0 / value));
56         }
57     }
58
59     @Override
60     public int @Nullable [] convertFromState(State value, ComfoAirCommandType commandType) {
61         if (value instanceof UnDefType) {
62             logger.trace("\"DataTypeRPM\" class \"convertFromState\" undefined state");
63             return null;
64         } else {
65             int[] template = commandType.getChangeDataTemplate();
66             // transferred value is (1875000 / rpm) per protocol
67             template[commandType.getChangeDataPos()] = (int) (1875000 / ((DecimalType) value).doubleValue());
68             return template;
69         }
70     }
71 }