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