]> git.basschouten.com Git - openhab-addons.git/blob
47c8454e5dfe8f0bfbf5622e0550f97ea7824db1
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2021 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
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);
38     }
39
40     public ScheduledExecutorService getScheduler() {
41         return scheduler;
42     }
43
44     public void setDutycycle(double newDutycycle) {
45         if (dutycycle != newDutycycle) {
46             this.dutycycle = newDutycycle;
47             state.dutyCycleChanged();
48         }
49
50         state.dutyCycleUpdated();
51     }
52
53     public double getDutycycle() {
54         return dutycycle;
55     }
56
57     public long getPeriodMs() {
58         return periodMs;
59     }
60
61     public State getState() {
62         return state;
63     }
64
65     public void setState(State current) {
66         this.state = current;
67     }
68
69     public void controlOutput(boolean on) {
70         controlOutput.accept(on);
71     }
72
73     public void reset() {
74         state.nextState(OnState::new);
75     }
76
77     public void stop() {
78         state.nextState(AlwaysOffState::new);
79     }
80 }