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.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;
37 import com.google.gson.Gson;
40 * The {@link VolvoOnCallBridgeHandler} is responsible for handling commands, which are
41 * sent to one of the channels.
43 * @author Gaƫl L'hopital - Initial contribution
46 public class VolvoOnCallBridgeHandler extends BaseBridgeHandler {
48 private final Logger logger = LoggerFactory.getLogger(VolvoOnCallBridgeHandler.class);
49 private final Gson gson;
50 private final HttpClientFactory httpClientFactory;
52 private @Nullable VocHttpApi api;
54 public VolvoOnCallBridgeHandler(Bridge bridge, Gson gson, HttpClientFactory httpClientFactory) {
57 this.httpClientFactory = httpClientFactory;
61 public void initialize() {
62 logger.debug("Initializing VolvoOnCall API bridge handler.");
63 ApiBridgeConfiguration configuration = getConfigAs(ApiBridgeConfiguration.class);
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);
73 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR,
74 "@text/offline.config-error-invalid-credentials");
76 } catch (VolvoOnCallException e) {
77 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, e.getMessage());
82 public void dispose() {
83 VocHttpApi vocApi = this.api;
88 } catch (Exception e) {
89 logger.warn("Unable to stop VocHttpApi : {}", e.getMessage());
94 public @Nullable VocHttpApi getApi() {
99 public Collection<Class<? extends ThingHandlerService>> getServices() {
100 return Collections.singleton(VolvoVehicleDiscoveryService.class);
104 public void handleCommand(ChannelUID channelUID, Command command) {