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.amazonechocontrol.internal.channelhandler;
15 import java.io.IOException;
16 import java.net.URISyntaxException;
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19 import org.eclipse.jdt.annotation.Nullable;
20 import org.openhab.binding.amazonechocontrol.internal.Connection;
21 import org.openhab.binding.amazonechocontrol.internal.jsons.JsonDevices.Device;
22 import org.openhab.core.types.Command;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
26 import com.google.gson.Gson;
27 import com.google.gson.JsonSyntaxException;
30 * The {@link ChannelHandler} is the base class for all channel handlers
32 * @author Michael Geramb - Initial contribution
35 public abstract class ChannelHandler {
37 public abstract boolean tryHandleCommand(Device device, Connection connection, String channelId, Command command)
38 throws IOException, URISyntaxException, InterruptedException;
40 protected final IAmazonThingHandler thingHandler;
41 protected final Gson gson;
42 private final Logger logger;
44 protected ChannelHandler(IAmazonThingHandler thingHandler, Gson gson) {
45 this.logger = LoggerFactory.getLogger(this.getClass());
46 this.thingHandler = thingHandler;
50 protected <T> @Nullable T tryParseJson(String json, Class<T> type) {
52 return gson.fromJson(json, type);
53 } catch (JsonSyntaxException e) {
54 logger.debug("Json parse error", e);
59 protected <T> @Nullable T parseJson(String json, Class<T> type) throws JsonSyntaxException {
60 return gson.fromJson(json, type);