2 * Copyright (c) 2010-2021 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.wlanthermo.internal.api.esp32;
15 import static org.openhab.binding.wlanthermo.internal.WlanThermoBindingConstants.TRIGGER_NONE;
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;
29 * The {@link WlanThermoEsp32Handler} is responsible for handling commands, which are
30 * sent to one of the channels.
32 * @author Christian Schlipp - Initial contribution
35 public class WlanThermoEsp32Handler extends WlanThermoHandler {
37 private Data data = new Data();
38 private Settings settings = new Settings();
40 public WlanThermoEsp32Handler(Thing thing, HttpClient httpClient) {
41 super(thing, httpClient, true);
45 protected State getState(ChannelUID channelUID) throws WlanThermoInputException, WlanThermoUnknownChannelException {
46 return WlanThermoEsp32CommandHandler.getState(channelUID, data, settings);
50 protected boolean setState(ChannelUID channelUID, Command command) {
51 return WlanThermoEsp32CommandHandler.setState(channelUID, command, data, settings);
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()) {
59 String json = gson.toJson(c);
60 if (!doPost("/setchannels", json)) {
63 } catch (InterruptedException e) {
64 logger.debug("Push interrupted. {}", e.getMessage());
69 // push update for pitmaster channels
71 String json = gson.toJson(data.getPitmaster().getPm());
72 doPost("/setpitmaster", json);
73 } catch (InterruptedException e) {
74 logger.debug("Push interrupted. {}", e.getMessage());
79 protected void pull() {
81 // Update objects with data from device
82 data = doGet("/data", Data.class);
83 settings = doGet("/settings", Settings.class);
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();
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);
101 // Update channel state
102 for (Channel channel : thing.getChannels()) {
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
109 String trigger = WlanThermoEsp32CommandHandler.getTrigger(channel.getUID(), data);
110 if (!trigger.equals(TRIGGER_NONE)) {
111 triggerChannel(channel.getUID(), trigger);
113 } catch (WlanThermoUnknownChannelException e1) {
114 logger.debug("{}", e.getMessage());
118 } catch (WlanThermoException ignore) {
119 // Nothing more to do
120 } catch (InterruptedException e) {
121 logger.debug("Update interrupted. {}", e.getMessage());