]> git.basschouten.com Git - openhab-addons.git/blob
20f3c34a3957be930627cf4c56fe5dc3f327e52d
[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.ScheduledFuture;
16 import java.util.concurrent.TimeUnit;
17
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19
20 /**
21  * Active when, the PWM period ended with a duty cycle set to 0%.
22  *
23  * @author Fabian Wolter - Initial Contribution
24  */
25 @NonNullByDefault
26 public class DutycycleZeroState extends State {
27     private ScheduledFuture<?> periodTimer;
28
29     public DutycycleZeroState(StateMachine context) {
30         super(context);
31
32         controlOutput(false);
33
34         periodTimer = scheduler.schedule(this::periodEnded, context.getPeriodMs(), TimeUnit.MILLISECONDS);
35     }
36
37     private void periodEnded() {
38         long dutycycleRounded = Math.round(context.getDutycycle());
39
40         if (dutycycleRounded <= 0) {
41             nextState(AlwaysOffState::new);
42         } else if (dutycycleRounded >= 100) {
43             nextState(DutycycleHundredState::new);
44         } else {
45             nextState(OnState::new);
46         }
47     }
48
49     @Override
50     public void dutyCycleChanged() {
51         // nothing
52     }
53
54     @Override
55     protected void dutyCycleUpdated() {
56         // nothing
57     }
58
59     @Override
60     public void dispose() {
61         periodTimer.cancel(false);
62     }
63 }