2 * Copyright (c) 2010-2022 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.time.Instant;
16 import java.time.temporal.ChronoUnit;
17 import java.util.concurrent.ScheduledFuture;
18 import java.util.concurrent.TimeUnit;
20 import org.eclipse.jdt.annotation.NonNullByDefault;
23 * Active when, the output is currently ON and the duty cycle is between 0% and 100% (exclusively).
25 * @author Fabian Wolter - Initial Contribution
28 public class OnState extends State {
29 private @NonNullByDefault({}) ScheduledFuture<?> offTimer;
30 private Instant enabledAt = Instant.now();
32 public OnState(StateMachine context) {
35 context.controlOutput(true);
37 startOnTimer(calculateOnTimeMs(context.getDutycycle()));
40 private void startOnTimer(long timeMs) {
41 offTimer = scheduler.schedule(() -> {
42 if (Math.round(context.getDutycycle()) >= 100) {
43 nextState(DutycycleHundredState::new);
45 nextState(OffState::new);
47 }, timeMs, TimeUnit.MILLISECONDS);
51 public void dutyCycleChanged() {
52 // end current ON phase prematurely or extend it if the new duty cycle demands it
53 offTimer.cancel(false);
55 long newOnTimeMs = calculateOnTimeMs(context.getDutycycle());
56 long elapsedMs = enabledAt.until(Instant.now(), ChronoUnit.MILLIS);
58 if (elapsedMs - newOnTimeMs > 0) {
59 nextState(OffState::new);
61 startOnTimer(newOnTimeMs - elapsedMs);
66 protected void dutyCycleUpdated() {
71 public void dispose() {
72 offTimer.cancel(false);