]> git.basschouten.com Git - openhab-addons.git/blob
b687fdedb57e5aa311fb6bc2add67662be4fb36b
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2024 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.freeboxos.internal.handler;
14
15 import static org.openhab.binding.freeboxos.internal.FreeboxOsBindingConstants.*;
16 import static org.openhab.core.library.unit.Units.PERCENT;
17
18 import java.util.Set;
19
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;
33
34 /**
35  * The {@link RevolutionHandler} is responsible for handling take care of revolution server specifics
36  *
37  * @author GaĆ«l L'hopital - Initial contribution
38  */
39 @NonNullByDefault
40 public class RevolutionHandler extends ServerHandler {
41     private final Logger logger = LoggerFactory.getLogger(RevolutionHandler.class);
42
43     public RevolutionHandler(Thing thing) {
44         super(thing);
45     }
46
47     @Override
48     protected boolean internalHandleCommand(String channelId, Command command) throws FreeboxException {
49         LcdManager manager = getManager(LcdManager.class);
50         Config config = manager.getConfig();
51         switch (channelId) {
52             case LCD_BRIGHTNESS:
53                 setBrightness(manager, config, command);
54                 internalPoll();
55                 return true;
56             case LCD_ORIENTATION:
57                 setOrientation(manager, config, command);
58                 internalPoll();
59                 return true;
60             case LCD_FORCED:
61                 setForced(manager, config, command);
62                 internalPoll();
63                 return true;
64         }
65         return super.internalHandleCommand(channelId, command);
66     }
67
68     @Override
69     protected void internalPoll() throws FreeboxException {
70         super.internalPoll();
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());
76         }
77     }
78
79     private void setOrientation(LcdManager manager, Config config, Command command) throws FreeboxException {
80         if (command instanceof DecimalType orientation) {
81             manager.setOrientation(orientation.intValue());
82         } else {
83             logger.warn("Invalid command {} from channel {}", command, LCD_ORIENTATION);
84         }
85     }
86
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));
90         } else {
91             logger.warn("Invalid command {} from channel {}", command, LCD_FORCED);
92         }
93     }
94
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());
104         } else {
105             logger.warn("Invalid command {} from channel {}", command, LCD_BRIGHTNESS);
106         }
107     }
108 }