2 * Copyright (c) 2010-2022 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.boschshc.internal.devices.shuttercontrol;
15 import static org.openhab.binding.boschshc.internal.devices.BoschSHCBindingConstants.CHANNEL_LEVEL;
17 import java.util.List;
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;
33 * Control of your shutter to take any position you desire.
35 * @author Christian Oeing - Initial contribution
38 public class ShutterControlHandler extends BoschSHCDeviceHandler {
40 * Utility functions to convert data between Bosch things and openHAB items
42 static final class DataConversion {
43 public static int levelToOpenPercentage(double level) {
44 return (int) Math.round((1 - level) * 100);
47 public static double openPercentageToLevel(double openPercentage) {
48 return (100 - openPercentage) / 100.0;
52 private ShutterControlService shutterControlService;
54 public ShutterControlHandler(Thing thing) {
56 this.shutterControlService = new ShutterControlService();
60 protected void initializeServices() throws BoschSHCException {
61 super.initializeServices();
63 this.registerService(this.shutterControlService, this::updateChannels, List.of(CHANNEL_LEVEL));
67 public void handleCommand(ChannelUID channelUID, Command command) {
68 super.handleCommand(channelUID, command);
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) {
76 } else if (upDownType == UpDownType.DOWN) {
79 logger.warn("Received unknown UpDownType command: {}", upDownType);
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);
91 } else if (command instanceof PercentType) {
93 PercentType percentType = (PercentType) command;
94 double level = DataConversion.openPercentageToLevel(percentType.doubleValue());
95 this.updateServiceState(this.shutterControlService, new ShutterControlServiceState(level));
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));