]> git.basschouten.com Git - openhab-addons.git/blob
d233397eb67fcb24e4cf7ca00b9c812dcda69b02
[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 output is currently OFF and the duty cycle is between 0% and 100% (exclusively).
22  *
23  * @author Fabian Wolter - Initial Contribution
24  */
25 @NonNullByDefault
26 public class OffState extends State {
27     ScheduledFuture<?> offTimer;
28
29     public OffState(StateMachine context) {
30         super(context);
31
32         controlOutput(false);
33
34         long offTimeMs = context.getPeriodMs() - calculateOnTimeMs(context.getDutycycle());
35         offTimer = scheduler.schedule(this::periodEnded, offTimeMs, TimeUnit.MILLISECONDS);
36     }
37
38     private void periodEnded() {
39         long dutycycleRounded = Math.round(context.getDutycycle());
40
41         if (dutycycleRounded <= 0) {
42             nextState(DutycycleZeroState::new);
43         } else if (dutycycleRounded >= 100) {
44             nextState(DutycycleHundredState::new);
45         } else {
46             nextState(OnState::new);
47         }
48     }
49
50     @Override
51     public void dutyCycleChanged() {
52         // nothing
53     }
54
55     @Override
56     protected void dutyCycleUpdated() {
57         // nothing
58     }
59
60     @Override
61     public void dispose() {
62         offTimer.cancel(false);
63     }
64 }