2 * Copyright (c) 2010-2023 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.km200.internal.handler;
15 import java.util.ArrayList;
16 import java.util.HashMap;
17 import java.util.List;
21 import org.eclipse.jdt.annotation.NonNullByDefault;
22 import org.eclipse.jdt.annotation.Nullable;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
26 import com.google.gson.JsonArray;
27 import com.google.gson.JsonElement;
28 import com.google.gson.JsonObject;
31 * The KM200ErrorService representing an error service with its all capabilities
33 * @author Markus Eckhardt - Initial contribution
36 public class KM200ErrorServiceHandler {
38 private final Logger logger = LoggerFactory.getLogger(KM200ErrorServiceHandler.class);
40 private Integer activeError = 1;
42 /* List for all errors */
43 private final List<Map<String, String>> errorMap;
45 public KM200ErrorServiceHandler() {
46 errorMap = new ArrayList<>();
50 * This function removes all errors from the list
52 void removeAllErrors() {
53 synchronized (errorMap) {
59 * This function updates the errors
61 public void updateErrors(JsonObject nodeRoot) {
62 synchronized (errorMap) {
63 /* Update the list of errors */
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());
80 * This function returns the number of errors
82 public int getNbrErrors() {
83 synchronized (errorMap) {
84 return errorMap.size();
89 * This function sets the actual errors
91 public void setActiveError(int error) {
95 } else if (error > getNbrErrors()) {
96 actError = getNbrErrors();
100 synchronized (activeError) {
101 activeError = actError;
106 * This function returns the selected error
108 public int getActiveError() {
109 synchronized (activeError) {
115 * This function returns an error string with all parameters
117 public @Nullable String getErrorString() {
119 synchronized (errorMap) {
120 int actN = getActiveError();
121 if (errorMap.size() < actN || errorMap.isEmpty()) {
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);
133 for (String para : errorMap.get(actN - 1).keySet()) {
134 value += para + ":" + errorMap.get(actN - 1).get(para) + " ";