]> git.basschouten.com Git - openhab-addons.git/blob
ab77e89e3b3b308d7c9bc8117f447ea1da5d70ad
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2020 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.discovery;
14
15 import static org.openhab.binding.volvooncall.internal.VolvoOnCallBindingConstants.*;
16
17 import java.util.Arrays;
18
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;
30
31 /**
32  * The {@link VolvoOnCallDiscoveryService} searches for available
33  * cars discoverable through VocAPI
34  *
35  * @author GaĆ«l L'hopital - Initial contribution
36  */
37 @NonNullByDefault
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;
42
43     public VolvoOnCallDiscoveryService(VolvoOnCallBridgeHandler bridgeHandler) {
44         super(SUPPORTED_THING_TYPES_UIDS, SEARCH_TIME);
45         this.bridgeHandler = bridgeHandler;
46     }
47
48     @Override
49     public void startScan() {
50         String[] relations = bridgeHandler.getVehiclesRelationsURL();
51         Arrays.stream(relations).forEach(relationURL -> {
52             try {
53                 AccountVehicleRelation accountVehicle = bridgeHandler.getURL(relationURL, AccountVehicleRelation.class);
54                 logger.debug("Found vehicle : {}", accountVehicle.vehicleId);
55
56                 Vehicles vehicle = bridgeHandler.getURL(accountVehicle.vehicleURL, Vehicles.class);
57                 Attributes attributes = bridgeHandler.getURL(Attributes.class, vehicle.vehicleId);
58
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());
65
66             } catch (VolvoOnCallException e) {
67                 logger.warn("Error while discovering vehicle: {}", e.getMessage());
68             }
69         });
70
71         stopScan();
72     }
73 }