]> git.basschouten.com Git - openhab-addons.git/blob
fc3d8f4c7e619d10b5f1bce21c420ca11d562770
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2020 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.velux.internal.things;
14
15 import org.eclipse.jdt.annotation.NonNullByDefault;
16 import org.slf4j.Logger;
17 import org.slf4j.LoggerFactory;
18
19 /**
20  * <B>Velux</B> product representation.
21  * <P>
22  * Combined set of information describing a single Velux product.
23  *
24  * @author Guenther Schreiner - initial contribution.
25  */
26 @NonNullByDefault
27 public class VeluxProduct {
28     private final Logger logger = LoggerFactory.getLogger(VeluxProduct.class);
29
30     // Public definition
31
32     public static final VeluxProduct UNKNOWN = new VeluxProduct();
33
34     // Type definitions
35
36     public static class ProductBridgeIndex {
37
38         // Public definition
39         public static final ProductBridgeIndex UNKNOWN = new ProductBridgeIndex(0);
40
41         // Class internal
42         private int id;
43
44         // Constructor
45         public ProductBridgeIndex(int id) {
46             this.id = id;
47         }
48
49         // Class access methods
50         public int toInt() {
51             return id;
52         }
53
54         @Override
55         public String toString() {
56             return Integer.toString(id);
57         }
58     }
59
60     // State (of movement) of an actuator
61     public static enum State {
62         NON_EXECUTING(0),
63         ERROR(1),
64         NOT_USED(2),
65         WAITING_FOR_POWER(3),
66         EXECUTING(4),
67         DONE(5),
68         MANUAL_OVERRIDE(0x80),
69         UNKNOWN(0xFF);
70
71         public final int value;
72
73         private State(int value) {
74             this.value = value;
75         }
76     }
77
78     // Class internal
79
80     private VeluxProductName name;
81     private VeluxProductType typeId;
82     private ProductBridgeIndex bridgeProductIndex;
83
84     private boolean v2 = false;
85     private int order = 0;
86     private int placement = 0;
87     private int velocity = 0;
88     private int variation = 0;
89     private int powerMode = 0;
90     private String serialNumber = VeluxProductSerialNo.UNKNOWN;
91     private int state = State.UNKNOWN.value;
92     private int currentPosition = 0;
93     private int targetPosition = 0;
94     private int remainingTime = 0;
95     private int timeStamp = 0;
96
97     // Constructor
98
99     /**
100      * Constructor
101      *
102      * just for the dummy VeluxProduct.
103      */
104     public VeluxProduct() {
105         logger.trace("VeluxProduct() created.");
106         this.name = VeluxProductName.UNKNOWN;
107         this.typeId = VeluxProductType.UNDEFTYPE;
108         this.bridgeProductIndex = ProductBridgeIndex.UNKNOWN;
109     }
110
111     /**
112      * Constructor
113      *
114      * @param name This field Name holds the name of the actuator, ex. “Window 1”. This field is 64 bytes
115      *            long, formatted as UTF-8 characters.
116      * @param typeId This field indicates the node type, ex. Window, Roller shutter, Light etc.
117      * @param bridgeProductIndex NodeID is an Actuator index in the system table, to get information from. It must be a
118      *            value from 0 to 199.
119      */
120     public VeluxProduct(VeluxProductName name, VeluxProductType typeId, ProductBridgeIndex bridgeProductIndex) {
121         logger.trace("VeluxProduct(v1,name={}) created.", name.toString());
122         this.name = name;
123         this.typeId = typeId;
124         this.bridgeProductIndex = bridgeProductIndex;
125     }
126
127     /**
128      * Constructor
129      *
130      * @param name This field Name holds the name of the actuator, ex. “Window 1”. This field is 64 bytes
131      *            long, formatted as UTF-8 characters.
132      * @param typeId This field indicates the node type, ex. Window, Roller shutter, Light etc.
133      * @param bridgeProductIndex NodeID is an Actuator index in the system table, to get information from. It must be a
134      *            value from 0 to 199.
135      * @param order Order can be used to store a sort order. The sort order is used in client end, when
136      *            presenting a list of nodes for the user.
137      * @param placement Placement can be used to store a room group index or house group index number.
138      * @param velocity This field indicates what velocity the node is operation with.
139      * @param variation More detail information like top hung, kip, flat roof or sky light window.
140      * @param powerMode This field indicates the power mode of the node (ALWAYS_ALIVE/LOW_POWER_MODE).
141      * @param serialNumber This field tells the serial number of the node. This field is 8 bytes.
142      * @param state This field indicates the operating state of the node.
143      * @param currentPosition This field indicates the current position of the node.
144      * @param target This field indicates the target position of the current operation.
145      * @param remainingTime This field indicates the remaining time for a node activation in seconds.
146      * @param timeStamp UTC time stamp for last known position.
147      */
148     public VeluxProduct(VeluxProductName name, VeluxProductType typeId, ProductBridgeIndex bridgeProductIndex,
149             int order, int placement, int velocity, int variation, int powerMode, String serialNumber, int state,
150             int currentPosition, int target, int remainingTime, int timeStamp) {
151         logger.trace("VeluxProduct(v2,name={}) created.", name.toString());
152         this.name = name;
153         this.typeId = typeId;
154         this.bridgeProductIndex = bridgeProductIndex;
155         this.v2 = true;
156         this.order = order;
157         this.placement = placement;
158         this.velocity = velocity;
159         this.variation = variation;
160         this.powerMode = powerMode;
161         this.serialNumber = serialNumber;
162         this.state = state;
163         this.currentPosition = currentPosition;
164         this.targetPosition = target;
165         this.remainingTime = remainingTime;
166         this.timeStamp = timeStamp;
167     }
168
169     // Utility methods
170
171     @Override
172     public VeluxProduct clone() {
173         if (this.v2) {
174             return new VeluxProduct(this.name, this.typeId, this.bridgeProductIndex, this.order, this.placement,
175                     this.velocity, this.variation, this.powerMode, this.serialNumber, this.state, this.currentPosition,
176                     this.targetPosition, this.remainingTime, this.timeStamp);
177         } else {
178             return new VeluxProduct(this.name, this.typeId, this.bridgeProductIndex);
179         }
180     }
181
182     // Class access methods
183
184     /**
185      * Returns the name of the current product (aka actuator) for convenience as type-specific class.
186      *
187      * @return nameOfThisProduct as type {@link VeluxProductName}.
188      */
189     public VeluxProductName getProductName() {
190         return this.name;
191     }
192
193     /**
194      * Returns the type of the current product (aka actuator) for convenience as type-specific class.
195      *
196      * @return typeOfThisProduct as type {@link VeluxProductType}.
197      */
198     public VeluxProductType getProductType() {
199         return this.typeId;
200     }
201
202     public ProductBridgeIndex getBridgeProductIndex() {
203         return this.bridgeProductIndex;
204     }
205
206     @Override
207     public String toString() {
208         if (this.v2) {
209             return String.format("Product \"%s\" / %s (bridgeIndex=%d,serial=%s,position=%04X)", this.name, this.typeId,
210                     this.bridgeProductIndex.toInt(), this.serialNumber, this.currentPosition);
211         } else {
212             return String.format("Product \"%s\" / %s (bridgeIndex %d)", this.name, this.typeId,
213                     this.bridgeProductIndex.toInt());
214         }
215     }
216
217     // Class helper methods
218
219     public String getProductUniqueIndex() {
220         return this.name.toString().concat("#").concat(this.typeId.toString());
221     }
222
223     // Getter and Setter methods
224
225     /**
226      * @return <b>v2</b> as type boolean signals the availability of firmware version two (product) details.
227      */
228     public boolean isV2() {
229         return v2;
230     }
231
232     /**
233      * @return <b>order</b> as type int describes the user-oriented sort-order.
234      */
235     public int getOrder() {
236         return order;
237     }
238
239     /**
240      * @return <B>placement</B> as type int is used to describe a group index or house group index number.
241      */
242     public int getPlacement() {
243         return placement;
244     }
245
246     /**
247      * @return <B>velocity</B> as type int describes what velocity the node is operation with
248      */
249     public int getVelocity() {
250         return velocity;
251     }
252
253     /**
254      * @return <B>variation</B> as type int describes detail information like top hung, kip, flat roof or sky light
255      *         window.
256      */
257     public int getVariation() {
258         return variation;
259     }
260
261     /**
262      * @return <B>powerMode</B> as type int is used to show the power mode of the node (ALWAYS_ALIVE/LOW_POWER_MODE).
263      */
264     public int getPowerMode() {
265         return powerMode;
266     }
267
268     /**
269      * @return <B>serialNumber</B> as type String is the serial number of 8 bytes length of the node.
270      */
271     public String getSerialNumber() {
272         return serialNumber;
273     }
274
275     /**
276      * @return <B>state</B> as type int is used to operating state of the node.
277      */
278     public int getState() {
279         return state;
280     }
281
282     /**
283      * @param newState Update the operating state of the node.
284      * @return <B>modified</B> as type boolean to signal a real modification.
285      */
286     public boolean setState(int newState) {
287         if (this.state == newState) {
288             return false;
289         } else {
290             logger.trace("setState(name={},index={}) state {} replaced by {}.", name.toString(),
291                     bridgeProductIndex.toInt(), this.state, newState);
292             this.state = newState;
293             return true;
294         }
295     }
296
297     /**
298      * @return <B>currentPosition</B> as type int signals the current position of the node.
299      */
300     public int getCurrentPosition() {
301         return currentPosition;
302     }
303
304     /**
305      * @param newCurrentPosition Update the current position of the node.
306      * @return <B>modified</B> as boolean to signal a real modification.
307      */
308     public boolean setCurrentPosition(int newCurrentPosition) {
309         if (this.currentPosition == newCurrentPosition) {
310             return false;
311         } else {
312             logger.trace("setCurrentPosition(name={},index={}) currentPosition {} replaced by {}.", name.toString(),
313                     bridgeProductIndex.toInt(), this.currentPosition, newCurrentPosition);
314             this.currentPosition = newCurrentPosition;
315             return true;
316         }
317     }
318
319     /**
320      * @return <b>target</b> as type int shows the target position of the current operation.
321      */
322     public int getTarget() {
323         return targetPosition;
324     }
325
326     /**
327      * @param newTarget Update the target position of the current operation.
328      * @return <b>modified</b> as boolean to signal a real modification.
329      */
330     public boolean setTarget(int newTarget) {
331         if (this.targetPosition == newTarget) {
332             return false;
333         } else {
334             logger.trace("setCurrentPosition(name={},index={}) target {} replaced by {}.", name.toString(),
335                     bridgeProductIndex.toInt(), this.targetPosition, newTarget);
336             this.targetPosition = newTarget;
337             return true;
338         }
339     }
340
341     /**
342      * @return <b>remainingTime</b> as type int describes the intended remaining time of current operation.
343      */
344     public int getRemainingTime() {
345         return remainingTime;
346     }
347
348     /**
349      * @return <b>timeStamp</b> as type int describes the current time.
350      */
351     public int getTimeStamp() {
352         return timeStamp;
353     }
354
355     /**
356      * Returns the display position of the actuator.
357      * <li>As a general rule it returns <b>currentPosition</b>, except as follows..
358      * <li>If the actuator is in a motion state it returns <b>targetPosition</b>
359      * <li>If the motion state is 'done' but the currentPosition is invalid it returns <b>targetPosition</b>
360      * <li>If the manual override flag is set it returns the <b>unknown</b> position value
361      *
362      * @return The display position of the actuator
363      */
364     public int getDisplayPosition() {
365         // manual override flag set: position is 'unknown'
366         if ((state & State.MANUAL_OVERRIDE.value) != 0) {
367             return VeluxProductPosition.VPP_VELUX_UNKNOWN;
368         }
369         // only check other conditions if targetPosition is valid and differs from currentPosition
370         if ((targetPosition != currentPosition) && (targetPosition <= VeluxProductPosition.VPP_VELUX_MAX)
371                 && (targetPosition >= VeluxProductPosition.VPP_VELUX_MIN)) {
372             int state = this.state & 0xf;
373             // actuator is in motion: for quicker UI update, return targetPosition
374             if ((state > State.ERROR.value) && (state < State.DONE.value)) {
375                 return targetPosition;
376             }
377             // motion complete but currentPosition is not valid: return targetPosition
378             if ((state == State.DONE.value) && ((currentPosition > VeluxProductPosition.VPP_VELUX_MAX)
379                     || (currentPosition < VeluxProductPosition.VPP_VELUX_MIN))) {
380                 return targetPosition;
381             }
382         }
383         return currentPosition;
384     }
385 }