]> git.basschouten.com Git - openhab-addons.git/blob
713fbbe6f67c52fd60c0f81db2551c832cb60e8a
[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.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             VocHttpApi vocApi = new VocHttpApi(configuration, gson, httpClient);
66             CustomerAccounts account = vocApi.getURL("customeraccounts/", CustomerAccounts.class);
67             if (account.username != null) {
68                 updateStatus(ThingStatus.ONLINE, ThingStatusDetail.NONE, account.username);
69                 this.api = vocApi;
70             } else {
71                 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR,
72                         "@text/offline.config-error-invalid-credentials");
73             }
74         } catch (VolvoOnCallException e) {
75             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, e.getMessage());
76         }
77     }
78
79     @Override
80     public void dispose() {
81         VocHttpApi vocApi = this.api;
82         if (vocApi != null) {
83             try {
84                 vocApi.dispose();
85                 api = null;
86             } catch (Exception e) {
87                 logger.warn("Unable to stop VocHttpApi : {}", e.getMessage());
88             }
89         }
90     }
91
92     public @Nullable VocHttpApi getApi() {
93         return api;
94     }
95
96     @Override
97     public Collection<Class<? extends ThingHandlerService>> getServices() {
98         return Collections.singleton(VolvoVehicleDiscoveryService.class);
99     }
100
101     @Override
102     public void handleCommand(ChannelUID channelUID, Command command) {
103         // Do nothing
104     }
105 }