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.nikobus.internal.handler;
15 import java.util.concurrent.Future;
16 import java.util.concurrent.TimeUnit;
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;
28 * The {@link NikobusDimmerModuleHandler} is responsible for communication between Nikobus dim-controller and binding.
30 * @author Boris Krivonog - Initial contribution
33 public class NikobusDimmerModuleHandler extends NikobusSwitchModuleHandler {
34 private @Nullable Future<?> requestUpdateFuture;
36 public NikobusDimmerModuleHandler(Thing thing) {
41 public void dispose() {
42 Utils.cancel(requestUpdateFuture);
43 requestUpdateFuture = null;
49 public void requestStatus(SwitchModuleGroup group) {
50 Utils.cancel(requestUpdateFuture);
51 super.requestStatus(group);
52 requestUpdateFuture = scheduler.schedule(() -> super.requestStatus(group), 1, TimeUnit.SECONDS);
56 protected int valueFromCommand(String channelId, Command command) {
57 if (command instanceof PercentType percentCommand) {
58 return Math.round(percentCommand.floatValue() / 100f * 255f);
61 return super.valueFromCommand(channelId, command);
65 protected State stateFromValue(String channelId, int value) {
66 int result = Math.round(value * 100f / 255f);
67 return new PercentType(result);