2 * Copyright (c) 2010-2023 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;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
35 * Control of your shutter to take any position you desire.
37 * @author Christian Oeing - Initial contribution
40 public class ShutterControlHandler extends BoschSHCDeviceHandler {
42 * Utility functions to convert data between Bosch things and openHAB items
44 static final class DataConversion {
45 public static int levelToOpenPercentage(double level) {
46 return (int) Math.round((1 - level) * 100);
49 public static double openPercentageToLevel(double openPercentage) {
50 return (100 - openPercentage) / 100.0;
54 private final Logger logger = LoggerFactory.getLogger(getClass());
56 private ShutterControlService shutterControlService;
58 public ShutterControlHandler(Thing thing) {
60 this.shutterControlService = new ShutterControlService();
64 protected void initializeServices() throws BoschSHCException {
65 super.initializeServices();
67 this.registerService(this.shutterControlService, this::updateChannels, List.of(CHANNEL_LEVEL));
71 public void handleCommand(ChannelUID channelUID, Command command) {
72 super.handleCommand(channelUID, command);
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) {
80 } else if (upDownType == UpDownType.DOWN) {
83 logger.warn("Received unknown UpDownType command: {}", upDownType);
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);
95 } else if (command instanceof PercentType) {
97 PercentType percentType = (PercentType) command;
98 double level = DataConversion.openPercentageToLevel(percentType.doubleValue());
99 this.updateServiceState(this.shutterControlService, new ShutterControlServiceState(level));
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));