]> git.basschouten.com Git - openhab-addons.git/blob
f852200d172f3074413c704f61aa693accef6a79
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2022 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.boschshc.internal.devices.shuttercontrol;
14
15 import static org.openhab.binding.boschshc.internal.devices.BoschSHCBindingConstants.CHANNEL_LEVEL;
16
17 import java.util.List;
18
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.openhab.binding.boschshc.internal.devices.BoschSHCDeviceHandler;
21 import org.openhab.binding.boschshc.internal.exceptions.BoschSHCException;
22 import org.openhab.binding.boschshc.internal.services.shuttercontrol.OperationState;
23 import org.openhab.binding.boschshc.internal.services.shuttercontrol.ShutterControlService;
24 import org.openhab.binding.boschshc.internal.services.shuttercontrol.dto.ShutterControlServiceState;
25 import org.openhab.core.library.types.PercentType;
26 import org.openhab.core.library.types.StopMoveType;
27 import org.openhab.core.library.types.UpDownType;
28 import org.openhab.core.thing.ChannelUID;
29 import org.openhab.core.thing.Thing;
30 import org.openhab.core.types.Command;
31
32 /**
33  * Control of your shutter to take any position you desire.
34  * 
35  * @author Christian Oeing - Initial contribution
36  */
37 @NonNullByDefault
38 public class ShutterControlHandler extends BoschSHCDeviceHandler {
39     /**
40      * Utility functions to convert data between Bosch things and openHAB items
41      */
42     static final class DataConversion {
43         public static int levelToOpenPercentage(double level) {
44             return (int) Math.round((1 - level) * 100);
45         }
46
47         public static double openPercentageToLevel(double openPercentage) {
48             return (100 - openPercentage) / 100.0;
49         }
50     }
51
52     private ShutterControlService shutterControlService;
53
54     public ShutterControlHandler(Thing thing) {
55         super(thing);
56         this.shutterControlService = new ShutterControlService();
57     }
58
59     @Override
60     protected void initializeServices() throws BoschSHCException {
61         super.initializeServices();
62
63         this.registerService(this.shutterControlService, this::updateChannels, List.of(CHANNEL_LEVEL));
64     }
65
66     @Override
67     public void handleCommand(ChannelUID channelUID, Command command) {
68         super.handleCommand(channelUID, command);
69
70         if (command instanceof UpDownType) {
71             // Set full close/open as target state
72             UpDownType upDownType = (UpDownType) command;
73             ShutterControlServiceState state = new ShutterControlServiceState();
74             if (upDownType == UpDownType.UP) {
75                 state.level = 1.0;
76             } else if (upDownType == UpDownType.DOWN) {
77                 state.level = 0.0;
78             } else {
79                 logger.warn("Received unknown UpDownType command: {}", upDownType);
80                 return;
81             }
82             this.updateServiceState(this.shutterControlService, state);
83         } else if (command instanceof StopMoveType) {
84             StopMoveType stopMoveType = (StopMoveType) command;
85             if (stopMoveType == StopMoveType.STOP) {
86                 // Set STOPPED operation state
87                 ShutterControlServiceState state = new ShutterControlServiceState();
88                 state.operationState = OperationState.STOPPED;
89                 this.updateServiceState(this.shutterControlService, state);
90             }
91         } else if (command instanceof PercentType) {
92             // Set specific level
93             PercentType percentType = (PercentType) command;
94             double level = DataConversion.openPercentageToLevel(percentType.doubleValue());
95             this.updateServiceState(this.shutterControlService, new ShutterControlServiceState(level));
96         }
97     }
98
99     private void updateChannels(ShutterControlServiceState state) {
100         if (state.level != null) {
101             // Convert level to open ratio
102             int openPercentage = DataConversion.levelToOpenPercentage(state.level);
103             updateState(CHANNEL_LEVEL, new PercentType(openPercentage));
104         }
105     }
106 }