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.slf4j.Logger;
17 import org.slf4j.LoggerFactory;
19 import com.google.gson.Gson;
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 {
31 * gson instance to convert a class to json string and back.
33 private static final Gson gson = new Gson();
35 private static final Logger logger = LoggerFactory.getLogger(BoschSHCServiceState.class);
38 * State type. Initialized when instance is created.
40 private @Nullable String stateType = null;
42 @SerializedName("@type")
43 public final String type;
45 protected BoschSHCServiceState(String type) {
48 if (stateType == null) {
53 public String getType() {
57 protected boolean isValid() {
58 String expectedType = stateType;
59 if (expectedType == null || !expectedType.equals(this.type)) {
60 var className = this.getClass().getName();
61 logger.debug("Expected state type {} for state class {}, received {}", expectedType, className, this.type);
68 public static <TState extends BoschSHCServiceState> @Nullable TState fromJson(String json,
69 Class<TState> stateClass) {
70 var state = gson.fromJson(json, stateClass);
71 if (state == null || !state.isValid()) {
78 public static <TState extends BoschSHCServiceState> @Nullable TState fromJson(JsonElement json,
79 Class<TState> stateClass) {
80 var state = gson.fromJson(json, stateClass);
81 if (state == null || !state.isValid()) {