2 * Copyright (c) 2010-2020 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.*;
17 import java.util.Arrays;
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.openhab.binding.volvooncall.internal.VolvoOnCallException;
21 import org.openhab.binding.volvooncall.internal.dto.AccountVehicleRelation;
22 import org.openhab.binding.volvooncall.internal.dto.Attributes;
23 import org.openhab.binding.volvooncall.internal.dto.Vehicles;
24 import org.openhab.binding.volvooncall.internal.handler.VolvoOnCallBridgeHandler;
25 import org.openhab.core.config.discovery.AbstractDiscoveryService;
26 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
27 import org.openhab.core.thing.ThingUID;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
32 * The {@link VolvoOnCallDiscoveryService} searches for available
33 * cars discoverable through VocAPI
35 * @author Gaƫl L'hopital - Initial contribution
38 public class VolvoOnCallDiscoveryService extends AbstractDiscoveryService {
39 private static final int SEARCH_TIME = 2;
40 private final Logger logger = LoggerFactory.getLogger(VolvoOnCallDiscoveryService.class);
41 private final VolvoOnCallBridgeHandler bridgeHandler;
43 public VolvoOnCallDiscoveryService(VolvoOnCallBridgeHandler bridgeHandler) {
44 super(SUPPORTED_THING_TYPES_UIDS, SEARCH_TIME);
45 this.bridgeHandler = bridgeHandler;
49 public void startScan() {
50 String[] relations = bridgeHandler.getVehiclesRelationsURL();
51 Arrays.stream(relations).forEach(relationURL -> {
53 AccountVehicleRelation accountVehicle = bridgeHandler.getURL(relationURL, AccountVehicleRelation.class);
54 logger.debug("Found vehicle : {}", accountVehicle.vehicleId);
56 Vehicles vehicle = bridgeHandler.getURL(accountVehicle.vehicleURL, Vehicles.class);
57 Attributes attributes = bridgeHandler.getURL(Attributes.class, vehicle.vehicleId);
59 thingDiscovered(DiscoveryResultBuilder
60 .create(new ThingUID(VEHICLE_THING_TYPE, bridgeHandler.getThing().getUID(),
61 accountVehicle.vehicleId))
62 .withLabel(attributes.vehicleType + " " + attributes.registrationNumber)
63 .withBridge(bridgeHandler.getThing().getUID()).withProperty(VIN, attributes.vin)
64 .withRepresentationProperty(accountVehicle.vehicleId).build());
66 } catch (VolvoOnCallException e) {
67 logger.warn("Error while discovering vehicle: {}", e.getMessage());