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.freeboxos.internal.handler;
15 import static org.openhab.binding.freeboxos.internal.FreeboxOsBindingConstants.*;
16 import static org.openhab.core.library.unit.Units.PERCENT;
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19 import org.openhab.binding.freeboxos.internal.api.FreeboxException;
20 import org.openhab.binding.freeboxos.internal.api.rest.LcdManager;
21 import org.openhab.binding.freeboxos.internal.api.rest.LcdManager.Config;
22 import org.openhab.core.library.types.DecimalType;
23 import org.openhab.core.library.types.IncreaseDecreaseType;
24 import org.openhab.core.library.types.OnOffType;
25 import org.openhab.core.library.types.PercentType;
26 import org.openhab.core.library.types.QuantityType;
27 import org.openhab.core.thing.Thing;
28 import org.openhab.core.types.Command;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
33 * The {@link RevolutionHandler} is responsible for handling take care of revolution server specifics
35 * @author Gaƫl L'hopital - Initial contribution
38 public class RevolutionHandler extends ServerHandler {
39 private final Logger logger = LoggerFactory.getLogger(RevolutionHandler.class);
41 public RevolutionHandler(Thing thing) {
46 protected boolean internalHandleCommand(String channelId, Command command) throws FreeboxException {
47 LcdManager manager = getManager(LcdManager.class);
48 Config config = manager.getConfig();
51 setBrightness(manager, config, command);
55 setOrientation(manager, config, command);
59 setForced(manager, config, command);
63 return super.internalHandleCommand(channelId, command);
67 protected void internalPoll() throws FreeboxException {
69 Config config = getManager(LcdManager.class).getConfig();
70 updateChannelQuantity(DISPLAY, LCD_BRIGHTNESS, config.brightness(), PERCENT);
71 updateChannelDecimal(DISPLAY, LCD_ORIENTATION, config.orientation());
72 updateChannelOnOff(DISPLAY, LCD_FORCED, config.orientationForced());
75 private void setOrientation(LcdManager manager, Config config, Command command) throws FreeboxException {
76 if (command instanceof DecimalType) {
77 manager.setOrientation(((DecimalType) command).intValue());
79 logger.warn("Invalid command {} from channel {}", command, LCD_ORIENTATION);
83 private void setForced(LcdManager manager, Config config, Command command) throws FreeboxException {
84 if (ON_OFF_CLASSES.contains(command.getClass())) {
85 manager.setOrientationForced(TRUE_COMMANDS.contains(command));
87 logger.warn("Invalid command {} from channel {}", command, LCD_FORCED);
91 private void setBrightness(LcdManager manager, Config config, Command command) throws FreeboxException {
92 if (command instanceof IncreaseDecreaseType) {
93 manager.setBrightness(() -> config.brightness() + (command == IncreaseDecreaseType.INCREASE ? 1 : -1));
94 } else if (command instanceof OnOffType) {
95 manager.setBrightness(() -> command == OnOffType.ON ? 100 : 0);
96 } else if (command instanceof QuantityType) {
97 manager.setBrightness(() -> ((QuantityType<?>) command).intValue());
98 } else if (command instanceof DecimalType || command instanceof PercentType) {
99 manager.setBrightness(() -> ((DecimalType) command).intValue());
101 logger.warn("Invalid command {} from channel {}", command, LCD_BRIGHTNESS);