]> git.basschouten.com Git - openhab-addons.git/blob
f143ef16a69d2a60a44227801efc50424606ace0
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 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.nikobus.internal.handler;
14
15 import java.util.concurrent.Future;
16 import java.util.concurrent.TimeUnit;
17
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19 import org.eclipse.jdt.annotation.Nullable;
20 import org.openhab.binding.nikobus.internal.protocol.SwitchModuleGroup;
21 import org.openhab.binding.nikobus.internal.utils.Utils;
22 import org.openhab.core.library.types.PercentType;
23 import org.openhab.core.thing.Thing;
24 import org.openhab.core.types.Command;
25 import org.openhab.core.types.State;
26
27 /**
28  * The {@link NikobusDimmerModuleHandler} is responsible for communication between Nikobus dim-controller and binding.
29  *
30  * @author Boris Krivonog - Initial contribution
31  */
32 @NonNullByDefault
33 public class NikobusDimmerModuleHandler extends NikobusSwitchModuleHandler {
34     private @Nullable Future<?> requestUpdateFuture;
35
36     public NikobusDimmerModuleHandler(Thing thing) {
37         super(thing);
38     }
39
40     @Override
41     public void dispose() {
42         Utils.cancel(requestUpdateFuture);
43         requestUpdateFuture = null;
44
45         super.dispose();
46     }
47
48     @Override
49     public void requestStatus(SwitchModuleGroup group) {
50         Utils.cancel(requestUpdateFuture);
51         super.requestStatus(group);
52         requestUpdateFuture = scheduler.schedule(() -> super.requestStatus(group), 1, TimeUnit.SECONDS);
53     }
54
55     @Override
56     protected int valueFromCommand(String channelId, Command command) {
57         if (command instanceof PercentType) {
58             return Math.round(((PercentType) command).floatValue() / 100f * 255f);
59         }
60
61         return super.valueFromCommand(channelId, command);
62     }
63
64     @Override
65     protected State stateFromValue(String channelId, int value) {
66         int result = Math.round(value * 100f / 255f);
67         return new PercentType(result);
68     }
69 }