]> git.basschouten.com Git - openhab-addons.git/blob
9e9f74ff604e1b8fa88be754011b9666bfb41d50
[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.IOException;
16 import java.util.concurrent.ScheduledFuture;
17 import java.util.concurrent.TimeUnit;
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 import org.openhab.core.thing.binding.BaseThingHandler;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28
29 import com.google.gson.FieldNamingPolicy;
30 import com.google.gson.Gson;
31 import com.google.gson.GsonBuilder;
32
33 /**
34  * The {@link HomeWizardDeviceHandler} is a base class for all
35  * HomeWizard devices. It provides configuration and polling of
36  * data from a device. It also processes common data.
37  * 
38  * @author DaniĆ«l van Os - Initial contribution
39  */
40 @NonNullByDefault
41 public abstract class HomeWizardDeviceHandler extends BaseThingHandler {
42
43     protected final Logger logger = LoggerFactory.getLogger(HomeWizardDeviceHandler.class);
44     protected final Gson gson = new GsonBuilder().setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES)
45             .create();
46
47     private HomeWizardConfiguration config = new HomeWizardConfiguration();
48     private @Nullable ScheduledFuture<?> pollingJob;
49
50     protected String apiURL = "";
51
52     /**
53      * Constructor
54      *
55      * @param thing The thing to handle
56      */
57     public HomeWizardDeviceHandler(Thing thing) {
58         super(thing);
59     }
60
61     /**
62      * If a host has been specified start polling it
63      */
64     @Override
65     public void initialize() {
66         config = getConfigAs(HomeWizardConfiguration.class);
67         if (configure()) {
68             pollingJob = scheduler.scheduleWithFixedDelay(this::pollingCode, 0, config.refreshDelay, TimeUnit.SECONDS);
69         }
70     }
71
72     /**
73      * Check the current configuration
74      *
75      * @return true if the configuration is ok to start polling, false otherwise
76      */
77     private boolean configure() {
78         if (config.ipAddress.trim().isEmpty()) {
79             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR,
80                     "Missing ipAddress/host configuration");
81             return false;
82         } else {
83             updateStatus(ThingStatus.UNKNOWN);
84             apiURL = String.format("http://%s/api/v1/", config.ipAddress.trim());
85             return true;
86         }
87     }
88
89     /**
90      * dispose: stop the poller
91      */
92     @Override
93     public void dispose() {
94         var job = pollingJob;
95         if (job != null) {
96             job.cancel(true);
97         }
98         pollingJob = null;
99     }
100
101     /**
102      * Device specific handling of the returned data payload.
103      *
104      * @param payload The data parsed from the data Json file
105      */
106     protected abstract void handleDataPayload(DataPayload payload);
107
108     /**
109      *
110      */
111     protected void pollData() {
112         final String dataResult;
113
114         try {
115             dataResult = HttpUtil.executeUrl("GET", apiURL + "data", 30000);
116         } catch (IOException e) {
117             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR,
118                     String.format("Unable to query device data: %s", e.getMessage()));
119             return;
120         }
121
122         if (dataResult.trim().isEmpty()) {
123             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, "Device returned empty data");
124             return;
125         }
126
127         DataPayload dataPayload = gson.fromJson(dataResult, DataPayload.class);
128         if (dataPayload == null) {
129             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR,
130                     "Unable to parse data response from device");
131             return;
132         }
133
134         if ("".equals(dataPayload.getWifiSsid())) {
135             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, "Results from API are empty");
136             return;
137         }
138
139         updateStatus(ThingStatus.ONLINE);
140         handleDataPayload(dataPayload);
141     }
142
143     /**
144      * The actual polling loop
145      */
146     protected void pollingCode() {
147         pollData();
148     }
149 }