2 * Copyright (c) 2010-2022 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.hdpowerview.internal.database;
15 import java.util.Arrays;
17 import java.util.function.Function;
18 import java.util.stream.Collectors;
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
25 * Class containing the database of all known shade 'types' and their respective 'capabilities'.
27 * If user systems detect shade types that are not in the database, then this class can issue logger warning messages
28 * indicating such absence, and prompting the user to report it to developers so that the database and the respective
29 * binding functionality can (hopefully) be extended over time.
31 * @author Andrew Fiddian-Green - Initial Contribution
34 public class ShadeCapabilitiesDatabase {
36 private final Logger logger = LoggerFactory.getLogger(ShadeCapabilitiesDatabase.class);
39 * Database of known shade capabilities.
41 private static final Map<Integer, Capabilities> CAPABILITIES_DATABASE = Arrays.asList(
43 new Capabilities(0).primary().tiltOnClosed() .text("Bottom Up"),
44 new Capabilities(1).primary().tiltAnywhere() .text("Bottom Up Tilt 90°"),
45 new Capabilities(2).primary().tiltAnywhere().tilt180() .text("Bottom Up Tilt 180°"),
46 new Capabilities(3).primary().tiltOnClosed() .text("Vertical"),
47 new Capabilities(4).primary().tiltAnywhere().tilt180() .text("Vertical Tilt 180°"),
48 new Capabilities(5) .tiltAnywhere().tilt180() .text("Tilt Only 180°"),
49 new Capabilities(6).primary() .text("Top Down") .primaryStateInverted(),
50 new Capabilities(7).primary() .secondary().text("Top Down Bottom Up"),
51 new Capabilities(8).primary() .text("Duolite Lift"),
52 new Capabilities(9).primary().tiltAnywhere() .text("Duolite Lift and Tilt 90°"),
54 new Capabilities()).stream().collect(Collectors.toMap(Capabilities::getValue, Function.identity()));
57 * Database of known shade types and corresponding capabilities.
59 private static final Map<Integer, Type> TYPE_DATABASE = Arrays.asList(
61 new Type( 4).capabilities(0).text("Roman"),
62 new Type( 5).capabilities(0).text("Bottom Up"),
63 new Type( 6).capabilities(0).text("Duette"),
64 new Type( 7).capabilities(6).text("Top Down"),
65 new Type( 8).capabilities(7).text("Duette Top Down Bottom Up"),
66 new Type( 9).capabilities(7).text("Duette DuoLite Top Down Bottom Up"),
67 new Type(18).capabilities(1).text("Silhouette"),
68 new Type(23).capabilities(1).text("Silhouette"),
69 new Type(42).capabilities(0).text("M25T Roller Blind"),
70 new Type(43).capabilities(1).text("Facette"),
71 new Type(44).capabilities(0).text("Twist"),
72 new Type(47).capabilities(7).text("Pleated Top Down Bottom Up"),
73 new Type(49).capabilities(0).text("AC Roller"),
74 new Type(51).capabilities(2).text("Venetian"),
75 new Type(54).capabilities(3).text("Vertical Slats Left Stack"),
76 new Type(55).capabilities(3).text("Vertical Slats Right Stack"),
77 new Type(56).capabilities(3).text("Vertical Slats Split Stack"),
78 new Type(62).capabilities(2).text("Venetian"),
79 new Type(65).capabilities(8).text("Vignette Duolite"),
80 new Type(69).capabilities(3).text("Curtain Left Stack"),
81 new Type(70).capabilities(3).text("Curtain Right Stack"),
82 new Type(71).capabilities(3).text("Curtain Split Stack"),
83 new Type(79).capabilities(8).text("Duolite Lift"),
85 new Type()).stream().collect(Collectors.toMap(Type::getValue, Function.identity()));
88 * Base class that is extended by Type and Capabilities classes.
90 * @author Andrew Fiddian-Green - Initial Contribution
92 private static class Base {
93 protected int intValue = -1;
94 protected String text = "-- not in database --";
96 public Integer getValue() {
101 public String toString() {
102 return String.format("%s (%d)", text, intValue);
107 * Describes a shade type entry in the database; implements 'capabilities' parameter.
109 * @author Andrew Fiddian-Green - Initial Contribution
111 public static class Type extends Base {
112 private int capabilities = -1;
117 protected Type(int type) {
121 protected Type text(String text) {
126 protected Type capabilities(int capabilities) {
127 this.capabilities = capabilities;
132 * Get shade types's 'capabilities'.
134 * @return 'capabilities'.
136 public int getCapabilities() {
142 * Describes a shade 'capabilities' entry in the database; adds properties indicating its supported functionality.
144 * @author Andrew Fiddian-Green - Initial Contribution
146 public static class Capabilities extends Base {
147 private boolean supportsPrimary;
148 private boolean supportsSecondary;
149 private boolean supportsTiltOnClosed;
150 private boolean supportsTiltAnywhere;
151 private boolean primaryStateInverted;
152 private boolean tilt180Degrees;
154 public Capabilities() {
157 protected Capabilities(int capabilities) {
158 intValue = capabilities;
161 protected Capabilities text(String text) {
166 protected Capabilities primary() {
167 supportsPrimary = true;
171 protected Capabilities tiltOnClosed() {
172 supportsTiltOnClosed = true;
176 protected Capabilities secondary() {
177 supportsSecondary = true;
181 protected Capabilities tiltAnywhere() {
182 supportsTiltAnywhere = true;
186 protected Capabilities primaryStateInverted() {
187 primaryStateInverted = true;
191 protected Capabilities tilt180() {
192 tilt180Degrees = true;
197 * Check if the Capabilities class instance supports a primary shade.
199 * @return true if it supports a primary shade.
201 public boolean supportsPrimary() {
202 return supportsPrimary;
206 * Check if the Capabilities class instance supports a vane/tilt function (by means of a second motor).
208 * @return true if it supports a vane/tilt function (by means of a second motor).
210 public boolean supportsTiltAnywhere() {
211 return supportsTiltAnywhere;
215 * Check if the Capabilities class instance supports a secondary shade.
217 * @return true if it supports a secondary shade.
219 public boolean supportsSecondary() {
220 return supportsSecondary;
224 * Check if the Capabilities class instance supports a secondary shade.
226 * @return true if the primary shade is inverted.
228 public boolean isPrimaryStateInverted() {
229 return primaryStateInverted;
233 * Check if the Capabilities class instance supports 'tilt when closed'.
235 * Note: Simple bottom up or vertical shades that do not have independent vane controls, can be tilted in a
236 * simple way, only when they are fully closed, by moving the shade motor a bit further.
238 * @return true if the primary shade is inverted.
240 public boolean supportsTiltOnClosed() {
241 return supportsTiltOnClosed && !supportsTiltAnywhere;
245 * Check if the Capabilities class instance supports 180 degrees tilt.
247 * @return true if the primary shade supports 180 degrees.
249 public boolean supportsTilt180() {
250 return tilt180Degrees;
255 * Determines if a given shade 'type' is in the database.
257 * @param type the shade 'type' parameter.
258 * @return true if the shade 'type' is known.
260 public boolean isTypeInDatabase(int type) {
261 return TYPE_DATABASE.containsKey(type);
265 * Determines if a given 'capabilities' value is in the database.
267 * @param capabilities the shade 'capabilities' parameter
268 * @return true if the 'capabilities' value is known
270 public boolean isCapabilitiesInDatabase(int capabilities) {
271 return CAPABILITIES_DATABASE.containsKey(capabilities);
275 * Return a Type class instance that corresponds to the given 'type' parameter.
277 * @param type the shade 'type' parameter.
278 * @return corresponding instance of Type class.
280 public Type getType(int type) {
281 return TYPE_DATABASE.getOrDefault(type, new Type());
285 * Return a Capabilities class instance that corresponds to the given 'capabilities' parameter.
287 * @param capabilities the shade 'capabilities' parameter.
288 * @return corresponding instance of Capabilities class.
290 public Capabilities getCapabilities(int capabilities) {
291 return CAPABILITIES_DATABASE.getOrDefault(capabilities, new Capabilities());
294 private static final String REQUEST_DEVELOPERS_TO_UPDATE = " => Please request developers to update the database!";
297 * Log a message indicating that 'type' is not in database.
301 public void logTypeNotInDatabase(int type) {
302 logger.warn("The shade 'type:{}' is not in the database!{}", type, REQUEST_DEVELOPERS_TO_UPDATE);
306 * Log a message indicating that 'capabilities' is not in database.
308 * @param capabilities
310 public void logCapabilitiesNotInDatabase(int type, int capabilities) {
311 logger.warn("The 'capabilities:{}' for shade 'type:{}' are not in the database!{}", capabilities, type,
312 REQUEST_DEVELOPERS_TO_UPDATE);
316 * Log a message indicating the type's capabilities and the passed capabilities are not equal.
319 * @param capabilities
321 public void logCapabilitiesMismatch(int type, int capabilities) {
322 logger.warn("The 'capabilities:{}' reported by shade 'type:{}' don't match the database!{}", capabilities, type,
323 REQUEST_DEVELOPERS_TO_UPDATE);
327 * Log a message indicating that a shade's secondary/vanes support, as observed via its actual JSON payload, does
328 * not match the expected value as declared in its 'type' and 'capabilities'.
332 * @param capabilities
333 * @param propertyValue
335 public void logPropertyMismatch(String propertyKey, int type, int capabilities, boolean propertyValue) {
337 "The '{}:{}' property actually reported by shade 'type:{}' is different "
338 + "than expected from its 'capabilities:{}' in the database!{}",
339 propertyKey, propertyValue, type, capabilities, REQUEST_DEVELOPERS_TO_UPDATE);