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.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.*;
20 import org.openhab.binding.wlanthermo.internal.api.nano.dto.data.Data;
21 import org.openhab.binding.wlanthermo.internal.api.nano.dto.settings.Settings;
22 import org.openhab.core.thing.*;
23 import org.openhab.core.types.Command;
24 import org.openhab.core.types.State;
27 * The {@link WlanThermoNanoV1Handler} is responsible for handling commands, which are
28 * sent to one of the channels.
30 * @author Christian Schlipp - Initial contribution
33 public class WlanThermoNanoV1Handler extends WlanThermoHandler {
35 private Data data = new Data();
36 private Settings settings = new Settings();
38 public WlanThermoNanoV1Handler(Thing thing, HttpClient httpClient) {
39 super(thing, httpClient, true);
43 protected State getState(ChannelUID channelUID) throws WlanThermoInputException, WlanThermoUnknownChannelException {
44 return WlanThermoNanoV1CommandHandler.getState(channelUID, data, settings);
48 protected boolean setState(ChannelUID channelUID, Command command) {
49 return WlanThermoNanoV1CommandHandler.setState(channelUID, command, data);
53 protected void push() {
54 // push update for sensor channels
55 for (org.openhab.binding.wlanthermo.internal.api.nano.dto.data.Channel c : data.getChannel()) {
57 String json = gson.toJson(c);
58 if (!doPost("/setchannels", json)) {
61 } catch (InterruptedException e) {
62 logger.debug("Push interrupted. {}", e.getMessage());
67 // push update for pitmaster channels
69 String json = gson.toJson(data.getPitmaster().getPm());
70 doPost("/setpitmaster", json);
71 } catch (InterruptedException e) {
72 logger.debug("Push interrupted. {}", e.getMessage());
77 protected void pull() {
79 // Update objects with data from device
80 data = doGet("/data", Data.class);
81 settings = doGet("/settings", Settings.class);
84 for (Channel channel : thing.getChannels()) {
86 State state = WlanThermoNanoV1CommandHandler.getState(channel.getUID(), data, settings);
87 updateState(channel.getUID(), state);
88 } catch (WlanThermoUnknownChannelException e) {
89 // if we could not obtain a state, try trigger instead
91 String trigger = WlanThermoNanoV1CommandHandler.getTrigger(channel.getUID(), data);
92 if (!trigger.equals(TRIGGER_NONE)) {
93 triggerChannel(channel.getUID(), trigger);
95 } catch (WlanThermoUnknownChannelException e1) {
96 logger.debug("{}", e.getMessage());
100 } catch (WlanThermoException ignore) {
101 // Nothing more to do
102 } catch (InterruptedException e) {
103 logger.debug("Update interrupted. {}", e.getMessage());