]> git.basschouten.com Git - openhab-addons.git/blob
190e747632ab104bdff86deef22a26dd6b055884
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2020 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.hydrawise.internal;
14
15 import org.eclipse.jdt.annotation.NonNullByDefault;
16 import org.eclipse.jetty.client.HttpClient;
17 import org.openhab.binding.hydrawise.internal.api.HydrawiseAuthenticationException;
18 import org.openhab.binding.hydrawise.internal.api.HydrawiseCommandException;
19 import org.openhab.binding.hydrawise.internal.api.HydrawiseConnectionException;
20 import org.openhab.binding.hydrawise.internal.api.HydrawiseLocalApiClient;
21 import org.openhab.binding.hydrawise.internal.api.model.Relay;
22 import org.openhab.core.thing.Thing;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25
26 /**
27  * The {@link HydrawiseLocalHandler} is responsible for handling commands, which are
28  * sent to one of the channels.
29  *
30  * @author Dan Cunningham - Initial contribution
31  */
32 @NonNullByDefault
33 public class HydrawiseLocalHandler extends HydrawiseHandler {
34     private final Logger logger = LoggerFactory.getLogger(HydrawiseLocalHandler.class);
35     HydrawiseLocalApiClient client;
36
37     public HydrawiseLocalHandler(Thing thing, HttpClient httpClient) {
38         super(thing);
39         client = new HydrawiseLocalApiClient(httpClient);
40     }
41
42     @Override
43     protected void configure() throws HydrawiseConnectionException, HydrawiseAuthenticationException {
44         HydrawiseLocalConfiguration configuration = getConfig().as(HydrawiseLocalConfiguration.class);
45         this.refresh = Math.max(configuration.refresh, MIN_REFRESH_SECONDS);
46         logger.trace("Connecting to host {}", configuration.host);
47         client.setCredentials(configuration.host, configuration.username, configuration.password);
48         pollController();
49     }
50
51     @Override
52     protected void pollController() throws HydrawiseConnectionException, HydrawiseAuthenticationException {
53         updateZones(client.getLocalSchedule());
54     }
55
56     @Override
57     protected void sendRunCommand(int seconds, Relay relay)
58             throws HydrawiseCommandException, HydrawiseConnectionException, HydrawiseAuthenticationException {
59         client.runRelay(seconds, relay.relay);
60     }
61
62     @Override
63     protected void sendRunCommand(Relay relay)
64             throws HydrawiseCommandException, HydrawiseConnectionException, HydrawiseAuthenticationException {
65         client.runRelay(relay.relay);
66     }
67
68     @Override
69     protected void sendStopCommand(Relay relay)
70             throws HydrawiseCommandException, HydrawiseConnectionException, HydrawiseAuthenticationException {
71         client.stopRelay(relay.relay);
72     }
73
74     @Override
75     protected void sendRunAllCommand()
76             throws HydrawiseCommandException, HydrawiseConnectionException, HydrawiseAuthenticationException {
77         client.runAllRelays();
78     }
79
80     @Override
81     protected void sendRunAllCommand(int seconds)
82             throws HydrawiseCommandException, HydrawiseConnectionException, HydrawiseAuthenticationException {
83         client.runAllRelays(seconds);
84     }
85
86     @Override
87     protected void sendStopAllCommand()
88             throws HydrawiseCommandException, HydrawiseConnectionException, HydrawiseAuthenticationException {
89         client.stopAllRelays();
90     }
91 }