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(23).capabilities(1).text("Silhouette"),
68 new Type(42).capabilities(0).text("M25T Roller Blind"),
69 new Type(43).capabilities(1).text("Facette"),
70 new Type(44).capabilities(0).text("Twist"),
71 new Type(47).capabilities(7).text("Pleated Top Down Bottom Up"),
72 new Type(49).capabilities(0).text("AC Roller"),
73 new Type(51).capabilities(2).text("Venetian"),
74 new Type(54).capabilities(3).text("Vertical Slats Left Stack"),
75 new Type(55).capabilities(3).text("Vertical Slats Right Stack"),
76 new Type(56).capabilities(3).text("Vertical Slats Split Stack"),
77 new Type(62).capabilities(2).text("Venetian"),
78 new Type(69).capabilities(3).text("Curtain Left Stack"),
79 new Type(70).capabilities(3).text("Curtain Right Stack"),
80 new Type(71).capabilities(3).text("Curtain Split Stack"),
81 new Type(79).capabilities(8).text("Duolite Lift"),
83 new Type()).stream().collect(Collectors.toMap(Type::getValue, Function.identity()));
86 * Base class that is extended by Type and Capabilities classes.
88 * @author Andrew Fiddian-Green - Initial Contribution
90 private static class Base {
91 protected int intValue = -1;
92 protected String text = "-- not in database --";
94 protected Integer getValue() {
99 public String toString() {
100 return String.format("%s (%d)", text, intValue);
105 * Describes a shade type entry in the database; implements 'capabilities' parameter.
107 * @author Andrew Fiddian-Green - Initial Contribution
109 public static class Type extends Base {
110 private int capabilities = -1;
115 protected Type(int type) {
119 protected Type text(String text) {
124 protected Type capabilities(int capabilities) {
125 this.capabilities = capabilities;
130 * Get shade types's 'capabilities'.
132 * @return 'capabilities'.
134 public int getCapabilities() {
140 * Describes a shade 'capabilities' entry in the database; adds properties indicating its supported functionality.
142 * @author Andrew Fiddian-Green - Initial Contribution
144 public static class Capabilities extends Base {
145 private boolean supportsPrimary;
146 private boolean supportsSecondary;
147 private boolean supportsTiltOnClosed;
148 private boolean supportsTiltAnywhere;
149 private boolean primaryStateInverted;
150 private boolean tilt180Degrees;
152 public Capabilities() {
155 protected Capabilities(int capabilities) {
156 intValue = capabilities;
159 protected Capabilities text(String text) {
164 protected Capabilities primary() {
165 supportsPrimary = true;
169 protected Capabilities tiltOnClosed() {
170 supportsTiltOnClosed = true;
174 protected Capabilities secondary() {
175 supportsSecondary = true;
179 protected Capabilities tiltAnywhere() {
180 supportsTiltAnywhere = true;
184 protected Capabilities primaryStateInverted() {
185 primaryStateInverted = true;
189 protected Capabilities tilt180() {
190 tilt180Degrees = true;
195 * Check if the Capabilities class instance supports a primary shade.
197 * @return true if it supports a primary shade.
199 public boolean supportsPrimary() {
200 return supportsPrimary;
204 * Check if the Capabilities class instance supports a vane/tilt function (by means of a second motor).
206 * @return true if it supports a vane/tilt function (by means of a second motor).
208 public boolean supportsTiltAnywhere() {
209 return supportsTiltAnywhere;
213 * Check if the Capabilities class instance supports a secondary shade.
215 * @return true if it supports a secondary shade.
217 public boolean supportsSecondary() {
218 return supportsSecondary;
222 * Check if the Capabilities class instance supports a secondary shade.
224 * @return true if the primary shade is inverted.
226 public boolean isPrimaryStateInverted() {
227 return primaryStateInverted;
231 * Check if the Capabilities class instance supports 'tilt when closed'.
233 * Note: Simple bottom up or vertical shades that do not have independent vane controls, can be tilted in a
234 * simple way, only when they are fully closed, by moving the shade motor a bit further.
236 * @return true if the primary shade is inverted.
238 public boolean supportsTiltOnClosed() {
239 return supportsTiltOnClosed && !supportsTiltAnywhere;
243 * Check if the Capabilities class instance supports 180 degrees tilt.
245 * @return true if the primary shade supports 180 degrees.
247 public boolean supportsTilt180() {
248 return tilt180Degrees;
253 * Determines if a given shade 'type' is in the database.
255 * @param type the shade 'type' parameter.
256 * @return true if the shade 'type' is known.
258 public boolean isTypeInDatabase(int type) {
259 return TYPE_DATABASE.containsKey(type);
263 * Determines if a given 'capabilities' value is in the database.
265 * @param capabilities the shade 'capabilities' parameter
266 * @return true if the 'capabilities' value is known
268 public boolean isCapabilitiesInDatabase(int capabilities) {
269 return CAPABILITIES_DATABASE.containsKey(capabilities);
273 * Return a Type class instance that corresponds to the given 'type' parameter.
275 * @param type the shade 'type' parameter.
276 * @return corresponding instance of Type class.
278 public Type getType(int type) {
279 return TYPE_DATABASE.getOrDefault(type, new Type());
283 * Return a Capabilities class instance that corresponds to the given 'capabilities' parameter.
285 * @param capabilities the shade 'capabilities' parameter.
286 * @return corresponding instance of Capabilities class.
288 public Capabilities getCapabilities(int capabilities) {
289 return CAPABILITIES_DATABASE.getOrDefault(capabilities, new Capabilities());
292 private static final String REQUEST_DEVELOPERS_TO_UPDATE = " => Please request developers to update the database!";
295 * Log a message indicating that 'type' is not in database.
299 public void logTypeNotInDatabase(int type) {
300 logger.warn("The shade 'type:{}' is not in the database!{}", type, REQUEST_DEVELOPERS_TO_UPDATE);
304 * Log a message indicating that 'capabilities' is not in database.
306 * @param capabilities
308 public void logCapabilitiesNotInDatabase(int type, int capabilities) {
309 logger.warn("The 'capabilities:{}' for shade 'type:{}' are not in the database!{}", capabilities, type,
310 REQUEST_DEVELOPERS_TO_UPDATE);
314 * Log a message indicating the type's capabilities and the passed capabilities are not equal.
317 * @param capabilities
319 public void logCapabilitiesMismatch(int type, int capabilities) {
320 logger.warn("The 'capabilities:{}' reported by shade 'type:{}' don't match the database!{}", capabilities, type,
321 REQUEST_DEVELOPERS_TO_UPDATE);
325 * Log a message indicating that a shade's secondary/vanes support, as observed via its actual JSON payload, does
326 * not match the expected value as declared in its 'type' and 'capabilities'.
330 * @param capabilities
331 * @param propertyValue
333 public void logPropertyMismatch(String propertyKey, int type, int capabilities, boolean propertyValue) {
335 "The '{}:{}' property actually reported by shade 'type:{}' is different "
336 + "than expected from its 'capabilities:{}' in the database!{}",
337 propertyKey, propertyValue, type, capabilities, REQUEST_DEVELOPERS_TO_UPDATE);