2 * Copyright (c) 2010-2023 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.CHANNEL_ENERGY_CONSUMED_SINCE_LAST_CALL;
16 import static org.openhab.binding.mystrom.internal.MyStromBindingConstants.CHANNEL_POWER;
17 import static org.openhab.binding.mystrom.internal.MyStromBindingConstants.CHANNEL_SWITCH;
18 import static org.openhab.binding.mystrom.internal.MyStromBindingConstants.CHANNEL_TEMPERATURE;
19 import static org.openhab.core.library.unit.SIUnits.CELSIUS;
20 import static org.openhab.core.library.unit.Units.WATT;
21 import static org.openhab.core.library.unit.Units.WATT_SECOND;
23 import java.time.Duration;
24 import java.util.concurrent.TimeUnit;
26 import org.eclipse.jdt.annotation.NonNullByDefault;
27 import org.eclipse.jdt.annotation.Nullable;
28 import org.eclipse.jetty.client.HttpClient;
29 import org.eclipse.jetty.http.HttpMethod;
30 import org.openhab.core.cache.ExpiringCache;
31 import org.openhab.core.library.types.OnOffType;
32 import org.openhab.core.library.types.QuantityType;
33 import org.openhab.core.thing.ChannelUID;
34 import org.openhab.core.thing.Thing;
35 import org.openhab.core.thing.ThingStatus;
36 import org.openhab.core.thing.ThingStatusDetail;
37 import org.openhab.core.types.Command;
38 import org.openhab.core.types.RefreshType;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
43 * The {@link MyStromPlugHandler} is responsible for handling commands, which are
44 * sent to one of the channels.
46 * @author Paul Frank - Initial contribution
47 * @author Frederic Chastagnol - Extends from new abstract class
50 public class MyStromPlugHandler extends AbstractMyStromHandler {
52 private static class MyStromReport {
57 public float temperature;
60 private final Logger logger = LoggerFactory.getLogger(MyStromPlugHandler.class);
62 private final ExpiringCache<MyStromReport> cache = new ExpiringCache<>(Duration.ofSeconds(3), this::getReport);
64 public MyStromPlugHandler(Thing thing, HttpClient httpClient) {
65 super(thing, httpClient);
69 public void handleCommand(ChannelUID channelUID, Command command) {
71 if (command instanceof RefreshType) {
74 if (command instanceof OnOffType && CHANNEL_SWITCH.equals(channelUID.getId())) {
75 sendHttpRequest(HttpMethod.GET, "/relay?state=" + (command == OnOffType.ON ? "1" : "0"), null);
76 scheduler.schedule(this::pollDevice, 500, TimeUnit.MILLISECONDS);
79 } catch (MyStromException e) {
80 logger.warn("Error while handling command {}", e.getMessage());
84 private @Nullable MyStromReport getReport() {
86 String returnContent = sendHttpRequest(HttpMethod.GET, "/report", null);
87 MyStromReport report = gson.fromJson(returnContent, MyStromReport.class);
88 updateStatus(ThingStatus.ONLINE);
90 } catch (MyStromException e) {
91 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, e.getMessage());
97 protected void pollDevice() {
98 MyStromReport report = cache.getValue();
100 updateState(CHANNEL_SWITCH, OnOffType.from(report.relay));
101 updateState(CHANNEL_POWER, QuantityType.valueOf(report.power, WATT));
102 updateState(CHANNEL_ENERGY_CONSUMED_SINCE_LAST_CALL, QuantityType.valueOf(report.Ws, WATT_SECOND));
103 updateState(CHANNEL_TEMPERATURE, QuantityType.valueOf(report.temperature, CELSIUS));