]> git.basschouten.com Git - openhab-addons.git/blob
383a15ad21309c24a01cc0458d8eaed18ad0fe51
[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     @NonNullByDefault
37     public static class ProductBridgeIndex {
38
39         // Public definition
40         public static final ProductBridgeIndex UNKNOWN = new ProductBridgeIndex(0);
41
42         // Class internal
43         private int id;
44
45         // Constructor
46         public ProductBridgeIndex(int id) {
47             this.id = id;
48         }
49
50         // Class access methods
51         public int toInt() {
52             return id;
53         }
54
55         @Override
56         public String toString() {
57             return Integer.toString(id);
58         }
59     }
60
61     // Class internal
62
63     private VeluxProductName name;
64     private VeluxProductType typeId;
65     private ProductBridgeIndex bridgeProductIndex;
66
67     private boolean v2 = false;
68     private int order = 0;
69     private int placement = 0;
70     private int velocity = 0;
71     private int variation = 0;
72     private int powerMode = 0;
73     private String serialNumber = VeluxProductSerialNo.UNKNOWN;
74     private int state = 0;
75     private int currentPosition = 0;
76     private int target = 0;
77     private int remainingTime = 0;
78     private int timeStamp = 0;
79
80     // Constructor
81
82     /**
83      * Constructor
84      *
85      * just for the dummy VeluxProduct.
86      */
87     public VeluxProduct() {
88         logger.trace("VeluxProduct() created.");
89         this.name = VeluxProductName.UNKNOWN;
90         this.typeId = VeluxProductType.UNDEFTYPE;
91         this.bridgeProductIndex = ProductBridgeIndex.UNKNOWN;
92     }
93
94     /**
95      * Constructor
96      *
97      * @param name This field Name holds the name of the actuator, ex. “Window 1”. This field is 64 bytes
98      *            long, formatted as UTF-8 characters.
99      * @param typeId This field indicates the node type, ex. Window, Roller shutter, Light etc.
100      * @param bridgeProductIndex NodeID is an Actuator index in the system table, to get information from. It must be a
101      *            value from 0 to 199.
102      */
103     public VeluxProduct(VeluxProductName name, VeluxProductType typeId, ProductBridgeIndex bridgeProductIndex) {
104         logger.trace("VeluxProduct(v1,name={}) created.", name.toString());
105         this.name = name;
106         this.typeId = typeId;
107         this.bridgeProductIndex = bridgeProductIndex;
108     }
109
110     /**
111      * Constructor
112      *
113      * @param name This field Name holds the name of the actuator, ex. “Window 1”. This field is 64 bytes
114      *            long, formatted as UTF-8 characters.
115      * @param typeId This field indicates the node type, ex. Window, Roller shutter, Light etc.
116      * @param bridgeProductIndex NodeID is an Actuator index in the system table, to get information from. It must be a
117      *            value from 0 to 199.
118      * @param order Order can be used to store a sort order. The sort order is used in client end, when
119      *            presenting a list of nodes for the user.
120      * @param placement Placement can be used to store a room group index or house group index number.
121      * @param velocity This field indicates what velocity the node is operation with.
122      * @param variation More detail information like top hung, kip, flat roof or sky light window.
123      * @param powerMode This field indicates the power mode of the node (ALWAYS_ALIVE/LOW_POWER_MODE).
124      * @param serialNumber This field tells the serial number of the node. This field is 8 bytes.
125      * @param state This field indicates the operating state of the node.
126      * @param currentPosition This field indicates the current position of the node.
127      * @param target This field indicates the target position of the current operation.
128      * @param remainingTime This field indicates the remaining time for a node activation in seconds.
129      * @param timeStamp UTC time stamp for last known position.
130      */
131     public VeluxProduct(VeluxProductName name, VeluxProductType typeId, ProductBridgeIndex bridgeProductIndex,
132             int order, int placement, int velocity, int variation, int powerMode, String serialNumber, int state,
133             int currentPosition, int target, int remainingTime, int timeStamp) {
134         logger.trace("VeluxProduct(v2,name={}) created.", name.toString());
135         this.name = name;
136         this.typeId = typeId;
137         this.bridgeProductIndex = bridgeProductIndex;
138         this.v2 = true;
139         this.order = order;
140         this.placement = placement;
141         this.velocity = velocity;
142         this.variation = variation;
143         this.powerMode = powerMode;
144         this.serialNumber = serialNumber;
145         this.state = state;
146         this.currentPosition = currentPosition;
147         this.target = target;
148         this.remainingTime = remainingTime;
149         this.timeStamp = timeStamp;
150     }
151
152     // Utility methods
153
154     @Override
155     public VeluxProduct clone() {
156         if (this.v2) {
157             return new VeluxProduct(this.name, this.typeId, this.bridgeProductIndex, this.order, this.placement,
158                     this.velocity, this.variation, this.powerMode, this.serialNumber, this.state, this.currentPosition,
159                     this.target, this.remainingTime, this.timeStamp);
160         } else {
161             return new VeluxProduct(this.name, this.typeId, this.bridgeProductIndex);
162         }
163     }
164
165     // Class access methods
166
167     /**
168      * Returns the name of the current product (aka actuator) for convenience as type-specific class.
169      *
170      * @return nameOfThisProduct as type {@link VeluxProductName}.
171      */
172     public VeluxProductName getProductName() {
173         return this.name;
174     }
175
176     /**
177      * Returns the type of the current product (aka actuator) for convenience as type-specific class.
178      *
179      * @return typeOfThisProduct as type {@link VeluxProductType}.
180      */
181     public VeluxProductType getProductType() {
182         return this.typeId;
183     }
184
185     public ProductBridgeIndex getBridgeProductIndex() {
186         return this.bridgeProductIndex;
187     }
188
189     @Override
190     public String toString() {
191         if (this.v2) {
192             return String.format("Product \"%s\" / %s (bridgeIndex=%d,serial=%s,position=%04X)", this.name, this.typeId,
193                     this.bridgeProductIndex.toInt(), this.serialNumber, this.currentPosition);
194         } else {
195             return String.format("Product \"%s\" / %s (bridgeIndex %d)", this.name, this.typeId,
196                     this.bridgeProductIndex.toInt());
197         }
198     }
199
200     // Class helper methods
201
202     public String getProductUniqueIndex() {
203         return this.name.toString().concat("#").concat(this.typeId.toString());
204     }
205
206     // Getter and Setter methods
207
208     /**
209      * @return <b>v2</b> as type boolean signals the availability of firmware version two (product) details.
210      */
211     public boolean isV2() {
212         return v2;
213     }
214
215     /**
216      * @return <b>order</b> as type int describes the user-oriented sort-order.
217      */
218     public int getOrder() {
219         return order;
220     }
221
222     /**
223      * @return <B>placement</B> as type int is used to describe a group index or house group index number.
224      */
225     public int getPlacement() {
226         return placement;
227     }
228
229     /**
230      * @return <B>velocity</B> as type int describes what velocity the node is operation with
231      */
232     public int getVelocity() {
233         return velocity;
234     }
235
236     /**
237      * @return <B>variation</B> as type int describes detail information like top hung, kip, flat roof or sky light
238      *         window.
239      */
240     public int getVariation() {
241         return variation;
242     }
243
244     /**
245      * @return <B>powerMode</B> as type int is used to show the power mode of the node (ALWAYS_ALIVE/LOW_POWER_MODE).
246      */
247     public int getPowerMode() {
248         return powerMode;
249     }
250
251     /**
252      * @return <B>serialNumber</B> as type String is the serial number of 8 bytes length of the node.
253      */
254     public String getSerialNumber() {
255         return serialNumber;
256     }
257
258     /**
259      * @return <B>state</B> as type int is used to operating state of the node.
260      */
261     public int getState() {
262         return state;
263     }
264
265     /**
266      * @param newState Update the operating state of the node.
267      * @return <B>modified</B> as type boolean to signal a real modification.
268      */
269     public boolean setState(int newState) {
270         if (this.state == newState) {
271             return false;
272         } else {
273             logger.trace("setState(name={},index={}) state {} replaced by {}.", name.toString(),
274                     bridgeProductIndex.toInt(), this.state, newState);
275             this.state = newState;
276             return true;
277         }
278     }
279
280     /**
281      * @return <B>currentPosition</B> as type int signals the current position of the node.
282      */
283     public int getCurrentPosition() {
284         return currentPosition;
285     }
286
287     /**
288      * @param newCurrentPosition Update the current position of the node.
289      * @return <B>modified</B> as boolean to signal a real modification.
290      */
291     public boolean setCurrentPosition(int newCurrentPosition) {
292         if (this.currentPosition == newCurrentPosition) {
293             return false;
294         } else {
295             logger.trace("setCurrentPosition(name={},index={}) currentPosition {} replaced by {}.", name.toString(),
296                     bridgeProductIndex.toInt(), this.currentPosition, newCurrentPosition);
297             this.currentPosition = newCurrentPosition;
298             return true;
299         }
300     }
301
302     /**
303      * @return <b>target</b> as type int shows the target position of the current operation.
304      */
305     public int getTarget() {
306         return target;
307     }
308
309     /**
310      * @param newTarget Update the target position of the current operation.
311      * @return <b>modified</b> as boolean to signal a real modification.
312      */
313     public boolean setTarget(int newTarget) {
314         if (this.target == newTarget) {
315             return false;
316         } else {
317             logger.trace("setCurrentPosition(name={},index={}) target {} replaced by {}.", name.toString(),
318                     bridgeProductIndex.toInt(), this.target, newTarget);
319             this.target = newTarget;
320             return true;
321         }
322     }
323
324     /**
325      * @return <b>remainingTime</b> as type int describes the intended remaining time of current operation.
326      */
327     public int getRemainingTime() {
328         return remainingTime;
329     }
330
331     /**
332      * @return <b>timeStamp</b> as type int describes the current time.
333      */
334     public int getTimeStamp() {
335         return timeStamp;
336     }
337 }