]> git.basschouten.com Git - openhab-addons.git/blob
11197b7f9f76f70336fd57e5f1ee12ed12c0e016
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2024 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.homewizard.internal;
14
15 import java.io.ByteArrayInputStream;
16 import java.io.IOException;
17 import java.io.InputStream;
18
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.eclipse.jdt.annotation.Nullable;
21 import org.openhab.core.io.net.http.HttpUtil;
22 import org.openhab.core.thing.Thing;
23 import org.openhab.core.thing.ThingStatus;
24 import org.openhab.core.thing.ThingStatusDetail;
25
26 /**
27  * The {@link HomeWizardStatefulDeviceHandler} extends the base class
28  * to provide support for devices that also have a 'state' interface.
29  * This interface can be used to query and control the state of a device.
30  *
31  * @author DaniĆ«l van Os - Initial contribution
32  */
33 @NonNullByDefault
34 public abstract class HomeWizardStatefulDeviceHandler extends HomeWizardDeviceHandler {
35
36     /**
37      * Constructor
38      *
39      * @param thing The thing to handle
40      */
41     public HomeWizardStatefulDeviceHandler(Thing thing) {
42         super(thing);
43     }
44
45     /**
46      * Device specific handling of the returned state payload.
47      *
48      * @param payload The data parsed from the state Json file
49      */
50     protected abstract void handleStatePayload(StatePayload payload);
51
52     protected void pollState() {
53         final String stateResult;
54
55         try {
56             stateResult = HttpUtil.executeUrl("GET", apiURL + "state", 30000);
57         } catch (IOException e) {
58             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR,
59                     String.format("Unable to query device state: %s", e.getMessage()));
60             return;
61         }
62
63         if (stateResult.trim().isEmpty()) {
64             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, "Device returned empty state");
65             return;
66         }
67
68         StatePayload statePayload = gson.fromJson(stateResult, StatePayload.class);
69         if (statePayload == null) {
70             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR,
71                     "Unable to parse state response from device");
72             return;
73         }
74
75         handleStatePayload(statePayload);
76     }
77
78     /**
79      * Sends a command to the state interface of the device.
80      *
81      * @param command The command to send.
82      */
83     protected @Nullable StatePayload sendStateCommand(String command) {
84         try (InputStream is = new ByteArrayInputStream(command.getBytes())) {
85             String updatedState = HttpUtil.executeUrl("PUT", apiURL + "state", is, "application/json", 30000);
86             return gson.fromJson(updatedState, StatePayload.class);
87         } catch (IOException e) {
88             logger.warn("Failed to send command {} to {}", command, apiURL + "state");
89             return null;
90         }
91     }
92
93     /*
94      * This overrides the original polling loop by including a request for the current state..
95      */
96     @Override
97     protected void pollingCode() {
98         pollData();
99         pollState();
100     }
101 }