]> git.basschouten.com Git - openhab-addons.git/blob
bc52b6df17557ea6110ba860797af9ab95c858b6
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.amazonechocontrol.internal.channelhandler;
14
15 import java.io.IOException;
16 import java.net.URISyntaxException;
17
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;
25
26 import com.google.gson.Gson;
27 import com.google.gson.JsonSyntaxException;
28
29 /**
30  * The {@link ChannelHandler} is the base class for all channel handlers
31  *
32  * @author Michael Geramb - Initial contribution
33  */
34 @NonNullByDefault
35 public abstract class ChannelHandler {
36
37     public abstract boolean tryHandleCommand(Device device, Connection connection, String channelId, Command command)
38             throws IOException, URISyntaxException, InterruptedException;
39
40     protected final IAmazonThingHandler thingHandler;
41     protected final Gson gson;
42     private final Logger logger;
43
44     protected ChannelHandler(IAmazonThingHandler thingHandler, Gson gson) {
45         this.logger = LoggerFactory.getLogger(this.getClass());
46         this.thingHandler = thingHandler;
47         this.gson = gson;
48     }
49
50     protected <T> @Nullable T tryParseJson(String json, Class<T> type) {
51         try {
52             return gson.fromJson(json, type);
53         } catch (JsonSyntaxException e) {
54             logger.debug("Json parse error", e);
55             return null;
56         }
57     }
58
59     protected <T> @Nullable T parseJson(String json, Class<T> type) throws JsonSyntaxException {
60         return gson.fromJson(json, type);
61     }
62 }