]> git.basschouten.com Git - openhab-addons.git/blob
cfc85e18d474b43f861bfe9f9919ab444964d1d9
[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.tplinksmarthome.internal.device;
14
15 import java.io.IOException;
16
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.eclipse.jdt.annotation.Nullable;
19 import org.openhab.binding.tplinksmarthome.internal.Commands;
20 import org.openhab.binding.tplinksmarthome.internal.Connection;
21 import org.openhab.binding.tplinksmarthome.internal.TPLinkSmartHomeConfiguration;
22 import org.openhab.binding.tplinksmarthome.internal.model.ErrorResponse;
23 import org.openhab.binding.tplinksmarthome.internal.model.HasErrorResponse;
24 import org.openhab.core.thing.ChannelUID;
25 import org.openhab.core.types.Command;
26 import org.openhab.core.types.State;
27
28 /**
29  * Abstract class as base for Smart Home device implementations.
30  *
31  * @author Hilbrand Bouwkamp - Initial contribution
32  */
33 @NonNullByDefault
34 public abstract class SmartHomeDevice {
35
36     protected final Commands commands = new Commands();
37     protected @NonNullByDefault({}) Connection connection;
38     protected @NonNullByDefault({}) TPLinkSmartHomeConfiguration configuration;
39
40     /**
41      * Checks if the response object contains errors and if so throws an {@link IOException} when an error code was set.
42      *
43      * @param response The response to check for errors.
44      * @throws IOException if an error code was set in the response object
45      */
46     protected void checkErrors(@Nullable HasErrorResponse response) throws IOException {
47         final ErrorResponse errorResponse = response == null ? null : response.getErrorResponse();
48
49         if (errorResponse != null && errorResponse.getErrorCode() != 0) {
50             throw new IOException("Error (" + errorResponse.getErrorCode() + "): " + errorResponse.getErrorMessage());
51         }
52     }
53
54     /**
55      * Sets connection and configuration values.
56      *
57      * @param connection The connection to the device
58      * @param configuration The global configuration
59      */
60     public void initialize(Connection connection, TPLinkSmartHomeConfiguration configuration) {
61         this.connection = connection;
62         this.configuration = configuration;
63     }
64
65     /**
66      * @return the json string to send to the device to get the state of the device.
67      */
68     public abstract String getUpdateCommand();
69
70     /**
71      * Handle the command for the given channel
72      *
73      * @param channelUID The channel the command is for
74      * @param command The command to be send to the device
75      * @return Returns true if the commands successfully was send to the device
76      * @throws IOException In case of communications error or the device returned an error
77      */
78     public abstract boolean handleCommand(ChannelUID channelUID, Command command) throws IOException;
79
80     /**
81      * Returns the {@link State} value for the given value extracted from the deviceState data.
82      *
83      * @param channelUid channel to get state for
84      * @param deviceState state object containing the state
85      * @return {@link State} value for the given channel
86      */
87     public abstract State updateChannel(ChannelUID channelUid, DeviceState deviceState);
88
89     /**
90      * Called with the new device state after the new device state is retrieved from the device.
91      *
92      * @param deviceState new device state
93      */
94     public void refreshedDeviceState(@Nullable DeviceState deviceState) {
95     }
96 }