]> git.basschouten.com Git - openhab-addons.git/blob
203ba4d1768b4c7ca2a2d6e96ab62e614f4c2ef6
[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.binding.lcn.internal;
14
15 import java.math.BigDecimal;
16 import java.util.Optional;
17
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19 import org.openhab.binding.lcn.internal.common.DimmerOutputCommand;
20 import org.openhab.binding.lcn.internal.common.LcnDefs;
21 import org.openhab.core.library.types.DecimalType;
22 import org.openhab.core.library.types.OnOffType;
23 import org.openhab.core.thing.profiles.ProfileCallback;
24 import org.openhab.core.thing.profiles.ProfileContext;
25 import org.openhab.core.thing.profiles.ProfileTypeUID;
26 import org.openhab.core.thing.profiles.StateProfile;
27 import org.openhab.core.types.Command;
28 import org.openhab.core.types.State;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31
32 /**
33  * A profile to control multiple dimmer outputs simultaneously with ramp.
34  *
35  * @author Fabian Wolter - Initial Contribution
36  */
37 @NonNullByDefault
38 public class DimmerOutputProfile implements StateProfile {
39     private final Logger logger = LoggerFactory.getLogger(DimmerOutputProfile.class);
40     /** The Profile's UID */
41     static final ProfileTypeUID UID = new ProfileTypeUID(LcnBindingConstants.BINDING_ID, "output");
42     private final ProfileCallback callback;
43     private int rampMs;
44     private boolean controlAllOutputs;
45     private boolean controlOutputs12;
46
47     public DimmerOutputProfile(ProfileCallback callback, ProfileContext profileContext) {
48         this.callback = callback;
49
50         Optional<Object> ramp = getConfig(profileContext, "ramp");
51         Optional<Object> allOutputs = getConfig(profileContext, "controlAllOutputs");
52         Optional<Object> outputs12 = getConfig(profileContext, "controlOutputs12");
53
54         ramp.ifPresent(b -> {
55             if (b instanceof BigDecimal) {
56                 rampMs = (int) (((BigDecimal) b).doubleValue() * 1000);
57             } else {
58                 logger.warn("Could not parse 'ramp', unexpected type, should be float: {}", ramp);
59             }
60         });
61
62         allOutputs.ifPresent(b -> {
63             if (b instanceof Boolean) {
64                 controlAllOutputs = true;
65             } else {
66                 logger.warn("Could not parse 'controlAllOutputs', unexpected type, should be true/false: {}", b);
67             }
68         });
69
70         outputs12.ifPresent(b -> {
71             if (b instanceof Boolean) {
72                 controlOutputs12 = true;
73             } else {
74                 logger.warn("Could not parse 'controlOutputs12', unexpected type, should be true/false: {}", b);
75             }
76         });
77     }
78
79     private Optional<Object> getConfig(ProfileContext profileContext, String key) {
80         return Optional.ofNullable(profileContext.getConfiguration().get(key));
81     }
82
83     @Override
84     public void onCommandFromItem(Command command) {
85         if (rampMs != 0 && rampMs != LcnDefs.FIXED_RAMP_MS && controlOutputs12) {
86             logger.warn("Unsupported 'ramp' setting. Will be forced to 250ms: {}", rampMs);
87         }
88         BigDecimal value;
89         if (command instanceof DecimalType) {
90             value = ((DecimalType) command).toBigDecimal();
91         } else if (command instanceof OnOffType) {
92             value = ((OnOffType) command) == OnOffType.ON ? BigDecimal.valueOf(100) : BigDecimal.ZERO;
93         } else {
94             logger.warn("Unsupported type: {}", command.toFullString());
95             return;
96         }
97         callback.handleCommand(new DimmerOutputCommand(value, controlAllOutputs, controlOutputs12, rampMs));
98     }
99
100     @Override
101     public void onStateUpdateFromHandler(State state) {
102         callback.sendUpdate(state);
103     }
104
105     @Override
106     public ProfileTypeUID getProfileTypeUID() {
107         return UID;
108     }
109
110     @Override
111     public void onCommandFromHandler(Command command) {
112         // nothing
113     }
114
115     @Override
116     public void onStateUpdateFromItem(State state) {
117         // nothing
118     }
119 }