]> git.basschouten.com Git - openhab-addons.git/blob
a1875aecc6a7991892558149a5644ab6e6cb9343
[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) {
75             // Set full close/open as target state
76             UpDownType upDownType = (UpDownType) command;
77             ShutterControlServiceState state = new ShutterControlServiceState();
78             if (upDownType == UpDownType.UP) {
79                 state.level = 1.0;
80             } else if (upDownType == UpDownType.DOWN) {
81                 state.level = 0.0;
82             } else {
83                 logger.warn("Received unknown UpDownType command: {}", upDownType);
84                 return;
85             }
86             this.updateServiceState(this.shutterControlService, state);
87         } else if (command instanceof StopMoveType) {
88             StopMoveType stopMoveType = (StopMoveType) command;
89             if (stopMoveType == StopMoveType.STOP) {
90                 // Set STOPPED operation state
91                 ShutterControlServiceState state = new ShutterControlServiceState();
92                 state.operationState = OperationState.STOPPED;
93                 this.updateServiceState(this.shutterControlService, state);
94             }
95         } else if (command instanceof PercentType) {
96             // Set specific level
97             PercentType percentType = (PercentType) command;
98             double level = DataConversion.openPercentageToLevel(percentType.doubleValue());
99             this.updateServiceState(this.shutterControlService, new ShutterControlServiceState(level));
100         }
101     }
102
103     private void updateChannels(ShutterControlServiceState state) {
104         if (state.level != null) {
105             // Convert level to open ratio
106             int openPercentage = DataConversion.levelToOpenPercentage(state.level);
107             updateState(CHANNEL_LEVEL, new PercentType(openPercentage));
108         }
109     }
110 }