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.heos.internal.json.dto;
15 import static org.openhab.binding.heos.internal.json.dto.HeosCommunicationAttribute.*;
17 import java.util.Collections;
18 import java.util.List;
21 import java.util.stream.Collectors;
23 import org.eclipse.jdt.annotation.NonNullByDefault;
24 import org.eclipse.jdt.annotation.Nullable;
25 import org.openhab.binding.heos.internal.json.HeosOption;
28 * Class for HEOS response objects
30 * @author Martin van Wingerden - Initial contribution
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;
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);
49 private Map<String, HeosOption> processOptions(@Nullable List<Map<String, List<HeosOption>>> options) {
50 if (options == null) {
51 return Collections.emptyMap();
54 return options.stream().map(Map::entrySet).flatMap(Set::stream)
55 .collect(Collectors.toMap(Map.Entry::getKey, e -> e.getValue().get(0)));
58 public boolean isFinished() {
59 return (result || hasAttribute(ERROR_ID)) && !hasAttribute(COMMAND_UNDER_PROCESS);
62 public @Nullable HeosError getError() {
63 if (result || !hasAttribute(ERROR_ID)) {
67 return new HeosError(getNumericAttribute(ERROR_ID), getNumericAttribute(SYSTEM_ERROR_NUMBER));