2 * Copyright (c) 2010-2021 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.ScheduledExecutorService;
16 import java.util.function.Consumer;
18 import org.eclipse.jdt.annotation.NonNullByDefault;
21 * The context of all states.
23 * @author Fabian Wolter - Initial Contribution
26 public class StateMachine {
27 private ScheduledExecutorService scheduler;
28 private Consumer<Boolean> controlOutput;
30 private long periodMs;
31 private double dutycycle;
33 public StateMachine(ScheduledExecutorService scheduler, Consumer<Boolean> controlOutput, long periodMs) {
34 this.scheduler = scheduler;
35 this.controlOutput = controlOutput;
36 this.periodMs = periodMs;
37 this.state = new AlwaysOffState(this);
40 public ScheduledExecutorService getScheduler() {
44 public void setDutycycle(double newDutycycle) {
45 if (dutycycle != newDutycycle) {
46 this.dutycycle = newDutycycle;
47 state.dutyCycleChanged();
50 state.dutyCycleUpdated();
53 public double getDutycycle() {
57 public long getPeriodMs() {
61 public State getState() {
65 public void setState(State current) {
69 public void controlOutput(boolean on) {
70 controlOutput.accept(on);
74 state.nextState(OnState::new);
78 state.nextState(AlwaysOffState::new);