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();
36 * Logger marked as transient to exclude the logger from JSON serialization.
38 private final transient Logger logger = LoggerFactory.getLogger(BoschSHCServiceState.class);
41 * State type. Initialized when instance is created.
43 private @Nullable String stateType = null;
45 @SerializedName("@type")
46 public final String type;
48 protected BoschSHCServiceState(String type) {
51 if (stateType == null) {
56 public String getType() {
60 protected boolean isValid() {
61 String expectedType = stateType;
62 if (expectedType == null || !expectedType.equals(this.type)) {
63 var className = this.getClass().getName();
64 logger.debug("Expected state type {} for state class {}, received {}", expectedType, className, this.type);
71 public static <TState extends BoschSHCServiceState> @Nullable TState fromJson(String json,
72 Class<TState> stateClass) {
73 var state = GSON.fromJson(json, stateClass);
74 if (state == null || !state.isValid()) {
81 public static <TState extends BoschSHCServiceState> @Nullable TState fromJson(JsonElement json,
82 Class<TState> stateClass) {
83 var state = GSON.fromJson(json, stateClass);
84 if (state == null || !state.isValid()) {