]> git.basschouten.com Git - openhab-addons.git/blob
ee739cb2d63381298c119217ddfa7a4ee72a8f90
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.automation.pwm.internal.handler.state;
14
15 import java.util.concurrent.ScheduledExecutorService;
16 import java.util.function.Consumer;
17
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19
20 /**
21  * The context of all states.
22  *
23  * @author Fabian Wolter - Initial Contribution
24  */
25 @NonNullByDefault
26 public class StateMachine {
27     private ScheduledExecutorService scheduler;
28     private Consumer<Boolean> controlOutput;
29     private State state;
30     private long periodMs;
31     private double dutycycle;
32     private String ruleUID;
33
34     public StateMachine(ScheduledExecutorService scheduler, Consumer<Boolean> controlOutput, long periodMs,
35             String ruleUID) {
36         this.scheduler = scheduler;
37         this.controlOutput = controlOutput;
38         this.periodMs = periodMs;
39         this.ruleUID = ruleUID;
40         this.state = new AlwaysOffState(this);
41     }
42
43     public ScheduledExecutorService getScheduler() {
44         return scheduler;
45     }
46
47     public void setDutycycle(double newDutycycle) {
48         if (dutycycle != newDutycycle) {
49             this.dutycycle = newDutycycle;
50             state.dutyCycleChanged();
51         }
52
53         state.dutyCycleUpdated();
54     }
55
56     public double getDutycycle() {
57         return dutycycle;
58     }
59
60     public long getPeriodMs() {
61         return periodMs;
62     }
63
64     public State getState() {
65         return state;
66     }
67
68     public void setState(State current) {
69         this.state = current;
70     }
71
72     public String getRuleUID() {
73         return ruleUID;
74     }
75
76     public void controlOutput(boolean on) {
77         controlOutput.accept(on);
78     }
79
80     public void reset() {
81         state.nextState(OnState::new);
82     }
83
84     public void stop() {
85         state.nextState(AlwaysOffState::new);
86     }
87 }