]> git.basschouten.com Git - openhab-addons.git/blob
ee2367d60dc263c21c352ed1247fb54ea8810a3b
[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.*;
16 import static org.openhab.core.library.unit.SIUnits.CELSIUS;
17 import static org.openhab.core.library.unit.Units.PERCENT;
18
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.eclipse.jdt.annotation.Nullable;
21 import org.eclipse.jetty.client.HttpClient;
22 import org.eclipse.jetty.http.HttpMethod;
23 import org.openhab.core.library.types.OnOffType;
24 import org.openhab.core.library.types.QuantityType;
25 import org.openhab.core.thing.ChannelUID;
26 import org.openhab.core.thing.Thing;
27 import org.openhab.core.thing.ThingStatus;
28 import org.openhab.core.thing.ThingStatusDetail;
29 import org.openhab.core.types.Command;
30
31 import com.google.gson.JsonParseException;
32
33 /**
34  *
35  * @author Stefan Navratil - Initial Contribution
36  *
37  */
38
39 @NonNullByDefault
40 public class MyStromPIRHandler extends AbstractMyStromHandler {
41
42     private static class MyStromReport {
43
44         public float light;
45         public boolean motion;
46         public float temperature;
47     }
48
49     public MyStromPIRHandler(Thing thing, HttpClient httpClient) {
50         super(thing, httpClient);
51         try {
52             sendHttpRequest(HttpMethod.POST, "/api/v1/settings/pir",
53                     "{\"backoff_time\":" + config.getBackoffTime() + ",\"led_enable\":" + config.getLedEnable() + "}");
54         } catch (MyStromException e) {
55             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, e.getMessage());
56         }
57     }
58
59     @Override
60     public void handleCommand(ChannelUID channelUID, Command command) {
61     }
62
63     @Override
64     protected void pollDevice() {
65         MyStromReport report = getReport();
66         if (report != null) {
67             updateState(CHANNEL_MOTION, OnOffType.from(report.motion));
68             updateState(CHANNEL_TEMPERATURE, QuantityType.valueOf(report.temperature, CELSIUS));
69             // The Default Light thresholds are from 30 to 300.
70             updateState(CHANNEL_LIGHT, QuantityType.valueOf(report.light / 3, PERCENT));
71         }
72     }
73
74     private @Nullable MyStromReport getReport() {
75         try {
76             String json = sendHttpRequest(HttpMethod.GET, "/api/v1/sensors", null);
77             MyStromReport report = gson.fromJson(json, MyStromReport.class);
78             updateStatus(ThingStatus.ONLINE);
79             return report;
80         } catch (MyStromException | JsonParseException e) {
81             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, e.getMessage());
82             return null;
83         }
84     }
85 }