]> git.basschouten.com Git - openhab-addons.git/blob
2f9f31fe09f23a0c38661babe55f65f1cfa864ac
[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.api;
14
15 import java.util.concurrent.ScheduledExecutorService;
16 import java.util.concurrent.TimeUnit;
17
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19 import org.openhab.binding.volvooncall.internal.VolvoOnCallException;
20 import org.openhab.binding.volvooncall.internal.VolvoOnCallException.ErrorType;
21 import org.openhab.binding.volvooncall.internal.dto.PostResponse;
22 import org.openhab.core.thing.binding.ThingHandler;
23 import org.openhab.core.types.RefreshType;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26
27 /**
28  * The {@link ActionResultController} is responsible for triggering information
29  * update after a post has been submitted to the webservice.
30  *
31  * @author GaĆ«l L'hopital - Initial contribution
32  */
33 @NonNullByDefault
34 public class ActionResultController implements Runnable {
35     private final Logger logger = LoggerFactory.getLogger(ActionResultController.class);
36
37     private final VocHttpApi service;
38     private final ScheduledExecutorService scheduler;
39     private final PostResponse postResponse;
40     private final ThingHandler vehicle;
41
42     public ActionResultController(VocHttpApi service, PostResponse postResponse, ScheduledExecutorService scheduler,
43             ThingHandler vehicle) {
44         this.postResponse = postResponse;
45         this.service = service;
46         this.scheduler = scheduler;
47         this.vehicle = vehicle;
48     }
49
50     @Override
51     public void run() {
52         switch (postResponse.status) {
53             case SUCCESSFULL:
54             case FAILED:
55                 logger.debug("Action {} for vehicle {} resulted : {}.", postResponse.serviceType,
56                         postResponse.vehicleId, postResponse.status);
57                 vehicle.handleCommand(vehicle.getThing().getChannels().get(0).getUID(), RefreshType.REFRESH);
58                 break;
59             default:
60                 try {
61                     scheduler.schedule(
62                             new ActionResultController(service,
63                                     service.getURL(postResponse.serviceURL, PostResponse.class), scheduler, vehicle),
64                             10000, TimeUnit.MILLISECONDS);
65                 } catch (VolvoOnCallException e) {
66                     if (e.getType() == ErrorType.SERVICE_UNAVAILABLE) {
67                         scheduler.schedule(this, 10000, TimeUnit.MILLISECONDS);
68                     }
69                 }
70         }
71     }
72 }