]> git.basschouten.com Git - openhab-addons.git/blob
b42c882c0aa2ed948fa000f3c55fbe36a985fcc5
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2021 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.wlanthermo.internal.api.esp32;
14
15 import static org.openhab.binding.wlanthermo.internal.WlanThermoBindingConstants.TRIGGER_NONE;
16
17 import java.util.Map;
18
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.eclipse.jetty.client.HttpClient;
21 import org.openhab.binding.wlanthermo.internal.*;
22 import org.openhab.binding.wlanthermo.internal.api.esp32.dto.data.Data;
23 import org.openhab.binding.wlanthermo.internal.api.esp32.dto.settings.Settings;
24 import org.openhab.core.thing.*;
25 import org.openhab.core.types.Command;
26 import org.openhab.core.types.State;
27
28 /**
29  * The {@link WlanThermoEsp32Handler} is responsible for handling commands, which are
30  * sent to one of the channels.
31  *
32  * @author Christian Schlipp - Initial contribution
33  */
34 @NonNullByDefault
35 public class WlanThermoEsp32Handler extends WlanThermoHandler {
36
37     private Data data = new Data();
38     private Settings settings = new Settings();
39
40     public WlanThermoEsp32Handler(Thing thing, HttpClient httpClient) {
41         super(thing, httpClient, true);
42     }
43
44     @Override
45     protected State getState(ChannelUID channelUID) throws WlanThermoInputException, WlanThermoUnknownChannelException {
46         return WlanThermoEsp32CommandHandler.getState(channelUID, data, settings);
47     }
48
49     @Override
50     protected boolean setState(ChannelUID channelUID, Command command) {
51         return WlanThermoEsp32CommandHandler.setState(channelUID, command, data, settings);
52     }
53
54     @Override
55     protected void push() {
56         // Push update for sensor channels
57         for (org.openhab.binding.wlanthermo.internal.api.esp32.dto.data.Channel c : data.getChannel()) {
58             try {
59                 String json = gson.toJson(c);
60                 if (!doPost("/setchannels", json)) {
61                     break;
62                 }
63             } catch (InterruptedException e) {
64                 logger.debug("Push interrupted. {}", e.getMessage());
65                 return;
66             }
67         }
68
69         // push update for pitmaster channels
70         try {
71             String json = gson.toJson(data.getPitmaster().getPm());
72             doPost("/setpitmaster", json);
73         } catch (InterruptedException e) {
74             logger.debug("Push interrupted. {}", e.getMessage());
75         }
76     }
77
78     @Override
79     protected void pull() {
80         try {
81             // Update objects with data from device
82             data = doGet("/data", Data.class);
83             settings = doGet("/settings", Settings.class);
84
85             // Update Channels if required
86             Map<String, String> properties = editProperties();
87             Boolean pmEnabled = settings.getFeatures().getBluetooth();
88             int pmChannels = pmEnabled ? data.getPitmaster().getPm().size() : 0;
89             int tempChannels = data.getChannel().size();
90
91             // Update properties
92             properties.putIfAbsent(WlanThermoBindingConstants.PROPERTY_MODEL, settings.getDevice().getDevice());
93             properties.putIfAbsent(WlanThermoBindingConstants.PROPERTY_SERIAL, settings.getDevice().getSerial());
94             properties.putIfAbsent(WlanThermoBindingConstants.PROPERTY_ESP32_BT_ENABLED,
95                     settings.getFeatures().getBluetooth().toString());
96             properties.putIfAbsent(WlanThermoBindingConstants.PROPERTY_ESP32_PM_ENABLED, pmEnabled.toString());
97             properties.put(WlanThermoBindingConstants.PROPERTY_ESP32_TEMP_CHANNELS, String.valueOf(tempChannels));
98             properties.put(WlanThermoBindingConstants.PROPERTY_ESP32_PM_CHANNELS, String.valueOf(pmChannels));
99             updateProperties(properties);
100
101             // Update channel state
102             for (Channel channel : thing.getChannels()) {
103                 try {
104                     State state = WlanThermoEsp32CommandHandler.getState(channel.getUID(), data, settings);
105                     updateState(channel.getUID(), state);
106                 } catch (WlanThermoUnknownChannelException e) {
107                     // if we could not obtain a state, try trigger instead
108                     try {
109                         String trigger = WlanThermoEsp32CommandHandler.getTrigger(channel.getUID(), data);
110                         if (!trigger.equals(TRIGGER_NONE)) {
111                             triggerChannel(channel.getUID(), trigger);
112                         }
113                     } catch (WlanThermoUnknownChannelException e1) {
114                         logger.debug("{}", e.getMessage());
115                     }
116                 }
117             }
118         } catch (WlanThermoException ignore) {
119             // Nothing more to do
120         } catch (InterruptedException e) {
121             logger.debug("Update interrupted. {}", e.getMessage());
122         }
123     }
124 }