]> git.basschouten.com Git - openhab-addons.git/blob
ebd169362d2de6062d0769097bd87b37513b5341
[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.pjlinkdevice.internal.device.command.errorstatus;
14
15 import java.util.Arrays;
16 import java.util.HashMap;
17 import java.util.HashSet;
18 import java.util.Map;
19
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.openhab.binding.pjlinkdevice.internal.device.command.ErrorCode;
22 import org.openhab.binding.pjlinkdevice.internal.device.command.PrefixedResponse;
23 import org.openhab.binding.pjlinkdevice.internal.device.command.ResponseException;
24
25 /**
26  * The response part of {@link ErrorStatusQueryCommand}
27  *
28  * @author Nils Schnabel - Initial contribution
29  */
30 @NonNullByDefault
31 public class ErrorStatusQueryResponse extends
32         PrefixedResponse<Map<ErrorStatusQueryResponse.ErrorStatusDevicePart, ErrorStatusQueryResponse.ErrorStatusQueryResponseState>> {
33
34     public enum ErrorStatusQueryResponseState {
35         OK_UNKOWN("OK/no failure detection", "0"),
36         WARNING("Warning", "1"),
37         ERROR("Error", "2");
38
39         private String text;
40         private String code;
41
42         private ErrorStatusQueryResponseState(String text, String code) {
43             this.text = text;
44             this.code = code;
45         }
46
47         public String getText() {
48             return this.text;
49         }
50
51         public static ErrorStatusQueryResponseState parseString(String code) throws ResponseException {
52             for (ErrorStatusQueryResponseState result : ErrorStatusQueryResponseState.values()) {
53                 if (result.code.equals(code)) {
54                     return result;
55                 }
56             }
57
58             throw new ResponseException("Cannot understand error status: " + code);
59         }
60     }
61
62     public enum ErrorStatusDevicePart {
63         FAN("Fan error", "FanError", 0),
64         LAMP("Lamp error", "LampError", 1),
65         TEMPERATURE("Temperature error", "TemperatureError", 2),
66         COVER_OPEN("Cover open error", "CoverOpenError", 3),
67         FILTER("Filter error", "FilterError", 4),
68         OTHER("Other errors", "OtherErrors", 5);
69
70         private String text;
71         private String camelCaseText;
72         private int positionInResponse;
73
74         private ErrorStatusDevicePart(String text, String camelCaseText, int positionInResponse) {
75             this.text = text;
76             this.camelCaseText = camelCaseText;
77             this.positionInResponse = positionInResponse;
78         }
79
80         public String getText() {
81             return this.text;
82         }
83
84         public String getCamelCaseText() {
85             return this.camelCaseText;
86         }
87
88         public static ErrorStatusDevicePart getDevicePartByResponsePosition(int pos) {
89             for (ErrorStatusDevicePart result : ErrorStatusDevicePart.values()) {
90                 if (result.positionInResponse == pos) {
91                     return result;
92                 }
93             }
94
95             return OTHER;
96         }
97     }
98
99     private static final HashSet<ErrorCode> SPECIFIED_ERRORCODES = new HashSet<>(
100             Arrays.asList(ErrorCode.UNAVAILABLE_TIME, ErrorCode.DEVICE_FAILURE));
101
102     public ErrorStatusQueryResponse(String response) throws ResponseException {
103         super("ERST=", SPECIFIED_ERRORCODES, response);
104     }
105
106     @Override
107     protected Map<ErrorStatusDevicePart, ErrorStatusQueryResponseState> parseResponseWithoutPrefix(
108             String responseWithoutPrefix) throws ResponseException {
109         Map<ErrorStatusDevicePart, ErrorStatusQueryResponseState> result = new HashMap<>();
110         for (int i = 0; i < ErrorStatusDevicePart.values().length; i++) {
111             result.put(ErrorStatusDevicePart.getDevicePartByResponsePosition(i),
112                     ErrorStatusQueryResponseState.parseString(responseWithoutPrefix.substring(i, i + 1)));
113         }
114         return result;
115     }
116 }