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.nano;
15 import static org.openhab.binding.wlanthermo.internal.WlanThermoBindingConstants.TRIGGER_NONE;
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.eclipse.jetty.client.HttpClient;
19 import org.openhab.binding.wlanthermo.internal.WlanThermoException;
20 import org.openhab.binding.wlanthermo.internal.WlanThermoHandler;
21 import org.openhab.binding.wlanthermo.internal.WlanThermoInputException;
22 import org.openhab.binding.wlanthermo.internal.WlanThermoUnknownChannelException;
23 import org.openhab.binding.wlanthermo.internal.api.nano.dto.data.Data;
24 import org.openhab.binding.wlanthermo.internal.api.nano.dto.settings.Settings;
25 import org.openhab.core.thing.Channel;
26 import org.openhab.core.thing.ChannelUID;
27 import org.openhab.core.thing.Thing;
28 import org.openhab.core.types.Command;
29 import org.openhab.core.types.State;
32 * The {@link WlanThermoNanoV1Handler} is responsible for handling commands, which are
33 * sent to one of the channels.
35 * @author Christian Schlipp - Initial contribution
38 public class WlanThermoNanoV1Handler extends WlanThermoHandler {
40 private Data data = new Data();
41 private Settings settings = new Settings();
43 public WlanThermoNanoV1Handler(Thing thing, HttpClient httpClient) {
44 super(thing, httpClient, true);
48 protected State getState(ChannelUID channelUID) throws WlanThermoInputException, WlanThermoUnknownChannelException {
49 return WlanThermoNanoV1CommandHandler.getState(channelUID, data, settings);
53 protected boolean setState(ChannelUID channelUID, Command command) {
54 return WlanThermoNanoV1CommandHandler.setState(channelUID, command, data);
58 protected void push() {
59 // push update for sensor channels
60 for (org.openhab.binding.wlanthermo.internal.api.nano.dto.data.Channel c : data.getChannel()) {
62 String json = gson.toJson(c);
63 if (!doPost("/setchannels", json)) {
66 } catch (InterruptedException e) {
67 logger.debug("Push interrupted. {}", e.getMessage());
72 // push update for pitmaster channels
74 String json = gson.toJson(data.getPitmaster().getPm());
75 doPost("/setpitmaster", json);
76 } catch (InterruptedException e) {
77 logger.debug("Push interrupted. {}", e.getMessage());
82 protected void pull() {
84 // Update objects with data from device
85 data = doGet("/data", Data.class);
86 settings = doGet("/settings", Settings.class);
89 for (Channel channel : thing.getChannels()) {
91 State state = WlanThermoNanoV1CommandHandler.getState(channel.getUID(), data, settings);
92 updateState(channel.getUID(), state);
93 } catch (WlanThermoUnknownChannelException e) {
94 // if we could not obtain a state, try trigger instead
96 String trigger = WlanThermoNanoV1CommandHandler.getTrigger(channel.getUID(), data);
97 if (!trigger.equals(TRIGGER_NONE)) {
98 triggerChannel(channel.getUID(), trigger);
100 } catch (WlanThermoUnknownChannelException e1) {
101 logger.debug("{}", e.getMessage());
105 } catch (WlanThermoException ignore) {
106 // Nothing more to do
107 } catch (InterruptedException e) {
108 logger.debug("Update interrupted. {}", e.getMessage());