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.automation.pwm.internal.handler.state;
15 import java.util.concurrent.ScheduledFuture;
16 import java.util.concurrent.TimeUnit;
18 import org.eclipse.jdt.annotation.NonNullByDefault;
21 * Active when, the output is currently OFF and the duty cycle is between 0% and 100% (exclusively).
23 * @author Fabian Wolter - Initial Contribution
26 public class OffState extends State {
27 ScheduledFuture<?> offTimer;
29 public OffState(StateMachine context) {
34 long offTimeMs = context.getPeriodMs() - calculateOnTimeMs(context.getDutycycle());
35 offTimer = scheduler.schedule(this::periodEnded, offTimeMs, TimeUnit.MILLISECONDS);
38 private void periodEnded() {
39 long dutycycleRounded = Math.round(context.getDutycycle());
41 if (dutycycleRounded <= 0) {
42 nextState(DutycycleZeroState::new);
43 } else if (dutycycleRounded >= 100) {
44 nextState(DutycycleHundredState::new);
46 nextState(OnState::new);
51 public void dutyCycleChanged() {
56 protected void dutyCycleUpdated() {
61 public void dispose() {
62 offTimer.cancel(false);