2 * Copyright (c) 2010-2023 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.WlanThermoBindingConstants;
22 import org.openhab.binding.wlanthermo.internal.WlanThermoException;
23 import org.openhab.binding.wlanthermo.internal.WlanThermoHandler;
24 import org.openhab.binding.wlanthermo.internal.WlanThermoInputException;
25 import org.openhab.binding.wlanthermo.internal.WlanThermoUnknownChannelException;
26 import org.openhab.binding.wlanthermo.internal.api.esp32.dto.data.Data;
27 import org.openhab.binding.wlanthermo.internal.api.esp32.dto.settings.Settings;
28 import org.openhab.core.thing.Channel;
29 import org.openhab.core.thing.ChannelUID;
30 import org.openhab.core.thing.Thing;
31 import org.openhab.core.types.Command;
32 import org.openhab.core.types.State;
35 * The {@link WlanThermoEsp32Handler} is responsible for handling commands, which are
36 * sent to one of the channels.
38 * @author Christian Schlipp - Initial contribution
41 public class WlanThermoEsp32Handler extends WlanThermoHandler {
43 private Data data = new Data();
44 private Settings settings = new Settings();
46 public WlanThermoEsp32Handler(Thing thing, HttpClient httpClient) {
47 super(thing, httpClient, true);
51 protected State getState(ChannelUID channelUID) throws WlanThermoInputException, WlanThermoUnknownChannelException {
52 return WlanThermoEsp32CommandHandler.getState(channelUID, data, settings);
56 protected boolean setState(ChannelUID channelUID, Command command) {
57 return WlanThermoEsp32CommandHandler.setState(channelUID, command, data, settings);
61 protected void push() {
62 // Push update for sensor channels
63 for (org.openhab.binding.wlanthermo.internal.api.esp32.dto.data.Channel c : data.getChannel()) {
65 String json = gson.toJson(c);
66 if (!doPost("/setchannels", json)) {
69 } catch (InterruptedException e) {
70 logger.debug("Push interrupted. {}", e.getMessage());
75 // push update for pitmaster channels
77 String json = gson.toJson(data.getPitmaster().getPm());
78 doPost("/setpitmaster", json);
79 } catch (InterruptedException e) {
80 logger.debug("Push interrupted. {}", e.getMessage());
85 protected void pull() {
87 // Update objects with data from device
88 data = doGet("/data", Data.class);
89 settings = doGet("/settings", Settings.class);
91 // Update Channels if required
92 Map<String, String> properties = editProperties();
93 Boolean pmEnabled = settings.getFeatures().getBluetooth();
94 int pmChannels = pmEnabled ? data.getPitmaster().getPm().size() : 0;
95 int tempChannels = data.getChannel().size();
98 properties.putIfAbsent(WlanThermoBindingConstants.PROPERTY_MODEL, settings.getDevice().getDevice());
99 properties.putIfAbsent(WlanThermoBindingConstants.PROPERTY_SERIAL, settings.getDevice().getSerial());
100 properties.putIfAbsent(WlanThermoBindingConstants.PROPERTY_ESP32_BT_ENABLED,
101 settings.getFeatures().getBluetooth().toString());
102 properties.putIfAbsent(WlanThermoBindingConstants.PROPERTY_ESP32_PM_ENABLED, pmEnabled.toString());
103 properties.put(WlanThermoBindingConstants.PROPERTY_ESP32_TEMP_CHANNELS, String.valueOf(tempChannels));
104 properties.put(WlanThermoBindingConstants.PROPERTY_ESP32_PM_CHANNELS, String.valueOf(pmChannels));
105 updateProperties(properties);
107 // Update channel state
108 for (Channel channel : thing.getChannels()) {
110 State state = WlanThermoEsp32CommandHandler.getState(channel.getUID(), data, settings);
111 updateState(channel.getUID(), state);
112 } catch (WlanThermoUnknownChannelException e) {
113 // if we could not obtain a state, try trigger instead
115 String trigger = WlanThermoEsp32CommandHandler.getTrigger(channel.getUID(), data);
116 if (!trigger.equals(TRIGGER_NONE)) {
117 triggerChannel(channel.getUID(), trigger);
119 } catch (WlanThermoUnknownChannelException e1) {
120 logger.debug("{}", e.getMessage());
124 } catch (WlanThermoException ignore) {
125 // Nothing more to do
126 } catch (InterruptedException e) {
127 logger.debug("Update interrupted. {}", e.getMessage());