]> git.basschouten.com Git - openhab-addons.git/blob
bff71c5b19b79b3e7b5abd85d7713d39cd92ff78
[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.heos.internal.json.dto;
14
15 import static org.openhab.binding.heos.internal.json.dto.HeosCommunicationAttribute.*;
16
17 import java.util.Collections;
18 import java.util.List;
19 import java.util.Map;
20 import java.util.Set;
21 import java.util.stream.Collectors;
22
23 import org.eclipse.jdt.annotation.NonNullByDefault;
24 import org.eclipse.jdt.annotation.Nullable;
25 import org.openhab.binding.heos.internal.json.HeosOption;
26
27 /**
28  * Class for HEOS response objects
29  *
30  * @author Martin van Wingerden - Initial contribution
31  */
32 @NonNullByDefault
33 public class HeosResponseObject<T> extends HeosObject {
34     public final @Nullable HeosCommandTuple heosCommand;
35     public final boolean result;
36     public final @Nullable T payload;
37     public final Map<String, HeosOption> options;
38
39     public HeosResponseObject(@Nullable HeosCommandTuple heosCommand, String rawCommand, @Nullable String result,
40             Map<String, String> attributes, @Nullable T payload,
41             @Nullable List<Map<String, List<HeosOption>>> options) {
42         super(rawCommand, attributes);
43         this.heosCommand = heosCommand;
44         this.result = "success".equals(result);
45         this.payload = payload;
46         this.options = processOptions(options);
47     }
48
49     private Map<String, HeosOption> processOptions(@Nullable List<Map<String, List<HeosOption>>> options) {
50         if (options == null) {
51             return Collections.emptyMap();
52         }
53
54         return options.stream().map(Map::entrySet).flatMap(Set::stream)
55                 .collect(Collectors.toMap(Map.Entry::getKey, e -> e.getValue().get(0)));
56     }
57
58     public boolean isFinished() {
59         return (result || hasAttribute(ERROR_ID)) && !hasAttribute(COMMAND_UNDER_PROCESS);
60     }
61
62     public @Nullable HeosError getError() {
63         if (result || !hasAttribute(ERROR_ID)) {
64             return null;
65         }
66
67         return new HeosError(getNumericAttribute(ERROR_ID), getNumericAttribute(SYSTEM_ERROR_NUMBER));
68     }
69 }