2 * Copyright (c) 2010-2024 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.discovery;
15 import static org.openhab.binding.volvooncall.internal.VolvoOnCallBindingConstants.*;
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.eclipse.jdt.annotation.Nullable;
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.VehicleConfiguration;
24 import org.openhab.binding.volvooncall.internal.dto.AccountVehicleRelation;
25 import org.openhab.binding.volvooncall.internal.dto.Attributes;
26 import org.openhab.binding.volvooncall.internal.dto.CustomerAccounts;
27 import org.openhab.binding.volvooncall.internal.dto.Vehicles;
28 import org.openhab.binding.volvooncall.internal.handler.VolvoOnCallBridgeHandler;
29 import org.openhab.core.config.discovery.AbstractDiscoveryService;
30 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
31 import org.openhab.core.thing.ThingUID;
32 import org.openhab.core.thing.binding.ThingHandler;
33 import org.openhab.core.thing.binding.ThingHandlerService;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
38 * The {@link VolvoVehicleDiscoveryService} searches for available
39 * cars discoverable through VocAPI
41 * @author Gaƫl L'hopital - Initial contribution
44 public class VolvoVehicleDiscoveryService extends AbstractDiscoveryService implements ThingHandlerService {
45 private static final int SEARCH_TIME = 2;
46 private final Logger logger = LoggerFactory.getLogger(VolvoVehicleDiscoveryService.class);
47 private @Nullable VolvoOnCallBridgeHandler handler;
49 public VolvoVehicleDiscoveryService() {
50 super(SUPPORTED_THING_TYPES_UIDS, SEARCH_TIME);
54 public void setThingHandler(@Nullable ThingHandler handler) {
55 if (handler instanceof VolvoOnCallBridgeHandler volvoOnCallBridgeHandler) {
56 this.handler = volvoOnCallBridgeHandler;
61 public @Nullable ThingHandler getThingHandler() {
66 public void activate(@Nullable Map<String, Object> configProperties) {
67 super.activate(configProperties);
71 public void deactivate() {
76 protected void startScan() {
77 VolvoOnCallBridgeHandler bridgeHandler = this.handler;
78 if (bridgeHandler != null) {
79 ThingUID bridgeUID = bridgeHandler.getThing().getUID();
80 VocHttpApi api = bridgeHandler.getApi();
83 CustomerAccounts account = api.getURL("customeraccounts/", CustomerAccounts.class);
84 account.accountVehicleRelationsURL.forEach(relationURL -> {
86 AccountVehicleRelation accountVehicle = api.getURL(relationURL,
87 AccountVehicleRelation.class);
88 logger.debug("Found vehicle : {}", accountVehicle.vehicleId);
90 Vehicles vehicle = api.getURL(accountVehicle.vehicleURL, Vehicles.class);
91 Attributes attributes = api.getURL(Attributes.class, vehicle.vehicleId);
93 thingDiscovered(DiscoveryResultBuilder
94 .create(new ThingUID(VEHICLE_THING_TYPE, bridgeUID, accountVehicle.vehicleId))
95 .withLabel(attributes.vehicleType + " " + attributes.registrationNumber)
96 .withBridge(bridgeUID).withProperty(VehicleConfiguration.VIN, attributes.vin)
97 .withRepresentationProperty(VehicleConfiguration.VIN).build());
99 } catch (VolvoOnCallException e) {
100 logger.warn("Error while getting vehicle informations : {}", e.getMessage());
103 } catch (VolvoOnCallException e) {
104 logger.warn("Error while discovering vehicle: {}", e.getMessage());