]> git.basschouten.com Git - openhab-addons.git/blob
e75e2a430cd90dd31f81b48e15242a81b882ffe0
[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.velux.internal.things;
14
15 import java.util.List;
16 import java.util.Map;
17 import java.util.function.Function;
18 import java.util.stream.Collectors;
19 import java.util.stream.Stream;
20
21 import org.eclipse.jdt.annotation.NonNullByDefault;
22
23 /**
24  * An enum that describes the various predefined Status Reply code values and their meanings.
25  *
26  * @author Andrew Fiddian-Green - Initial contribution
27  *
28  */
29 @NonNullByDefault
30 public enum StatusReply {
31     UNKNOWN_STATUS_REPLY(0x00),
32     COMMAND_COMPLETED_OK(0x01),
33     NO_CONTACT(0x02),
34     MANUALLY_OPERATED(0x03),
35     BLOCKED(0x04),
36     WRONG_SYSTEMKEY(0x05),
37     PRIORITY_LEVEL_LOCKED(0x06),
38     REACHED_WRONG_POSITION(0x07),
39     ERROR_DURING_EXECUTION(0x08),
40     NO_EXECUTION(0x09),
41     CALIBRATING(0x0A),
42     POWER_CONSUMPTION_TOO_HIGH(0x0B),
43     POWER_CONSUMPTION_TOO_LOW(0x0C),
44     LOCK_POSITION_OPEN(0x0D),
45     MOTION_TIME_TOO_LONG(0x0E),
46     THERMAL_PROTECTION(0x0F),
47     PRODUCT_NOT_OPERATIONAL(0x10),
48     FILTER_MAINTENANCE_NEEDED(0x11),
49     BATTERY_LEVEL(0x12),
50     TARGET_MODIFIED(0x13),
51     MODE_NOT_IMPLEMENTED(0x14),
52     COMMAND_INCOMPATIBLE_TO_MOVEMENT(0x15),
53     USER_ACTION(0x16),
54     DEAD_BOLT_ERROR(0x17),
55     AUTOMATIC_CYCLE_ENGAGED(0x18),
56     WRONG_LOAD_CONNECTED(0x19),
57     COLOUR_NOT_REACHABLE(0x1A),
58     TARGET_NOT_REACHABLE(0x1B),
59     BAD_INDEX_RECEIVED(0x1C),
60     COMMAND_OVERRULED(0x1D),
61     NODE_WAITING_FOR_POWER(0x1E),
62     INFORMATION_CODE(0xDF),
63     PARAMETER_LIMITED(0xE0),
64     LIMITATION_BY_LOCAL_USER(0xE1),
65     LIMITATION_BY_USER(0xE2),
66     LIMITATION_BY_RAIN(0xE3),
67     LIMITATION_BY_TIMER(0xE4),
68     LIMITATION_BY_UPS(0xE6),
69     LIMITATION_BY_UNKNOWN_DEVICE(0xE7),
70     LIMITATION_BY_SAAC(0xEA),
71     LIMITATION_BY_WIND(0xEB),
72     LIMITATION_BY_MYSELF(0xEC),
73     LIMITATION_BY_AUTOMATIC_CYCLE(0xED),
74     LIMITATION_BY_EMERGENCY(0xEE);
75
76     private final int code;
77
78     private StatusReply(int code) {
79         this.code = code;
80     }
81
82     /*
83      * List of critical errors
84      */
85     private static final List<StatusReply> CRITICAL_ERRORS = List.of(BLOCKED, POWER_CONSUMPTION_TOO_HIGH,
86             THERMAL_PROTECTION, LOCK_POSITION_OPEN, PRODUCT_NOT_OPERATIONAL, DEAD_BOLT_ERROR, FILTER_MAINTENANCE_NEEDED,
87             BATTERY_LEVEL, NODE_WAITING_FOR_POWER);
88
89     public int getCode() {
90         return code;
91     }
92
93     private static final Map<Integer, StatusReply> LOOKUP = Stream.of(StatusReply.values())
94             .collect(Collectors.toMap(StatusReply::getCode, Function.identity()));
95
96     /**
97      * Get the StatusReply value that corresponds to the given status code.
98      *
99      * @param statusReplyCode the status code value
100      * @return the StatusReply value that corresponds to the status code
101      */
102     public static StatusReply fromCode(int statusReplyCode) {
103         return LOOKUP.getOrDefault(statusReplyCode, UNKNOWN_STATUS_REPLY);
104     }
105
106     /**
107      * Check if this Status Reply indicates an error.
108      *
109      * @return true if the status code is an error code.
110      */
111     public boolean isError() {
112         return this != COMMAND_COMPLETED_OK;
113     }
114
115     /**
116      * Check if this Status Reply indicates a critical error.
117      *
118      * @return true if the status code is a critical error code.
119      */
120     public boolean isCriticalError() {
121         return isError() && CRITICAL_ERRORS.contains(this);
122     }
123 }