]> git.basschouten.com Git - openhab-addons.git/blob
983073070465573a6c30cf1b4022d9b2e83d72ef
[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.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.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;
33
34 /**
35  * The {@link WlanThermoEsp32Handler} is responsible for handling commands, which are
36  * sent to one of the channels.
37  *
38  * @author Christian Schlipp - Initial contribution
39  */
40 @NonNullByDefault
41 public class WlanThermoEsp32Handler extends WlanThermoHandler {
42
43     private Data data = new Data();
44     private Settings settings = new Settings();
45
46     public WlanThermoEsp32Handler(Thing thing, HttpClient httpClient) {
47         super(thing, httpClient, true);
48     }
49
50     @Override
51     protected State getState(ChannelUID channelUID) throws WlanThermoInputException, WlanThermoUnknownChannelException {
52         return WlanThermoEsp32CommandHandler.getState(channelUID, data, settings);
53     }
54
55     @Override
56     protected boolean setState(ChannelUID channelUID, Command command) {
57         return WlanThermoEsp32CommandHandler.setState(channelUID, command, data, settings);
58     }
59
60     @Override
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()) {
64             try {
65                 String json = gson.toJson(c);
66                 if (!doPost("/setchannels", json)) {
67                     break;
68                 }
69             } catch (InterruptedException e) {
70                 logger.debug("Push interrupted. {}", e.getMessage());
71                 return;
72             }
73         }
74
75         // push update for pitmaster channels
76         try {
77             String json = gson.toJson(data.getPitmaster().getPm());
78             doPost("/setpitmaster", json);
79         } catch (InterruptedException e) {
80             logger.debug("Push interrupted. {}", e.getMessage());
81         }
82     }
83
84     @Override
85     protected void pull() {
86         try {
87             // Update objects with data from device
88             data = doGet("/data", Data.class);
89             settings = doGet("/settings", Settings.class);
90
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();
96
97             // Update properties
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);
106
107             // Update channel state
108             for (Channel channel : thing.getChannels()) {
109                 try {
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
114                     try {
115                         String trigger = WlanThermoEsp32CommandHandler.getTrigger(channel.getUID(), data);
116                         if (!trigger.equals(TRIGGER_NONE)) {
117                             triggerChannel(channel.getUID(), trigger);
118                         }
119                     } catch (WlanThermoUnknownChannelException e1) {
120                         logger.debug("{}", e.getMessage());
121                     }
122                 }
123             }
124         } catch (WlanThermoException ignore) {
125             // Nothing more to do
126         } catch (InterruptedException e) {
127             logger.debug("Update interrupted. {}", e.getMessage());
128         }
129     }
130 }