]> git.basschouten.com Git - openhab-addons.git/blob
3623479c937efbba161a62a681116e1d40e532a2
[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.rfxcom.internal.messages;
14
15 import static org.openhab.binding.rfxcom.internal.RFXComBindingConstants.CHANNEL_BATTERY_LEVEL;
16
17 import org.openhab.binding.rfxcom.internal.config.RFXComDeviceConfiguration;
18 import org.openhab.binding.rfxcom.internal.exceptions.RFXComInvalidStateException;
19 import org.openhab.binding.rfxcom.internal.exceptions.RFXComUnsupportedChannelException;
20 import org.openhab.binding.rfxcom.internal.handler.DeviceState;
21 import org.openhab.core.library.types.DecimalType;
22 import org.openhab.core.types.State;
23
24 /**
25  * A base class for all battery device messages
26  *
27  * @author Martin van Wingerden - Initial contribution
28  */
29 abstract class RFXComBatteryDeviceMessage<T> extends RFXComDeviceMessageImpl<T> {
30     int batteryLevel;
31
32     RFXComBatteryDeviceMessage(PacketType packetType) {
33         super(packetType);
34     }
35
36     RFXComBatteryDeviceMessage() {
37         // deliberately empty
38     }
39
40     @Override
41     public State convertToState(String channelId, RFXComDeviceConfiguration config, DeviceState deviceState)
42             throws RFXComUnsupportedChannelException, RFXComInvalidStateException {
43         switch (channelId) {
44             case CHANNEL_BATTERY_LEVEL:
45                 return convertBatteryLevelToSystemWideLevel(batteryLevel);
46
47             default:
48                 return super.convertToState(channelId, config, deviceState);
49         }
50     }
51
52     /**
53      * Convert internal battery level (0-9) to system wide battery level (0-100%).
54      *
55      * @param batteryLevel Internal battery level
56      * @return Battery level in system wide level
57      */
58     private State convertBatteryLevelToSystemWideLevel(int batteryLevel) {
59         int ohLevel = (batteryLevel + 1) * 10;
60         return new DecimalType(ohLevel);
61     }
62 }