]> git.basschouten.com Git - openhab-addons.git/blob
078431ac3aab929cb03d988ff66dbc89cb5b6f76
[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.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 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33
34 /**
35  * Control of your shutter to take any position you desire.
36  *
37  * @author Christian Oeing - Initial contribution
38  */
39 @NonNullByDefault
40 public class ShutterControlHandler extends BoschSHCDeviceHandler {
41     /**
42      * Utility functions to convert data between Bosch things and openHAB items
43      */
44     static final class DataConversion {
45         public static int levelToOpenPercentage(double level) {
46             return (int) Math.round((1 - level) * 100);
47         }
48
49         public static double openPercentageToLevel(double openPercentage) {
50             return (100 - openPercentage) / 100.0;
51         }
52     }
53
54     private final Logger logger = LoggerFactory.getLogger(getClass());
55
56     private ShutterControlService shutterControlService;
57
58     public ShutterControlHandler(Thing thing) {
59         super(thing);
60         this.shutterControlService = new ShutterControlService();
61     }
62
63     @Override
64     protected void initializeServices() throws BoschSHCException {
65         super.initializeServices();
66
67         this.registerService(this.shutterControlService, this::updateChannels, List.of(CHANNEL_LEVEL));
68     }
69
70     @Override
71     public void handleCommand(ChannelUID channelUID, Command command) {
72         super.handleCommand(channelUID, command);
73
74         if (command instanceof UpDownType upDownCommand) {
75             // Set full close/open as target state
76             ShutterControlServiceState state = new ShutterControlServiceState();
77             if (upDownCommand == UpDownType.UP) {
78                 state.level = 1.0;
79             } else if (upDownCommand == UpDownType.DOWN) {
80                 state.level = 0.0;
81             } else {
82                 logger.warn("Received unknown UpDownType command: {}", upDownCommand);
83                 return;
84             }
85             this.updateServiceState(this.shutterControlService, state);
86         } else if (command instanceof StopMoveType stopMoveCommand) {
87             if (stopMoveCommand == StopMoveType.STOP) {
88                 // Set STOPPED operation state
89                 ShutterControlServiceState state = new ShutterControlServiceState();
90                 state.operationState = OperationState.STOPPED;
91                 this.updateServiceState(this.shutterControlService, state);
92             }
93         } else if (command instanceof PercentType percentCommand) {
94             // Set specific level
95             double level = DataConversion.openPercentageToLevel(percentCommand.doubleValue());
96             this.updateServiceState(this.shutterControlService, new ShutterControlServiceState(level));
97         }
98     }
99
100     private void updateChannels(ShutterControlServiceState state) {
101         if (state.level != null) {
102             // Convert level to open ratio
103             int openPercentage = DataConversion.levelToOpenPercentage(state.level);
104             updateState(CHANNEL_LEVEL, new PercentType(openPercentage));
105         }
106     }
107 }