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_POWER;
16 import static org.openhab.binding.mystrom.internal.MyStromBindingConstants.CHANNEL_SWITCH;
17 import static org.openhab.binding.mystrom.internal.MyStromBindingConstants.CHANNEL_TEMPERATURE;
18 import static org.openhab.core.library.unit.SIUnits.CELSIUS;
19 import static org.openhab.core.library.unit.Units.WATT;
21 import java.time.Duration;
22 import java.util.concurrent.TimeUnit;
24 import org.eclipse.jdt.annotation.NonNullByDefault;
25 import org.eclipse.jdt.annotation.Nullable;
26 import org.eclipse.jetty.client.HttpClient;
27 import org.eclipse.jetty.http.HttpMethod;
28 import org.openhab.core.cache.ExpiringCache;
29 import org.openhab.core.library.types.OnOffType;
30 import org.openhab.core.library.types.QuantityType;
31 import org.openhab.core.thing.ChannelUID;
32 import org.openhab.core.thing.Thing;
33 import org.openhab.core.thing.ThingStatus;
34 import org.openhab.core.thing.ThingStatusDetail;
35 import org.openhab.core.types.Command;
36 import org.openhab.core.types.RefreshType;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
41 * The {@link MyStromPlugHandler} is responsible for handling commands, which are
42 * sent to one of the channels.
44 * @author Paul Frank - Initial contribution
45 * @author Frederic Chastagnol - Extends from new abstract class
48 public class MyStromPlugHandler extends AbstractMyStromHandler {
50 private static class MyStromReport {
54 public float temperature;
57 private final Logger logger = LoggerFactory.getLogger(MyStromPlugHandler.class);
59 private final ExpiringCache<MyStromReport> cache = new ExpiringCache<>(Duration.ofSeconds(3), this::getReport);
61 public MyStromPlugHandler(Thing thing, HttpClient httpClient) {
62 super(thing, httpClient);
66 public void handleCommand(ChannelUID channelUID, Command command) {
68 if (command instanceof RefreshType) {
71 if (command instanceof OnOffType && CHANNEL_SWITCH.equals(channelUID.getId())) {
72 sendHttpRequest(HttpMethod.GET, "/relay?state=" + (command == OnOffType.ON ? "1" : "0"), null);
73 scheduler.schedule(this::pollDevice, 500, TimeUnit.MILLISECONDS);
76 } catch (MyStromException e) {
77 logger.warn("Error while handling command {}", e.getMessage());
81 private @Nullable MyStromReport getReport() {
83 String returnContent = sendHttpRequest(HttpMethod.GET, "/report", null);
84 MyStromReport report = gson.fromJson(returnContent, MyStromReport.class);
85 updateStatus(ThingStatus.ONLINE);
87 } catch (MyStromException e) {
88 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, e.getMessage());
94 protected void pollDevice() {
95 MyStromReport report = cache.getValue();
97 updateState(CHANNEL_SWITCH, report.relay ? OnOffType.ON : OnOffType.OFF);
98 updateState(CHANNEL_POWER, QuantityType.valueOf(report.power, WATT));
99 updateState(CHANNEL_TEMPERATURE, QuantityType.valueOf(report.temperature, CELSIUS));