]> git.basschouten.com Git - openhab-addons.git/blob
c430e9c84c424fc277c7f39bba79c8bc73fa7eb3
[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.lutron.internal.grxprg;
14
15 /**
16  * Configuration class for the Grafik Eye controlled by the PRG interface
17  *
18  * @author Tim Roberts - Initial contribution
19  */
20 public class GrafikEyeConfig {
21     /**
22      * The control unit identifier
23      */
24     private int controlUnit;
25
26     /**
27      * The default fade for the unit
28      */
29     private int fade;
30
31     /**
32      * The zones designated as shades as parsed
33      */
34     private boolean[] shades = new boolean[8];
35
36     /**
37      * A string representing if the shade configuration was invalid. Will be null if valid, not-null if invalid
38      */
39     private String shadeError;
40
41     /**
42      * Polling time (in seconds) to refresh state for the unit.
43      */
44     private int polling;
45
46     /**
47      * Validates the configuration. Ensures the control unit. fade and shadeError are valid.
48      *
49      * @return a non-null text if invalid (explaining why), a null if valid
50      */
51     String validate() {
52         if (controlUnit < 1 || controlUnit > 8) {
53             return "controlUnit must be between 1-8";
54         }
55
56         if (fade < 0 || fade > 3600) {
57             return "fade must be between 0-3600";
58         }
59
60         if (shadeError != null) {
61             return shadeError;
62         }
63         return null;
64     }
65
66     /**
67      * Returns the Control Unit identifier
68      *
69      * @return the control unit identifier
70      */
71     public int getControlUnit() {
72         return controlUnit;
73     }
74
75     /**
76      * Sets the control unit identifier
77      *
78      * @param controlUnit the control unit identifier
79      */
80     public void setControlUnit(int controlUnit) {
81         this.controlUnit = controlUnit;
82     }
83
84     /**
85      * Returns the default fade
86      *
87      * @return the default fade
88      */
89     public int getFade() {
90         return fade;
91     }
92
93     /**
94      * Sets the default fade
95      *
96      * @param fade the default fade
97      */
98     public void setFade(int fade) {
99         this.fade = fade;
100     }
101
102     /**
103      * Helper method to determine if the zone is a shade zone or not. If zone number is invalid, false will be returned.
104      *
105      * @param zone the zone number
106      * @return true if designated as a shade, false otherwise
107      */
108     boolean isShadeZone(int zone) {
109         if (zone >= 1 && zone <= shades.length) {
110             return shades[zone - 1];
111         }
112         return false;
113     }
114
115     /**
116      * Returns a comma formatted list of shade zones
117      *
118      * @returna non-null, non-empty comma delimited list of shade zones
119      */
120     public String getShadeZones() {
121         final StringBuilder sb = new StringBuilder();
122         for (int z = 0; z < shades.length; z++) {
123             if (shades[z]) {
124                 if (sb.length() > 0) {
125                     sb.append(',');
126                 }
127                 sb.append((z + 1));
128             }
129         }
130         return sb.toString();
131     }
132
133     /**
134      * Sets the shade zones from a comma delimited list (ex: "2,3,4")
135      *
136      * @param shadeZones a, possibly null, list of zones
137      */
138     public void setShadeZones(String shadeZones) {
139         shadeError = null;
140
141         for (int zone = 0; zone < 8; zone++) {
142             shades[zone] = false;
143         }
144
145         if (shadeZones != null) {
146             for (String shadeZone : shadeZones.split(",")) {
147                 try {
148                     final int zone = Integer.parseInt(shadeZone);
149                     if (zone >= 1 && zone <= 8) {
150                         shades[zone - 1] = true;
151                     } else {
152                         shadeError = "Shade zone must be between 1-8: " + zone + " - ignoring";
153                     }
154                 } catch (NumberFormatException e) {
155                     shadeError = "Unknown shade zone (can't parse to numeric): " + shadeZone + " - ignoring";
156                 }
157             }
158         }
159     }
160
161     /**
162      * Gets the polling (in seconds) to refresh state
163      *
164      * @return the polling (in seconds) to refresh state
165      */
166     public int getPolling() {
167         return polling;
168     }
169
170     /**
171      * Sets the polling (in seconds) to refresh state
172      *
173      * @param polling the polling (in seconds) to refresh state
174      */
175     public void setPolling(int polling) {
176         this.polling = polling;
177     }
178 }