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