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