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.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;
32 private String ruleUID;
34 public StateMachine(ScheduledExecutorService scheduler, Consumer<Boolean> controlOutput, long periodMs,
36 this.scheduler = scheduler;
37 this.controlOutput = controlOutput;
38 this.periodMs = periodMs;
39 this.ruleUID = ruleUID;
40 this.state = new AlwaysOffState(this);
43 public ScheduledExecutorService getScheduler() {
47 public void setDutycycle(double newDutycycle) {
48 if (dutycycle != newDutycycle) {
49 this.dutycycle = newDutycycle;
50 state.dutyCycleChanged();
53 state.dutyCycleUpdated();
56 public double getDutycycle() {
60 public long getPeriodMs() {
64 public State getState() {
68 public void setState(State current) {
72 public String getRuleUID() {
76 public void controlOutput(boolean on) {
77 controlOutput.accept(on);
81 state.nextState(OnState::new);
85 state.nextState(AlwaysOffState::new);