]> git.basschouten.com Git - openhab-addons.git/blob
ce3ee8cd694569d607feb42a07594df14ba96f29
[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.volvooncall.internal.handler;
14
15 import java.util.Collection;
16 import java.util.Collections;
17
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19 import org.eclipse.jdt.annotation.Nullable;
20 import org.eclipse.jetty.client.HttpClient;
21 import org.openhab.binding.volvooncall.internal.VolvoOnCallException;
22 import org.openhab.binding.volvooncall.internal.api.VocHttpApi;
23 import org.openhab.binding.volvooncall.internal.config.ApiBridgeConfiguration;
24 import org.openhab.binding.volvooncall.internal.discovery.VolvoVehicleDiscoveryService;
25 import org.openhab.binding.volvooncall.internal.dto.CustomerAccounts;
26 import org.openhab.core.thing.Bridge;
27 import org.openhab.core.thing.ChannelUID;
28 import org.openhab.core.thing.ThingStatus;
29 import org.openhab.core.thing.ThingStatusDetail;
30 import org.openhab.core.thing.binding.BaseBridgeHandler;
31 import org.openhab.core.thing.binding.ThingHandlerService;
32 import org.openhab.core.types.Command;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35
36 import com.google.gson.Gson;
37
38 /**
39  * The {@link VolvoOnCallBridgeHandler} is responsible for handling commands, which are
40  * sent to one of the channels.
41  *
42  * @author GaĆ«l L'hopital - Initial contribution
43  */
44 @NonNullByDefault
45 public class VolvoOnCallBridgeHandler extends BaseBridgeHandler {
46
47     private final Logger logger = LoggerFactory.getLogger(VolvoOnCallBridgeHandler.class);
48     private final Gson gson;
49     private final HttpClient httpClient;
50
51     private @Nullable VocHttpApi api;
52
53     public VolvoOnCallBridgeHandler(Bridge bridge, Gson gson, HttpClient httpClient) {
54         super(bridge);
55         this.gson = gson;
56         this.httpClient = httpClient;
57     }
58
59     @Override
60     public void initialize() {
61         logger.debug("Initializing VolvoOnCall API bridge handler.");
62         ApiBridgeConfiguration configuration = getConfigAs(ApiBridgeConfiguration.class);
63
64         try {
65             api = new VocHttpApi(configuration, gson, httpClient);
66             CustomerAccounts account = api.getURL("customeraccounts/", CustomerAccounts.class);
67             if (account.username != null) {
68                 updateStatus(ThingStatus.ONLINE, ThingStatusDetail.NONE, account.username);
69             } else {
70                 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "Incorrect login credentials");
71             }
72         } catch (VolvoOnCallException e) {
73             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, e.getMessage());
74         }
75     }
76
77     @Override
78     public void dispose() {
79         if (api != null) {
80             try {
81                 api.dispose();
82                 api = null;
83             } catch (Exception e) {
84                 logger.warn("Unable to stop VocHttpApi : {}", e.getMessage());
85             }
86         }
87     }
88
89     public @Nullable VocHttpApi getApi() {
90         return api;
91     }
92
93     @Override
94     public Collection<Class<? extends ThingHandlerService>> getServices() {
95         return Collections.singleton(VolvoVehicleDiscoveryService.class);
96     }
97
98     @Override
99     public void handleCommand(ChannelUID channelUID, Command command) {
100         // Do nothing
101     }
102 }