]> git.basschouten.com Git - openhab-addons.git/blob
74b98c9b1dfb126efe8728383eec47d99b48726e
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 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.Map;
18
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;
36
37 /**
38  * The {@link VolvoVehicleDiscoveryService} searches for available
39  * cars discoverable through VocAPI
40  *
41  * @author GaĆ«l L'hopital - Initial contribution
42  */
43 @NonNullByDefault
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;
48
49     public VolvoVehicleDiscoveryService() {
50         super(SUPPORTED_THING_TYPES_UIDS, SEARCH_TIME);
51     }
52
53     @Override
54     public void setThingHandler(@Nullable ThingHandler handler) {
55         if (handler instanceof VolvoOnCallBridgeHandler) {
56             this.handler = (VolvoOnCallBridgeHandler) handler;
57         }
58     }
59
60     @Override
61     public @Nullable ThingHandler getThingHandler() {
62         return handler;
63     }
64
65     @Override
66     public void activate(@Nullable Map<String, Object> configProperties) {
67         super.activate(configProperties);
68     }
69
70     @Override
71     public void deactivate() {
72         super.deactivate();
73     }
74
75     @Override
76     protected void startScan() {
77         VolvoOnCallBridgeHandler bridgeHandler = this.handler;
78         if (bridgeHandler != null) {
79             ThingUID bridgeUID = bridgeHandler.getThing().getUID();
80             VocHttpApi api = bridgeHandler.getApi();
81             if (api != null) {
82                 try {
83                     CustomerAccounts account = api.getURL("customeraccounts/", CustomerAccounts.class);
84                     account.accountVehicleRelationsURL.forEach(relationURL -> {
85                         try {
86                             AccountVehicleRelation accountVehicle = api.getURL(relationURL,
87                                     AccountVehicleRelation.class);
88                             logger.debug("Found vehicle : {}", accountVehicle.vehicleId);
89
90                             Vehicles vehicle = api.getURL(accountVehicle.vehicleURL, Vehicles.class);
91                             Attributes attributes = api.getURL(Attributes.class, vehicle.vehicleId);
92
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());
98
99                         } catch (VolvoOnCallException e) {
100                             logger.warn("Error while getting vehicle informations : {}", e.getMessage());
101                         }
102                     });
103                 } catch (VolvoOnCallException e) {
104                     logger.warn("Error while discovering vehicle: {}", e.getMessage());
105                 }
106             }
107             ;
108         }
109         stopScan();
110     }
111 }