]> git.basschouten.com Git - openhab-addons.git/blob
250203f76c868a0a7d2229d85465a66ed9427b54
[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.mini;
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.mini.dto.builtin.App;
24 import org.openhab.core.thing.Channel;
25 import org.openhab.core.thing.ChannelUID;
26 import org.openhab.core.thing.Thing;
27 import org.openhab.core.types.Command;
28 import org.openhab.core.types.State;
29
30 /**
31  * The {@link WlanThermoMiniHandler} is responsible for handling commands, which are
32  * sent to one of the channels.
33  *
34  * @author Christian Schlipp - Initial contribution
35  */
36 @NonNullByDefault
37 public class WlanThermoMiniHandler extends WlanThermoHandler {
38
39     private App app = new App();
40
41     public WlanThermoMiniHandler(Thing thing, HttpClient httpClient) {
42         super(thing, httpClient, false);
43     }
44
45     @Override
46     protected State getState(ChannelUID channelUID) throws WlanThermoInputException, WlanThermoUnknownChannelException {
47         return WlanThermoMiniCommandHandler.getState(channelUID, app);
48     }
49
50     @Override
51     protected boolean setState(ChannelUID channelUID, Command command) {
52         // Mini is read-only!
53         return false;
54     }
55
56     @Override
57     protected void push() {
58         // Unused, Mini is read-only!
59     }
60
61     @Override
62     protected void pull() {
63         try {
64             // Update objects with data from device
65             app = doGet("/app.php", App.class);
66
67             // Update channels
68             for (Channel channel : thing.getChannels()) {
69                 try {
70                     State state = WlanThermoMiniCommandHandler.getState(channel.getUID(), app);
71                     updateState(channel.getUID(), state);
72                 } catch (WlanThermoUnknownChannelException e) {
73                     // if we could not obtain a state, try trigger instead
74                     try {
75                         String trigger = WlanThermoMiniCommandHandler.getTrigger(channel.getUID(), app);
76                         if (!trigger.equals(TRIGGER_NONE)) {
77                             triggerChannel(channel.getUID(), trigger);
78                         }
79                     } catch (WlanThermoUnknownChannelException e1) {
80                         logger.debug("{}", e.getMessage());
81                     }
82                 }
83             }
84         } catch (WlanThermoException ignore) {
85             // Nothing more to do
86         } catch (InterruptedException e) {
87             logger.debug("Update interrupted. {}", e.getMessage());
88         }
89     }
90 }