]> git.basschouten.com Git - openhab-addons.git/blob
a01e3eebc6a8627b6ca4514fe8e11e25a692c926
[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.qbus.internal.protocol;
14
15 import java.io.IOException;
16
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.eclipse.jdt.annotation.Nullable;
19 import org.openhab.binding.qbus.internal.handler.QbusDimmerHandler;
20
21 /**
22  * The {@link QbusDimmer} class represents the action Qbus Dimmer output.
23  *
24  * @author Koen Schockaert - Initial Contribution
25  */
26
27 @NonNullByDefault
28 public final class QbusDimmer {
29
30     private @Nullable QbusCommunication qComm;
31
32     private Integer id;
33
34     private @Nullable Integer state;
35
36     private @Nullable QbusDimmerHandler thingHandler;
37
38     QbusDimmer(Integer id) {
39         this.id = id;
40     }
41
42     /**
43      * This method should be called if the ThingHandler for the thing corresponding to this dimmer is initialized.
44      * It keeps a record of the thing handler in this object so the thing can be updated when
45      * the dimmer receives an update from the Qbus client.
46      *
47      * @param handler
48      */
49     public void setThingHandler(QbusDimmerHandler handler) {
50         this.thingHandler = handler;
51     }
52
53     /**
54      * This method sets a pointer to the qComm Dimmer of class {@link QbusCommuncation}.
55      * This is then used to be able to call back the sendCommand method in this class to send a command to the
56      * Qbus client.
57      *
58      * @param qComm
59      */
60     public void setQComm(QbusCommunication qComm) {
61         this.qComm = qComm;
62     }
63
64     /**
65      * Update the value of the dimmer
66      *
67      * @param state
68      */
69     public void updateState(@Nullable Integer state) {
70         this.state = state;
71         QbusDimmerHandler handler = this.thingHandler;
72         if (handler != null) {
73             handler.handleStateUpdate(this);
74         }
75     }
76
77     /**
78      * Get the state of dimmer.
79      *
80      * @return dimmer state
81      */
82     public @Nullable Integer getState() {
83         return this.state;
84     }
85
86     /**
87      * Sets the state of Dimmer.
88      *
89      * @param dimmer state
90      */
91     void setState(int state) {
92         this.state = state;
93         QbusDimmerHandler handler = thingHandler;
94         if (handler != null) {
95             handler.handleStateUpdate(this);
96         }
97     }
98
99     /**
100      * Sends the dimmer state to Qbus.
101      *
102      * @throws IOException
103      * @throws InterruptedException
104      */
105     public void execute(int percent, String sn) throws InterruptedException, IOException {
106         QbusMessageCmd qCmd = new QbusMessageCmd(sn, "executeDimmer").withId(this.id).withState(percent);
107         QbusCommunication comm = this.qComm;
108         if (comm != null) {
109             comm.sendMessage(qCmd);
110         }
111     }
112 }