]> git.basschouten.com Git - openhab-addons.git/blob
685136253da0ca2f0c53eb24aad5cdd220a66b00
[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.km200.internal.handler;
14
15 import java.util.ArrayList;
16 import java.util.HashMap;
17 import java.util.List;
18 import java.util.Map;
19 import java.util.Set;
20
21 import org.eclipse.jdt.annotation.NonNullByDefault;
22 import org.eclipse.jdt.annotation.Nullable;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25
26 import com.google.gson.JsonArray;
27 import com.google.gson.JsonElement;
28 import com.google.gson.JsonObject;
29
30 /**
31  * The KM200ErrorService representing an error service with its all capabilities
32  *
33  * @author Markus Eckhardt - Initial contribution
34  */
35 @NonNullByDefault
36 public class KM200ErrorServiceHandler {
37
38     private final Logger logger = LoggerFactory.getLogger(KM200ErrorServiceHandler.class);
39
40     private Integer activeError = 1;
41
42     /* List for all errors */
43     private final List<Map<String, String>> errorMap;
44
45     public KM200ErrorServiceHandler() {
46         errorMap = new ArrayList<>();
47     }
48
49     /**
50      * This function removes all errors from the list
51      */
52     void removeAllErrors() {
53         synchronized (errorMap) {
54             errorMap.clear();
55         }
56     }
57
58     /**
59      * This function updates the errors
60      */
61     public void updateErrors(JsonObject nodeRoot) {
62         synchronized (errorMap) {
63             /* Update the list of errors */
64             removeAllErrors();
65             JsonArray sPoints = nodeRoot.get("values").getAsJsonArray();
66             for (int i = 0; i < sPoints.size(); i++) {
67                 JsonObject subJSON = sPoints.get(i).getAsJsonObject();
68                 Map<String, String> valMap = new HashMap<>();
69                 Set<Map.Entry<String, JsonElement>> oMap = subJSON.entrySet();
70                 oMap.forEach(item -> {
71                     logger.trace("Set: {} val: {}", item.getKey(), item.getValue().getAsString());
72                     valMap.put(item.getKey(), item.getValue().getAsString());
73                 });
74                 errorMap.add(valMap);
75             }
76         }
77     }
78
79     /**
80      * This function returns the number of errors
81      */
82     public int getNbrErrors() {
83         synchronized (errorMap) {
84             return errorMap.size();
85         }
86     }
87
88     /**
89      * This function sets the actual errors
90      */
91     public void setActiveError(int error) {
92         int actError;
93         if (error < 1) {
94             actError = 1;
95         } else if (error > getNbrErrors()) {
96             actError = getNbrErrors();
97         } else {
98             actError = error;
99         }
100         synchronized (activeError) {
101             activeError = actError;
102         }
103     }
104
105     /**
106      * This function returns the selected error
107      */
108     public int getActiveError() {
109         synchronized (activeError) {
110             return activeError;
111         }
112     }
113
114     /**
115      * This function returns an error string with all parameters
116      */
117     public @Nullable String getErrorString() {
118         String value = "";
119         synchronized (errorMap) {
120             int actN = getActiveError();
121             if (errorMap.size() < actN || errorMap.isEmpty()) {
122                 return null;
123             }
124             /* is the time value existing ("t") then use it on the begin */
125             if (errorMap.get(actN - 1).containsKey("t")) {
126                 value = errorMap.get(actN - 1).get("t");
127                 for (String para : errorMap.get(actN - 1).keySet()) {
128                     if (!"t".equals(para)) {
129                         value += " " + para + ":" + errorMap.get(actN - 1).get(para);
130                     }
131                 }
132             } else {
133                 for (String para : errorMap.get(actN - 1).keySet()) {
134                     value += para + ":" + errorMap.get(actN - 1).get(para) + " ";
135                 }
136             }
137             return value;
138         }
139     }
140 }