]> git.basschouten.com Git - openhab-addons.git/blob
8c8cbe5ab8213cf83900955ca4328fddb5ebb2b4
[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 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;
31
32 /**
33  * The {@link RevolutionHandler} is responsible for handling take care of revolution server specifics
34  *
35  * @author GaĆ«l L'hopital - Initial contribution
36  */
37 @NonNullByDefault
38 public class RevolutionHandler extends ServerHandler {
39     private final Logger logger = LoggerFactory.getLogger(RevolutionHandler.class);
40
41     public RevolutionHandler(Thing thing) {
42         super(thing);
43     }
44
45     @Override
46     protected boolean internalHandleCommand(String channelId, Command command) throws FreeboxException {
47         LcdManager manager = getManager(LcdManager.class);
48         Config config = manager.getConfig();
49         switch (channelId) {
50             case LCD_BRIGHTNESS:
51                 setBrightness(manager, config, command);
52                 internalPoll();
53                 return true;
54             case LCD_ORIENTATION:
55                 setOrientation(manager, config, command);
56                 internalPoll();
57                 return true;
58             case LCD_FORCED:
59                 setForced(manager, config, command);
60                 internalPoll();
61                 return true;
62         }
63         return super.internalHandleCommand(channelId, command);
64     }
65
66     @Override
67     protected void internalPoll() throws FreeboxException {
68         super.internalPoll();
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());
73     }
74
75     private void setOrientation(LcdManager manager, Config config, Command command) throws FreeboxException {
76         if (command instanceof DecimalType orientation) {
77             manager.setOrientation(orientation.intValue());
78         } else {
79             logger.warn("Invalid command {} from channel {}", command, LCD_ORIENTATION);
80         }
81     }
82
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));
86         } else {
87             logger.warn("Invalid command {} from channel {}", command, LCD_FORCED);
88         }
89     }
90
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 brightness) {
97             manager.setBrightness(() -> brightness.intValue());
98         } else if (command instanceof DecimalType || command instanceof PercentType) {
99             manager.setBrightness(() -> ((DecimalType) command).intValue());
100         } else {
101             logger.warn("Invalid command {} from channel {}", command, LCD_BRIGHTNESS);
102         }
103     }
104 }