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.boschshc.internal.services.dto;
15 import org.eclipse.jdt.annotation.Nullable;
16 import org.openhab.binding.boschshc.internal.serialization.GsonUtils;
17 import org.slf4j.Logger;
18 import org.slf4j.LoggerFactory;
20 import com.google.gson.JsonElement;
21 import com.google.gson.annotations.SerializedName;
24 * Base Bosch Smart Home Controller service state.
26 * @author Christian Oeing - Initial contribution
28 public class BoschSHCServiceState {
30 private final Logger logger = LoggerFactory.getLogger(getClass());
33 * State type. Initialized when instance is created.
35 private @Nullable String stateType = null;
37 @SerializedName("@type")
38 public final String type;
40 protected BoschSHCServiceState(String type) {
43 if (stateType == null) {
48 public String getType() {
52 protected boolean isValid() {
53 String expectedType = stateType;
54 if (expectedType == null || !expectedType.equals(this.type)) {
55 var className = this.getClass().getName();
56 logger.debug("Expected state type {} for state class {}, received {}", expectedType, className, this.type);
63 public static <TState extends BoschSHCServiceState> @Nullable TState fromJson(String json,
64 Class<TState> stateClass) {
65 var state = GsonUtils.DEFAULT_GSON_INSTANCE.fromJson(json, stateClass);
66 if (state == null || !state.isValid()) {
73 public static <TState extends BoschSHCServiceState> @Nullable TState fromJson(JsonElement json,
74 Class<TState> stateClass) {
75 var state = GsonUtils.DEFAULT_GSON_INSTANCE.fromJson(json, stateClass);
76 if (state == null || !state.isValid()) {