]> git.basschouten.com Git - openhab-addons.git/blob
7adbeb690982ef6a4b71c9bd9d6f69b671e7f98a
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2022 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.mystrom.internal;
14
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;
20
21 import java.time.Duration;
22 import java.util.concurrent.TimeUnit;
23
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;
39
40 /**
41  * The {@link MyStromPlugHandler} is responsible for handling commands, which are
42  * sent to one of the channels.
43  *
44  * @author Paul Frank - Initial contribution
45  * @author Frederic Chastagnol - Extends from new abstract class
46  */
47 @NonNullByDefault
48 public class MyStromPlugHandler extends AbstractMyStromHandler {
49
50     private static class MyStromReport {
51
52         public float power;
53         public boolean relay;
54         public float temperature;
55     }
56
57     private final Logger logger = LoggerFactory.getLogger(MyStromPlugHandler.class);
58
59     private final ExpiringCache<MyStromReport> cache = new ExpiringCache<>(Duration.ofSeconds(3), this::getReport);
60
61     public MyStromPlugHandler(Thing thing, HttpClient httpClient) {
62         super(thing, httpClient);
63     }
64
65     @Override
66     public void handleCommand(ChannelUID channelUID, Command command) {
67         try {
68             if (command instanceof RefreshType) {
69                 pollDevice();
70             } else {
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);
74                 }
75             }
76         } catch (MyStromException e) {
77             logger.warn("Error while handling command {}", e.getMessage());
78         }
79     }
80
81     private @Nullable MyStromReport getReport() {
82         try {
83             String returnContent = sendHttpRequest(HttpMethod.GET, "/report", null);
84             MyStromReport report = gson.fromJson(returnContent, MyStromReport.class);
85             updateStatus(ThingStatus.ONLINE);
86             return report;
87         } catch (MyStromException e) {
88             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, e.getMessage());
89             return null;
90         }
91     }
92
93     @Override
94     protected void pollDevice() {
95         MyStromReport report = cache.getValue();
96         if (report != null) {
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));
100         }
101     }
102 }