2 * Copyright (c) 2010-2021 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.mystrom.internal;
15 import static org.openhab.binding.mystrom.internal.MyStromBindingConstants.PROPERTY_CONNECTED;
16 import static org.openhab.binding.mystrom.internal.MyStromBindingConstants.PROPERTY_DNS;
17 import static org.openhab.binding.mystrom.internal.MyStromBindingConstants.PROPERTY_GW;
18 import static org.openhab.binding.mystrom.internal.MyStromBindingConstants.PROPERTY_IP;
19 import static org.openhab.binding.mystrom.internal.MyStromBindingConstants.PROPERTY_LAST_REFRESH;
20 import static org.openhab.binding.mystrom.internal.MyStromBindingConstants.PROPERTY_MAC;
21 import static org.openhab.binding.mystrom.internal.MyStromBindingConstants.PROPERTY_MASK;
22 import static org.openhab.binding.mystrom.internal.MyStromBindingConstants.PROPERTY_SSID;
23 import static org.openhab.binding.mystrom.internal.MyStromBindingConstants.PROPERTY_STATIC;
24 import static org.openhab.binding.mystrom.internal.MyStromBindingConstants.PROPERTY_TYPE;
25 import static org.openhab.binding.mystrom.internal.MyStromBindingConstants.PROPERTY_VERSION;
27 import java.text.DateFormat;
28 import java.util.Calendar;
29 import java.util.Locale;
31 import java.util.concurrent.ExecutionException;
32 import java.util.concurrent.ScheduledFuture;
33 import java.util.concurrent.TimeUnit;
34 import java.util.concurrent.TimeoutException;
36 import org.eclipse.jdt.annotation.NonNullByDefault;
37 import org.eclipse.jdt.annotation.Nullable;
38 import org.eclipse.jetty.client.HttpClient;
39 import org.eclipse.jetty.client.api.ContentResponse;
40 import org.eclipse.jetty.client.api.Request;
41 import org.eclipse.jetty.client.util.StringContentProvider;
42 import org.eclipse.jetty.http.HttpHeader;
43 import org.eclipse.jetty.http.HttpMethod;
44 import org.eclipse.jetty.http.HttpStatus;
45 import org.openhab.core.thing.Thing;
46 import org.openhab.core.thing.ThingStatus;
47 import org.openhab.core.thing.ThingStatusDetail;
48 import org.openhab.core.thing.binding.BaseThingHandler;
50 import com.google.gson.Gson;
53 * The {@link AbstractMyStromHandler} is responsible for handling commands, which are
54 * sent to one of the channels.
56 * @author Frederic Chastagnol - Initial contribution
59 public abstract class AbstractMyStromHandler extends BaseThingHandler {
60 protected static final String COMMUNICATION_ERROR = "Error while communicating to the myStrom plug: ";
61 protected static final String HTTP_REQUEST_URL_PREFIX = "http://";
63 protected final HttpClient httpClient;
64 protected String hostname = "";
65 protected String mac = "";
67 private @Nullable ScheduledFuture<?> pollingJob;
68 protected final Gson gson = new Gson();
70 public AbstractMyStromHandler(Thing thing, HttpClient httpClient) {
72 this.httpClient = httpClient;
76 public final void initialize() {
77 MyStromConfiguration config = getConfigAs(MyStromConfiguration.class);
78 this.hostname = HTTP_REQUEST_URL_PREFIX + config.hostname;
80 updateStatus(ThingStatus.UNKNOWN);
81 scheduler.schedule(this::initializeInternal, 0, TimeUnit.SECONDS);
85 public final void dispose() {
86 ScheduledFuture<?> pollingJob = this.pollingJob;
87 if (pollingJob != null) {
88 pollingJob.cancel(true);
89 this.pollingJob = null;
94 private void updateProperties() throws MyStromException {
95 String json = sendHttpRequest(HttpMethod.GET, "/api/v1/info", null);
96 MyStromDeviceInfo deviceInfo = gson.fromJson(json, MyStromDeviceInfo.class);
97 if (deviceInfo == null) {
98 throw new MyStromException("Cannot retrieve device info from myStrom device " + getThing().getUID());
100 this.mac = deviceInfo.mac;
101 Map<String, String> properties = editProperties();
102 properties.put(PROPERTY_MAC, deviceInfo.mac);
103 properties.put(PROPERTY_VERSION, deviceInfo.version);
104 properties.put(PROPERTY_TYPE, Long.toString(deviceInfo.type));
105 properties.put(PROPERTY_SSID, deviceInfo.ssid);
106 properties.put(PROPERTY_IP, deviceInfo.ip);
107 properties.put(PROPERTY_MASK, deviceInfo.mask);
108 properties.put(PROPERTY_GW, deviceInfo.gw);
109 properties.put(PROPERTY_DNS, deviceInfo.dns);
110 properties.put(PROPERTY_STATIC, Boolean.toString(deviceInfo.staticState));
111 properties.put(PROPERTY_CONNECTED, Boolean.toString(deviceInfo.connected));
112 Calendar calendar = Calendar.getInstance();
113 DateFormat formatter = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.MEDIUM, Locale.getDefault());
114 properties.put(PROPERTY_LAST_REFRESH, formatter.format(calendar.getTime()));
115 updateProperties(properties);
119 * Calls the API with the given http method, request path and actual data.
121 * @param method the http method to make the call with
122 * @param path The path of the API endpoint
123 * @param requestData the actual raw data to send in the request body, may be {@code null}
124 * @return String contents of the response for the GET request.
125 * @throws MyStromException Throws on communication error
127 protected final String sendHttpRequest(HttpMethod method, String path, @Nullable String requestData)
128 throws MyStromException {
129 String url = hostname + path;
131 Request request = httpClient.newRequest(url).timeout(10, TimeUnit.SECONDS).method(method);
132 if (requestData != null) {
133 request = request.content(new StringContentProvider(requestData)).header(HttpHeader.CONTENT_TYPE,
134 "application/x-www-form-urlencoded");
136 ContentResponse response = request.send();
137 if (response.getStatus() != HttpStatus.OK_200) {
138 throw new MyStromException("Error sending HTTP " + method + " request to " + url
139 + ". Got response code: " + response.getStatus());
141 return response.getContentAsString();
142 } catch (InterruptedException | TimeoutException | ExecutionException e) {
143 throw new MyStromException(COMMUNICATION_ERROR + e.getMessage());
147 private void initializeInternal() {
150 updateStatus(ThingStatus.ONLINE);
151 MyStromConfiguration config = getConfigAs(MyStromConfiguration.class);
152 pollingJob = scheduler.scheduleWithFixedDelay(this::pollDevice, 0, config.refresh, TimeUnit.SECONDS);
153 } catch (MyStromException e) {
154 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, e.getMessage());
158 protected abstract void pollDevice();