]> git.basschouten.com Git - openhab-addons.git/blob
0232ccc9dd5611157210bbd06b07f2d8775aca06
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2021 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.nano;
14
15 import static org.openhab.binding.wlanthermo.internal.WlanThermoBindingConstants.TRIGGER_NONE;
16
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;
25
26 /**
27  * The {@link WlanThermoNanoV1Handler} is responsible for handling commands, which are
28  * sent to one of the channels.
29  *
30  * @author Christian Schlipp - Initial contribution
31  */
32 @NonNullByDefault
33 public class WlanThermoNanoV1Handler extends WlanThermoHandler {
34
35     private Data data = new Data();
36     private Settings settings = new Settings();
37
38     public WlanThermoNanoV1Handler(Thing thing, HttpClient httpClient) {
39         super(thing, httpClient, true);
40     }
41
42     @Override
43     protected State getState(ChannelUID channelUID) throws WlanThermoInputException, WlanThermoUnknownChannelException {
44         return WlanThermoNanoV1CommandHandler.getState(channelUID, data, settings);
45     }
46
47     @Override
48     protected boolean setState(ChannelUID channelUID, Command command) {
49         return WlanThermoNanoV1CommandHandler.setState(channelUID, command, data);
50     }
51
52     @Override
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()) {
56             try {
57                 String json = gson.toJson(c);
58                 if (!doPost("/setchannels", json)) {
59                     break;
60                 }
61             } catch (InterruptedException e) {
62                 logger.debug("Push interrupted. {}", e.getMessage());
63                 return;
64             }
65         }
66
67         // push update for pitmaster channels
68         try {
69             String json = gson.toJson(data.getPitmaster().getPm());
70             doPost("/setpitmaster", json);
71         } catch (InterruptedException e) {
72             logger.debug("Push interrupted. {}", e.getMessage());
73         }
74     }
75
76     @Override
77     protected void pull() {
78         try {
79             // Update objects with data from device
80             data = doGet("/data", Data.class);
81             settings = doGet("/settings", Settings.class);
82
83             // Update channels
84             for (Channel channel : thing.getChannels()) {
85                 try {
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
90                     try {
91                         String trigger = WlanThermoNanoV1CommandHandler.getTrigger(channel.getUID(), data);
92                         if (!trigger.equals(TRIGGER_NONE)) {
93                             triggerChannel(channel.getUID(), trigger);
94                         }
95                     } catch (WlanThermoUnknownChannelException e1) {
96                         logger.debug("{}", e.getMessage());
97                     }
98                 }
99             }
100         } catch (WlanThermoException ignore) {
101             // Nothing more to do
102         } catch (InterruptedException e) {
103             logger.debug("Update interrupted. {}", e.getMessage());
104         }
105     }
106 }