2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.volvooncall.internal.handler;
15 import java.util.Collection;
16 import java.util.Collections;
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;
36 import com.google.gson.Gson;
39 * The {@link VolvoOnCallBridgeHandler} is responsible for handling commands, which are
40 * sent to one of the channels.
42 * @author Gaƫl L'hopital - Initial contribution
45 public class VolvoOnCallBridgeHandler extends BaseBridgeHandler {
47 private final Logger logger = LoggerFactory.getLogger(VolvoOnCallBridgeHandler.class);
48 private final Gson gson;
49 private final HttpClient httpClient;
51 private @Nullable VocHttpApi api;
53 public VolvoOnCallBridgeHandler(Bridge bridge, Gson gson, HttpClient httpClient) {
56 this.httpClient = httpClient;
60 public void initialize() {
61 logger.debug("Initializing VolvoOnCall API bridge handler.");
62 ApiBridgeConfiguration configuration = getConfigAs(ApiBridgeConfiguration.class);
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);
71 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR,
72 "@text/offline.config-error-invalid-credentials");
74 } catch (VolvoOnCallException e) {
75 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, e.getMessage());
80 public void dispose() {
81 VocHttpApi vocApi = this.api;
86 } catch (Exception e) {
87 logger.warn("Unable to stop VocHttpApi : {}", e.getMessage());
92 public @Nullable VocHttpApi getApi() {
97 public Collection<Class<? extends ThingHandlerService>> getServices() {
98 return Collections.singleton(VolvoVehicleDiscoveryService.class);
102 public void handleCommand(ChannelUID channelUID, Command command) {