2 * Copyright (c) 2010-2024 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.freeboxos.internal.handler;
15 import static org.openhab.binding.freeboxos.internal.FreeboxOsBindingConstants.*;
16 import static org.openhab.core.library.unit.Units.PERCENT;
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.openhab.binding.freeboxos.internal.api.FreeboxException;
22 import org.openhab.binding.freeboxos.internal.api.rest.LcdManager;
23 import org.openhab.binding.freeboxos.internal.api.rest.LcdManager.Config;
24 import org.openhab.core.library.types.DecimalType;
25 import org.openhab.core.library.types.IncreaseDecreaseType;
26 import org.openhab.core.library.types.OnOffType;
27 import org.openhab.core.library.types.PercentType;
28 import org.openhab.core.library.types.QuantityType;
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 * The {@link RevolutionHandler} is responsible for handling take care of revolution server specifics
37 * @author Gaƫl L'hopital - Initial contribution
40 public class RevolutionHandler extends ServerHandler {
41 private final Logger logger = LoggerFactory.getLogger(RevolutionHandler.class);
43 public RevolutionHandler(Thing thing) {
48 protected boolean internalHandleCommand(String channelId, Command command) throws FreeboxException {
49 LcdManager manager = getManager(LcdManager.class);
50 Config config = manager.getConfig();
53 setBrightness(manager, config, command);
57 setOrientation(manager, config, command);
61 setForced(manager, config, command);
65 return super.internalHandleCommand(channelId, command);
69 protected void internalPoll() throws FreeboxException {
71 if (anyChannelLinked(GROUP_DISPLAY, Set.of(LCD_BRIGHTNESS, LCD_ORIENTATION, LCD_FORCED))) {
72 Config config = getManager(LcdManager.class).getConfig();
73 updateChannelQuantity(GROUP_DISPLAY, LCD_BRIGHTNESS, config.brightness(), PERCENT);
74 updateChannelDecimal(GROUP_DISPLAY, LCD_ORIENTATION, config.orientation());
75 updateChannelOnOff(GROUP_DISPLAY, LCD_FORCED, config.orientationForced());
79 private void setOrientation(LcdManager manager, Config config, Command command) throws FreeboxException {
80 if (command instanceof DecimalType orientation) {
81 manager.setOrientation(orientation.intValue());
83 logger.warn("Invalid command {} from channel {}", command, LCD_ORIENTATION);
87 private void setForced(LcdManager manager, Config config, Command command) throws FreeboxException {
88 if (ON_OFF_CLASSES.contains(command.getClass())) {
89 manager.setOrientationForced(TRUE_COMMANDS.contains(command));
91 logger.warn("Invalid command {} from channel {}", command, LCD_FORCED);
95 private void setBrightness(LcdManager manager, Config config, Command command) throws FreeboxException {
96 if (command instanceof IncreaseDecreaseType) {
97 manager.setBrightness(() -> config.brightness() + (command == IncreaseDecreaseType.INCREASE ? 1 : -1));
98 } else if (command instanceof OnOffType) {
99 manager.setBrightness(() -> command == OnOffType.ON ? 100 : 0);
100 } else if (command instanceof QuantityType brightness) {
101 manager.setBrightness(() -> brightness.intValue());
102 } else if (command instanceof DecimalType || command instanceof PercentType) {
103 manager.setBrightness(() -> ((DecimalType) command).intValue());
105 logger.warn("Invalid command {} from channel {}", command, LCD_BRIGHTNESS);