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