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